Cloud computing coursework:Saas 图片社交网站
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.

111 rivejä
4.7 KiB

3 vuotta sitten
  1. # -*- coding: utf-8 -*-
  2. from flask import url_for
  3. from phshare.models import User
  4. from phshare.settings import Operations
  5. from phshare.utils import generate_token
  6. from tests.base import BaseTestCase
  7. class AuthTestCase(BaseTestCase):
  8. def test_login_normal_user(self):
  9. response = self.login()
  10. data = response.get_data(as_text=True)
  11. self.assertIn('Login success.', data)
  12. def test_login_locked_user(self):
  13. self.login(email='locked@helloflask.com', password='123')
  14. response = self.client.get(url_for('user.index', username='locked'))
  15. data = response.get_data(as_text=True)
  16. self.assertIn('Your account is locked.', data)
  17. def test_login_blocked_user(self):
  18. response = self.login(email='blocked@helloflask.com', password='123')
  19. data = response.get_data(as_text=True)
  20. self.assertIn('Your account is blocked.', data)
  21. def test_fail_login(self):
  22. response = self.login(email='wrong-username@helloflask.com', password='wrong-password')
  23. data = response.get_data(as_text=True)
  24. self.assertIn('Invalid email or password.', data)
  25. def test_logout_user(self):
  26. self.login()
  27. response = self.logout()
  28. data = response.get_data(as_text=True)
  29. self.assertIn('Logout success.', data)
  30. def test_login_protect(self):
  31. response = self.client.get(url_for('main.upload'), follow_redirects=True)
  32. data = response.get_data(as_text=True)
  33. self.assertIn('Please log in to access this page.', data)
  34. def test_unconfirmed_user_permission(self):
  35. self.login(email='unconfirmed@helloflask.com', password='123')
  36. response = self.client.get(url_for('main.upload'), follow_redirects=True)
  37. data = response.get_data(as_text=True)
  38. self.assertIn('Please confirm your account first.', data)
  39. def test_locked_user_permission(self):
  40. self.login(email='locked@helloflask.com', password='123')
  41. response = self.client.get(url_for('main.upload'), follow_redirects=True)
  42. self.assertEqual(response.status_code, 403)
  43. def test_register_account(self):
  44. response = self.client.post(url_for('auth.register'), data=dict(
  45. name='swz',
  46. email='test@helloflask.com',
  47. username='test',
  48. password='12345678',
  49. password2='12345678'
  50. ), follow_redirects=True)
  51. data = response.get_data(as_text=True)
  52. self.assertIn('Confirm email sent, check your inbox.', data)
  53. def test_confirm_account(self):
  54. user = User.query.filter_by(email='unconfirmed@helloflask.com').first()
  55. self.assertFalse(user.confirmed)
  56. token = generate_token(user=user, operation='confirm')
  57. self.login(email='unconfirmed@helloflask.com', password='123')
  58. response = self.client.get(url_for('auth.confirm', token=token), follow_redirects=True)
  59. data = response.get_data(as_text=True)
  60. self.assertIn('Account confirmed.', data)
  61. self.assertTrue(user.confirmed)
  62. def test_bad_confirm_token(self):
  63. self.login(email='unconfirmed@helloflask.com', password='123')
  64. response = self.client.get(url_for('auth.confirm', token='bad token'), follow_redirects=True)
  65. data = response.get_data(as_text=True)
  66. self.assertIn('Invalid or expired token.', data)
  67. self.assertNotIn('Account confirmed.', data)
  68. def test_reset_password(self):
  69. response = self.client.post(url_for('auth.forget_password'), data=dict(
  70. email='normal@helloflask.com',
  71. ), follow_redirects=True)
  72. data = response.get_data(as_text=True)
  73. self.assertIn('Password reset email sent, check your inbox.', data)
  74. user = User.query.filter_by(email='normal@helloflask.com').first()
  75. self.assertTrue(user.validate_password('123'))
  76. token = generate_token(user=user, operation=Operations.RESET_PASSWORD)
  77. response = self.client.post(url_for('auth.reset_password', token=token), data=dict(
  78. email='normal@helloflask.com',
  79. password='new-password',
  80. password2='new-password'
  81. ), follow_redirects=True)
  82. data = response.get_data(as_text=True)
  83. self.assertIn('Password updated.', data)
  84. self.assertTrue(user.validate_password('new-password'))
  85. self.assertFalse(user.validate_password('123'))
  86. # bad token
  87. response = self.client.post(url_for('auth.reset_password', token='bad token'), data=dict(
  88. email='normal@helloflask.com',
  89. password='new-password',
  90. password2='new-password'
  91. ), follow_redirects=True)
  92. data = response.get_data(as_text=True)
  93. self.assertIn('Invalid or expired link.', data)
  94. self.assertNotIn('Password updated.', data)