test_views.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import json
  2. from django.contrib.auth.models import User
  3. try:
  4. from django.core.urlresolvers import reverse
  5. except ImportError: # Django 1.11
  6. from django.urls import reverse
  7. from django.test import TestCase, Client
  8. from jet.dashboard.modules import LinkList
  9. from jet.models import Bookmark
  10. from jet.dashboard.models import UserDashboardModule
  11. class ViewsTestCase(TestCase):
  12. def setUp(self):
  13. self.assertTrue(self._login())
  14. def _login(self):
  15. username = 'admin'
  16. email = 'admin@example.com'
  17. password = 'admin'
  18. self.admin = Client()
  19. self.admin_user = User.objects.create_superuser(username, email, password)
  20. return self.admin.login(username=username, password=password)
  21. def test_module_update_view(self):
  22. title = 'Quick links Test'
  23. new_title = title + '2'
  24. new_layout = 'stacked'
  25. module = UserDashboardModule.objects.create(
  26. title=title,
  27. module='jet.dashboard.modules.LinkList',
  28. app_label=None,
  29. user=self.admin_user.pk,
  30. column=0,
  31. order=0,
  32. settings='{"layout": "inline"}',
  33. children='[]'
  34. )
  35. response = self.admin.get(reverse('jet-dashboard:update_module', kwargs={'pk': module.pk}))
  36. self.assertEqual(response.status_code, 200)
  37. self.assertTrue(response.context['module'] is not None)
  38. self.assertTrue(isinstance(response.context['module'], LinkList))
  39. self.assertEqual(response.context['module'].title, title)
  40. post = {
  41. 'title': new_title,
  42. 'layout': new_layout,
  43. 'children-TOTAL_FORMS': '2',
  44. 'children-INITIAL_FORMS': '1',
  45. 'children-MIN_NUM_FORMS': '0',
  46. 'children-MAX_NUM_FORMS': '1000',
  47. 'children-0-url': 'http://docs.djangoproject.com/',
  48. 'children-0-title': 'Django documentation',
  49. 'children-0-external': 'on',
  50. 'children-1-url': '',
  51. 'children-1-title': '',
  52. 'children-__prefix__-url': '',
  53. 'children-__prefix__-title': '',
  54. '_save': 'Save'
  55. }
  56. self.admin.post(reverse('jet-dashboard:update_module', kwargs={'pk': module.pk}), post)
  57. self.assertEqual(response.status_code, 200)
  58. module = UserDashboardModule.objects.get(pk=module.pk)
  59. settings = json.loads(module.settings)
  60. self.assertEqual(module.title, new_title)
  61. self.assertEqual(settings['layout'], new_layout)
  62. module.delete()
  63. def test_add_bookmark_view(self):
  64. url = 'http://test.com/'
  65. title = 'Title'
  66. response = self.admin.post(reverse('jet:add_bookmark'), {'url': url, 'title': title})
  67. self.assertEqual(response.status_code, 200)
  68. response = json.loads(response.content.decode())
  69. self.assertFalse(response['error'])
  70. self.assertNotEqual(response['id'], None)
  71. bookmark = Bookmark.objects.get(pk=response['id'])
  72. self.assertNotEqual(bookmark, None)
  73. self.assertEqual(bookmark.title, title)
  74. self.assertEqual(bookmark.url, url)
  75. def test_add_bookmark_view_unauthorized(self):
  76. url = 'http://test.com/'
  77. title = 'Title'
  78. response = self.client.post(reverse('jet:add_bookmark'), {'url': url, 'title': title})
  79. self.assertEqual(response.status_code, 200)
  80. response = json.loads(response.content.decode())
  81. self.assertTrue(response['error'])
  82. def test_remove_bookmark_view(self):
  83. url = 'http://test.com/'
  84. title = 'Title'
  85. bookmark = Bookmark.objects.create(url=url, title=title, user=self.admin_user.pk)
  86. response = self.admin.post(reverse('jet:remove_bookmark'), {'id': bookmark.id})
  87. self.assertEqual(response.status_code, 200)
  88. response = json.loads(response.content.decode())
  89. self.assertFalse(response['error'])
  90. self.assertFalse(Bookmark.objects.filter(pk=bookmark.pk).exists())
  91. def test_toggle_application_pin_view(self):
  92. app_label = 'test_app'
  93. response = self.admin.post(reverse('jet:toggle_application_pin'), {'app_label': app_label})
  94. self.assertEqual(response.status_code, 200)
  95. response = json.loads(response.content.decode())
  96. self.assertFalse(response['error'])
  97. self.assertTrue(response['pinned'])
  98. response = self.admin.post(reverse('jet:toggle_application_pin'), {'app_label': app_label})
  99. self.assertEqual(response.status_code, 200)
  100. response = json.loads(response.content.decode())
  101. self.assertFalse(response['error'])
  102. self.assertFalse(response['pinned'])
  103. def test_update_dashboard_modules_view(self):
  104. app_label = None
  105. module_0 = UserDashboardModule.objects.create(
  106. title='',
  107. module='jet.dashboard.modules.LinkList',
  108. app_label=app_label,
  109. user=self.admin_user.pk,
  110. column=0,
  111. order=0
  112. )
  113. module_1 = UserDashboardModule.objects.create(
  114. title='',
  115. module='jet.dashboard.modules.LinkList',
  116. app_label=app_label,
  117. user=self.admin_user.pk,
  118. column=0,
  119. order=1
  120. )
  121. response = self.admin.post(reverse('jet-dashboard:update_dashboard_modules'), {
  122. 'app_label': '',
  123. 'modules': json.dumps([
  124. {'id': module_0.pk, 'column': 0, 'order': 1},
  125. {'id': module_1.pk, 'column': 0, 'order': 0}
  126. ])
  127. })
  128. self.assertEqual(response.status_code, 200)
  129. response = json.loads(response.content.decode())
  130. self.assertFalse(response['error'])
  131. module_0 = UserDashboardModule.objects.get(pk=module_0.pk)
  132. module_1 = UserDashboardModule.objects.get(pk=module_1.pk)
  133. self.assertEqual(module_0.order, 1)
  134. self.assertEqual(module_1.order, 0)
  135. module_0.delete()
  136. module_1.delete()
  137. def test_add_user_dashboard_module_view(self):
  138. response = self.admin.post(reverse('jet-dashboard:add_user_dashboard_module'), {
  139. 'app_label': '',
  140. 'type': 'available_children',
  141. 'module': 0
  142. })
  143. self.assertEqual(response.status_code, 200)
  144. response = json.loads(response.content.decode())
  145. self.assertFalse(response['error'])
  146. self.assertNotEqual(response['id'], None)
  147. module = UserDashboardModule.objects.get(pk=response['id'])
  148. self.assertNotEqual(module, None)
  149. def test_add_user_app_dashboard_module_view(self):
  150. app_label = 'auth'
  151. response = self.admin.post(reverse('jet-dashboard:add_user_dashboard_module'), {
  152. 'app_label': app_label,
  153. 'type': 'available_children',
  154. 'module': 0
  155. })
  156. self.assertEqual(response.status_code, 200)
  157. response = json.loads(response.content.decode())
  158. self.assertFalse(response['error'])
  159. self.assertNotEqual(response['id'], None)
  160. module = UserDashboardModule.objects.get(pk=response['id'])
  161. self.assertNotEqual(module, None)
  162. self.assertEqual(module.app_label, app_label)
  163. def test_update_dashboard_module_collapse_view(self):
  164. module = UserDashboardModule.objects.create(
  165. title='',
  166. module='jet.dashboard.modules.LinkList',
  167. app_label=None,
  168. user=self.admin_user.pk,
  169. column=0,
  170. order=0
  171. )
  172. response = self.admin.post(reverse('jet-dashboard:update_dashboard_module_collapse'), {
  173. 'id': module.pk, 'collapsed': True
  174. })
  175. self.assertEqual(response.status_code, 200)
  176. response = json.loads(response.content.decode())
  177. self.assertFalse(response['error'])
  178. self.assertTrue(response['collapsed'])
  179. module = UserDashboardModule.objects.get(pk=module.pk)
  180. response = self.admin.post(reverse('jet-dashboard:update_dashboard_module_collapse'), {
  181. 'id': module.pk, 'collapsed': False
  182. })
  183. self.assertEqual(response.status_code, 200)
  184. response = json.loads(response.content.decode())
  185. self.assertFalse(response['error'])
  186. self.assertFalse(response['collapsed'])
  187. module.delete()
  188. def test_remove_dashboard_module_view(self):
  189. module = UserDashboardModule.objects.create(
  190. title='',
  191. module='jet.dashboard.modules.LinkList',
  192. app_label=None,
  193. user=self.admin_user.pk,
  194. column=0,
  195. order=0
  196. )
  197. response = self.admin.post(reverse('jet-dashboard:remove_dashboard_module'), {'id': module.pk})
  198. self.assertEqual(response.status_code, 200)
  199. response = json.loads(response.content.decode())
  200. self.assertFalse(response['error'])
  201. self.assertFalse(UserDashboardModule.objects.filter(pk=module.pk).exists())