model.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import pytest
  2. import config # noqa: F401
  3. import lc.config as c
  4. import lc.error as e
  5. import lc.request as r
  6. import lc.model as m
  7. class Testdb:
  8. def setup_method(self, _):
  9. c.app.in_memory_db()
  10. m.create_tables()
  11. def teardown_method(self, _):
  12. c.app.close_db()
  13. def mk_user(self, name="gdritter", password="foo") -> m.User:
  14. return m.User.from_request(
  15. r.User(
  16. name=name,
  17. password=password,
  18. )
  19. )
  20. def test_create_user(self):
  21. name = "gdritter"
  22. u = self.mk_user(name=name)
  23. # it should be the only thing in the db
  24. all_users = m.User.select()
  25. assert len(all_users) == 1
  26. assert all_users[0].id == u.id
  27. assert all_users[0].name == name
  28. # we should be able to find it with the given name, too
  29. named_user = m.User.get(m.User.name == name)
  30. assert named_user.id == u.id
  31. assert named_user.name == name
  32. def test_user_passwords(self):
  33. name = "gdritter"
  34. password = "foo"
  35. u = self.mk_user(name=name, password=password)
  36. print(u.name, u.passhash)
  37. assert u.authenticate(password)
  38. assert u.authenticate("wrong password") is False
  39. def test_no_duplicate_users(self):
  40. name = "gdritter"
  41. self.mk_user(name=name)
  42. with pytest.raises(e.UserExists):
  43. self.mk_user(name=name)
  44. def test_get_or_create_tag(self):
  45. u = self.mk_user()
  46. tag_name = "food"
  47. t = m.Tag.get_or_create_tag(u, tag_name)
  48. # we should be able to find the tag with the given name
  49. named_tags = m.Tag.select(m.Tag.user == u and m.Tag.name == tag_name)
  50. assert len(named_tags) == 1
  51. # subsequent calls to get_or_create_tag should return the same db row
  52. t2 = m.Tag.get_or_create_tag(u, tag_name)
  53. assert t.id == t2.id
  54. def test_find_hierarchy(self):
  55. u = self.mk_user()
  56. t = m.Tag.get_or_create_tag(u, "food/bread/rye")
  57. # this should have created three db rows: for 'food', for
  58. # 'food/bread', and for 'food/bread/rye':
  59. assert len(m.Tag.select()) == 3
  60. # searching for a prefix of the tag should yield the same
  61. # parent tag
  62. assert t.parent.id == m.Tag.get(name="food/bread").id
  63. assert t.parent.parent.id == m.Tag.get(name="food").id
  64. # creating a new hierarchical tag with a shared prefix should
  65. # only create the new child tag
  66. t2 = m.Tag.get_or_create_tag(u, "food/bread/baguette")
  67. print([t.name for t in m.Tag.select()])
  68. assert len(m.Tag.select()) == 4
  69. # it should share the same parent tags
  70. assert t2.parent.id == t.parent.id
  71. assert t2.parent.parent.id == t.parent.parent.id
  72. # trying to get a hierarchical tag should result in the same
  73. # one already entered
  74. assert t.id == m.Tag.get(name="food/bread/rye").id
  75. assert t2.id == m.Tag.get(name="food/bread/baguette").id
  76. def test_add_hierarchy(self):
  77. u = self.mk_user()
  78. req = r.Link("http://foo.com", "foo", "", False, ["food/bread/rye"])
  79. link = m.Link.from_request(u, req)
  80. assert link.name == req.name
  81. tag_names = {t.tag.name for t in link.tags} # type: ignore
  82. assert tag_names == {"food", "food/bread", "food/bread/rye"}
  83. def test_bad_tag(self):
  84. u = self.mk_user()
  85. req = r.Link("http://foo.com", "foo", "", False, ["foo{bar}"])
  86. with pytest.raises(e.BadTagName):
  87. m.Link.from_request(u, req)
  88. def test_create_invite(self):
  89. u = self.mk_user()
  90. invite = m.UserInvite.manufacture(u)
  91. # the invite should reference the user and be unclaimed
  92. assert invite.created_by.id == u.id
  93. assert invite.created_at is not None
  94. assert invite.claimed_by is None
  95. assert invite.claimed_at is None
  96. # deserializing the unique token should reveal the encrypted data
  97. raw_data = c.app.load_token(invite.token)
  98. assert raw_data["created_by"] == u.name
  99. def test_use_invite(self):
  100. u = self.mk_user()
  101. initial_invite = m.UserInvite.manufacture(u)
  102. assert initial_invite.claimed_by is None
  103. assert initial_invite.claimed_at is None
  104. u2 = m.User.from_invite(r.User(name="u2", password="u2"), initial_invite.token)
  105. invite = m.UserInvite.by_code(initial_invite.token)
  106. assert invite.token == initial_invite.token
  107. assert invite.created_by.id == u.id
  108. assert invite.claimed_by.id == u2.id
  109. assert invite.created_at is not None
  110. assert invite.claimed_at is not None
  111. def bad_use_invite(self):
  112. initial_invite = m.UserInvite.manufacture(self.mk_user())
  113. # creating this user claims the invite
  114. m.User.from_invite(r.User(name="u2", password="u2"), initial_invite.token)
  115. # using the invite again raise an error
  116. with pytest.raises(e.AlreadyUsedInvite):
  117. m.User.from_invite(r.User(name="u3", password="u3"), initial_invite.token)
  118. with pytest.raises(e.NoSuchInvite):
  119. m.User.from_invite(r.User(name="u4", password="u4"), "a-non-existent-token")
  120. def check_tags(self, link, tags):
  121. present = set(map(lambda hastag: hastag.tag.name, link.tags))
  122. assert present == set(tags)
  123. def test_edit_link(self):
  124. u = self.mk_user()
  125. req = r.Link("http://foo.com", "foo", "", False, ["foo", "bar"])
  126. link = m.Link.from_request(u, req)
  127. assert link.name == req.name
  128. assert link.tags == ["foo", "bar"] # type: ignore
  129. # check the in-place update
  130. req.name = "bar"
  131. req.tags = ["bar", "baz"]
  132. req.private = True
  133. link.update_from_request(u, req)
  134. assert link.name == req.name
  135. assert link.private
  136. assert link.created != req.created
  137. self.check_tags(link, req.tags)
  138. # check that the link was persisted
  139. link2 = m.Link.by_id(link.id)
  140. assert link2
  141. assert link2.name == req.name
  142. assert link2.private
  143. assert link2.created != req.created
  144. self.check_tags(link2, req.tags)