5509 add test cases for custom fields

This commit is contained in:
Arthur 2022-09-12 14:41:51 -07:00
parent e3576e2614
commit c02f89e00b
3 changed files with 61 additions and 26 deletions

View File

@ -200,6 +200,7 @@ class ProviderNetworkTestCase(ViewTestCases.PrimaryObjectViewTestCase):
class CircuitTerminationTestCase( class CircuitTerminationTestCase(
ViewTestCases.EditObjectViewTestCase, ViewTestCases.EditObjectViewTestCase,
ViewTestCases.DeleteObjectViewTestCase, ViewTestCases.DeleteObjectViewTestCase,
ViewTestCases.CustomFieldViewTestCase,
): ):
model = CircuitTermination model = CircuitTermination

View File

@ -11,7 +11,7 @@ from extras.models import *
from utilities.testing import ViewTestCases, TestCase from utilities.testing import ViewTestCases, TestCase
class CustomFieldTestCase(ViewTestCases.PrimaryObjectViewTestCase): class CustomFieldTestCase(ViewTestCases.BaseObjectViewTestCase, ViewTestCases.CreateObjectViewTestCase):
model = CustomField model = CustomField
@classmethod @classmethod
@ -53,7 +53,7 @@ class CustomFieldTestCase(ViewTestCases.PrimaryObjectViewTestCase):
} }
class CustomLinkTestCase(ViewTestCases.PrimaryObjectViewTestCase): class CustomLinkTestCase(ViewTestCases.BaseObjectViewTestCase, ViewTestCases.CreateObjectViewTestCase):
model = CustomLink model = CustomLink
@classmethod @classmethod
@ -90,7 +90,7 @@ class CustomLinkTestCase(ViewTestCases.PrimaryObjectViewTestCase):
} }
class ExportTemplateTestCase(ViewTestCases.PrimaryObjectViewTestCase): class ExportTemplateTestCase(ViewTestCases.BaseObjectViewTestCase, ViewTestCases.CreateObjectViewTestCase):
model = ExportTemplate model = ExportTemplate
@classmethod @classmethod
@ -124,7 +124,7 @@ class ExportTemplateTestCase(ViewTestCases.PrimaryObjectViewTestCase):
} }
class WebhookTestCase(ViewTestCases.PrimaryObjectViewTestCase): class WebhookTestCase(ViewTestCases.BaseObjectViewTestCase, ViewTestCases.CreateObjectViewTestCase):
model = Webhook model = Webhook
@classmethod @classmethod
@ -168,7 +168,7 @@ class WebhookTestCase(ViewTestCases.PrimaryObjectViewTestCase):
} }
class TagTestCase(ViewTestCases.OrganizationalObjectViewTestCase): class TagTestCase(ViewTestCases.BaseObjectViewTestCase, ViewTestCases.CreateObjectViewTestCase):
model = Tag model = Tag
@classmethod @classmethod
@ -291,7 +291,8 @@ class JournalEntryTestCase(
ViewTestCases.DeleteObjectViewTestCase, ViewTestCases.DeleteObjectViewTestCase,
ViewTestCases.ListObjectsViewTestCase, ViewTestCases.ListObjectsViewTestCase,
ViewTestCases.BulkEditObjectsViewTestCase, ViewTestCases.BulkEditObjectsViewTestCase,
ViewTestCases.BulkDeleteObjectsViewTestCase ViewTestCases.BulkDeleteObjectsViewTestCase,
ViewTestCases.CustomFieldViewTestCase,
): ):
model = JournalEntry model = JournalEntry

View File

@ -3,8 +3,8 @@ from django.core.exceptions import ObjectDoesNotExist
from django.test import override_settings from django.test import override_settings
from django.urls import reverse from django.urls import reverse
from extras.choices import ObjectChangeActionChoices from extras.choices import ObjectChangeActionChoices, CustomFieldTypeChoices
from extras.models import ObjectChange from extras.models import ObjectChange, CustomField
from users.models import ObjectPermission from users.models import ObjectPermission
from .base import ModelTestCase from .base import ModelTestCase
from .utils import disable_warnings, post_data from .utils import disable_warnings, post_data
@ -857,16 +857,62 @@ class ViewTestCases:
for i, instance in enumerate(self._get_queryset().filter(pk__in=pk_list)): for i, instance in enumerate(self._get_queryset().filter(pk__in=pk_list)):
self.assertEqual(instance.name, f'{objects[i].name}X') self.assertEqual(instance.name, f'{objects[i].name}X')
class PrimaryObjectViewTestCase( class CustomFieldViewTestCase(ModelViewTestCase):
"""
Edit a single existing instance.
:form_data: Data to be used when updating the first existing object.
"""
form_data = {}
@override_settings(EXEMPT_VIEW_PERMISSIONS=['*'])
def test_save_custom_field(self):
initial_count = self._get_queryset().count()
# Assign unconstrained permission
obj_perm = ObjectPermission(
name='Test permission',
actions=['add']
)
obj_perm.save()
obj_perm.users.add(self.user)
obj_perm.object_types.add(ContentType.objects.get_for_model(self.model))
cf = CustomField.objects.create(type=CustomFieldTypeChoices.TYPE_TEXT, name='testcf_field', default='foo', required=False)
cf.content_types.set([ContentType.objects.get_for_model(self.model)])
data = self.form_data
cf_name = f'cf_{cf.name}'
data[cf_name] = "XYZZY"
# Try POST with custom field data
request = {
'path': self._get_url('add'),
'data': post_data(data),
}
self.assertHttpStatus(self.client.post(**request), 302)
self.assertEqual(initial_count + 1, self._get_queryset().count())
instance = self._get_queryset().order_by('pk').last()
self.assertEqual(instance.custom_field_data[cf.name], data[cf_name])
class BaseObjectViewTestCase(
GetObjectViewTestCase, GetObjectViewTestCase,
GetObjectChangelogViewTestCase, GetObjectChangelogViewTestCase,
CreateObjectViewTestCase,
EditObjectViewTestCase, EditObjectViewTestCase,
DeleteObjectViewTestCase, DeleteObjectViewTestCase,
ListObjectsViewTestCase, ListObjectsViewTestCase,
BulkImportObjectsViewTestCase, BulkImportObjectsViewTestCase,
BulkEditObjectsViewTestCase, BulkEditObjectsViewTestCase,
BulkDeleteObjectsViewTestCase, BulkDeleteObjectsViewTestCase,
):
"""
Base TestCase suitable for most views
"""
maxDiff = None
class PrimaryObjectViewTestCase(
BaseObjectViewTestCase,
CreateObjectViewTestCase,
CustomFieldViewTestCase,
): ):
""" """
TestCase suitable for testing all standard View functions for primary objects TestCase suitable for testing all standard View functions for primary objects
@ -874,15 +920,9 @@ class ViewTestCases:
maxDiff = None maxDiff = None
class OrganizationalObjectViewTestCase( class OrganizationalObjectViewTestCase(
GetObjectViewTestCase, BaseObjectViewTestCase,
GetObjectChangelogViewTestCase,
CreateObjectViewTestCase, CreateObjectViewTestCase,
EditObjectViewTestCase, CustomFieldViewTestCase,
DeleteObjectViewTestCase,
ListObjectsViewTestCase,
BulkImportObjectsViewTestCase,
BulkEditObjectsViewTestCase,
BulkDeleteObjectsViewTestCase,
): ):
""" """
TestCase suitable for all organizational objects TestCase suitable for all organizational objects
@ -903,16 +943,9 @@ class ViewTestCases:
maxDiff = None maxDiff = None
class DeviceComponentViewTestCase( class DeviceComponentViewTestCase(
GetObjectViewTestCase, BaseObjectViewTestCase,
GetObjectChangelogViewTestCase,
EditObjectViewTestCase,
DeleteObjectViewTestCase,
ListObjectsViewTestCase,
CreateMultipleObjectsViewTestCase, CreateMultipleObjectsViewTestCase,
BulkImportObjectsViewTestCase,
BulkEditObjectsViewTestCase,
BulkRenameObjectsViewTestCase, BulkRenameObjectsViewTestCase,
BulkDeleteObjectsViewTestCase,
): ):
""" """
TestCase suitable for testing device component models (ConsolePorts, Interfaces, etc.) TestCase suitable for testing device component models (ConsolePorts, Interfaces, etc.)