forked from netbox-community/netbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes netbox-community#14755: ValueError in web UI after REST API acc…
…epts invalid custom-field choice-set data
- Loading branch information
julio.oliveira
committed
Jan 15, 2024
1 parent
3d94141
commit e87e113
Showing
3 changed files
with
70 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
netbox/extras/tests/test_custom_field_choice_sets_endpoint.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from django.contrib.auth import get_user_model | ||
from rest_framework.test import APITestCase | ||
|
||
from users.models import Token | ||
|
||
User = get_user_model() | ||
|
||
|
||
class CustomFieldChoiceSetsEndpointTest(APITestCase): | ||
|
||
def setUp(self): | ||
self.super_user = User.objects.create_user(username='testuser', is_staff=True, is_superuser=True) | ||
self.token = Token.objects.create(user=self.super_user) | ||
self.header = {'HTTP_AUTHORIZATION': f'Token {self.token.key}'} | ||
self.url = '/api/extras/custom-field-choice-sets/' | ||
|
||
def test_extra_choices_only_one_choice_element_return_400(self): | ||
payload = { | ||
"name": "test", | ||
"extra_choices": [["choice1"]] | ||
} | ||
|
||
response = self.client.post(self.url, payload, format='json', **self.header) | ||
|
||
self.assertEqual(response.status_code, 400) | ||
|
||
def test_extra_choices_two_wrong_choice_elements_return_400(self): | ||
payload = { | ||
"name": "test", | ||
"extra_choices": [["choice1"], ["choice2"]] | ||
} | ||
|
||
response = self.client.post(self.url, payload, format='json', **self.header) | ||
|
||
self.assertEqual(response.status_code, 400) | ||
|
||
def test_extra_choices_one_is_wrong_other_correct_choice_elements_return_400(self): | ||
payload = { | ||
"name": "test", | ||
"extra_choices": [["1A", "choice1"], ["choice2"]] | ||
} | ||
|
||
response = self.client.post(self.url, payload, format='json', **self.header) | ||
|
||
self.assertEqual(response.status_code, 400) | ||
|
||
def test_extra_choices_correct_choices_return_201(self): | ||
payload = { | ||
'name': 'Choice Set', | ||
'extra_choices': [ | ||
['4A', 'Choice 1'], | ||
['4B', 'Choice 2'], | ||
['4C', 'Choice 3'], | ||
], | ||
} | ||
|
||
response = self.client.post(self.url, payload, format='json', **self.header) | ||
|
||
self.assertEqual(response.status_code, 201) |