From 0f76390b6798d23faefde8382953a544b27e1514 Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Wed, 14 Dec 2022 13:06:18 +0100
Subject: [PATCH 8/8] api_entreprise: use v3 in etablissement endpoint (#70610)

---
 passerelle/apps/api_entreprise/models.py | 18 ++++++++++++-
 tests/test_api_entreprise.py             | 34 +++++++++++++-----------
 2 files changed, 36 insertions(+), 16 deletions(-)

diff --git a/passerelle/apps/api_entreprise/models.py b/passerelle/apps/api_entreprise/models.py
index b1b23eb6..a853f89b 100644
--- a/passerelle/apps/api_entreprise/models.py
+++ b/passerelle/apps/api_entreprise/models.py
@@ -355,7 +355,23 @@ class APIEntreprise(BaseResource):
         },
     )
     def etablissements(self, request, siret, **kwargs):
-        return self.get('v2/etablissements/%s/' % siret, **kwargs)
+        raw_data = self.get('v3/insee/sirene/etablissements/diffusibles/%s' % siret, raw=True, **kwargs)
+        res = {}
+        data = raw_data.get('data')
+        naf = data.get('activite_principale', {}).get('code', '')
+        libelle_naf = data.get('activite_principale', {}).get('libelle', '')
+        data['naf'] = naf
+        data['libelle_naf'] = libelle_naf
+        data['date_creation_etablissement'] = data['date_creation']
+        localite = data.get('adresse', {}).get('libelle_commune', '')
+        code_insee_localite = data.get('adresse', {}).get('code_commune', '')
+        data['adresse']['localite'] = localite
+        data['adresse']['code_insee_localite'] = code_insee_localite
+        data['commune_implantation'] = {'code': code_insee_localite, 'value': localite}
+        for i in range(1, 8):
+            data['adresse']['l%s' % i] = data['adresse'].get('acheminement_postal', {}).get('l%s' % i)
+        res['etablissement'] = data
+        return {'data': res}
 
     @endpoint(
         perm='can_access',
diff --git a/tests/test_api_entreprise.py b/tests/test_api_entreprise.py
index 38956aed..60d7c53f 100644
--- a/tests/test_api_entreprise.py
+++ b/tests/test_api_entreprise.py
@@ -25,11 +25,9 @@ from passerelle.apps.api_entreprise.models import APIEntreprise
 from tests.utils import FakedResponse, make_resource
 
 ETABLISSEMENTS_RESPONSE = {
-    "etablissement": {
+    "data": {
         "siege_social": True,
         "siret": "41816609600051",
-        "naf": "6202A",
-        "libelle_naf": "Conseil en systèmes et logiciels informatiques",
         "date_mise_a_jour": 1449183600,
         "tranche_effectif_salarie_etablissement": {
             "de": 200,
@@ -38,25 +36,30 @@ ETABLISSEMENTS_RESPONSE = {
             "date_reference": "2014",
             "intitule": "200 à 249 salariés",
         },
-        "date_creation_etablissement": 1108594800,
+        "date_creation": 1108594800,
         "region_implantation": {"code": "11", "value": "Île-de-France"},
-        "commune_implantation": {"code": "75108", "value": "PARIS 8"},
         "adresse": {
-            "l1": "OCTO TECHNOLOGY",
-            "l2": None,
-            "l3": None,
-            "l4": "50 AVENUE DES CHAMPS ELYSEES",
-            "l5": None,
-            "l6": "75008 PARIS",
-            "l7": "FRANCE",
+            "acheminement_postal": {
+                "l1": "OCTO TECHNOLOGY",
+                "l2": None,
+                "l3": None,
+                "l4": "50 AVENUE DES CHAMPS ELYSEES",
+                "l5": None,
+                "l6": "75008 PARIS",
+                "l7": "FRANCE",
+            },
             "numero_voie": "50",
             "type_voie": "AV",
             "nom_voie": "DES CHAMPS ELYSEES",
             "code_postal": "75008",
-            "localite": "PARIS 8",
-            "code_insee_localite": "75108",
+            "libelle_commune": "PARIS 8",
+            "code_commune": "75108",
         },
         "etat_administratif": {"value": "F", "date_fermeture": 1315173600},
+        "activite_principale": {
+            "code": "6202A",
+            "libelle": "Conseil en systèmes et logiciels informatiques",
+        },
     },
     "gateway_error": False,
 }
@@ -412,7 +415,8 @@ def test_entreprises_endpoint_include_private(app, resource):
 def test_etablissements_endpoint(app, resource):
     with responses.RequestsMock() as rsps:
         rsps.get(
-            'https://entreprise.api.gouv.fr/v2/etablissements/44317013900036/', json=ETABLISSEMENTS_RESPONSE
+            'https://entreprise.api.gouv.fr/v3/insee/sirene/etablissements/diffusibles/44317013900036',
+            json=ETABLISSEMENTS_RESPONSE,
         )
         response = app.get('/api-entreprise/test/etablissements/44317013900036/', params=REQUEST_PARAMS)
     assert 'data' in response.json
-- 
2.30.2

