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.

162 lines
6.5 KiB

3 years ago
  1. # -*- coding: utf-8 -*-
  2. from flask import url_for
  3. from phshare.extensions import db
  4. from phshare.models import User, Role, Tag
  5. from tests.base import BaseTestCase
  6. class AdminTestCase(BaseTestCase):
  7. def setUp(self):
  8. super(AdminTestCase, self).setUp()
  9. self.login(email='admin@helloflask.com', password='123')
  10. def test_index_page(self):
  11. response = self.client.get(url_for('admin.index'))
  12. data = response.get_data(as_text=True)
  13. self.assertIn('Phshare Dashboard', data)
  14. def test_bad_permission(self):
  15. self.logout()
  16. response = self.client.get(url_for('admin.index'), follow_redirects=True)
  17. data = response.get_data(as_text=True)
  18. self.assertIn('Please log in to access this page.', data)
  19. self.assertNotIn('Phshare Dashboard', data)
  20. self.login() # normal user, without MODERATOR permission
  21. response = self.client.get(url_for('admin.index'))
  22. data = response.get_data(as_text=True)
  23. self.assertEqual(response.status_code, 403)
  24. self.assertNotIn('Phshare Dashboard', data)
  25. def test_edit_profile_admin(self):
  26. role_id = Role.query.filter_by(name='Locked').first().id
  27. response = self.client.post(url_for('admin.edit_profile_admin', user_id=2), data=dict(
  28. username='newname',
  29. role=role_id,
  30. confirmed=True,
  31. active=True,
  32. name='New Name',
  33. email='new@helloflask.com'
  34. ), follow_redirects=True)
  35. data = response.get_data(as_text=True)
  36. self.assertIn('Profile updated.', data)
  37. user = User.query.get(2)
  38. self.assertEqual(user.name, 'New Name')
  39. self.assertEqual(user.username, 'newname')
  40. self.assertEqual(user.email, 'new@helloflask.com')
  41. self.assertEqual(user.role.name, 'Locked')
  42. def test_block_user(self):
  43. response = self.client.post(url_for('admin.block_user', user_id=2), follow_redirects=True)
  44. data = response.get_data(as_text=True)
  45. self.assertIn('Account blocked.', data)
  46. user = User.query.get(2)
  47. self.assertEqual(user.active, False)
  48. def test_unblock_user(self):
  49. user = User.query.get(2)
  50. user.active = False
  51. db.session.commit()
  52. self.assertEqual(user.active, False)
  53. response = self.client.post(url_for('admin.unblock_user', user_id=2), follow_redirects=True)
  54. data = response.get_data(as_text=True)
  55. self.assertIn('Block canceled.', data)
  56. user = User.query.get(2)
  57. self.assertEqual(user.active, True)
  58. def test_lock_user(self):
  59. response = self.client.post(url_for('admin.lock_user', user_id=2), follow_redirects=True)
  60. data = response.get_data(as_text=True)
  61. self.assertIn('Account locked.', data)
  62. user = User.query.get(2)
  63. self.assertEqual(user.role.name, 'Locked')
  64. def test_unlock_user(self):
  65. user = User.query.get(2)
  66. user.role = Role.query.filter_by(name='Locked').first()
  67. db.session.commit()
  68. self.assertEqual(user.role.name, 'Locked')
  69. response = self.client.post(url_for('admin.unlock_user', user_id=2), follow_redirects=True)
  70. data = response.get_data(as_text=True)
  71. self.assertIn('Lock canceled.', data)
  72. user = User.query.get(2)
  73. self.assertEqual(user.role.name, 'User')
  74. def test_delete_tag(self):
  75. tag = Tag()
  76. db.session.add(tag)
  77. db.session.commit()
  78. self.assertIsNotNone(Tag.query.get(1))
  79. response = self.client.post(url_for('admin.delete_tag', tag_id=1), follow_redirects=True)
  80. data = response.get_data(as_text=True)
  81. self.assertIn('Tag deleted.', data)
  82. self.assertEqual(Tag.query.get(1), None)
  83. def test_manage_user_page(self):
  84. response = self.client.get(url_for('admin.manage_user'))
  85. data = response.get_data(as_text=True)
  86. self.assertIn('Manage Users', data)
  87. self.assertIn('Admin', data)
  88. self.assertIn('Locked', data)
  89. self.assertIn('Normal', data)
  90. response = self.client.get(url_for('admin.manage_user', filter='locked'))
  91. data = response.get_data(as_text=True)
  92. self.assertIn('Manage Users', data)
  93. self.assertIn('Locked User', data)
  94. self.assertNotIn('Normal User', data)
  95. response = self.client.get(url_for('admin.manage_user', filter='blocked'))
  96. data = response.get_data(as_text=True)
  97. self.assertIn('Manage Users', data)
  98. self.assertIn('Blocked User', data)
  99. self.assertNotIn('Locked User', data)
  100. self.assertNotIn('Normal User', data)
  101. response = self.client.get(url_for('admin.manage_user', filter='administrator'))
  102. data = response.get_data(as_text=True)
  103. self.assertIn('Manage Users', data)
  104. self.assertIn('Admin', data)
  105. self.assertNotIn('Blocked User', data)
  106. self.assertNotIn('Locked User', data)
  107. self.assertNotIn('Normal User', data)
  108. response = self.client.get(url_for('admin.manage_user', filter='moderator'))
  109. data = response.get_data(as_text=True)
  110. self.assertIn('Manage Users', data)
  111. self.assertIn('Admin', data)
  112. self.assertNotIn('Blocked User', data)
  113. self.assertNotIn('Locked User', data)
  114. self.assertNotIn('Normal User', data)
  115. def test_manage_photo_page(self):
  116. response = self.client.get(url_for('admin.manage_photo'))
  117. data = response.get_data(as_text=True)
  118. self.assertIn('Manage Photos', data)
  119. self.assertIn('Order by flag <span class="oi oi-elevator"></span>', data)
  120. response = self.client.get(url_for('admin.manage_photo', order='by_time'))
  121. data = response.get_data(as_text=True)
  122. self.assertIn('Manage Photos', data)
  123. self.assertIn('Order by time <span class="oi oi-elevator"></span>', data)
  124. def test_manage_tag_page(self):
  125. response = self.client.get(url_for('admin.manage_tag'))
  126. data = response.get_data(as_text=True)
  127. self.assertIn('Manage Tags', data)
  128. def test_manage_comment_page(self):
  129. response = self.client.get(url_for('admin.manage_comment'))
  130. data = response.get_data(as_text=True)
  131. self.assertIn('Manage Comments', data)
  132. self.assertIn('Order by flag <span class="oi oi-elevator"></span>', data)
  133. response = self.client.get(url_for('admin.manage_comment', order='by_time'))
  134. data = response.get_data(as_text=True)
  135. self.assertIn('Manage Comments', data)
  136. self.assertIn('Order by time <span class="oi oi-elevator"></span>', data)