Skip to content

Commit

Permalink
fix(API): Exclure les structures OPCS des résultats (#1129)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn authored Mar 18, 2024
1 parent 9844546 commit b79133e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
58 changes: 48 additions & 10 deletions lemarche/api/siaes/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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"])
Expand Down
4 changes: 2 additions & 2 deletions lemarche/api/siaes/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions lemarche/siaes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit b79133e

Please sign in to comment.