test_dashboard.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from django.contrib.auth.models import User
  2. try:
  3. from django.core.urlresolvers import reverse
  4. except ImportError: # Django 1.11
  5. from django.urls import reverse
  6. from django.test import TestCase, Client
  7. from jet.dashboard.dashboard import Dashboard
  8. from jet.dashboard.modules import LinkList, RecentActions
  9. from jet.dashboard.models import UserDashboardModule
  10. from jet.tests.dashboard import TestIndexDashboard
  11. class DashboardTestCase(TestCase):
  12. class Request:
  13. def __init__(self, user):
  14. self.user = user
  15. def setUp(self):
  16. self._login()
  17. self._init_dashboard()
  18. def _login(self):
  19. username = 'admin'
  20. email = 'admin@example.com'
  21. password = 'admin'
  22. self.admin = Client()
  23. self.admin_user = User.objects.create_superuser(username, email, password)
  24. return self.admin.login(username=username, password=password)
  25. def _init_dashboard(self):
  26. UserDashboardModule.objects.create(
  27. title='',
  28. module='jet.dashboard.modules.LinkList',
  29. app_label=None,
  30. user=self.admin_user.pk,
  31. column=0,
  32. order=0
  33. )
  34. UserDashboardModule.objects.create(
  35. title='',
  36. module='jet.dashboard.modules.RecentActions',
  37. app_label=None,
  38. user=self.admin_user.pk,
  39. column=0,
  40. order=1
  41. )
  42. self.dashboard = TestIndexDashboard({'request': self.Request(self.admin_user)})
  43. def test_custom_columns(self):
  44. self.assertEqual(self.dashboard.columns, 3)
  45. def test_init_with_context_called(self):
  46. self.assertTrue(self.dashboard.init_with_context_called)
  47. def test_load_modules(self):
  48. self.assertEqual(len(self.dashboard.modules), 2)
  49. self.assertTrue(isinstance(self.dashboard.modules[0], LinkList))
  50. self.assertTrue(isinstance(self.dashboard.modules[1], RecentActions))
  51. def test_media(self):
  52. media = self.dashboard.media()
  53. self.assertEqual(len(media.js), 2)
  54. self.assertEqual(media.js[0], 'file.js')
  55. self.assertEqual(media.js[1], 'file2.js')
  56. self.assertEqual(len(media.css), 2)
  57. self.assertEqual(media.css[0], 'file.css')
  58. self.assertEqual(media.css[1], 'file2.css')
  59. def test_index_dashboard_view(self):
  60. response = self.admin.get(reverse('admin:index'))
  61. self.assertEqual(response.status_code, 200)
  62. self.assertTrue('dashboard' in response.context)
  63. dashboard = response.context['dashboard']
  64. self.assertIsInstance(dashboard, Dashboard)
  65. self.assertIsNone(dashboard.app_label)
  66. def test_app_index_dashboard_view(self):
  67. app_label = 'tests'
  68. response = self.admin.get(reverse('admin:app_list', args=(app_label,)))
  69. self.assertEqual(response.status_code, 200)
  70. self.assertTrue('dashboard' in response.context)
  71. dashboard = response.context['dashboard']
  72. self.assertIsInstance(dashboard, Dashboard)
  73. self.assertEqual(dashboard.app_label, app_label)