Skip to content

Commit 19d1282

Browse files
authored
Fixes #18838: Correctly reject invalid falsy local context data (#18860)
* Correctly reject invalid falsy local context data. * move tests
1 parent 2266a8a commit 19d1282

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

netbox/extras/models/configs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def clean(self):
200200
super().clean()
201201

202202
# Verify that JSON data is provided as an object
203-
if self.local_context_data and type(self.local_context_data) is not dict:
203+
if self.local_context_data is not None and type(self.local_context_data) is not dict:
204204
raise ValidationError(
205205
{'local_context_data': _('JSON data must be in object form. Example:') + ' {"foo": 123}'}
206206
)

netbox/extras/tests/test_models.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from django.forms import ValidationError
12
from django.test import TestCase
23

34
from core.models import ObjectType
@@ -478,3 +479,30 @@ def test_multiple_tags_return_distinct_objects_with_seperate_config_contexts(sel
478479
annotated_queryset = Device.objects.filter(name=device.name).annotate_config_context_data()
479480
self.assertEqual(ConfigContext.objects.get_for_object(device).count(), 2)
480481
self.assertEqual(device.get_config_context(), annotated_queryset[0].get_config_context())
482+
483+
def test_valid_local_context_data(self):
484+
device = Device.objects.first()
485+
device.local_context_data = None
486+
device.clean()
487+
488+
device.local_context_data = {"foo": "bar"}
489+
device.clean()
490+
491+
def test_invalid_local_context_data(self):
492+
device = Device.objects.first()
493+
494+
device.local_context_data = ""
495+
with self.assertRaises(ValidationError):
496+
device.clean()
497+
498+
device.local_context_data = 0
499+
with self.assertRaises(ValidationError):
500+
device.clean()
501+
502+
device.local_context_data = False
503+
with self.assertRaises(ValidationError):
504+
device.clean()
505+
506+
device.local_context_data = 'foo'
507+
with self.assertRaises(ValidationError):
508+
device.clean()

0 commit comments

Comments
 (0)