Skip to content

Commit

Permalink
[AAP-18000] Restore non_field_errors key in validation errors (#509)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsong-rh authored Nov 13, 2023
1 parent 85e9635 commit 93ae979
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 24 deletions.
3 changes: 0 additions & 3 deletions src/aap_eda/api/serializers/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
12 changes: 2 additions & 10 deletions src/aap_eda/api/views/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
19 changes: 8 additions & 11 deletions tests/integration/api/test_activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'}"
)

Expand All @@ -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'}"
)


Expand Down Expand Up @@ -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"
)


Expand All @@ -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"
)

0 comments on commit 93ae979

Please sign in to comment.