mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-27 10:58:37 -06:00
Correctly reject invalid falsy local context data.
This commit is contained in:
parent
a9fd191086
commit
a6eb2d0103
@ -1396,6 +1396,34 @@ class DeviceTest(APIViewTestCases.APIViewTestCase):
|
||||
self.assertHttpStatus(response, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data['content'], f'Config for device {device.name}')
|
||||
|
||||
def test_valid_local_context_data(self):
|
||||
self.add_permissions('dcim.change_device')
|
||||
device = Device.objects.first()
|
||||
url = reverse('dcim-api:device-detail', kwargs={'pk': device.pk})
|
||||
|
||||
response = self.client.patch(url, {"local_context_data": None}, format='json', **self.header)
|
||||
self.assertHttpStatus(response, status.HTTP_200_OK)
|
||||
|
||||
response = self.client.patch(url, {"local_context_data": {"foo": "bar"}}, format='json', **self.header)
|
||||
self.assertHttpStatus(response, status.HTTP_200_OK)
|
||||
|
||||
def test_invalid_local_context_data(self):
|
||||
self.add_permissions('dcim.change_device')
|
||||
device = Device.objects.first()
|
||||
url = reverse('dcim-api:device-detail', kwargs={'pk': device.pk})
|
||||
|
||||
response = self.client.patch(url, {"local_context_data": ""}, format='json', **self.header)
|
||||
self.assertContains(response, 'JSON data must be in object form.', status_code=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
response = self.client.patch(url, {"local_context_data": 0}, format='json', **self.header)
|
||||
self.assertContains(response, 'JSON data must be in object form.', status_code=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
response = self.client.patch(url, {"local_context_data": False}, format='json', **self.header)
|
||||
self.assertContains(response, 'JSON data must be in object form.', status_code=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
response = self.client.patch(url, {"local_context_data": "foo"}, format='json', **self.header)
|
||||
self.assertContains(response, 'JSON data must be in object form.', status_code=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
class ModuleTest(APIViewTestCases.APIViewTestCase):
|
||||
model = Module
|
||||
|
@ -200,7 +200,7 @@ class ConfigContextModel(models.Model):
|
||||
super().clean()
|
||||
|
||||
# Verify that JSON data is provided as an object
|
||||
if self.local_context_data and type(self.local_context_data) is not dict:
|
||||
if self.local_context_data is not None and type(self.local_context_data) is not dict:
|
||||
raise ValidationError(
|
||||
{'local_context_data': _('JSON data must be in object form. Example:') + ' {"foo": 123}'}
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user