Skip to content

Commit d40d041

Browse files
authored
fix: when doing credential test inputs and metadata are mandatory (#1384)
When doing external sms credential test we need both inputs and metadata. There is no UI field related to this unlike other ValidationErrors We return a 400 when either of them is missing https://issues.redhat.com/browse/AAP-51130 When the test is run through the UI it adheres by the schema and ensures that all required fields are identified in red and only once the user has provided all the parameters is the test run. When the same test is run via scripts or collection these checks are not performed so there is a chance they might pass in an empty dictionary this PR basically proectes the backend from these cases. <img width="1089" height="490" alt="Screenshot 2025-08-20 at 9 49 19 PM" src="https://github.com/user-attachments/assets/f7696ee1-b3a4-427c-8e32-27037a5e9f04" />
1 parent 0ab80fa commit d40d041

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

src/aap_eda/core/validators.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,11 @@ def check_if_refspec_valid(refspec: str) -> str:
598598
def check_credential_test_data(
599599
credential_type: models.CredentialType, inputs: dict, metadata: dict
600600
):
601+
if not inputs or not metadata:
602+
raise serializers.ValidationError(
603+
"In test mode both inputs and metadata have to be provided"
604+
)
605+
601606
errors = validate_inputs(
602607
credential_type, credential_type.inputs, inputs, "fields"
603608
)

tests/integration/api/test_credential_type.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,31 @@ def test_create_credential_type_with_duplicates(
911911
True,
912912
None,
913913
),
914+
(
915+
{
916+
"url": "https://www.example.com",
917+
"api_version": "v2",
918+
"token": "token123",
919+
},
920+
None,
921+
status.HTTP_400_BAD_REQUEST,
922+
True,
923+
None,
924+
),
925+
(
926+
None,
927+
{"secret_path": "secret/foo", "secret_key": "bar"},
928+
status.HTTP_400_BAD_REQUEST,
929+
True,
930+
None,
931+
),
932+
(
933+
None,
934+
None,
935+
status.HTTP_400_BAD_REQUEST,
936+
True,
937+
None,
938+
),
914939
],
915940
)
916941
def test_credential_type_test(
@@ -927,10 +952,11 @@ def test_credential_type_test(
927952
name=enums.DefaultCredentialType.HASHICORP_LOOKUP
928953
)
929954

930-
data_in = {
931-
"inputs": inputs,
932-
"metadata": metadata,
933-
}
955+
data_in = {}
956+
if inputs:
957+
data_in["inputs"] = inputs
958+
if metadata:
959+
data_in["metadata"] = metadata
934960
if credential_type_id:
935961
url = f"{api_url_v1}/credential-types/{credential_type_id}/test/"
936962
else:

tests/integration/api/test_eda_credential.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,14 +1459,19 @@ def test_create_external_credential_type(
14591459
status.HTTP_400_BAD_REQUEST,
14601460
True,
14611461
),
1462+
(
1463+
None,
1464+
status.HTTP_400_BAD_REQUEST,
1465+
True,
1466+
),
14621467
],
14631468
)
14641469
@pytest.mark.django_db
14651470
def test_create_external_credential_test(
14661471
admin_client: APIClient,
14671472
default_organization: models.Organization,
14681473
preseed_credential_types,
1469-
metadata: dict,
1474+
metadata: Optional[dict],
14701475
status_code: int,
14711476
raise_exception: bool,
14721477
):
@@ -1496,9 +1501,12 @@ def test_create_external_credential_test(
14961501
"aap_eda.api.views.eda_credential.run_plugin",
14971502
side_effect=Exception("kaboom"),
14981503
):
1504+
data = {}
1505+
if metadata:
1506+
data["metadata"] = metadata
14991507
response = admin_client.post(
15001508
f"{api_url_v1}/eda-credentials/{obj['id']}/test/",
1501-
data={"metadata": metadata},
1509+
data=data,
15021510
)
15031511
assert response.status_code == status_code
15041512
else:

0 commit comments

Comments
 (0)