test_views.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import urllib.parse
  2. import uuid
  3. from django.contrib.auth.models import User
  4. from django.contrib.contenttypes.models import ContentType
  5. from django.test import override_settings
  6. from django.urls import reverse
  7. from dcim.models import Site
  8. from extras.choices import ObjectChangeActionChoices
  9. from extras.models import ConfigContext, CustomLink, ObjectChange, Tag
  10. from utilities.testing import ViewTestCases, TestCase
  11. class TagTestCase(ViewTestCases.OrganizationalObjectViewTestCase):
  12. model = Tag
  13. @classmethod
  14. def setUpTestData(cls):
  15. Tag.objects.bulk_create((
  16. Tag(name='Tag 1', slug='tag-1'),
  17. Tag(name='Tag 2', slug='tag-2'),
  18. Tag(name='Tag 3', slug='tag-3'),
  19. ))
  20. cls.form_data = {
  21. 'name': 'Tag X',
  22. 'slug': 'tag-x',
  23. 'color': 'c0c0c0',
  24. 'comments': 'Some comments',
  25. }
  26. cls.csv_data = (
  27. "name,slug,color,description",
  28. "Tag 4,tag-4,ff0000,Fourth tag",
  29. "Tag 5,tag-5,00ff00,Fifth tag",
  30. "Tag 6,tag-6,0000ff,Sixth tag",
  31. )
  32. cls.bulk_edit_data = {
  33. 'color': '00ff00',
  34. }
  35. # TODO: Change base class to PrimaryObjectViewTestCase
  36. # Blocked by absence of standard create/edit, bulk create views
  37. class ConfigContextTestCase(
  38. ViewTestCases.GetObjectViewTestCase,
  39. ViewTestCases.GetObjectChangelogViewTestCase,
  40. ViewTestCases.DeleteObjectViewTestCase,
  41. ViewTestCases.ListObjectsViewTestCase,
  42. ViewTestCases.BulkEditObjectsViewTestCase,
  43. ViewTestCases.BulkDeleteObjectsViewTestCase
  44. ):
  45. model = ConfigContext
  46. @classmethod
  47. def setUpTestData(cls):
  48. site = Site.objects.create(name='Site 1', slug='site-1')
  49. # Create three ConfigContexts
  50. for i in range(1, 4):
  51. configcontext = ConfigContext(
  52. name='Config Context {}'.format(i),
  53. data={'foo': i}
  54. )
  55. configcontext.save()
  56. configcontext.sites.add(site)
  57. cls.form_data = {
  58. 'name': 'Config Context X',
  59. 'weight': 200,
  60. 'description': 'A new config context',
  61. 'is_active': True,
  62. 'regions': [],
  63. 'sites': [site.pk],
  64. 'roles': [],
  65. 'platforms': [],
  66. 'tenant_groups': [],
  67. 'tenants': [],
  68. 'tags': [],
  69. 'data': '{"foo": 123}',
  70. }
  71. cls.bulk_edit_data = {
  72. 'weight': 300,
  73. 'is_active': False,
  74. 'description': 'New description',
  75. }
  76. # TODO: Convert to StandardTestCases.Views
  77. class ObjectChangeTestCase(TestCase):
  78. user_permissions = (
  79. 'extras.view_objectchange',
  80. )
  81. @classmethod
  82. def setUpTestData(cls):
  83. site = Site(name='Site 1', slug='site-1')
  84. site.save()
  85. # Create three ObjectChanges
  86. user = User.objects.create_user(username='testuser2')
  87. for i in range(1, 4):
  88. oc = site.to_objectchange(action=ObjectChangeActionChoices.ACTION_UPDATE)
  89. oc.user = user
  90. oc.request_id = uuid.uuid4()
  91. oc.save()
  92. def test_objectchange_list(self):
  93. url = reverse('extras:objectchange_list')
  94. params = {
  95. "user": User.objects.first().pk,
  96. }
  97. response = self.client.get('{}?{}'.format(url, urllib.parse.urlencode(params)))
  98. self.assertHttpStatus(response, 200)
  99. def test_objectchange(self):
  100. objectchange = ObjectChange.objects.first()
  101. response = self.client.get(objectchange.get_absolute_url())
  102. self.assertHttpStatus(response, 200)
  103. class CustomLinkTest(TestCase):
  104. user_permissions = ['dcim.view_site']
  105. def test_view_object_with_custom_link(self):
  106. customlink = CustomLink(
  107. content_type=ContentType.objects.get_for_model(Site),
  108. name='Test',
  109. link_text='FOO {{ obj.name }} BAR',
  110. link_url='http://example.com/?site={{ obj.slug }}',
  111. new_window=False
  112. )
  113. customlink.save()
  114. site = Site(name='Test Site', slug='test-site')
  115. site.save()
  116. response = self.client.get(site.get_absolute_url(), follow=True)
  117. self.assertEqual(response.status_code, 200)
  118. self.assertIn(f'FOO {site.name} BAR', str(response.content))