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.

213 regels
8.7 KiB

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