Cloud computing coursework:Saas 图片社交网站
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

317 строки
14 KiB

3 лет назад
  1. # -*- coding: utf-8 -*-
  2. from flask import url_for
  3. from phshare.extensions import db
  4. from phshare.models import User, Photo, Comment, Notification, Tag
  5. from tests.base import BaseTestCase
  6. class MainTestCase(BaseTestCase):
  7. def test_index_page(self):
  8. response = self.client.get(url_for('main.index'))
  9. data = response.get_data(as_text=True)
  10. self.assertIn('Join Now', data)
  11. self.login()
  12. response = self.client.get(url_for('main.index'))
  13. data = response.get_data(as_text=True)
  14. self.assertNotIn('Join Now', data)
  15. self.assertIn('My Home', data)
  16. def test_explore_page(self):
  17. response = self.client.get(url_for('main.explore'))
  18. data = response.get_data(as_text=True)
  19. self.assertIn('Change', data)
  20. def test_search(self):
  21. response = self.client.get(url_for('main.search', q=''), follow_redirects=True)
  22. data = response.get_data(as_text=True)
  23. self.assertIn('Enter keyword about photo, user or tag.', data)
  24. response = self.client.get(url_for('main.search', q='normal'))
  25. data = response.get_data(as_text=True)
  26. self.assertNotIn('Enter keyword about photo, user or tag.', data)
  27. self.assertIn('No results.', data)
  28. response = self.client.get(url_for('main.search', q='normal', category='tag'))
  29. data = response.get_data(as_text=True)
  30. self.assertNotIn('Enter keyword about photo, user or tag.', data)
  31. self.assertIn('No results.', data)
  32. response = self.client.get(url_for('main.search', q='normal', category='user'))
  33. data = response.get_data(as_text=True)
  34. self.assertNotIn('Enter keyword about photo, user or tag.', data)
  35. self.assertNotIn('No results.', data)
  36. self.assertIn('Normal User', data)
  37. def test_show_notifications(self):
  38. user = User.query.get(2)
  39. notification1 = Notification(message='test 1', is_read=True, receiver=user)
  40. notification2 = Notification(message='test 2', is_read=False, receiver=user)
  41. db.session.add_all([notification1, notification2])
  42. db.session.commit()
  43. self.login()
  44. response = self.client.get(url_for('main.show_notifications'))
  45. data = response.get_data(as_text=True)
  46. self.assertIn('test 1', data)
  47. self.assertIn('test 2', data)
  48. response = self.client.get(url_for('main.show_notifications', filter='unread'))
  49. data = response.get_data(as_text=True)
  50. self.assertNotIn('test 1', data)
  51. self.assertIn('test 2', data)
  52. def test_read_notification(self):
  53. user = User.query.get(2)
  54. notification1 = Notification(message='test 1', receiver=user)
  55. notification2 = Notification(message='test 2', receiver=user)
  56. db.session.add_all([notification1, notification2])
  57. db.session.commit()
  58. self.login(email='admin@helloflask.com', password='123')
  59. response = self.client.post(url_for('main.read_notification', notification_id=1))
  60. self.assertEqual(response.status_code, 403)
  61. self.logout()
  62. self.login()
  63. response = self.client.post(url_for('main.read_notification', notification_id=1), follow_redirects=True)
  64. data = response.get_data(as_text=True)
  65. self.assertIn('Notification archived.', data)
  66. self.assertTrue(Notification.query.get(1).is_read)
  67. def test_read_all_notification(self):
  68. user = User.query.get(2)
  69. notification1 = Notification(message='test 1', receiver=user)
  70. notification2 = Notification(message='test 2', receiver=user)
  71. db.session.add_all([notification1, notification2])
  72. db.session.commit()
  73. self.login()
  74. response = self.client.post(url_for('main.read_all_notification'), follow_redirects=True)
  75. data = response.get_data(as_text=True)
  76. self.assertIn('All notifications archived.', data)
  77. self.assertTrue(Notification.query.get(1).is_read)
  78. self.assertTrue(Notification.query.get(2).is_read)
  79. def test_show_photo(self):
  80. response = self.client.get(url_for('main.show_photo', photo_id=1), follow_redirects=True)
  81. data = response.get_data(as_text=True)
  82. self.assertNotIn('Delete', data)
  83. self.assertIn('test tag', data)
  84. self.assertIn('test comment body', data)
  85. self.login(email='admin@helloflask.com', password='123')
  86. response = self.client.get(url_for('main.show_photo', photo_id=1), follow_redirects=True)
  87. data = response.get_data(as_text=True)
  88. self.assertIn('Delete', data)
  89. def test_photo_next(self):
  90. user = User.query.get(1)
  91. photo2 = Photo(filename='test.jpg', filename_s='test_s.jpg', filename_m='test_m.jpg',
  92. description='Photo 2', author=user)
  93. photo3 = Photo(filename='test.jpg', filename_s='test_s.jpg', filename_m='test_m.jpg',
  94. description='Photo 3', author=user)
  95. photo4 = Photo(filename='test.jpg', filename_s='test_s.jpg', filename_m='test_m.jpg',
  96. description='Photo 4', author=user)
  97. db.session.add_all([photo2, photo3, photo4])
  98. db.session.commit()
  99. response = self.client.get(url_for('main.photo_next', photo_id=5), follow_redirects=True)
  100. data = response.get_data(as_text=True)
  101. self.assertIn('Photo 3', data)
  102. response = self.client.get(url_for('main.photo_next', photo_id=4), follow_redirects=True)
  103. data = response.get_data(as_text=True)
  104. self.assertIn('Photo 2', data)
  105. response = self.client.get(url_for('main.photo_next', photo_id=3), follow_redirects=True)
  106. data = response.get_data(as_text=True)
  107. self.assertIn('Photo 1', data)
  108. response = self.client.get(url_for('main.photo_next', photo_id=1), follow_redirects=True)
  109. data = response.get_data(as_text=True)
  110. self.assertIn('This is already the last one.', data)
  111. def test_photo_prev(self):
  112. user = User.query.get(1)
  113. photo2 = Photo(filename='test.jpg', filename_s='test_s.jpg', filename_m='test_m.jpg',
  114. description='Photo 2', author=user)
  115. photo3 = Photo(filename='test.jpg', filename_s='test_s.jpg', filename_m='test_m.jpg',
  116. description='Photo 3', author=user)
  117. photo4 = Photo(filename='test.jpg', filename_s='test_s.jpg', filename_m='test_m.jpg',
  118. description='Photo 4', author=user)
  119. db.session.add_all([photo2, photo3, photo4])
  120. db.session.commit()
  121. response = self.client.get(url_for('main.photo_previous', photo_id=1), follow_redirects=True)
  122. data = response.get_data(as_text=True)
  123. self.assertIn('Photo 2', data)
  124. response = self.client.get(url_for('main.photo_previous', photo_id=3), follow_redirects=True)
  125. data = response.get_data(as_text=True)
  126. self.assertIn('Photo 3', data)
  127. response = self.client.get(url_for('main.photo_previous', photo_id=4), follow_redirects=True)
  128. data = response.get_data(as_text=True)
  129. self.assertIn('Photo 4', data)
  130. response = self.client.get(url_for('main.photo_previous', photo_id=5), follow_redirects=True)
  131. data = response.get_data(as_text=True)
  132. self.assertIn('This is already the first one.', data)
  133. def test_collect(self):
  134. photo = Photo(filename='test.jpg', filename_s='test_s.jpg', filename_m='test_m.jpg',
  135. description='Photo 3', author=User.query.get(2))
  136. db.session.add(photo)
  137. db.session.commit()
  138. self.assertEqual(Photo.query.get(3).collectors, [])
  139. self.login()
  140. response = self.client.post(url_for('main.collect', photo_id=3), follow_redirects=True)
  141. data = response.get_data(as_text=True)
  142. self.assertIn('Photo collected.', data)
  143. self.assertEqual(Photo.query.get(3).collectors[0].collector.name, 'Normal User')
  144. response = self.client.post(url_for('main.collect', photo_id=3), follow_redirects=True)
  145. data = response.get_data(as_text=True)
  146. self.assertIn('Already collected.', data)
  147. def test_uncollect(self):
  148. self.login()
  149. self.client.post(url_for('main.collect', photo_id=1), follow_redirects=True)
  150. response = self.client.post(url_for('main.uncollect', photo_id=1), follow_redirects=True)
  151. data = response.get_data(as_text=True)
  152. self.assertIn('Photo uncollected.', data)
  153. response = self.client.post(url_for('main.uncollect', photo_id=1), follow_redirects=True)
  154. data = response.get_data(as_text=True)
  155. self.assertIn('Not collect yet.', data)
  156. def test_report_comment(self):
  157. self.assertEqual(Comment.query.get(1).flag, 0)
  158. self.login()
  159. response = self.client.post(url_for('main.report_comment', comment_id=1), follow_redirects=True)
  160. data = response.get_data(as_text=True)
  161. self.assertIn('Comment reported.', data)
  162. self.assertEqual(Comment.query.get(1).flag, 1)
  163. def test_report_photo(self):
  164. self.assertEqual(Photo.query.get(1).flag, 0)
  165. self.login()
  166. response = self.client.post(url_for('main.report_photo', photo_id=1), follow_redirects=True)
  167. data = response.get_data(as_text=True)
  168. self.assertIn('Photo reported.', data)
  169. self.assertEqual(Photo.query.get(1).flag, 1)
  170. def test_show_collectors(self):
  171. user = User.query.get(2)
  172. user.collect(Photo.query.get(1))
  173. response = self.client.get(url_for('main.show_collectors', photo_id=1), follow_redirects=True)
  174. data = response.get_data(as_text=True)
  175. self.assertIn('1 Collectors', data)
  176. self.assertIn('Normal User', data)
  177. def test_edit_description(self):
  178. self.assertEqual(Photo.query.get(2).description, 'Photo 2')
  179. self.login()
  180. response = self.client.post(url_for('main.edit_description', photo_id=2), data=dict(
  181. description='test description.'
  182. ), follow_redirects=True)
  183. data = response.get_data(as_text=True)
  184. self.assertIn('Description updated.', data)
  185. self.assertEqual(Photo.query.get(2).description, 'test description.')
  186. def test_new_comment(self):
  187. self.login()
  188. response = self.client.post(url_for('main.new_comment', photo_id=1), data=dict(
  189. body='test comment from normal user.'
  190. ), follow_redirects=True)
  191. data = response.get_data(as_text=True)
  192. self.assertIn('Comment published.', data)
  193. self.assertEqual(Photo.query.get(1).comments[1].body, 'test comment from normal user.')
  194. def test_new_tag(self):
  195. self.login(email='admin@helloflask.com', password='123')
  196. response = self.client.post(url_for('main.new_tag', photo_id=1), data=dict(
  197. tag='hello dog pet happy'
  198. ), follow_redirects=True)
  199. data = response.get_data(as_text=True)
  200. self.assertEqual(response.status_code, 200)
  201. self.assertIn('Tag added.', data)
  202. self.assertEqual(Photo.query.get(1).tags[1].name, 'hello')
  203. self.assertEqual(Photo.query.get(1).tags[2].name, 'dog')
  204. self.assertEqual(Photo.query.get(1).tags[3].name, 'pet')
  205. self.assertEqual(Photo.query.get(1).tags[4].name, 'happy')
  206. def test_set_comment(self):
  207. self.login()
  208. response = self.client.post(url_for('main.set_comment', photo_id=1), follow_redirects=True)
  209. self.assertEqual(response.status_code, 403)
  210. self.logout()
  211. self.login(email='admin@helloflask.com', password='123')
  212. response = self.client.post(url_for('main.set_comment', photo_id=1), follow_redirects=True)
  213. data = response.get_data(as_text=True)
  214. self.assertEqual(response.status_code, 200)
  215. self.assertIn('Comment disabled', data)
  216. self.assertFalse(Photo.query.get(1).can_comment)
  217. response = self.client.post(url_for('main.set_comment', photo_id=1), follow_redirects=True)
  218. data = response.get_data(as_text=True)
  219. self.assertEqual(response.status_code, 200)
  220. self.assertIn('Comment enabled', data)
  221. self.assertTrue(Photo.query.get(1).can_comment)
  222. def test_reply_comment(self):
  223. self.login()
  224. response = self.client.get(url_for('main.reply_comment', comment_id=1), follow_redirects=True)
  225. data = response.get_data(as_text=True)
  226. self.assertIn('Reply to', data)
  227. def test_delete_photo(self):
  228. self.login()
  229. response = self.client.post(url_for('main.delete_photo', photo_id=2), follow_redirects=True)
  230. data = response.get_data(as_text=True)
  231. self.assertIn('Photo deleted.', data)
  232. self.assertIn('Normal User', data)
  233. def test_delete_comment(self):
  234. self.login()
  235. response = self.client.post(url_for('main.delete_comment', comment_id=1), follow_redirects=True)
  236. data = response.get_data(as_text=True)
  237. self.assertIn('Comment deleted.', data)
  238. def test_show_tag(self):
  239. response = self.client.get(url_for('main.show_tag', tag_id=1))
  240. data = response.get_data(as_text=True)
  241. self.assertIn('Order by time', data)
  242. response = self.client.get(url_for('main.show_tag', tag_id=1, order='by_collects'))
  243. data = response.get_data(as_text=True)
  244. self.assertIn('Order by collects', data)
  245. def test_delete_tag(self):
  246. photo = Photo.query.get(2)
  247. tag = Tag(name='test')
  248. photo.tags.append(tag)
  249. db.session.commit()
  250. self.login()
  251. response = self.client.post(url_for('main.delete_tag', photo_id=2, tag_id=2), follow_redirects=True)
  252. data = response.get_data(as_text=True)
  253. self.assertIn('Tag deleted.', data)
  254. self.assertEqual(photo.tags, [])
  255. self.assertIsNone(Tag.query.get(2))