Skip to content

Commit 04935c7

Browse files
authored
fix: the test function was broken for eda_credential (#1388)
The test function for eda_credential was broken, it needs to support some of the Credential Types that have optional metadata fields like GitHub App Installation Access Token Lookup We need to support empty metadata during the test. If the metadata is empty we have to look at the credentials schema and see if it has mandatory fields and then raise an error. If the fields are optional then we should allow empty metadata https://issues.redhat.com/browse/AAP-52232
1 parent 84707c3 commit 04935c7

File tree

4 files changed

+64
-13
lines changed

4 files changed

+64
-13
lines changed

src/aap_eda/api/serializers/credential_type.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class CredentialTypeTestSerializer(serializers.ModelSerializer):
9292
)
9393

9494
def validate(self, data):
95-
metadata = data.get("metadata")
95+
metadata = data.get("metadata", {})
9696
inputs = data.get("inputs")
9797

9898
validators.check_credential_test_data(self.instance, inputs, metadata)

src/aap_eda/api/serializers/eda_credential.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ class EdaCredentialTestSerializer(serializers.ModelSerializer):
318318
)
319319

320320
def validate(self, attrs):
321-
metadata = attrs.get("metadata")
321+
metadata = attrs.get("metadata", {})
322322
inputs = inputs_from_store(self.instance.inputs.get_secret_value())
323323

324324
validators.check_credential_test_data(

src/aap_eda/core/validators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,9 +566,9 @@ def check_if_refspec_valid(refspec: str) -> str:
566566
def check_credential_test_data(
567567
credential_type: models.CredentialType, inputs: dict, metadata: dict
568568
):
569-
if not inputs or not metadata:
569+
if not inputs:
570570
raise serializers.ValidationError(
571-
"In test mode both inputs and metadata have to be provided"
571+
"In test mode inputs have to be provided"
572572
)
573573

574574
errors = validate_inputs(

tests/integration/api/test_eda_credential.py

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,51 +1442,102 @@ def test_create_external_credential_type(
14421442

14431443

14441444
@pytest.mark.parametrize(
1445-
("metadata", "status_code", "raise_exception"),
1445+
(
1446+
"credential_type_name",
1447+
"inputs",
1448+
"metadata",
1449+
"status_code",
1450+
"raise_exception",
1451+
),
14461452
[
14471453
(
1454+
enums.DefaultCredentialType.HASHICORP_LOOKUP,
1455+
{
1456+
"url": "https://www.example.com",
1457+
"api_version": "v2",
1458+
"token": secrets.token_hex(32),
1459+
},
14481460
{"secret_path": "secret/foo", "secret_key": "bar"},
14491461
status.HTTP_202_ACCEPTED,
14501462
False,
14511463
),
14521464
(
1465+
enums.DefaultCredentialType.HASHICORP_LOOKUP,
1466+
{
1467+
"url": "https://www.example.com",
1468+
"api_version": "v2",
1469+
"token": secrets.token_hex(32),
1470+
},
14531471
{"secret_key": "bar"},
14541472
status.HTTP_400_BAD_REQUEST,
14551473
False,
14561474
),
14571475
(
1476+
enums.DefaultCredentialType.HASHICORP_LOOKUP,
1477+
{
1478+
"url": "https://www.example.com",
1479+
"api_version": "v2",
1480+
"token": secrets.token_hex(32),
1481+
},
1482+
{},
1483+
status.HTTP_400_BAD_REQUEST,
1484+
False,
1485+
),
1486+
(
1487+
enums.DefaultCredentialType.HASHICORP_LOOKUP,
1488+
{
1489+
"url": "https://www.example.com",
1490+
"api_version": "v2",
1491+
"token": secrets.token_hex(32),
1492+
},
14581493
{"secret_path": "secret/foo", "secret_key": "bar"},
14591494
status.HTTP_400_BAD_REQUEST,
14601495
True,
14611496
),
14621497
(
1498+
enums.DefaultCredentialType.HASHICORP_LOOKUP,
1499+
{
1500+
"url": "https://www.example.com",
1501+
"api_version": "v2",
1502+
"token": secrets.token_hex(32),
1503+
},
14631504
None,
14641505
status.HTTP_400_BAD_REQUEST,
14651506
True,
14661507
),
1508+
(
1509+
enums.DefaultCredentialType.GITHUB_APP,
1510+
{
1511+
"app_or_client_id": "1111111",
1512+
"github_api_url": "https://api.github.com",
1513+
"install_id": "11111111",
1514+
"private_rsa_key": DUMMY_SSH_KEY,
1515+
},
1516+
{},
1517+
status.HTTP_202_ACCEPTED,
1518+
False,
1519+
),
14671520
],
14681521
)
14691522
@pytest.mark.django_db
14701523
def test_create_external_credential_test(
14711524
admin_client: APIClient,
14721525
default_organization: models.Organization,
14731526
preseed_credential_types,
1527+
credential_type_name: str,
1528+
inputs: dict,
14741529
metadata: Optional[dict],
14751530
status_code: int,
14761531
raise_exception: bool,
14771532
):
1478-
hashi_type = models.CredentialType.objects.get(
1479-
name=enums.DefaultCredentialType.HASHICORP_LOOKUP
1533+
credential_type = models.CredentialType.objects.get(
1534+
name=credential_type_name
14801535
)
14811536

14821537
data_in = {
14831538
"name": "eda-credential",
1484-
"inputs": {
1485-
"url": "https://www.example.com",
1486-
"api_version": "v2",
1487-
"token": secrets.token_hex(32),
1488-
},
1489-
"credential_type_id": hashi_type.id,
1539+
"inputs": inputs,
1540+
"credential_type_id": credential_type.id,
14901541
"organization_id": default_organization.id,
14911542
}
14921543

0 commit comments

Comments
 (0)