mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-25 18:08:38 -06:00
Incorporate changelog messages for object view tests
This commit is contained in:
parent
f17438132a
commit
6acde0f432
@ -14,7 +14,7 @@ from netbox.choices import CSVDelimiterChoices, ImportFormatChoices
|
||||
from netbox.models.features import ChangeLoggingMixin, CustomFieldsMixin
|
||||
from users.models import ObjectPermission
|
||||
from .base import ModelTestCase
|
||||
from .utils import add_custom_field_data, disable_warnings, post_data
|
||||
from .utils import add_custom_field_data, disable_warnings, get_random_string, post_data
|
||||
|
||||
__all__ = (
|
||||
'ModelViewTestCase',
|
||||
@ -169,6 +169,11 @@ class ViewTestCases:
|
||||
if issubclass(self.model, CustomFieldsMixin):
|
||||
add_custom_field_data(self.form_data, self.model)
|
||||
|
||||
# If supported, add a changelog message
|
||||
if issubclass(self.model, ChangeLoggingMixin):
|
||||
if 'changelog_message' not in self.form_data:
|
||||
self.form_data['changelog_message'] = get_random_string(10)
|
||||
|
||||
# Try POST with model-level permission
|
||||
initial_count = self._get_queryset().count()
|
||||
request = {
|
||||
@ -181,13 +186,14 @@ class ViewTestCases:
|
||||
self.assertInstanceEqual(instance, self.form_data, exclude=self.validation_excluded_fields)
|
||||
|
||||
# Verify ObjectChange creation
|
||||
if issubclass(instance.__class__, ChangeLoggingMixin):
|
||||
if issubclass(self.model, ChangeLoggingMixin):
|
||||
objectchanges = ObjectChange.objects.filter(
|
||||
changed_object_type=ContentType.objects.get_for_model(instance),
|
||||
changed_object_id=instance.pk
|
||||
)
|
||||
self.assertEqual(len(objectchanges), 1)
|
||||
self.assertEqual(objectchanges[0].action, ObjectChangeActionChoices.ACTION_CREATE)
|
||||
self.assertEqual(objectchanges[0].message, self.form_data['changelog_message'])
|
||||
|
||||
@override_settings(EXEMPT_VIEW_PERMISSIONS=['*'], EXEMPT_EXCLUDE_MODELS=[])
|
||||
def test_create_object_with_constrained_permission(self):
|
||||
@ -272,6 +278,11 @@ class ViewTestCases:
|
||||
if issubclass(self.model, CustomFieldsMixin):
|
||||
add_custom_field_data(self.form_data, self.model)
|
||||
|
||||
# If supported, add a changelog message
|
||||
if issubclass(self.model, ChangeLoggingMixin):
|
||||
if 'changelog_message' not in self.form_data:
|
||||
self.form_data['changelog_message'] = get_random_string(10)
|
||||
|
||||
# Try POST with model-level permission
|
||||
request = {
|
||||
'path': self._get_url('edit', instance),
|
||||
@ -282,13 +293,14 @@ class ViewTestCases:
|
||||
self.assertInstanceEqual(instance, self.form_data, exclude=self.validation_excluded_fields)
|
||||
|
||||
# Verify ObjectChange creation
|
||||
if issubclass(instance.__class__, ChangeLoggingMixin):
|
||||
if issubclass(self.model, ChangeLoggingMixin):
|
||||
objectchanges = ObjectChange.objects.filter(
|
||||
changed_object_type=ContentType.objects.get_for_model(instance),
|
||||
changed_object_id=instance.pk
|
||||
)
|
||||
self.assertEqual(len(objectchanges), 1)
|
||||
self.assertEqual(objectchanges[0].action, ObjectChangeActionChoices.ACTION_UPDATE)
|
||||
self.assertEqual(objectchanges[0].message, self.form_data['changelog_message'])
|
||||
|
||||
@override_settings(EXEMPT_VIEW_PERMISSIONS=['*'], EXEMPT_EXCLUDE_MODELS=[])
|
||||
def test_edit_object_with_constrained_permission(self):
|
||||
@ -348,6 +360,7 @@ class ViewTestCases:
|
||||
@override_settings(EXEMPT_VIEW_PERMISSIONS=['*'])
|
||||
def test_delete_object_with_permission(self):
|
||||
instance = self._get_queryset().first()
|
||||
form_data = {'confirm': True}
|
||||
|
||||
# Assign model-level permission
|
||||
obj_perm = ObjectPermission(
|
||||
@ -361,23 +374,28 @@ class ViewTestCases:
|
||||
# Try GET with model-level permission
|
||||
self.assertHttpStatus(self.client.get(self._get_url('delete', instance)), 200)
|
||||
|
||||
# If supported, add a changelog message
|
||||
if issubclass(self.model, ChangeLoggingMixin):
|
||||
form_data['changelog_message'] = get_random_string(10)
|
||||
|
||||
# Try POST with model-level permission
|
||||
request = {
|
||||
'path': self._get_url('delete', instance),
|
||||
'data': post_data({'confirm': True}),
|
||||
'data': post_data(form_data),
|
||||
}
|
||||
self.assertHttpStatus(self.client.post(**request), 302)
|
||||
with self.assertRaises(ObjectDoesNotExist):
|
||||
self._get_queryset().get(pk=instance.pk)
|
||||
|
||||
# Verify ObjectChange creation
|
||||
if issubclass(instance.__class__, ChangeLoggingMixin):
|
||||
if issubclass(self.model, ChangeLoggingMixin):
|
||||
objectchanges = ObjectChange.objects.filter(
|
||||
changed_object_type=ContentType.objects.get_for_model(instance),
|
||||
changed_object_id=instance.pk
|
||||
)
|
||||
self.assertEqual(len(objectchanges), 1)
|
||||
self.assertEqual(objectchanges[0].action, ObjectChangeActionChoices.ACTION_DELETE)
|
||||
self.assertEqual(objectchanges[0].message, form_data['changelog_message'])
|
||||
|
||||
@override_settings(EXEMPT_VIEW_PERMISSIONS=['*'])
|
||||
def test_delete_object_with_constrained_permission(self):
|
||||
|
Loading…
Reference in New Issue
Block a user