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.

140 line
5.5 KiB

3 年之前
  1. # -*- coding: utf-8 -*-
  2. from flask import url_for
  3. from phshare.models import User, Photo
  4. from tests.base import BaseTestCase
  5. class AjaxTestCase(BaseTestCase):
  6. def test_notifications_count(self):
  7. response = self.client.get(url_for('ajax.notifications_count'))
  8. data = response.get_json()
  9. self.assertEqual(response.status_code, 403)
  10. self.assertEqual(data['message'], 'Login required.')
  11. self.login()
  12. response = self.client.get(url_for('ajax.notifications_count'))
  13. self.assertEqual(response.status_code, 200)
  14. def test_get_profile(self):
  15. response = self.client.get(url_for('ajax.get_profile', user_id=1))
  16. data = response.get_data(as_text=True)
  17. self.assertEqual(response.status_code, 200)
  18. self.assertIn('Admin', data)
  19. def test_followers_count(self):
  20. response = self.client.get(url_for('ajax.followers_count', user_id=1))
  21. data = response.get_json()
  22. self.assertEqual(response.status_code, 200)
  23. self.assertEqual(data['count'], 0)
  24. user = User.query.get(2)
  25. user.follow(User.query.get(1))
  26. response = self.client.get(url_for('ajax.followers_count', user_id=1))
  27. data = response.get_json()
  28. self.assertEqual(response.status_code, 200)
  29. self.assertEqual(data['count'], 1)
  30. def test_collectors_count(self):
  31. response = self.client.get(url_for('ajax.collectors_count', photo_id=1))
  32. data = response.get_json()
  33. self.assertEqual(response.status_code, 200)
  34. self.assertEqual(data['count'], 0)
  35. user = User.query.get(1)
  36. user.collect(Photo.query.get(1))
  37. response = self.client.get(url_for('ajax.collectors_count', photo_id=1))
  38. data = response.get_json()
  39. self.assertEqual(response.status_code, 200)
  40. self.assertEqual(data['count'], 1)
  41. def test_collect(self):
  42. response = self.client.post(url_for('ajax.collect', photo_id=1))
  43. data = response.get_json()
  44. self.assertEqual(response.status_code, 403)
  45. self.assertEqual(data['message'], 'Login required.')
  46. self.login(email='unconfirmed@helloflask.com', password='123')
  47. response = self.client.post(url_for('ajax.collect', photo_id=1))
  48. data = response.get_json()
  49. self.assertEqual(response.status_code, 400)
  50. self.assertEqual(data['message'], 'Confirm account required.')
  51. self.logout()
  52. self.login()
  53. response = self.client.post(url_for('ajax.collect', photo_id=1))
  54. data = response.get_json()
  55. self.assertEqual(response.status_code, 200)
  56. self.assertEqual(data['message'], 'Photo collected.')
  57. response = self.client.post(url_for('ajax.collect', photo_id=1))
  58. data = response.get_json()
  59. self.assertEqual(response.status_code, 400)
  60. self.assertEqual(data['message'], 'Already collected.')
  61. def test_uncollect(self):
  62. response = self.client.post(url_for('ajax.uncollect', photo_id=1))
  63. data = response.get_json()
  64. self.assertEqual(response.status_code, 403)
  65. self.assertEqual(data['message'], 'Login required.')
  66. self.login()
  67. response = self.client.post(url_for('ajax.uncollect', photo_id=1))
  68. data = response.get_json()
  69. self.assertEqual(response.status_code, 400)
  70. self.assertEqual(data['message'], 'Not collect yet.')
  71. user = User.query.get(2)
  72. user.collect(Photo.query.get(1))
  73. response = self.client.post(url_for('ajax.uncollect', photo_id=1))
  74. data = response.get_json()
  75. self.assertEqual(response.status_code, 200)
  76. self.assertEqual(data['message'], 'Collect canceled.')
  77. def test_follow(self):
  78. response = self.client.post(url_for('ajax.follow', username='admin'))
  79. data = response.get_json()
  80. self.assertEqual(response.status_code, 403)
  81. self.assertEqual(data['message'], 'Login required.')
  82. self.login(email='unconfirmed@helloflask.com', password='123')
  83. response = self.client.post(url_for('ajax.follow', username='admin'))
  84. data = response.get_json()
  85. self.assertEqual(response.status_code, 400)
  86. self.assertEqual(data['message'], 'Confirm account required.')
  87. self.logout()
  88. self.login()
  89. response = self.client.post(url_for('ajax.follow', username='normal'))
  90. data = response.get_json()
  91. self.assertEqual(response.status_code, 400)
  92. self.assertEqual(data['message'], 'Already followed.')
  93. response = self.client.post(url_for('ajax.follow', username='admin'))
  94. data = response.get_json()
  95. self.assertEqual(response.status_code, 200)
  96. self.assertEqual(data['message'], 'User followed.')
  97. def test_unfollow(self):
  98. response = self.client.post(url_for('ajax.unfollow', username='admin'))
  99. data = response.get_json()
  100. self.assertEqual(response.status_code, 403)
  101. self.assertEqual(data['message'], 'Login required.')
  102. self.login()
  103. response = self.client.post(url_for('ajax.unfollow', username='admin'))
  104. data = response.get_json()
  105. self.assertEqual(response.status_code, 400)
  106. self.assertEqual(data['message'], 'Not follow yet.')
  107. user = User.query.get(2)
  108. user.follow(User.query.get(1))
  109. response = self.client.post(url_for('ajax.unfollow', username='admin'))
  110. data = response.get_json()
  111. self.assertEqual(response.status_code, 200)
  112. self.assertEqual(data['message'], 'Follow canceled.')