test_views.py 8.7 KB

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