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.

168 line
6.7 KiB

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