From bfbf97aec9119539f7f42cf16f52d0ca8203ba60 Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Wed, 24 Aug 2022 15:49:36 -0400 Subject: [PATCH] Closes #10031: Enforce 'application/json' content type for REST API requests --- docs/release-notes/version-3.3.md | 1 + netbox/ipam/tests/test_api.py | 4 ++-- netbox/netbox/settings.py | 4 +++- netbox/users/tests/test_api.py | 4 ++-- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/docs/release-notes/version-3.3.md b/docs/release-notes/version-3.3.md index 5c4c8c654..765944950 100644 --- a/docs/release-notes/version-3.3.md +++ b/docs/release-notes/version-3.3.md @@ -5,6 +5,7 @@ ### Enhancements * [#6454](https://github.com/netbox-community/netbox/issues/6454) - Include contextual help when creating first objects in UI +* [#10031](https://github.com/netbox-community/netbox/issues/10031) - Enforce `application/json` content type for REST API requests * [#10033](https://github.com/netbox-community/netbox/issues/10033) - Disable "add termination" button for point-to-point L2VPNs with two terminations * [#10037](https://github.com/netbox-community/netbox/issues/10037) - Add link to create child interface to interface context menu * [#10061](https://github.com/netbox-community/netbox/issues/10061) - Replicate type when cloning L2VPN instances diff --git a/netbox/ipam/tests/test_api.py b/netbox/ipam/tests/test_api.py index 4c07e0a90..5dc708cd0 100644 --- a/netbox/ipam/tests/test_api.py +++ b/netbox/ipam/tests/test_api.py @@ -390,7 +390,7 @@ class PrefixTest(APIViewTestCases.APIViewTestCase): self.assertEqual(response.data['description'], data['description']) # Try to create one more IP - response = self.client.post(url, {}, **self.header) + response = self.client.post(url, {}, format='json', **self.header) self.assertHttpStatus(response, status.HTTP_409_CONFLICT) self.assertIn('detail', response.data) @@ -487,7 +487,7 @@ class IPRangeTest(APIViewTestCases.APIViewTestCase): self.assertEqual(response.data['description'], data['description']) # Try to create one more IP - response = self.client.post(url, {}, **self.header) + response = self.client.post(url, {}, format='json', **self.header) self.assertHttpStatus(response, status.HTTP_409_CONFLICT) self.assertIn('detail', response.data) diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index 0edce8f69..4438d338b 100644 --- a/netbox/netbox/settings.py +++ b/netbox/netbox/settings.py @@ -533,6 +533,9 @@ REST_FRAMEWORK = { ), 'DEFAULT_METADATA_CLASS': 'netbox.api.metadata.BulkOperationMetadata', 'DEFAULT_PAGINATION_CLASS': 'netbox.api.pagination.OptionalLimitOffsetPagination', + 'DEFAULT_PARSER_CLASSES': ( + 'rest_framework.parsers.JSONParser', + ), 'DEFAULT_PERMISSION_CLASSES': ( 'netbox.api.authentication.TokenPermissions', ), @@ -542,7 +545,6 @@ REST_FRAMEWORK = { ), 'DEFAULT_VERSION': REST_FRAMEWORK_VERSION, 'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.AcceptHeaderVersioning', - # 'PAGE_SIZE': PAGINATE_COUNT, 'SCHEMA_COERCE_METHOD_NAMES': { # Default mappings 'retrieve': 'read', diff --git a/netbox/users/tests/test_api.py b/netbox/users/tests/test_api.py index bcfc9cf14..a0bf8a49e 100644 --- a/netbox/users/tests/test_api.py +++ b/netbox/users/tests/test_api.py @@ -124,7 +124,7 @@ class TokenTest( user = User.objects.create_user(**data) url = reverse('users-api:token_provision') - response = self.client.post(url, **self.header, data=data) + response = self.client.post(url, data, format='json', **self.header) self.assertEqual(response.status_code, 201) self.assertIn('key', response.data) self.assertEqual(len(response.data['key']), 40) @@ -141,7 +141,7 @@ class TokenTest( } url = reverse('users-api:token_provision') - response = self.client.post(url, **self.header, data=data) + response = self.client.post(url, data, format='json', **self.header) self.assertEqual(response.status_code, 403)