From 24ab0826743474ef3414aa81fd1e99a4eb16fe5f Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 3 Feb 2020 10:04:09 -0500 Subject: [PATCH] Add bulk delete view tests --- netbox/dcim/tests/test_views.py | 1 + netbox/utilities/testing/testcases.py | 30 ++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/netbox/dcim/tests/test_views.py b/netbox/dcim/tests/test_views.py index 5a4d78d50..34c0f38b4 100644 --- a/netbox/dcim/tests/test_views.py +++ b/netbox/dcim/tests/test_views.py @@ -1129,6 +1129,7 @@ class VirtualChassisTestCase(StandardTestCases.Views): # Disable inapplicable tests test_get_object = None test_import_objects = None + test_bulk_delete_objects = None # TODO: Requires special form handling test_create_object = None diff --git a/netbox/utilities/testing/testcases.py b/netbox/utilities/testing/testcases.py index a29fd4de9..ff4353962 100644 --- a/netbox/utilities/testing/testcases.py +++ b/netbox/utilities/testing/testcases.py @@ -107,7 +107,7 @@ class StandardTestCases: self.model._meta.model_name ) - if action in ('list', 'add', 'import'): + if action in ('list', 'add', 'import', 'bulk_delete'): return reverse(url_format.format(action)) elif action in ('get', 'edit', 'delete'): @@ -253,3 +253,31 @@ class StandardTestCases: self.assertHttpStatus(response, 200) self.assertEqual(self.model.objects.count(), initial_count + len(self.csv_data) - 1) + + @override_settings(EXEMPT_VIEW_PERMISSIONS=[]) + def test_bulk_delete_objects(self): + pk_list = self.model.objects.values_list('pk', flat=True) + + request = { + 'path': self._get_url('bulk_delete'), + 'data': { + 'pk': pk_list, + 'confirm': True, + '_confirm': True, # Form button + }, + 'follow': False, # Do not follow 302 redirects + } + + # Attempt to make the request without required permissions + with disable_warnings('django.request'): + self.assertHttpStatus(self.client.post(**request), 403) + + # Assign the required permission and submit again + self.add_permissions( + '{}.delete_{}'.format(self.model._meta.app_label, self.model._meta.model_name) + ) + response = self.client.post(**request) + self.assertHttpStatus(response, 302) + + # Check that all objects were deleted + self.assertEqual(self.model.objects.count(), 0)