From 93ae9796957936b5a4ed89ea86c9ce9cdfd963d7 Mon Sep 17 00:00:00 2001 From: Hui Song Date: Mon, 13 Nov 2023 10:44:28 -0500 Subject: [PATCH] [AAP-18000] Restore non_field_errors key in validation errors (#509) --- src/aap_eda/api/serializers/activation.py | 3 --- src/aap_eda/api/views/activation.py | 12 ++---------- tests/integration/api/test_activation.py | 19 ++++++++----------- 3 files changed, 10 insertions(+), 24 deletions(-) diff --git a/src/aap_eda/api/serializers/activation.py b/src/aap_eda/api/serializers/activation.py index b7f305abc..de7dc8408 100644 --- a/src/aap_eda/api/serializers/activation.py +++ b/src/aap_eda/api/serializers/activation.py @@ -355,7 +355,4 @@ def is_activation_valid(activation: models.Activation) -> tuple[bool, str]: def parse_validation_errors(errors: dict) -> str: messages = {key: str(error[0]) for key, error in errors.items() if error} - if "non_field_errors" in messages: - messages["field_errors"] = messages.pop("non_field_errors") - return str(messages) diff --git a/src/aap_eda/api/views/activation.py b/src/aap_eda/api/views/activation.py index 16d1fa967..253f44b96 100644 --- a/src/aap_eda/api/views/activation.py +++ b/src/aap_eda/api/views/activation.py @@ -26,10 +26,7 @@ from rest_framework.response import Response from aap_eda.api import exceptions as api_exc, filters, serializers -from aap_eda.api.serializers.activation import ( - is_activation_valid, - parse_validation_errors, -) +from aap_eda.api.serializers.activation import is_activation_valid from aap_eda.core import models from aap_eda.core.enums import Action, ActivationStatus, ResourceType from aap_eda.tasks.ruleset import activate, deactivate, restart @@ -79,12 +76,7 @@ def create(self, request): serializer = serializers.ActivationCreateSerializer( data=request.data, context=context ) - valid = serializer.is_valid() - if not valid: - error = parse_validation_errors(serializer.errors) - return Response( - {"errors": error}, status=status.HTTP_400_BAD_REQUEST - ) + serializer.is_valid(raise_exception=True) response = serializer.create(serializer.validated_data) diff --git a/tests/integration/api/test_activation.py b/tests/integration/api/test_activation.py index 81c2c26f9..a3953f5c4 100644 --- a/tests/integration/api/test_activation.py +++ b/tests/integration/api/test_activation.py @@ -309,8 +309,7 @@ def test_create_activation_with_bad_entity( }, ) assert response.status_code == status.HTTP_400_BAD_REQUEST - error = f"'{key}_id': '{dependent_object[key]}'" - assert str(response.data["errors"]) == "{%s}" % error + assert response.data[f"{key}_id"][0] == f"{dependent_object[key]}" check_permission_mock.assert_called_once_with( mock.ANY, mock.ANY, ResourceType.ACTIVATION, Action.CREATE @@ -490,7 +489,7 @@ def test_restart_activation_with_invalid_tokens(client: APIClient, action): ) error_message = ( - "{'field_errors': 'More than one controller token found, " + "{'non_field_errors': 'More than one controller token found, " "currently only 1 token is supported'}" ) @@ -510,13 +509,13 @@ def test_restart_activation_with_invalid_tokens(client: APIClient, action): assert response.status_code == status.HTTP_400_BAD_REQUEST assert ( response.data["errors"] - == "{'field_errors': 'No controller token specified'}" + == "{'non_field_errors': 'No controller token specified'}" ) activation.refresh_from_db() assert activation.status == ActivationStatus.ERROR assert ( activation.status_message - == "{'field_errors': 'No controller token specified'}" + == "{'non_field_errors': 'No controller token specified'}" ) @@ -797,8 +796,7 @@ def test_create_activation_no_token(client: APIClient): response = client.post(f"{api_url_v1}/activations/", data=test_activation) assert response.status_code == status.HTTP_400_BAD_REQUEST assert ( - str(response.data["errors"]) - == "{'field_errors': 'No controller token specified'}" + response.data["non_field_errors"][0] == "No controller token specified" ) @@ -815,8 +813,7 @@ def test_create_activation_more_tokens(client: APIClient): client.post(f"{api_url_v1}/users/me/awx-tokens/", data=TEST_AWX_TOKEN_2) response = client.post(f"{api_url_v1}/activations/", data=test_activation) assert response.status_code == status.HTTP_400_BAD_REQUEST - assert ( - str(response.data["errors"]) - == "{'field_errors': 'More than one controller token found, " - "currently only 1 token is supported'}" + assert response.data["non_field_errors"][0] == ( + "More than one controller token found, " + "currently only 1 token is supported" )