From be318d1669e2e60b8661ee813400edeca487f6c4 Mon Sep 17 00:00:00 2001 From: Hui Song Date: Mon, 28 Oct 2024 12:02:44 -0400 Subject: [PATCH] fix: add event_streams into credentials refs list (#1124) --- src/aap_eda/api/serializers/eda_credential.py | 28 ++++++++++--- tests/integration/api/test_eda_credential.py | 39 +++++++++++++++++-- 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/src/aap_eda/api/serializers/eda_credential.py b/src/aap_eda/api/serializers/eda_credential.py index 0af063841..d1613a01b 100644 --- a/src/aap_eda/api/serializers/eda_credential.py +++ b/src/aap_eda/api/serializers/eda_credential.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. from django.db.models import Q +from django.urls import reverse from drf_spectacular.utils import extend_schema_field from rest_framework import serializers @@ -32,8 +33,8 @@ class EdaCredentialReferenceSerializer(serializers.Serializer): name = serializers.CharField( required=True, help_text="Name of the related resource" ) - url = serializers.URLField( - required=True, help_text="URL of the related resource" + uri = serializers.URLField( + required=True, help_text="URI of the related resource" ) @@ -192,13 +193,16 @@ def get_references(eda_credential: models.EdaCredential) -> list[dict]: Q(eda_credential=eda_credential) | Q(signature_validation_credential=eda_credential) ) + used_event_streams = models.EventStream.objects.filter( + eda_credential=eda_credential + ) for activation in used_activations: resource = { "type": "Activation", "id": activation.id, "name": activation.name, - "url": f"api/eda/v1/activations/{activation.id}/", + "uri": reverse("activation-detail", kwargs={"pk": activation.id}), } resources.append(resource) @@ -207,8 +211,9 @@ def get_references(eda_credential: models.EdaCredential) -> list[dict]: "type": "DecisionEnvironment", "id": decision_environment.id, "name": decision_environment.name, - "url": ( - f"api/eda/v1/decision-environments/{decision_environment.id}/" + "uri": reverse( + "decisionenvironment-detail", + kwargs={"pk": decision_environment.id}, ), } resources.append(resource) @@ -218,7 +223,18 @@ def get_references(eda_credential: models.EdaCredential) -> list[dict]: "type": "Project", "id": project.id, "name": project.name, - "url": f"api/eda/v1/projects/{project.id}/", + "uri": reverse("project-detail", kwargs={"pk": project.id}), + } + resources.append(resource) + + for event_stream in used_event_streams: + resource = { + "type": "EventStream", + "id": event_stream.id, + "name": event_stream.name, + "uri": reverse( + "eventstream-detail", kwargs={"pk": event_stream.id} + ), } resources.append(resource) diff --git a/tests/integration/api/test_eda_credential.py b/tests/integration/api/test_eda_credential.py index 4bc55ed12..024c793be 100644 --- a/tests/integration/api/test_eda_credential.py +++ b/tests/integration/api/test_eda_credential.py @@ -899,14 +899,27 @@ def test_delete_credential_used_by_project_with_gpg_credential( @pytest.mark.django_db def test_retrieve_eda_credential_with_refs( default_activation: models.Activation, + default_event_stream: models.EventStream, + default_decision_environment: models.DecisionEnvironment, + default_project: models.Project, + default_eda_credential: models.EdaCredential, admin_client: APIClient, refs, preseed_credential_types, ): - eda_credential = default_activation.eda_credentials.all()[0] + default_activation.eda_credentials.add(default_eda_credential) + + default_event_stream.eda_credential = default_eda_credential + default_event_stream.save(update_fields=["eda_credential"]) + + default_decision_environment.eda_credential = default_eda_credential + default_decision_environment.save(update_fields=["eda_credential"]) + + default_project.eda_credential = default_eda_credential + default_project.save(update_fields=["eda_credential"]) response = admin_client.get( - f"{api_url_v1}/eda-credentials/{eda_credential.id}/?refs={refs}", + f"{api_url_v1}/eda-credentials/{default_eda_credential.id}/?refs={refs}", # noqa 501 ) assert response.status_code == status.HTTP_200_OK @@ -914,12 +927,30 @@ def test_retrieve_eda_credential_with_refs( assert response.data["references"] is not None references = response.data["references"] - assert len(references) == 1 + assert len(references) == 4 references[0] = { "type": "Activation", "id": default_activation.id, "name": default_activation.name, - "url": f"api/eda/v1/activations/{default_activation.id}/", + "uri": f"api/eda/v1/activations/{default_activation.id}/", + } + references[1] = { + "type": "DecisionEnvironment", + "id": default_decision_environment.id, + "name": default_decision_environment.name, + "uri": f"api/eda/v1/decision-environments/{default_decision_environment.id}/", # noqa 501 + } + references[2] = { + "type": "Project", + "id": default_project.id, + "name": default_project.name, + "uri": f"api/eda/v1/projects/{default_project.id}/", + } + references[3] = { + "type": "EventStream", + "id": default_event_stream.id, + "name": default_event_stream.name, + "uri": f"api/eda/v1/event-streams/{default_event_stream.id}/", } else: assert response.data["references"] is None