model.py 6.1 KB

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