mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-27 02:48:38 -06:00
Incorporate changelog messages for object bulk view tests
This commit is contained in:
parent
6acde0f432
commit
3fba47eb08
@ -452,7 +452,7 @@ class BulkImportView(GetReturnURLMixin, BaseMultiObjectView):
|
|||||||
record[field_name] = cf.default
|
record[field_name] = cf.default
|
||||||
|
|
||||||
# Record changelog message (if any)
|
# Record changelog message (if any)
|
||||||
instance._changelog_message = form.cleaned_data.pop('changelog_message', '')
|
instance._changelog_message = form.cleaned_data.get('changelog_message', '')
|
||||||
|
|
||||||
# Instantiate the model form for the object
|
# Instantiate the model form for the object
|
||||||
model_form_kwargs = {
|
model_form_kwargs = {
|
||||||
|
@ -628,6 +628,10 @@ class ViewTestCases:
|
|||||||
'csv_delimiter': CSVDelimiterChoices.AUTO,
|
'csv_delimiter': CSVDelimiterChoices.AUTO,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# If supported, add a changelog message
|
||||||
|
if issubclass(self.model, ChangeLoggingMixin):
|
||||||
|
data['changelog_message'] = get_random_string(10)
|
||||||
|
|
||||||
# Assign model-level permission
|
# Assign model-level permission
|
||||||
obj_perm = ObjectPermission(
|
obj_perm = ObjectPermission(
|
||||||
name='Test permission',
|
name='Test permission',
|
||||||
@ -641,9 +645,20 @@ class ViewTestCases:
|
|||||||
self.assertHttpStatus(self.client.get(self._get_url('bulk_import')), 200)
|
self.assertHttpStatus(self.client.get(self._get_url('bulk_import')), 200)
|
||||||
|
|
||||||
# Test POST with permission
|
# Test POST with permission
|
||||||
self.assertHttpStatus(self.client.post(self._get_url('bulk_import'), data), 302)
|
response = self.client.post(self._get_url('bulk_import'), data)
|
||||||
|
self.assertHttpStatus(response, 302)
|
||||||
self.assertEqual(self._get_queryset().count(), initial_count + len(self.csv_data) - 1)
|
self.assertEqual(self._get_queryset().count(), initial_count + len(self.csv_data) - 1)
|
||||||
|
|
||||||
|
# Verify ObjectChange creation
|
||||||
|
if issubclass(self.model, ChangeLoggingMixin):
|
||||||
|
request_id = response.headers.get('X-Request-ID')
|
||||||
|
self.assertIsNotNone(request_id, "Unable to determine request ID from response")
|
||||||
|
objectchanges = ObjectChange.objects.filter(request_id=request_id)
|
||||||
|
self.assertEqual(len(objectchanges), len(self.csv_data) - 1)
|
||||||
|
for oc in objectchanges:
|
||||||
|
self.assertEqual(oc.action, ObjectChangeActionChoices.ACTION_CREATE)
|
||||||
|
self.assertEqual(oc.message, data['changelog_message'])
|
||||||
|
|
||||||
@override_settings(EXEMPT_VIEW_PERMISSIONS=['*'])
|
@override_settings(EXEMPT_VIEW_PERMISSIONS=['*'])
|
||||||
def test_bulk_update_objects_with_permission(self):
|
def test_bulk_update_objects_with_permission(self):
|
||||||
if not hasattr(self, 'csv_update_data'):
|
if not hasattr(self, 'csv_update_data'):
|
||||||
@ -745,6 +760,10 @@ class ViewTestCases:
|
|||||||
'_apply': True, # Form button
|
'_apply': True, # Form button
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# If supported, add a changelog message
|
||||||
|
if issubclass(self.model, ChangeLoggingMixin):
|
||||||
|
data['changelog_message'] = get_random_string(10)
|
||||||
|
|
||||||
# Append the form data to the request
|
# Append the form data to the request
|
||||||
data.update(post_data(self.bulk_edit_data))
|
data.update(post_data(self.bulk_edit_data))
|
||||||
|
|
||||||
@ -758,10 +777,21 @@ class ViewTestCases:
|
|||||||
obj_perm.object_types.add(ObjectType.objects.get_for_model(self.model))
|
obj_perm.object_types.add(ObjectType.objects.get_for_model(self.model))
|
||||||
|
|
||||||
# Try POST with model-level permission
|
# Try POST with model-level permission
|
||||||
self.assertHttpStatus(self.client.post(self._get_url('bulk_edit'), data), 302)
|
response = self.client.post(self._get_url('bulk_edit'), data)
|
||||||
|
self.assertHttpStatus(response, 302)
|
||||||
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.assertInstanceEqual(instance, self.bulk_edit_data)
|
self.assertInstanceEqual(instance, self.bulk_edit_data)
|
||||||
|
|
||||||
|
# Verify ObjectChange creation
|
||||||
|
if issubclass(self.model, ChangeLoggingMixin):
|
||||||
|
request_id = response.headers.get('X-Request-ID')
|
||||||
|
self.assertIsNotNone(request_id, "Unable to determine request ID from response")
|
||||||
|
objectchanges = ObjectChange.objects.filter(request_id=request_id)
|
||||||
|
self.assertEqual(len(objectchanges), len(pk_list))
|
||||||
|
for oc in objectchanges:
|
||||||
|
self.assertEqual(oc.action, ObjectChangeActionChoices.ACTION_UPDATE)
|
||||||
|
self.assertEqual(oc.message, data['changelog_message'])
|
||||||
|
|
||||||
@override_settings(EXEMPT_VIEW_PERMISSIONS=['*'], EXEMPT_EXCLUDE_MODELS=[])
|
@override_settings(EXEMPT_VIEW_PERMISSIONS=['*'], EXEMPT_EXCLUDE_MODELS=[])
|
||||||
def test_bulk_edit_objects_with_constrained_permission(self):
|
def test_bulk_edit_objects_with_constrained_permission(self):
|
||||||
pk_list = list(self._get_queryset().values_list('pk', flat=True)[:3])
|
pk_list = list(self._get_queryset().values_list('pk', flat=True)[:3])
|
||||||
@ -829,6 +859,10 @@ class ViewTestCases:
|
|||||||
'_confirm': True, # Form button
|
'_confirm': True, # Form button
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# If supported, add a changelog message
|
||||||
|
if issubclass(self.model, ChangeLoggingMixin):
|
||||||
|
data['changelog_message'] = get_random_string(10)
|
||||||
|
|
||||||
# Assign unconstrained permission
|
# Assign unconstrained permission
|
||||||
obj_perm = ObjectPermission(
|
obj_perm = ObjectPermission(
|
||||||
name='Test permission',
|
name='Test permission',
|
||||||
@ -839,9 +873,20 @@ class ViewTestCases:
|
|||||||
obj_perm.object_types.add(ObjectType.objects.get_for_model(self.model))
|
obj_perm.object_types.add(ObjectType.objects.get_for_model(self.model))
|
||||||
|
|
||||||
# Try POST with model-level permission
|
# Try POST with model-level permission
|
||||||
self.assertHttpStatus(self.client.post(self._get_url('bulk_delete'), data), 302)
|
response = self.client.post(self._get_url('bulk_delete'), data)
|
||||||
|
self.assertHttpStatus(response, 302)
|
||||||
self.assertEqual(self._get_queryset().count(), 0)
|
self.assertEqual(self._get_queryset().count(), 0)
|
||||||
|
|
||||||
|
# Verify ObjectChange creation
|
||||||
|
if issubclass(self.model, ChangeLoggingMixin):
|
||||||
|
request_id = response.headers.get('X-Request-ID')
|
||||||
|
self.assertIsNotNone(request_id, "Unable to determine request ID from response")
|
||||||
|
objectchanges = ObjectChange.objects.filter(request_id=request_id)
|
||||||
|
self.assertEqual(len(objectchanges), len(pk_list))
|
||||||
|
for oc in objectchanges:
|
||||||
|
self.assertEqual(oc.action, ObjectChangeActionChoices.ACTION_DELETE)
|
||||||
|
self.assertEqual(oc.message, data['changelog_message'])
|
||||||
|
|
||||||
def test_bulk_delete_objects_with_constrained_permission(self):
|
def test_bulk_delete_objects_with_constrained_permission(self):
|
||||||
pk_list = self._get_queryset().values_list('pk', flat=True)
|
pk_list = self._get_queryset().values_list('pk', flat=True)
|
||||||
data = {
|
data = {
|
||||||
|
Loading…
Reference in New Issue
Block a user