test_views.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. self.assertEqual(response.status_code, 200)
  54. module = UserDashboardModule.objects.get(pk=module.pk)
  55. settings = json.loads(module.settings)
  56. self.assertEqual(module.title, new_title)
  57. self.assertEqual(settings['layout'], new_layout)
  58. module.delete()
  59. def test_add_bookmark_view(self):
  60. url = 'http://test.com/'
  61. title = 'Title'
  62. response = self.admin.post(reverse('jet:add_bookmark'), {'url': url, 'title': title})
  63. self.assertEqual(response.status_code, 200)
  64. response = json.loads(response.content.decode())
  65. self.assertFalse(response['error'])
  66. self.assertNotEqual(response['id'], None)
  67. bookmark = Bookmark.objects.get(pk=response['id'])
  68. self.assertNotEqual(bookmark, None)
  69. self.assertEqual(bookmark.title, title)
  70. self.assertEqual(bookmark.url, url)
  71. def test_add_bookmark_view_unauthorized(self):
  72. url = 'http://test.com/'
  73. title = 'Title'
  74. response = self.client.post(reverse('jet:add_bookmark'), {'url': url, 'title': title})
  75. self.assertEqual(response.status_code, 200)
  76. response = json.loads(response.content.decode())
  77. self.assertTrue(response['error'])
  78. def test_remove_bookmark_view(self):
  79. url = 'http://test.com/'
  80. title = 'Title'
  81. bookmark = Bookmark.objects.create(url=url, title=title, user=self.admin_user.pk)
  82. response = self.admin.post(reverse('jet:remove_bookmark'), {'id': bookmark.id})
  83. self.assertEqual(response.status_code, 200)
  84. response = json.loads(response.content.decode())
  85. self.assertFalse(response['error'])
  86. self.assertFalse(Bookmark.objects.filter(pk=bookmark.pk).exists())
  87. def test_toggle_application_pin_view(self):
  88. app_label = 'test_app'
  89. response = self.admin.post(reverse('jet:toggle_application_pin'), {'app_label': app_label})
  90. self.assertEqual(response.status_code, 200)
  91. response = json.loads(response.content.decode())
  92. self.assertFalse(response['error'])
  93. self.assertTrue(response['pinned'])
  94. response = self.admin.post(reverse('jet:toggle_application_pin'), {'app_label': app_label})
  95. self.assertEqual(response.status_code, 200)
  96. response = json.loads(response.content.decode())
  97. self.assertFalse(response['error'])
  98. self.assertFalse(response['pinned'])
  99. def test_update_dashboard_modules_view(self):
  100. app_label = None
  101. module_0 = UserDashboardModule.objects.create(
  102. title='',
  103. module='jet.modules.LinkList',
  104. app_label=app_label,
  105. user=self.admin_user.pk,
  106. column=0,
  107. order=0
  108. )
  109. module_1 = UserDashboardModule.objects.create(
  110. title='',
  111. module='jet.modules.LinkList',
  112. app_label=app_label,
  113. user=self.admin_user.pk,
  114. column=0,
  115. order=1
  116. )
  117. response = self.admin.post(reverse('jet:update_dashboard_modules'), {
  118. 'app_label': '',
  119. 'modules': json.dumps([
  120. {'id': module_0.pk, 'column': 0, 'order': 1},
  121. {'id': module_1.pk, 'column': 0, 'order': 0}
  122. ])
  123. })
  124. self.assertEqual(response.status_code, 200)
  125. response = json.loads(response.content.decode())
  126. self.assertFalse(response['error'])
  127. module_0 = UserDashboardModule.objects.get(pk=module_0.pk)
  128. module_1 = UserDashboardModule.objects.get(pk=module_1.pk)
  129. self.assertEqual(module_0.order, 1)
  130. self.assertEqual(module_1.order, 0)
  131. module_0.delete()
  132. module_1.delete()
  133. def test_add_user_dashboard_module_view(self):
  134. response = self.admin.post(reverse('jet:add_user_dashboard_module'), {
  135. 'app_label': '',
  136. 'type': 'available_children',
  137. 'module': 0
  138. })
  139. self.assertEqual(response.status_code, 200)
  140. response = json.loads(response.content.decode())
  141. self.assertFalse(response['error'])
  142. self.assertNotEqual(response['id'], None)
  143. module = UserDashboardModule.objects.get(pk=response['id'])
  144. self.assertNotEqual(module, None)
  145. def test_update_dashboard_module_collapse_view(self):
  146. module = UserDashboardModule.objects.create(
  147. title='',
  148. module='jet.modules.LinkList',
  149. app_label=None,
  150. user=self.admin_user.pk,
  151. column=0,
  152. order=0
  153. )
  154. response = self.admin.post(reverse('jet:update_dashboard_module_collapse'), {
  155. 'id': module.pk, 'collapsed': True
  156. })
  157. self.assertEqual(response.status_code, 200)
  158. response = json.loads(response.content.decode())
  159. self.assertFalse(response['error'])
  160. self.assertTrue(response['collapsed'])
  161. module = UserDashboardModule.objects.get(pk=module.pk)
  162. response = self.admin.post(reverse('jet:update_dashboard_module_collapse'), {
  163. 'id': module.pk, 'collapsed': False
  164. })
  165. self.assertEqual(response.status_code, 200)
  166. response = json.loads(response.content.decode())
  167. self.assertFalse(response['error'])
  168. self.assertFalse(response['collapsed'])
  169. module.delete()
  170. def test_remove_dashboard_module_view(self):
  171. module = UserDashboardModule.objects.create(
  172. title='',
  173. module='jet.modules.LinkList',
  174. app_label=None,
  175. user=self.admin_user.pk,
  176. column=0,
  177. order=0
  178. )
  179. response = self.admin.post(reverse('jet:remove_dashboard_module'), {'id': module.pk})
  180. self.assertEqual(response.status_code, 200)
  181. response = json.loads(response.content.decode())
  182. self.assertFalse(response['error'])
  183. self.assertFalse(UserDashboardModule.objects.filter(pk=module.pk).exists())