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.

60 lines
1.7 KiB

  1. import unittest
  2. import time
  3. from app import create_app, db
  4. from app.models import User
  5. class UserModelTestCase(unittest.TestCase):
  6. def setUp(self):
  7. self.app = create_app('testing')
  8. self.app_context = self.app.app_context()
  9. self.app_context.push()
  10. db.create_all()
  11. def tearDown(self):
  12. db.session.remove()
  13. db.drop_all()
  14. self.app_context.pop()
  15. def test_password_setter(self):
  16. u = User(password='cat')
  17. self.assertTrue(u.password_hash is not None)
  18. def test_no_password_getter(self):
  19. u = User(password='cat')
  20. with self.assertRaises(AttributeError):
  21. u.password
  22. def test_password_verification(self):
  23. u = User(password='cat')
  24. self.assertTrue(u.verify_password('cat'))
  25. self.assertFalse(u.verify_password('dog'))
  26. def test_password_salts_are_random(self):
  27. u = User(password='cat')
  28. u2 = User(password='cat')
  29. self.assertTrue(u.password_hash != u2.password_hash)
  30. def test_valid_confirmation_token(self):
  31. u = User(password='cat')
  32. db.session.add(u)
  33. db.session.commit()
  34. token = u.generate_confirmation_token()
  35. self.assertTrue(u.confirm(token))
  36. def test_invalid_confirmation_token(self):
  37. u1 = User(password='cat')
  38. u2 = User(password='dog')
  39. db.session.add(u1)
  40. db.session.add(u2)
  41. db.session.commit()
  42. token = u1.generate_confirmation_token()
  43. self.assertFalse(u2.confirm(token))
  44. def test_expired_confirmation_token(self):
  45. u = User(password='cat')
  46. db.session.add(u)
  47. db.session.commit()
  48. token = u.generate_confirmation_token(1)
  49. time.sleep(2)
  50. self.assertFalse(u.confirm(token))