test_views.py 7.7 KB

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