Skip to content

Commit a3afbe4

Browse files
authored
fix: validate inputs payload to match with schema when creating/updating credentials (#886)
1 parent 5fc0d63 commit a3afbe4

File tree

2 files changed

+63
-26
lines changed

2 files changed

+63
-26
lines changed

src/aap_eda/core/utils/credentials.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,18 @@ def validate_inputs(schema: dict, inputs: dict) -> dict:
9090
errors = {}
9191
required_fields = schema.get("required", [])
9292

93-
for data in schema.get("fields", []):
93+
schema_fields = schema.get("fields", [])
94+
schema_keys = {field["id"] for field in schema_fields}
95+
invalid_keys = inputs.keys() - schema_keys
96+
if bool(invalid_keys):
97+
errors["inputs"] = (
98+
f"Input keys {invalid_keys} are not defined "
99+
f"in the schema. Allowed keys are: {schema_keys}"
100+
)
101+
102+
return errors
103+
104+
for data in schema_fields:
94105
field = data["id"]
95106
required = field in required_fields
96107
default = data.get("default")

tests/integration/api/test_eda_credential.py

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,27 @@
3838

3939

4040
@pytest.mark.parametrize(
41-
"inputs", [{}, {"username": "adam", "password": "secret"}]
41+
("inputs", "status_code", "status_message"),
42+
[
43+
(
44+
{"username": "adam", "password": "secret"},
45+
status.HTTP_201_CREATED,
46+
None,
47+
),
48+
(
49+
{"username": "adam", "password": "secret", "invalid_key": "bad"},
50+
status.HTTP_400_BAD_REQUEST,
51+
None,
52+
),
53+
],
4254
)
4355
@pytest.mark.django_db
4456
def test_create_eda_credential(
4557
admin_client: APIClient,
4658
credential_type: models.CredentialType,
4759
inputs,
60+
status_code,
61+
status_message,
4862
):
4963
data_in = {
5064
"name": "eda-credential",
@@ -54,9 +68,15 @@ def test_create_eda_credential(
5468
response = admin_client.post(
5569
f"{api_url_v1}/eda-credentials/", data=data_in
5670
)
57-
assert response.status_code == status.HTTP_201_CREATED
58-
assert response.data["name"] == "eda-credential"
59-
assert response.data["managed"] is False
71+
assert response.status_code == status_code
72+
if status_code == status.HTTP_201_CREATED:
73+
assert response.data["name"] == "eda-credential"
74+
assert response.data["managed"] is False
75+
else:
76+
assert (
77+
"Input keys {'invalid_key'} are not defined in the schema"
78+
in response.data["inputs"][0]
79+
)
6080

6181

6282
@pytest.mark.parametrize(
@@ -101,27 +121,6 @@ def test_create_eda_credential_with_gpg_key_data(
101121
assert status_message in response.data.get("inputs.gpg_public_key", "")
102122

103123

104-
@pytest.mark.django_db
105-
def test_create_eda_credential_with_type(
106-
admin_client: APIClient, credential_type: models.CredentialType
107-
):
108-
data_in = {
109-
"name": "eda-credential",
110-
"inputs": {"username": "adam", "password": "secret"},
111-
"credential_type_id": credential_type.id,
112-
}
113-
response = admin_client.post(
114-
f"{api_url_v1}/eda-credentials/", data=data_in
115-
)
116-
assert response.status_code == status.HTTP_201_CREATED
117-
assert response.data["name"] == "eda-credential"
118-
assert response.data["managed"] is False
119-
assert response.data["inputs"] == {
120-
"password": "$encrypted$",
121-
"username": "adam",
122-
}
123-
124-
125124
@pytest.mark.parametrize(
126125
("credential_type", "status_code", "key", "error_message"),
127126
[
@@ -366,6 +365,33 @@ def test_partial_update_eda_credential_without_inputs(
366365
}
367366

368367

368+
@pytest.mark.django_db
369+
def test_partial_update_eda_credential_with_invalid_inputs(
370+
admin_client: APIClient, credential_type: models.CredentialType
371+
):
372+
obj = models.EdaCredential.objects.create(
373+
name="eda-credential",
374+
inputs={"username": "adam", "password": "secret"},
375+
credential_type_id=credential_type.id,
376+
managed=True,
377+
)
378+
data = {
379+
"inputs": {
380+
"username": "bearny",
381+
"password": "demo",
382+
"invalid_key": "bad",
383+
}
384+
}
385+
response = admin_client.patch(
386+
f"{api_url_v1}/eda-credentials/{obj.id}/", data=data
387+
)
388+
assert response.status_code == status.HTTP_400_BAD_REQUEST
389+
assert (
390+
"Input keys {'invalid_key'} are not defined in the schema"
391+
in response.data["inputs"][0]
392+
)
393+
394+
369395
@pytest.mark.parametrize(
370396
("credential_type", "inputs"),
371397
[

0 commit comments

Comments
 (0)