test_dashboard.py 2.9 KB

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