model.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import peewee
  2. import pytest
  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.db.init(":memory:")
  10. c.db.create_tables(m.MODELS)
  11. def teardown_method(self, _):
  12. c.db.close()
  13. def mk_user(self, name="gdritter", password="foo") -> m.User:
  14. return m.User.from_request(r.User(name=name, password=password,))
  15. def test_create_user(self):
  16. name = "gdritter"
  17. u = self.mk_user(name=name)
  18. # it should be the only thing in the db
  19. all_users = m.User.select()
  20. assert len(all_users) == 1
  21. assert all_users[0].id == u.id
  22. assert all_users[0].name == name
  23. # we should be able to find it with the given name, too
  24. named_user = m.User.get(m.User.name == name)
  25. assert named_user.id == u.id
  26. assert named_user.name == name
  27. def test_user_passwords(self):
  28. name = "gdritter"
  29. password = "foo"
  30. u = self.mk_user(name=name, password=password)
  31. print(u.name, u.passhash)
  32. assert u.authenticate(password)
  33. assert u.authenticate("wrong password") is False
  34. def test_no_duplicate_users(self):
  35. name = "gdritter"
  36. u1 = self.mk_user(name=name)
  37. with pytest.raises(e.UserExists):
  38. u2 = self.mk_user(name=name)
  39. def test_get_or_create_tag(self):
  40. u = self.mk_user()
  41. tag_name = "food"
  42. t = m.Tag.get_or_create_tag(u, tag_name)
  43. # we should be able to find the tag with the given name
  44. named_tags = m.Tag.select(m.Tag.user == u and m.Tag.name == tag_name)
  45. assert len(named_tags) == 1
  46. # subsequent calls to get_or_create_tag should return the same db row
  47. t2 = m.Tag.get_or_create_tag(u, tag_name)
  48. assert t.id == t2.id
  49. def test_find_hierarchy(self):
  50. u = self.mk_user()
  51. t = m.Tag.get_or_create_tag(u, "food/bread/rye")
  52. # this should have created three db rows: for 'food', for
  53. # 'food/bread', and for 'food/bread/rye':
  54. assert len(m.Tag.select()) == 3
  55. # searching for a prefix of the tag should yield the same
  56. # parent tag
  57. assert t.parent.id == m.Tag.get(name="food/bread").id
  58. assert t.parent.parent.id == m.Tag.get(name="food").id
  59. # creating a new hierarchical tag with a shared prefix should
  60. # only create the new child tag
  61. t2 = m.Tag.get_or_create_tag(u, "food/bread/baguette")
  62. print([t.name for t in m.Tag.select()])
  63. assert len(m.Tag.select()) == 4
  64. # it should share the same parent tags
  65. assert t2.parent.id == t.parent.id
  66. assert t2.parent.parent.id == t.parent.parent.id
  67. # trying to get a hierarchical tag should result in the same
  68. # one already entered
  69. assert t.id == m.Tag.get(name="food/bread/rye").id
  70. assert t2.id == m.Tag.get(name="food/bread/baguette").id
  71. def test_create_invite(self):
  72. u = self.mk_user()
  73. invite = m.UserInvite.manufacture(u)
  74. # the invite should reference the user and be unclaimed
  75. assert invite.created_by.id == u.id
  76. assert invite.created_at is not None
  77. assert invite.claimed_by is None
  78. assert invite.claimed_at is None
  79. # deserializing the unique token should reveal the encrypted data
  80. raw_data = c.serializer.loads(invite.token)
  81. assert raw_data["created_by"] == u.name
  82. def test_use_invite(self):
  83. u = self.mk_user()
  84. initial_invite = m.UserInvite.manufacture(u)
  85. assert initial_invite.claimed_by is None
  86. assert initial_invite.claimed_at is None
  87. u2 = m.User.from_invite(r.User(name="u2", password="u2"), initial_invite.token)
  88. invite = m.UserInvite.by_code(initial_invite.token)
  89. assert invite.token == initial_invite.token
  90. assert invite.created_by.id == u.id
  91. assert invite.claimed_by.id == u2.id
  92. assert invite.created_at is not None
  93. assert invite.claimed_at is not None
  94. def bad_use_invite(self):
  95. initial_invite = m.UserInvite.manufacture(self.mk_user())
  96. # creating this user claims the invite
  97. m.User.from_invite(r.User(name="u2", password="u2"), initial_invite.token)
  98. # using the invite again raise an error
  99. with pytest.raises(e.AlreadyUsedInvite):
  100. m.User.from_invite(r.User(name="u3", password="u3"), initial_invite.token)
  101. with pytest.raises(e.NoSuchInvite):
  102. m.User.from_invite(r.User(name="u4", password="u4"), "a-non-existent-token")