Skip to content

Commit

Permalink
fix: add event_streams into credentials refs list (#1124)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsong-rh authored Oct 28, 2024
1 parent e624033 commit be318d1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
28 changes: 22 additions & 6 deletions src/aap_eda/api/serializers/eda_credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"
)


Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand All @@ -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)

Expand Down
39 changes: 35 additions & 4 deletions tests/integration/api/test_eda_credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,27 +899,58 @@ 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

if refs == "true":
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
Expand Down

0 comments on commit be318d1

Please sign in to comment.