From b79133e49f202073e0db13d203aa5d43ed33afd2 Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Mon, 18 Mar 2024 16:06:43 +0100 Subject: [PATCH] =?UTF-8?q?fix(API):=20Exclure=20les=20structures=20OPCS?= =?UTF-8?q?=20des=20r=C3=A9sultats=20(#1129)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lemarche/api/siaes/tests.py | 58 ++++++++++++++++++++++++++++++------- lemarche/api/siaes/views.py | 4 +-- lemarche/siaes/models.py | 3 ++ 3 files changed, 53 insertions(+), 12 deletions(-) diff --git a/lemarche/api/siaes/tests.py b/lemarche/api/siaes/tests.py index 6dff17956..060934db6 100644 --- a/lemarche/api/siaes/tests.py +++ b/lemarche/api/siaes/tests.py @@ -5,6 +5,7 @@ from lemarche.sectors.factories import SectorFactory from lemarche.siaes import constants as siae_constants from lemarche.siaes.factories import SiaeFactory +from lemarche.siaes.models import Siae from lemarche.users.factories import UserFactory @@ -53,19 +54,13 @@ def test_should_return_401_if_token_unknown(self): class SiaeListFilterApiTest(TestCase): @classmethod def setUpTestData(cls): - # default_siae = {"kind": siae_constants.KIND_EI, "presta_type": [siae_constants.PRESTA_DISP], "department": "01"} # noqa SiaeFactory( kind=siae_constants.KIND_EI, presta_type=[siae_constants.PRESTA_DISP], department="01", is_active=False ) - SiaeFactory( - kind=siae_constants.KIND_ETTI, presta_type=[siae_constants.PRESTA_DISP], department="01" - ) # siae_with_kind - SiaeFactory( - kind=siae_constants.KIND_ACI, presta_type=[siae_constants.PRESTA_BUILD], department="01" - ) # siae_with_presta_type - SiaeFactory( - kind=siae_constants.KIND_EI, presta_type=[siae_constants.PRESTA_PREST], department="38" - ) # siae_with_department + SiaeFactory(kind=siae_constants.KIND_ETTI, presta_type=[siae_constants.PRESTA_DISP], department="01") + SiaeFactory(kind=siae_constants.KIND_ACI, presta_type=[siae_constants.PRESTA_BUILD], department="01") + SiaeFactory(kind=siae_constants.KIND_EI, presta_type=[siae_constants.PRESTA_PREST], department="38") + SiaeFactory(kind="OPCS", presta_type=[siae_constants.PRESTA_PREST], department="38") cls.sector_1 = SectorFactory() cls.sector_2 = SectorFactory() siae_with_sector_1 = SiaeFactory( @@ -88,6 +83,9 @@ def setUpTestData(cls): siae_with_network_2.networks.add(cls.network_2) UserFactory(api_key="admin") + def test_siae_count(self): + self.assertEqual(Siae.objects.count(), 9) + def test_should_return_siae_list(self): url = reverse("api:siae-list") + "?token=admin" response = self.client.get(url) @@ -176,6 +174,16 @@ def setUpTestData(cls): cls.siae = SiaeFactory() UserFactory(api_key="admin") + def test_should_return_4O4_if_siae_excluded(self): + siae_opcs = SiaeFactory(kind="OPCS") + for siae in [siae_opcs]: + url = reverse("api:siae-detail", args=[siae.id]) # anonymous + response = self.client.get(url) + self.assertEqual(response.status_code, 404) + url = reverse("api:siae-detail", args=[siae.id]) + "?token=admin" + response = self.client.get(url) + self.assertEqual(response.status_code, 404) + def test_should_return_simple_siae_object_to_anonymous_users(self): url = reverse("api:siae-detail", args=[self.siae.id]) # anonymous user response = self.client.get(url) @@ -220,6 +228,16 @@ def test_should_return_404_if_slug_unknown(self): response = self.client.get(url) self.assertEqual(response.status_code, 404) + def test_should_return_4O4_if_siae_excluded(self): + siae_opcs = SiaeFactory(kind="OPCS") + for siae in [siae_opcs]: + url = reverse("api:siae-retrieve-by-slug", args=[siae.slug]) # anonymous + response = self.client.get(url) + self.assertEqual(response.status_code, 404) + url = reverse("api:siae-retrieve-by-slug", args=[siae.slug]) + "?token=admin" + response = self.client.get(url) + self.assertEqual(response.status_code, 404) + def test_should_return_siae_if_slug_known(self): url = reverse("api:siae-retrieve-by-slug", args=["une-structure-38"]) # anonymous user response = self.client.get(url) @@ -261,6 +279,16 @@ def test_should_return_empty_list_if_siren_unknown(self): # self.assertEqual(type(response.data), list) self.assertEqual(len(response.data), 0) + def test_should_return_4O4_if_siae_excluded(self): + siae_opcs = SiaeFactory(kind="OPCS", siret="99999999999999") + for siae in [siae_opcs]: + url = reverse("api:siae-retrieve-by-siren", args=[siae.siren]) # anonymous + response = self.client.get(url) + self.assertEqual(len(response.data), 0) + url = reverse("api:siae-retrieve-by-siren", args=[siae.siren]) + "?token=admin" + response = self.client.get(url) + self.assertEqual(len(response.data), 0) + def test_should_return_siae_list_if_siren_known(self): # anonymous user url = reverse("api:siae-retrieve-by-siren", args=["123123123"]) @@ -321,6 +349,16 @@ def test_should_return_empty_list_if_siret_unknown(self): # self.assertEqual(type(response.data), list) self.assertEqual(len(response.data), 0) + def test_should_return_4O4_if_siae_excluded(self): + siae_opcs = SiaeFactory(kind="OPCS", siret="99999999999999") + for siae in [siae_opcs]: + url = reverse("api:siae-retrieve-by-siret", args=[siae.siret]) # anonymous + response = self.client.get(url) + self.assertEqual(len(response.data), 0) + url = reverse("api:siae-retrieve-by-siret", args=[siae.siret]) + "?token=admin" + response = self.client.get(url) + self.assertEqual(len(response.data), 0) + def test_should_return_siae_list_if_siret_known(self): # anonymous user url = reverse("api:siae-retrieve-by-siret", args=["12312312312345"]) diff --git a/lemarche/api/siaes/views.py b/lemarche/api/siaes/views.py index 9ea8c14c4..11affd56f 100644 --- a/lemarche/api/siaes/views.py +++ b/lemarche/api/siaes/views.py @@ -16,7 +16,7 @@ class SiaeViewSet(mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.Gen Données d'une structure d'insertion par l'activité économique (SIAE). """ - queryset = Siae.objects.prefetch_many_to_many() + queryset = Siae.objects.api_query_set() serializer_class = SiaeListSerializer filter_class = SiaeFilter @@ -37,7 +37,7 @@ def list(self, request, format=None): token = request.GET.get("token", None) if not token: serializer = SiaeListSerializer( - Siae.objects.all()[:10], + self.get_queryset()[:10], many=True, ) return Response(serializer.data) diff --git a/lemarche/siaes/models.py b/lemarche/siaes/models.py index ece4dd621..366aac106 100644 --- a/lemarche/siaes/models.py +++ b/lemarche/siaes/models.py @@ -181,6 +181,9 @@ def prefetch_many_to_many(self): def prefetch_many_to_one(self): return self.prefetch_related("offers", "client_references", "labels_old", "images") + def api_query_set(self): + return self.exclude(kind="OPCS").prefetch_many_to_many() + def search_query_set(self): return self.is_live().exclude(kind="OPCS").prefetch_many_to_many()