Skip to content

Commit

Permalink
feat(api) Pouvoir chercher les structures par SIREN (#1074)
Browse files Browse the repository at this point in the history
* API: new endpoint to filter siae by siren

* Add tests
  • Loading branch information
raphodn authored Feb 14, 2024
1 parent b9fb4a6 commit 3ad2c2f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
60 changes: 60 additions & 0 deletions lemarche/api/siaes/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,66 @@ def test_should_return_detailed_siae_object_to_authenticated_user(self):
self.assertTrue("sectors" in response.data)


class SiaeRetrieveBySirenApiTest(TestCase):
@classmethod
def setUpTestData(cls):
SiaeFactory(name="Une structure", siret="12312312312345", department="38")
SiaeFactory(name="Une autre structure", siret="22222222222222", department="69")
SiaeFactory(name="Une autre structure avec le meme siret", siret="22222222222222", department="69")
UserFactory(api_key="admin")

def test_should_return_400_if_siren_malformed(self):
# anonymous user
for siren in ["123", "12312312312345"]:
url = reverse("api:siae-retrieve-by-siren", args=[siren])
response = self.client.get(url)
self.assertEqual(response.status_code, 400)

def test_should_return_empty_list_if_siren_unknown(self):
# anonymous user
url = reverse("api:siae-retrieve-by-siren", args=["444444444"])
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
# self.assertEqual(type(response.data), list)
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"])
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
# self.assertEqual(type(response.data), list)
self.assertEqual(len(response.data), 1)
self.assertEqual(response.data[0]["siret"], "12312312312345")
self.assertEqual(response.data[0]["slug"], "une-structure-38")
self.assertTrue("sectors" not in response.data)
url = reverse("api:siae-retrieve-by-siren", args=["222222222"])
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
# self.assertEqual(type(response.data), list)
self.assertEqual(len(response.data), 2)
self.assertEqual(response.data[0]["siret"], "22222222222222")
self.assertEqual(response.data[1]["siret"], "22222222222222")
self.assertTrue("sectors" not in response.data[0])
# authenticated user
url = reverse("api:siae-retrieve-by-siren", args=["123123123"]) + "?token=admin"
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
# self.assertEqual(type(response.data), list)
self.assertEqual(len(response.data), 1)
self.assertEqual(response.data[0]["siret"], "12312312312345")
self.assertEqual(response.data[0]["slug"], "une-structure-38")
self.assertTrue("sectors" in response.data[0])
url = reverse("api:siae-retrieve-by-siren", args=["222222222"]) + "?token=admin"
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
# self.assertEqual(type(response.data), list)
self.assertEqual(len(response.data), 2)
self.assertEqual(response.data[0]["siret"], "22222222222222")
self.assertEqual(response.data[1]["siret"], "22222222222222")
self.assertTrue("sectors" in response.data[0])


class SiaeRetrieveBySiretApiTest(TestCase):
@classmethod
def setUpTestData(cls):
Expand Down
20 changes: 19 additions & 1 deletion lemarche/api/siaes/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.http import Http404
from django.http import Http404, HttpResponseBadRequest
from django.shortcuts import get_object_or_404
from drf_spectacular.utils import OpenApiParameter, extend_schema
from rest_framework import mixins, viewsets
Expand Down Expand Up @@ -78,6 +78,24 @@ def retrieve_by_slug(self, request, slug=None, format=None):
queryset_or_404 = get_object_or_404(queryset, slug=slug)
return self._retrieve_return(request, queryset_or_404, format)

@extend_schema(
summary="Détail d'une structure (par son siren)",
tags=[Siae._meta.verbose_name_plural],
parameters=[
OpenApiParameter(name="token", description="Token Utilisateur", required=False, type=str),
],
responses=SiaeDetailSerializer,
)
def retrieve_by_siren(self, request, siren=None, format=None):
"""
Note : le siren n'est pas nécessairement unique, il peut y avoir plusieurs structures retournées.<br /><br />
<i>Un <strong>token</strong> est nécessaire pour l'accès complet à cette ressource.</i>
"""
if len(siren) != 9:
return HttpResponseBadRequest("siren must be 9 caracters long")
queryset = self.get_queryset().prefetch_many_to_one().filter(siret__startswith=siren)
return self._list_return(request, queryset, format)

@extend_schema(
summary="Détail d'une structure (par son siret)",
tags=[Siae._meta.verbose_name_plural],
Expand Down
1 change: 1 addition & 0 deletions lemarche/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
path("", TemplateView.as_view(template_name="api/home.html"), name="home"),
# Additional API endpoints
path("siae/slug/<str:slug>/", SiaeViewSet.as_view({"get": "retrieve_by_slug"}), name="siae-retrieve-by-slug"),
path("siae/siren/<str:siren>/", SiaeViewSet.as_view({"get": "retrieve_by_siren"}), name="siae-retrieve-by-siren"),
path("siae/siret/<str:siret>/", SiaeViewSet.as_view({"get": "retrieve_by_siret"}), name="siae-retrieve-by-siret"),
path(
"siaes/siret/<str:siret>/",
Expand Down

0 comments on commit 3ad2c2f

Please sign in to comment.