Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: OBS-416 - add change_id to the response with errors in apply-change-set API endpoint #52

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions diode-netbox-plugin/netbox_diode_plugin/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,26 @@ def post(self, request, *args, **kwargs):
if not request_serializer.is_valid():
for field_error_name in request_serializer.errors:
if isinstance(request_serializer.errors[field_error_name], dict):
for _, error_values in request_serializer.errors[
for error_index, error_values in request_serializer.errors[
field_error_name
].items():
errors_dict = {}

errors_dict["change_id"] = request_serializer.data.get(
"change_set"
)[error_index].get("change_id")

for field_name, field_errors in error_values.items():
serializer_errors.append(
{field_name: f"{str(field_errors[0])}"}
)
errors_dict[field_name] = f"{str(field_errors[0])}"

serializer_errors.append(errors_dict)
else:
errors_dict = {
errors = {
field_error_name: f"{str(field_errors)}"
for field_errors in request_serializer.errors[field_error_name]
}

serializer_errors.append(errors_dict)
serializer_errors.append(errors)

return self._get_error_response(change_set_id, serializer_errors)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
)
from django.contrib.auth import get_user_model
from ipam.models import ASN, RIR
from rest_framework import status
from users.models import Token
from utilities.testing import APITestCase
from virtualization.models import Cluster, ClusterType
Expand Down Expand Up @@ -124,10 +125,23 @@ def setUp(self):

self.url = "/api/plugins/diode/apply-change-set/"

def send_request(self, payload, status_code=status.HTTP_200_OK):
"""Post the payload to the url and return the response."""
response = self.client.post(
self.url, data=payload, format="json", **self.user_header
)
self.assertEqual(response.status_code, status_code)
return response


class ApplyChangeSetTestCase(BaseApplyChangeSet):
"""ApplyChangeSet test cases."""

@staticmethod
def get_change_id(payload, index):
Julio-Oliveira-Encora marked this conversation as resolved.
Show resolved Hide resolved
"""Get change_id from payload."""
return payload.get("change_set")[index].get("change_id")

def test_change_type_create_return_200(self):
"""Test create change_type with successful."""
payload = {
Expand All @@ -153,11 +167,9 @@ def test_change_type_create_return_200(self):
],
}

response = self.client.post(
self.url, payload, format="json", **self.user_header
)
response = self.send_request(payload)

self.assertEqual(response.json().get("result"), "success")
self.assertEqual(response.status_code, 200)

def test_change_type_update_return_200(self):
"""Test update change_type with successful."""
Expand Down Expand Up @@ -190,7 +202,6 @@ def test_change_type_update_return_200(self):

site_updated = Site.objects.get(id=20)

self.assertEqual(response.status_code, 200)
self.assertEqual(response.json().get("result"), "success")
self.assertEqual(site_updated.name, "Site A")

Expand Down Expand Up @@ -219,14 +230,15 @@ def test_change_type_create_with_error_return_400(self):
],
}

response = self.client.post(
self.url, payload, format="json", **self.user_header
)
response = self.send_request(payload, status_code=status.HTTP_400_BAD_REQUEST)

site_created = Site.objects.filter(name="Site A")

self.assertEqual(response.status_code, 400)
self.assertEqual(response.json().get("result"), "failed")
self.assertEqual(
response.json().get("errors")[0].get("change_id"),
self.get_change_id(payload, 0),
)
self.assertIn(
'Expected a list of items but got type "int".',
response.json().get("errors")[0].get("asns"),
Expand Down Expand Up @@ -258,14 +270,15 @@ def test_change_type_update_with_error_return_400(self):
],
}

response = self.client.post(
self.url, payload, format="json", **self.user_header
)
response = self.send_request(payload, status_code=status.HTTP_400_BAD_REQUEST)

site_updated = Site.objects.get(id=20)

self.assertEqual(response.status_code, 400)
self.assertEqual(response.json().get("result"), "failed")
self.assertEqual(
response.json().get("errors")[0].get("change_id"),
self.get_change_id(payload, 0),
)
self.assertIn(
'Expected a list of items but got type "int".',
response.json().get("errors")[0].get("asns"),
Expand Down Expand Up @@ -312,10 +325,7 @@ def test_change_type_create_with_multiples_objects_return_200(self):
],
}

response = self.client.post(
self.url, payload, format="json", **self.user_header
)
self.assertEqual(response.status_code, 200)
response = self.send_request(payload)

self.assertEqual(response.json().get("result"), "success")

Expand Down Expand Up @@ -359,14 +369,11 @@ def test_change_type_update_with_multiples_objects_return_200(self):
],
}

response = self.client.post(
self.url, payload, format="json", **self.user_header
)
response = self.send_request(payload)

site_updated = Site.objects.get(id=20)
device_updated = Device.objects.get(id=10)

self.assertEqual(response.status_code, 200)
self.assertEqual(response.json().get("result"), "success")
self.assertEqual(site_updated.name, "Site A")
self.assertEqual(device_updated.name, "Test Device 3")
Expand Down Expand Up @@ -411,24 +418,25 @@ def test_change_type_create_and_update_with_error_in_one_object_return_400(self)
],
}

response = self.client.post(
self.url, payload, format="json", **self.user_header
)
response = self.send_request(payload, status_code=status.HTTP_400_BAD_REQUEST)

site_created = Site.objects.filter(name="Site Z")
device_created = Device.objects.filter(name="Test Device 4")

self.assertEqual(response.status_code, 400)
self.assertEqual(response.json().get("result"), "failed")
self.assertEqual(
response.json().get("errors")[0].get("change_id"),
self.get_change_id(payload, 1),
)
self.assertIn(
"Related object not found using the provided numeric ID",
response.json().get("errors")[0].get("device_type"),
)
self.assertFalse(site_created.exists())
self.assertFalse(device_created.exists())

def test_multiples_change_type_create_with_error_in_two_objects_return_400(self):
"""Test create change_type with error in two object."""
def test_multiples_create_type_error_in_two_objects_return_400(self):
"""Test create with error in two objects."""
payload = {
"change_set_id": str(uuid.uuid4()),
"change_set": [
Expand Down Expand Up @@ -482,22 +490,31 @@ def test_multiples_change_type_create_with_error_in_two_objects_return_400(self)
],
}

response = self.client.post(
self.url, payload, format="json", **self.user_header
)
response = self.send_request(payload, status_code=status.HTTP_400_BAD_REQUEST)

site_created = Site.objects.filter(name="Site Z")
device_created = Device.objects.filter(name="Test Device 4")

self.assertEqual(response.status_code, 400)
self.assertEqual(response.json().get("result"), "failed")

self.assertEqual(
response.json().get("errors")[0].get("change_id"),
self.get_change_id(payload, 1),
)
self.assertIn(
"Related object not found using the provided numeric ID",
response.json().get("errors")[0].get("device_type"),
)

self.assertEqual(
response.json().get("errors")[1].get("change_id"),
self.get_change_id(payload, 2),
)
self.assertIn(
"Related object not found using the provided numeric ID",
response.json().get("errors")[1].get("device_type"),
)

self.assertFalse(site_created.exists())
self.assertFalse(device_created.exists())

Expand Down Expand Up @@ -531,7 +548,7 @@ def test_change_type_update_with_object_id_not_exist_return_400(self):
)

site_updated = Site.objects.get(id=20)
self.assertEqual(response.status_code, 400)

self.assertEqual(response.json()[0], "Object with id 30 does not exist")
self.assertEqual(site_updated.name, "Site 2")

Expand Down Expand Up @@ -560,10 +577,9 @@ def test_change_set_id_field_not_provided_return_400(self):
],
}

response = self.client.post(
self.url, payload, format="json", **self.user_header
)
self.assertEqual(response.status_code, 400)
response = self.send_request(payload, status_code=status.HTTP_400_BAD_REQUEST)

self.assertIsNone(response.json().get("errors")[0].get("change_id"))
self.assertEqual(
response.json().get("errors")[0].get("change_set_id"),
"This field may not be null.",
Expand Down Expand Up @@ -596,10 +612,8 @@ def test_change_set_id_change_id_and_change_type_field_not_provided_return_400(
],
}

response = self.client.post(
self.url, payload, format="json", **self.user_header
)
self.assertEqual(response.status_code, 400)
response = self.send_request(payload, status_code=status.HTTP_400_BAD_REQUEST)

self.assertEqual(
response.json().get("errors")[0].get("change_set_id"),
"Must be a valid UUID.",
Expand All @@ -609,7 +623,7 @@ def test_change_set_id_change_id_and_change_type_field_not_provided_return_400(
"Must be a valid UUID.",
)
self.assertEqual(
response.json().get("errors")[2].get("change_type"),
response.json().get("errors")[1].get("change_type"),
"This field may not be blank.",
)

Expand All @@ -620,10 +634,8 @@ def test_change_set_id_field_and_change_set_not_provided_return_400(self):
"change_set": [],
}

response = self.client.post(
self.url, payload, format="json", **self.user_header
)
self.assertEqual(response.status_code, 400)
response = self.send_request(payload, status_code=status.HTTP_400_BAD_REQUEST)

self.assertEqual(
response.json().get("errors")[0].get("change_set_id"),
"Must be a valid UUID.",
Expand All @@ -632,3 +644,71 @@ def test_change_set_id_field_and_change_set_not_provided_return_400(self):
response.json().get("errors")[1].get("change_set"),
"This list may not be empty.",
)

def test_change_type_and_object_type_provided_return_400(
self,
):
"""Test change_type and object_type incorrect."""
payload = {
"change_set_id": str(uuid.uuid4()),
"change_set": [
{
"change_id": str(uuid.uuid4()),
"change_type": None,
"object_version": None,
"object_type": "",
"object_id": None,
"data": {
"name": "Site A",
"slug": "site-a",
"facility": "Alpha",
"description": "",
"physical_address": "123 Fake St Lincoln NE 68588",
"shipping_address": "123 Fake St Lincoln NE 68588",
"comments": "Lorem ipsum etcetera",
},
},
{
"change_id": str(uuid.uuid4()),
"change_type": "",
"object_version": None,
"object_type": "dcim.site",
"object_id": None,
"data": {
"name": "Site Z",
"slug": "site-z",
"facility": "Betha",
"description": "",
"physical_address": "123 Fake St Lincoln NE 68588",
"shipping_address": "123 Fake St Lincoln NE 68588",
"comments": "Lorem ipsum etcetera",
},
},
],
}

response = self.send_request(payload, status_code=status.HTTP_400_BAD_REQUEST)

# First item of change_set
self.assertEqual(
response.json().get("errors")[0].get("change_id"),
self.get_change_id(payload, 0),
)
self.assertEqual(
response.json().get("errors")[0].get("change_type"),
"This field may not be null.",
)
self.assertEqual(
response.json().get("errors")[0].get("object_type"),
"This field may not be blank.",
)

# Second item of change_set
self.assertEqual(
response.json().get("errors")[1].get("change_id"),
self.get_change_id(payload, 1),
)
self.assertEqual(
response.json().get("errors")[1].get("change_type"),
"This field may not be blank.",
)
Loading