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: [AAP-36846] - customize validation error messages #1175

Merged
merged 1 commit into from
Jan 8, 2025
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
19 changes: 16 additions & 3 deletions src/aap_eda/api/serializers/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,17 @@ class Meta:
required=True,
allow_null=False,
validators=[validators.check_if_organization_exists],
error_messages={
"null": "Organization is needed",
"required": "Organization is required",
},
)
rulebook_id = serializers.IntegerField(
validators=[validators.check_if_rulebook_exists]
validators=[validators.check_if_rulebook_exists],
error_messages={
"null": "Rulebook is needed",
"required": "Rulebook is required",
},
)
extra_var = serializers.CharField(
required=False,
Expand All @@ -408,7 +416,11 @@ class Meta:
validators=[validators.is_extra_var_dict],
)
decision_environment_id = serializers.IntegerField(
validators=[validators.check_if_de_exists]
validators=[validators.check_if_de_exists],
error_messages={
"null": "Decision Environment is needed",
"required": "Decision Environment is required",
},
)
user = serializers.HiddenField(default=serializers.CurrentUserDefault())

Expand Down Expand Up @@ -691,7 +703,8 @@ class PostActivationSerializer(serializers.ModelSerializer):
)
name = serializers.CharField(required=True)
decision_environment_id = serializers.IntegerField(
validators=[validators.check_if_de_exists]
validators=[validators.check_if_de_exists],
error_messages={"null": "Decision Environment is needed"},
)
# TODO: is_activation_valid needs to tell event stream/activation
awx_token_id = serializers.IntegerField(
Expand Down
1 change: 1 addition & 0 deletions src/aap_eda/api/serializers/decision_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class DecisionEnvironmentCreateSerializer(serializers.ModelSerializer):
required=True,
allow_null=False,
validators=[validators.check_if_organization_exists],
error_messages={"null": "Organization is needed"},
)
eda_credential_id = serializers.IntegerField(
required=False,
Expand Down
9 changes: 9 additions & 0 deletions src/aap_eda/api/serializers/eda_credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,19 @@ class EdaCredentialCreateSerializer(serializers.ModelSerializer):
required=True,
allow_null=False,
validators=[validators.check_if_credential_type_exists],
error_messages={
"null": "Credential Type is needed",
"required": "Credential Type is required",
},
)
organization_id = serializers.IntegerField(
required=True,
allow_null=False,
validators=[validators.check_if_organization_exists],
error_messages={
"null": "Organization is needed",
"required": "Organization is required",
},
)
inputs = serializers.JSONField()

Expand Down Expand Up @@ -146,6 +154,7 @@ class EdaCredentialUpdateSerializer(serializers.ModelSerializer):
required=True,
allow_null=False,
validators=[validators.check_if_organization_exists],
error_messages={"null": "Organization is needed"},
)
inputs = serializers.JSONField()

Expand Down
7 changes: 6 additions & 1 deletion src/aap_eda/api/serializers/event_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@


class EventStreamInSerializer(serializers.ModelSerializer):
organization_id = serializers.IntegerField(required=True, allow_null=False)
organization_id = serializers.IntegerField(
required=True,
allow_null=False,
error_messages={"null": "Organization is needed"},
)
owner = serializers.HiddenField(default=serializers.CurrentUserDefault())
eda_credential_id = serializers.IntegerField(
required=True,
allow_null=False,
validators=[
validators.check_credential_types_for_event_stream,
],
error_messages={"null": "EdaCredential is needed"},
)

def validate(self, data):
Expand Down
8 changes: 8 additions & 0 deletions src/aap_eda/api/serializers/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ class ProjectCreateRequestSerializer(serializers.ModelSerializer):
required=True,
allow_null=False,
validators=[validators.check_if_organization_exists],
error_messages={
"null": "Organization is needed",
"required": "Organization is required",
},
)
eda_credential_id = serializers.IntegerField(
required=False,
Expand Down Expand Up @@ -110,6 +114,10 @@ class ProjectUpdateRequestSerializer(serializers.ModelSerializer):
required=True,
allow_null=False,
validators=[validators.check_if_organization_exists],
error_messages={
"null": "Organization is needed",
"required": "Organization is required",
},
)
name = serializers.CharField(
required=False,
Expand Down
1 change: 1 addition & 0 deletions src/aap_eda/api/serializers/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class TeamCreateSerializer(
required=True,
allow_null=False,
validators=[validators.check_if_organization_exists],
error_messages={"null": "Organization is needed"},
)

class Meta:
Expand Down
38 changes: 36 additions & 2 deletions tests/integration/api/test_activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,40 @@ def test_restart_activation(
assert response.status_code == status.HTTP_204_NO_CONTENT


@pytest.mark.parametrize(
("missing_field", "error_message"),
[
(
"decision_environment_id",
"Decision Environment is required",
),
(
"organization_id",
"Organization is required",
),
(
"rulebook_id",
"Rulebook is required",
),
],
)
@pytest.mark.django_db
def test_create_activation_with_missing_required_fields(
activation_payload: Dict[str, Any],
admin_client: APIClient,
missing_field,
error_message,
preseed_credential_types,
):
activation_payload.pop(missing_field)
response = admin_client.post(
f"{api_url_v1}/activations/", data=activation_payload
)

assert response.status_code == status.HTTP_400_BAD_REQUEST
assert error_message in response.data[missing_field]


@pytest.mark.parametrize(
"enabled",
[True, False],
Expand Down Expand Up @@ -613,13 +647,13 @@ def test_restart_activation_without_de(
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert (
response.data["errors"]
== "{'decision_environment_id': 'This field may not be null.'}"
== "{'decision_environment_id': 'Decision Environment is needed'}"
)
default_activation.refresh_from_db()
assert default_activation.status == enums.ActivationStatus.ERROR
assert (
default_activation.status_message
== "{'decision_environment_id': 'This field may not be null.'}"
== "{'decision_environment_id': 'Decision Environment is needed'}"
)


Expand Down
19 changes: 19 additions & 0 deletions tests/integration/api/test_decision_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,25 @@ def test_create_decision_environment_with_empty_credential(
assert status_message in str(errors)


@pytest.mark.django_db
def test_create_decision_environment_with_none_organization(
admin_client: APIClient,
):
data_in = {
"name": "de1",
"description": "desc here",
"image_url": "registry.com/img1:tag1",
"organization_id": None,
}

response = admin_client.post(
f"{api_url_v1}/decision-environments/", data=data_in
)

assert response.status_code == status.HTTP_400_BAD_REQUEST
assert "Organization is needed" in str(response.data)


@pytest.mark.django_db
def test_create_decision_environment_bad_ids(admin_client: APIClient):
bad_ids = [
Expand Down
20 changes: 19 additions & 1 deletion tests/integration/api/test_eda_credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,25 @@ def test_create_eda_credential_with_none_credential_type(
f"{api_url_v1}/eda-credentials/", data=data_in
)
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert "This field may not be null." in response.data["credential_type_id"]
assert "Credential Type is needed" in response.data["credential_type_id"]


@pytest.mark.django_db
def test_create_eda_credential_with_none_organization(
admin_client: APIClient,
):
data = "secret"
data_in = {
"name": "eda-credential",
"inputs": {"username": "adam", "password": data},
"credential_type_id": None,
"organization_id": None,
}
response = admin_client.post(
f"{api_url_v1}/eda-credentials/", data=data_in
)
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert "Organization is needed" in response.data["organization_id"]


@pytest.mark.parametrize(
Expand Down
15 changes: 14 additions & 1 deletion tests/integration/api/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,19 @@ def test_create_or_update_project_with_right_eda_credential(
)


@pytest.mark.django_db
def test_create_project_with_none_organization(admin_client: APIClient):
body = {
"name": "none-organization",
"url": "https://git.example.com/acme/project-01",
"organization_id": None,
}

response = admin_client.post(f"{api_url_v1}/projects/", data=body)
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert "Organization is needed" in str(response.data)


@pytest.mark.django_db
def test_create_project_name_conflict(
default_project: models.Project, admin_client: APIClient
Expand Down Expand Up @@ -689,7 +702,7 @@ def test_partial_update_project_null_organization_id(
data,
)
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert "This field may not be null." in str(response.data)
assert "Organization is needed" in str(response.data)


@pytest.mark.django_db
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/services/activation/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def test_start_no_decision_environment(
activation_manager.start()
assert basic_activation.status == enums.ActivationStatus.ERROR
assert "decision_environment" in str(exc.value)
assert "This field may not be null" in str(exc.value)
assert "Decision Environment is needed" in str(exc.value)
assert str(exc.value) in basic_activation.status_message


Expand Down
Loading