test_views.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import urllib.parse
  2. import uuid
  3. from django.contrib.auth.models import User
  4. from django.urls import reverse
  5. from dcim.models import Site
  6. from extras.choices import ObjectChangeActionChoices
  7. from extras.models import ConfigContext, ObjectChange, Tag
  8. from utilities.testing import StandardTestCases, TestCase
  9. class TagTestCase(StandardTestCases.Views):
  10. model = Tag
  11. # Disable inapplicable tests
  12. test_create_object = None
  13. test_import_objects = None
  14. @classmethod
  15. def setUpTestData(cls):
  16. Tag.objects.bulk_create((
  17. Tag(name='Tag 1', slug='tag-1'),
  18. Tag(name='Tag 2', slug='tag-2'),
  19. Tag(name='Tag 3', slug='tag-3'),
  20. ))
  21. cls.form_data = {
  22. 'name': 'Tag X',
  23. 'slug': 'tag-x',
  24. 'color': 'c0c0c0',
  25. 'comments': 'Some comments',
  26. }
  27. cls.bulk_edit_data = {
  28. 'color': '00ff00',
  29. }
  30. class ConfigContextTestCase(StandardTestCases.Views):
  31. model = ConfigContext
  32. # Disable inapplicable tests
  33. test_import_objects = None
  34. # TODO: Resolve model discrepancies when creating/editing ConfigContexts
  35. test_create_object = None
  36. test_edit_object = None
  37. @classmethod
  38. def setUpTestData(cls):
  39. site = Site.objects.create(name='Site 1', slug='site-1')
  40. # Create three ConfigContexts
  41. for i in range(1, 4):
  42. configcontext = ConfigContext(
  43. name='Config Context {}'.format(i),
  44. data={'foo': i}
  45. )
  46. configcontext.save()
  47. configcontext.sites.add(site)
  48. cls.form_data = {
  49. 'name': 'Config Context X',
  50. 'weight': 200,
  51. 'description': 'A new config context',
  52. 'is_active': True,
  53. 'regions': [],
  54. 'sites': [site.pk],
  55. 'roles': [],
  56. 'platforms': [],
  57. 'tenant_groups': [],
  58. 'tenants': [],
  59. 'tags': [],
  60. 'data': '{"foo": 123}',
  61. }
  62. cls.bulk_edit_data = {
  63. 'weight': 300,
  64. 'is_active': False,
  65. 'description': 'New description',
  66. }
  67. # TODO: Convert to StandardTestCases.Views
  68. class ObjectChangeTestCase(TestCase):
  69. user_permissions = (
  70. 'extras.view_objectchange',
  71. )
  72. @classmethod
  73. def setUpTestData(cls):
  74. site = Site(name='Site 1', slug='site-1')
  75. site.save()
  76. # Create three ObjectChanges
  77. user = User.objects.create_user(username='testuser2')
  78. for i in range(1, 4):
  79. oc = site.to_objectchange(action=ObjectChangeActionChoices.ACTION_UPDATE)
  80. oc.user = user
  81. oc.request_id = uuid.uuid4()
  82. oc.save()
  83. def test_objectchange_list(self):
  84. url = reverse('extras:objectchange_list')
  85. params = {
  86. "user": User.objects.first(),
  87. }
  88. response = self.client.get('{}?{}'.format(url, urllib.parse.urlencode(params)))
  89. self.assertHttpStatus(response, 200)
  90. def test_objectchange(self):
  91. objectchange = ObjectChange.objects.first()
  92. response = self.client.get(objectchange.get_absolute_url())
  93. self.assertHttpStatus(response, 200)