From 02e5aa5621e32c652e45feb03ee1c61e77a4afbe Mon Sep 17 00:00:00 2001 From: Matthew Elwell Date: Thu, 10 Aug 2023 11:17:28 +0100 Subject: [PATCH] Fix issue retrieving project with master api key --- api/projects/views.py | 21 +++++++++++-------- .../unit/projects/test_unit_projects_views.py | 17 +++++++++------ 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/api/projects/views.py b/api/projects/views.py index 627e45c15c52..1c11040b195b 100644 --- a/api/projects/views.py +++ b/api/projects/views.py @@ -89,7 +89,18 @@ def get_queryset(self): else: queryset = self.request.user.get_permitted_projects( permission_key=VIEW_PROJECT - ).annotate( + ) + + organisation_id = self.request.query_params.get("organisation") + if organisation_id: + queryset = queryset.filter(organisation__id=organisation_id) + + project_uuid = self.request.query_params.get("uuid") + if project_uuid: + queryset = queryset.filter(uuid=project_uuid) + + if self.action == "retrieve": + queryset = queryset.annotate( total_features=Count( "features", filter=Q(features__deleted_at__isnull=True), @@ -102,14 +113,6 @@ def get_queryset(self): ), ) - organisation_id = self.request.query_params.get("organisation") - if organisation_id: - queryset = queryset.filter(organisation__id=organisation_id) - - project_uuid = self.request.query_params.get("uuid") - if project_uuid: - queryset = queryset.filter(uuid=project_uuid) - return queryset def perform_create(self, serializer): diff --git a/api/tests/unit/projects/test_unit_projects_views.py b/api/tests/unit/projects/test_unit_projects_views.py index 44a5f1f01659..bfe94af39dfa 100644 --- a/api/tests/unit/projects/test_unit_projects_views.py +++ b/api/tests/unit/projects/test_unit_projects_views.py @@ -1,5 +1,6 @@ import pytest from django.urls import reverse +from pytest_lazyfixture import lazy_fixture from rest_framework import status from projects.models import Project @@ -8,8 +9,10 @@ PROJECT_NAME = "Test project" -@pytest.mark.django_db -def test_get_project_list_data(admin_client, organisation): +@pytest.mark.parametrize( + "client", (lazy_fixture("admin_client"), lazy_fixture("master_api_key_client")) +) +def test_get_project_list_data(client, organisation): # Given hide_disabled_flags = False enable_dynamo_db = False @@ -28,7 +31,7 @@ def test_get_project_list_data(admin_client, organisation): ) # When - response = admin_client.get(list_url) + response = client.get(list_url) # Then assert response.status_code == status.HTTP_200_OK @@ -48,8 +51,10 @@ def test_get_project_list_data(admin_client, organisation): assert "total_segments" not in response.json()[0].keys() -@pytest.mark.django_db -def test_get_project_data_by_id(admin_client, organisation): +@pytest.mark.parametrize( + "client", (lazy_fixture("admin_client"), lazy_fixture("master_api_key_client")) +) +def test_get_project_data_by_id(client, organisation): # Given project = Project.objects.create( name=PROJECT_NAME, @@ -58,7 +63,7 @@ def test_get_project_data_by_id(admin_client, organisation): url = reverse("api-v1:projects:project-detail", args=[project.id]) # When - response = admin_client.get(url) + response = client.get(url) # Then assert response.status_code == status.HTTP_200_OK