test_views.py 5.4 KB

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