model.py 6.0 KB

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