Skip to content

Commit

Permalink
fix: disallow updating credential type on credential patch endpoint (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Dostonbek1 authored Nov 15, 2024
1 parent 8ebeecb commit 55ebbf3
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/aap_eda/api/serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from .eda_credential import (
EdaCredentialCreateSerializer,
EdaCredentialSerializer,
EdaCredentialUpdateSerializer,
)
from .event_stream import EventStreamInSerializer, EventStreamOutSerializer
from .organization import (
Expand Down Expand Up @@ -120,6 +121,7 @@
"CredentialTypeRefSerializer",
"EdaCredentialSerializer",
"EdaCredentialCreateSerializer",
"EdaCredentialUpdateSerializer",
# decision environment
"DecisionEnvironmentSerializer",
# organizations
Expand Down
43 changes: 32 additions & 11 deletions src/aap_eda/api/serializers/eda_credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,45 @@ class EdaCredentialCreateSerializer(serializers.ModelSerializer):
inputs = serializers.JSONField()

def validate(self, data):
credential_type_id = data.get("credential_type_id")
if credential_type_id:
credential_type = models.CredentialType.objects.get(
id=credential_type_id
)
else:
# for update
credential_type = self.instance.credential_type
credential_type = models.CredentialType.objects.get(
id=data.get("credential_type_id")
)

inputs = data.get("inputs", {})
errors = validate_inputs(credential_type.inputs, inputs)
if bool(errors):
raise serializers.ValidationError(errors)

# allow emtpy inputs during updating
return data

class Meta:
model = models.EdaCredential
fields = [
"name",
"description",
"inputs",
"credential_type_id",
"organization_id",
]


class EdaCredentialUpdateSerializer(serializers.ModelSerializer):
organization_id = serializers.IntegerField(
required=True,
allow_null=False,
validators=[validators.check_if_organization_exists],
)
inputs = serializers.JSONField()

def validate(self, data):
credential_type = self.instance.credential_type

inputs = data.get("inputs", {})
# allow empty inputs during updating
if self.partial and not bool(inputs):
return data

errors = validate_inputs(credential_type.inputs, inputs)

if bool(errors):
raise serializers.ValidationError(errors)

Expand All @@ -143,7 +165,6 @@ class Meta:
"name",
"description",
"inputs",
"credential_type_id",
"organization_id",
]

Expand Down
4 changes: 2 additions & 2 deletions src/aap_eda/api/views/eda_credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def list(self, request):

@extend_schema(
description="Partial update of an EDA credential",
request=serializers.EdaCredentialCreateSerializer,
request=serializers.EdaCredentialUpdateSerializer,
responses={
status.HTTP_200_OK: OpenApiResponse(
serializers.EdaCredentialSerializer,
Expand All @@ -202,7 +202,7 @@ def partial_update(self, request, pk):
data.get("inputs", {}), eda_credential.inputs
)

serializer = serializers.EdaCredentialCreateSerializer(
serializer = serializers.EdaCredentialUpdateSerializer(
eda_credential, data=data, partial=True
)
serializer.is_valid(raise_exception=True)
Expand Down
26 changes: 26 additions & 0 deletions tests/integration/api/test_eda_credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,32 @@ def test_partial_update_eda_credential_with_invalid_inputs(
)


@pytest.mark.django_db
def test_partial_update_eda_credential_type_not_changed(
admin_client: APIClient,
default_registry_credential: models.EdaCredential,
preseed_credential_types,
):
aap_cred_type = models.CredentialType.objects.get(
name=enums.DefaultCredentialType.AAP
)
data = {"credential_type_id": aap_cred_type.id}
response = admin_client.patch(
f"{api_url_v1}/eda-credentials/{default_registry_credential.id}/",
data=data,
)
assert response.status_code == status.HTTP_200_OK
result = response.data
assert (
result["credential_type"]["id"]
== default_registry_credential.credential_type.id
)
assert (
result["credential_type"]["name"]
== default_registry_credential.credential_type.name
)


@pytest.mark.parametrize(
("credential_type", "old_inputs", "inputs", "expected_inputs"),
[
Expand Down

0 comments on commit 55ebbf3

Please sign in to comment.