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.

219 lines
8.9 KiB

3 years ago
  1. # -*- coding: utf-8 -*-
  2. """
  3. :author: Grey Li ()
  4. :url: http://greyli.com
  5. :copyright: © 2018 Grey Li <withlihui@gmail.com>
  6. :license: MIT, see LICENSE for more details.
  7. """
  8. import io
  9. from flask import url_for
  10. from albumy.models import User, Photo
  11. from albumy.settings import Operations
  12. from albumy.utils import generate_token
  13. from tests.base import BaseTestCase
  14. class UserTestCase(BaseTestCase):
  15. def test_index_page(self):
  16. response = self.client.get(url_for('user.index', username='normal'))
  17. data = response.get_data(as_text=True)
  18. self.assertIn('Normal User', data)
  19. self.login(email='locked@helloflask.com', password='123')
  20. response = self.client.get(url_for('user.index', username='locked'))
  21. data = response.get_data(as_text=True)
  22. self.assertIn('Locked User', data)
  23. self.assertIn('Your account is locked.', data)
  24. def test_show_collections(self):
  25. response = self.client.get(url_for('user.show_collections', username='normal'))
  26. data = response.get_data(as_text=True)
  27. self.assertIn("Normal User's collection", data)
  28. self.assertIn('No collection.', data)
  29. user = User.query.get(2)
  30. user.collect(Photo.query.get(1))
  31. response = self.client.get(url_for('user.show_collections', username='normal'))
  32. data = response.get_data(as_text=True)
  33. self.assertNotIn('No collection.', data)
  34. def test_follow(self):
  35. response = self.client.post(url_for('user.follow', username='admin'), follow_redirects=True)
  36. data = response.get_data(as_text=True)
  37. self.assertIn('Please log in to access this page.', data)
  38. self.login(email='unconfirmed@helloflask.com', password='123')
  39. response = self.client.post(url_for('user.follow', username='admin'), follow_redirects=True)
  40. data = response.get_data(as_text=True)
  41. self.assertIn('Please confirm your account first.', data)
  42. self.logout()
  43. self.login()
  44. response = self.client.post(url_for('user.follow', username='admin'), follow_redirects=True)
  45. data = response.get_data(as_text=True)
  46. self.assertEqual(response.status_code, 200)
  47. self.assertIn('User followed.', data)
  48. response = self.client.post(url_for('user.follow', username='admin'), follow_redirects=True)
  49. data = response.get_data(as_text=True)
  50. self.assertEqual(response.status_code, 200)
  51. self.assertIn('Already followed.', data)
  52. user = User.query.get(1)
  53. self.assertEqual(len(user.notifications), 1)
  54. def test_unfollow(self):
  55. response = self.client.post(url_for('user.follow', username='admin'), follow_redirects=True)
  56. data = response.get_data(as_text=True)
  57. self.assertIn('Please log in to access this page.', data)
  58. self.login()
  59. response = self.client.post(url_for('user.unfollow', username='admin'), follow_redirects=True)
  60. data = response.get_data(as_text=True)
  61. self.assertEqual(response.status_code, 200)
  62. self.assertIn('Not follow yet.', data)
  63. self.client.post(url_for('user.follow', username='admin'), follow_redirects=True)
  64. response = self.client.post(url_for('user.unfollow', username='admin'), follow_redirects=True)
  65. data = response.get_data(as_text=True)
  66. self.assertEqual(response.status_code, 200)
  67. self.assertIn('User unfollowed.', data)
  68. def test_show_followers(self):
  69. response = self.client.get(url_for('user.show_followers', username='normal'))
  70. data = response.get_data(as_text=True)
  71. self.assertIn('Normal User\'s followers', data)
  72. self.assertIn('No followers.', data)
  73. user = User.query.get(1)
  74. user.follow(User.query.get(2))
  75. response = self.client.get(url_for('user.show_followers', username='normal'))
  76. data = response.get_data(as_text=True)
  77. self.assertIn('Admin', data)
  78. self.assertNotIn('No followers.', data)
  79. def test_show_following(self):
  80. response = self.client.get(url_for('user.show_following', username='normal'))
  81. data = response.get_data(as_text=True)
  82. self.assertIn('Normal User\'s following', data)
  83. self.assertIn('No followings.', data)
  84. user = User.query.get(2)
  85. user.follow(User.query.get(1))
  86. response = self.client.get(url_for('user.show_following', username='normal'))
  87. data = response.get_data(as_text=True)
  88. self.assertIn('Admin', data)
  89. self.assertNotIn('No followers.', data)
  90. def test_edit_profile(self):
  91. self.login()
  92. response = self.client.post(url_for('user.edit_profile'), data=dict(
  93. username='newname',
  94. name='New Name',
  95. ), follow_redirects=True)
  96. data = response.get_data(as_text=True)
  97. self.assertIn('Profile updated.', data)
  98. user = User.query.get(2)
  99. self.assertEqual(user.name, 'New Name')
  100. self.assertEqual(user.username, 'newname')
  101. def test_change_avatar(self):
  102. self.login()
  103. response = self.client.get(url_for('user.change_avatar'))
  104. data = response.get_data(as_text=True)
  105. self.assertEqual(response.status_code, 200)
  106. self.assertIn('Change Avatar', data)
  107. def test_upload_avatar(self):
  108. self.login()
  109. data = {'image': (io.BytesIO(b"abcdef"), 'test.jpg')}
  110. response = self.client.post(url_for('user.upload_avatar'), data=data, follow_redirects=True,
  111. content_type='multipart/form-data')
  112. data = response.get_data(as_text=True)
  113. self.assertIn('Image uploaded, please crop.', data)
  114. def test_change_password(self):
  115. user = User.query.get(2)
  116. self.assertTrue(user.validate_password('123'))
  117. self.login()
  118. response = self.client.post(url_for('user.change_password'), data=dict(
  119. old_password='123',
  120. password='new-password',
  121. password2='new-password',
  122. ), follow_redirects=True)
  123. data = response.get_data(as_text=True)
  124. self.assertIn('Password updated.', data)
  125. self.assertTrue(user.validate_password('new-password'))
  126. self.assertFalse(user.validate_password('old-password'))
  127. def test_change_email(self):
  128. user = User.query.get(2)
  129. self.assertEqual(user.email, 'normal@helloflask.com')
  130. token = generate_token(user=user, operation=Operations.CHANGE_EMAIL, new_email='new@helloflask.com')
  131. self.login()
  132. response = self.client.get(url_for('user.change_email', token=token), follow_redirects=True)
  133. data = response.get_data(as_text=True)
  134. self.assertIn('Email updated.', data)
  135. self.assertEqual(user.email, 'new@helloflask.com')
  136. response = self.client.get(url_for('user.change_email', token='bad'), follow_redirects=True)
  137. data = response.get_data(as_text=True)
  138. self.assertIn('Invalid or expired token.', data)
  139. def test_notification_setting(self):
  140. self.login()
  141. response = self.client.post(url_for('user.notification_setting'), data=dict(
  142. receive_collect_notification='',
  143. receive_comment_notification='',
  144. receive_follow_notification=''
  145. ), follow_redirects=True)
  146. data = response.get_data(as_text=True)
  147. self.assertIn('Notification settings updated.', data)
  148. user = User.query.get(2)
  149. self.assertEqual(user.receive_collect_notification, False)
  150. self.assertEqual(user.receive_comment_notification, False)
  151. self.assertEqual(user.receive_follow_notification, False)
  152. self.logout()
  153. self.login(email='admin@helloflask.com', password='123')
  154. self.client.post(url_for('user.follow', username='normal'))
  155. self.client.post(url_for('main.new_comment', photo_id=2), data=dict(body='test comment from admin user.'))
  156. self.client.post(url_for('main.collect', photo_id=2))
  157. self.assertEqual(len(user.notifications), 0)
  158. def test_privacy_setting(self):
  159. self.login()
  160. response = self.client.post(url_for('user.privacy_setting'), data=dict(
  161. public_collections='',
  162. ), follow_redirects=True)
  163. data = response.get_data(as_text=True)
  164. self.assertIn('Privacy settings updated.', data)
  165. user = User.query.get(2)
  166. self.assertEqual(user.public_collections, False)
  167. self.logout()
  168. response = self.client.get(url_for('user.show_collections', username='normal'))
  169. data = response.get_data(as_text=True)
  170. self.assertIn("Normal User's collection", data)
  171. self.assertIn('This user\'s collections was private.', data)
  172. def test_delete_account(self):
  173. self.login()
  174. response = self.client.post(url_for('user.delete_account'), data=dict(
  175. username='normal',
  176. ), follow_redirects=True)
  177. data = response.get_data(as_text=True)
  178. self.assertIn('Your are free, goodbye!', data)
  179. self.assertEqual(User.query.get(2), None)