You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
7.5 KiB

  1. import unittest
  2. import time
  3. from datetime import datetime
  4. from app import create_app, db
  5. from app.models import User, AnonymousUser, Role, Permission, Follow
  6. class UserModelTestCase(unittest.TestCase):
  7. def setUp(self):
  8. self.app = create_app('testing')
  9. self.app_context = self.app.app_context()
  10. self.app_context.push()
  11. db.create_all()
  12. Role.insert_roles()
  13. def tearDown(self):
  14. db.session.remove()
  15. db.drop_all()
  16. self.app_context.pop()
  17. def test_password_setter(self):
  18. u = User(password='cat')
  19. self.assertTrue(u.password_hash is not None)
  20. def test_no_password_getter(self):
  21. u = User(password='cat')
  22. with self.assertRaises(AttributeError):
  23. u.password
  24. def test_password_verification(self):
  25. u = User(password='cat')
  26. self.assertTrue(u.verify_password('cat'))
  27. self.assertFalse(u.verify_password('dog'))
  28. def test_password_salts_are_random(self):
  29. u = User(password='cat')
  30. u2 = User(password='cat')
  31. self.assertTrue(u.password_hash != u2.password_hash)
  32. def test_valid_confirmation_token(self):
  33. u = User(password='cat')
  34. db.session.add(u)
  35. db.session.commit()
  36. token = u.generate_confirmation_token()
  37. self.assertTrue(u.confirm(token))
  38. def test_invalid_confirmation_token(self):
  39. u1 = User(password='cat')
  40. u2 = User(password='dog')
  41. db.session.add(u1)
  42. db.session.add(u2)
  43. db.session.commit()
  44. token = u1.generate_confirmation_token()
  45. self.assertFalse(u2.confirm(token))
  46. def test_expired_confirmation_token(self):
  47. u = User(password='cat')
  48. db.session.add(u)
  49. db.session.commit()
  50. token = u.generate_confirmation_token(1)
  51. time.sleep(2)
  52. self.assertFalse(u.confirm(token))
  53. def test_valid_reset_token(self):
  54. u = User(password='cat')
  55. db.session.add(u)
  56. db.session.commit()
  57. token = u.generate_reset_token()
  58. self.assertTrue(User.reset_password(token, 'dog'))
  59. self.assertTrue(u.verify_password('dog'))
  60. def test_invalid_reset_token(self):
  61. u = User(password='cat')
  62. db.session.add(u)
  63. db.session.commit()
  64. token = u.generate_reset_token()
  65. self.assertFalse(User.reset_password(token+'a', 'horse'))
  66. self.assertTrue(u.verify_password('cat'))
  67. def test_valid_email_change_token(self):
  68. u = User(email='max@mustermann.de', password='cat')
  69. db.session.add(u)
  70. db.session.commit()
  71. token = u.generate_email_change_token('foo@bar.de')
  72. self.assertTrue(u.change_email(token))
  73. self.assertTrue(u.email == 'foo@bar.de')
  74. def test_invalid_email_change_token(self):
  75. u1 = User(email='max@mustermann.de', password='cat')
  76. u2 = User(email='dirk@mustermann.de', password='dog')
  77. db.session.add(u1)
  78. db.session.add(u2)
  79. db.session.commit()
  80. token = u1.generate_email_change_token('foo@bar.de')
  81. self.assertFalse(u2.change_email(token))
  82. self.assertTrue(u2.email == 'dirk@mustermann.de')
  83. def test_duplicate_email_change_token(self):
  84. u1 = User(email='max@mustermann.de', password='cat')
  85. u2 = User(email='dirk@mustermann.de', password='dog')
  86. db.session.add(u1)
  87. db.session.add(u2)
  88. db.session.commit()
  89. token = u2.generate_email_change_token(u1.email)
  90. self.assertFalse(u2.change_email(token))
  91. self.assertTrue(u2.email == 'dirk@mustermann.de')
  92. def test_user_role(self):
  93. u = User(email='max@mustermann.de', password='cat')
  94. self.assertTrue(u.can(Permission.FOLLOW))
  95. self.assertTrue(u.can(Permission.COMMENT))
  96. self.assertTrue(u.can(Permission.WRITE))
  97. self.assertFalse(u.can(Permission.MODERATE))
  98. self.assertFalse(u.can(Permission.ADMIN))
  99. def test_moderator_role(self):
  100. r = Role.query.filter_by(name='Moderator').first()
  101. u = User(email='max@mustermann.de', password='cat', role=r)
  102. self.assertTrue(u.can(Permission.FOLLOW))
  103. self.assertTrue(u.can(Permission.COMMENT))
  104. self.assertTrue(u.can(Permission.WRITE))
  105. self.assertTrue(u.can(Permission.MODERATE))
  106. self.assertFalse(u.can(Permission.ADMIN))
  107. def test_administrator_role(self):
  108. r = Role.query.filter_by(name='Administrator').first()
  109. u = User(email='max@mustermann.de', password='cat', role=r)
  110. self.assertTrue(u.can(Permission.FOLLOW))
  111. self.assertTrue(u.can(Permission.COMMENT))
  112. self.assertTrue(u.can(Permission.WRITE))
  113. self.assertTrue(u.can(Permission.MODERATE))
  114. self.assertTrue(u.can(Permission.ADMIN))
  115. def test_anonymous_user(self):
  116. u = AnonymousUser()
  117. self.assertFalse(u.can(Permission.FOLLOW))
  118. self.assertFalse(u.can(Permission.COMMENT))
  119. self.assertFalse(u.can(Permission.WRITE))
  120. self.assertFalse(u.can(Permission.MODERATE))
  121. self.assertFalse(u.can(Permission.ADMIN))
  122. def test_timestamps(self):
  123. u = User(password='cat')
  124. db.session.add(u)
  125. db.session.commit()
  126. self.assertTrue(
  127. (datetime.utcnow() - u.member_since).total_seconds() < 1)
  128. self.assertTrue(
  129. (datetime.utcnow() - u.last_seen).total_seconds() < 3)
  130. def test_ping(self):
  131. u = User(password='cat')
  132. db.session.add(u)
  133. db.session.commit()
  134. time.sleep(2)
  135. last_seen_before = u.last_seen
  136. u.ping()
  137. self.assertTrue(u.last_seen > last_seen_before)
  138. def test_gravatar(self):
  139. u = User(email='max@mustermann.de', password='cat')
  140. with self.app.test_request_context('/'):
  141. gravatar = u.gravatar()
  142. gravatar_256 = u.gravatar(size=256)
  143. gravatar_pg = u.gravatar(rating='pg')
  144. gravatar_retro = u.gravatar(default='retro')
  145. self.assertTrue('https://secure.gravatar.com/avatar/' +
  146. '3e64c371ba2b93f1c0fead369fe004ef' in gravatar)
  147. self.assertTrue('s=256' in gravatar_256)
  148. self.assertTrue('r=pg'in gravatar_pg)
  149. self.assertTrue('d=retro' in gravatar_retro)
  150. def test_follows(self):
  151. u1 = User(email='john@example.com', password='cat')
  152. u2 = User(email='susan@example.org', password='dog')
  153. db.session.add(u1)
  154. db.session.add(u2)
  155. db.session.commit()
  156. self.assertFalse(u1.is_following(u2))
  157. self.assertFalse(u1.is_followed_by(u2))
  158. timestamp_before = datetime.utcnow()
  159. u1.follow(u2)
  160. db.session.add(u1)
  161. db.session.commit()
  162. timestamp_after = datetime.utcnow()
  163. self.assertTrue(u1.is_following(u2))
  164. self.assertFalse(u1.is_followed_by(u2))
  165. self.assertTrue(u2.is_followed_by(u1))
  166. self.assertTrue(u1.followed.count() == 2)
  167. self.assertTrue(u2.followers.count() == 2)
  168. f = u1.followed.all()[-1]
  169. self.assertTrue(f.followed == u2)
  170. self.assertTrue(timestamp_before <= f.timestamp <= timestamp_after)
  171. f = u2.followers.all()[-1]
  172. self.assertTrue(f.follower == u1)
  173. u1.unfollow(u2)
  174. db.session.add(u1)
  175. db.session.commit()
  176. self.assertTrue(u1.followed.count() == 1)
  177. self.assertTrue(u2.followers.count() == 1)
  178. self.assertTrue(Follow.query.count() == 2)
  179. u2.follow(u1)
  180. db.session.add(u1)
  181. db.session.add(u2)
  182. db.session.commit()
  183. db.session.delete(u2)
  184. db.session.commit()
  185. self.assertTrue(Follow.query.count() == 1)