From 6809adb11c93da5fe5a865e0433426bce37e3c9d Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Tue, 25 Oct 2022 16:03:45 +0200
Subject: [PATCH 6/7] api_entreprise: use v3 in association's document related
 endpoints (#70610)

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

diff --git a/passerelle/apps/api_entreprise/models.py b/passerelle/apps/api_entreprise/models.py
index 0e2effe5..b1b23eb6 100644
--- a/passerelle/apps/api_entreprise/models.py
+++ b/passerelle/apps/api_entreprise/models.py
@@ -196,9 +196,12 @@ class APIEntreprise(BaseResource):
     )
     def documents_associations(self, request, association_id, **kwargs):
         data = []
-        resp = self.get('v2/documents_associations/%s/' % association_id, raw=True, **kwargs)
-        for item in resp.get('documents', []):
+        resp = self.get(
+            'v3/ministere_interieur/rna/associations/%s/documents' % association_id, raw=True, **kwargs
+        )
+        for _item in resp.get('data', []):
             # ignore documents with no type
+            item = _item.get('data', {})
             if not item.get('type'):
                 continue
             signature_elements = {
@@ -270,8 +273,14 @@ class APIEntreprise(BaseResource):
     )
     def get_last_document_of_type(self, request, association_id, document_type, **kwargs):
         document = None
-        resp = self.get('v2/documents_associations/%s/' % association_id, raw=True, **kwargs)
-        documents = [item for item in resp.get('documents', []) if item.get('type') == document_type]
+        resp = self.get(
+            'v3/ministere_interieur/rna/associations/%s/documents' % association_id, raw=True, **kwargs
+        )
+        documents = [
+            item.get('data')
+            for item in resp.get('data', [])
+            if item.get('data', {}).get('type') == document_type
+        ]
         if documents:
             documents.sort(key=lambda doc: doc['timestamp'])
             document = documents[-1]
diff --git a/tests/test_api_entreprise.py b/tests/test_api_entreprise.py
index 6849a8e9..67354773 100644
--- a/tests/test_api_entreprise.py
+++ b/tests/test_api_entreprise.py
@@ -183,22 +183,28 @@ ASSOCIATIONS_RESPONSE = {
 }
 
 DOCUMENTS_ASSOCIATION_RESPONSE = {
-    "nombre_documents": 2,
-    "documents": [
+    "meta": {"nombre_documents": 2, "nombre_documents_deficients": 0},
+    "data": [
         {
-            "type": "Statuts",
-            "url": "https://apientreprise.fr/attestations/40ab0b07d434d0417e8997ce7c5afbef/attestation_document_association.pdf",
-            "timestamp": "1500660325",
+            "data": {
+                "type": "Statuts",
+                "url": "https://apientreprise.fr/attestations/40ab0b07d434d0417e8997ce7c5afbef/attestation_document_association.pdf",
+                "timestamp": "1500660325",
+            },
         },
         {
-            "type": "Récépissé",
-            "url": "https://apientreprise.fr/attestations/40ab0b07d434d0417e8997ce7c5afbef/recepisse_association.pdf",
-            "timestamp": "1500667325",
+            "data": {
+                "type": "Récépissé",
+                "url": "https://apientreprise.fr/attestations/40ab0b07d434d0417e8997ce7c5afbef/recepisse_association.pdf",
+                "timestamp": "1500667325",
+            },
         },
         {
-            "timestamp": "1337158058",
-            "url": "https://apientreprise.fr/attestations/40ab0b07d434d0417e8997ce7c5afbef/attestation_document_association.pdf",
-            "type": "Statuts",
+            "data": {
+                "timestamp": "1337158058",
+                "url": "https://apientreprise.fr/attestations/40ab0b07d434d0417e8997ce7c5afbef/attestation_document_association.pdf",
+                "type": "Statuts",
+            },
         },
     ],
 }
@@ -446,7 +452,7 @@ def test_associations_endpoint(app, resource):
 def test_documents_associations_endpoint(app, resource):
     with responses.RequestsMock() as rsps:
         rsps.get(
-            'https://entreprise.api.gouv.fr/v2/documents_associations/443170139/',
+            'https://entreprise.api.gouv.fr/v3/ministere_interieur/rna/associations/443170139/documents',
             json=DOCUMENTS_ASSOCIATION_RESPONSE,
         )
 
@@ -465,7 +471,7 @@ def test_documents_associations_endpoint(app, resource):
 def test_associations_last_document_of_type_endpoint(app, resource):
     with responses.RequestsMock() as rsps:
         rsps.get(
-            'https://entreprise.api.gouv.fr/v2/documents_associations/443170139/',
+            'https://entreprise.api.gouv.fr/v3/ministere_interieur/rna/associations/443170139/documents',
             json=DOCUMENTS_ASSOCIATION_RESPONSE,
         )
 
@@ -501,7 +507,7 @@ def test_extraits_rcs(app, resource):
 def test_document_association(app, resource, freezer):
     with responses.RequestsMock() as rsps:
         rsps.get(
-            'https://entreprise.api.gouv.fr/v2/documents_associations/443170139/',
+            'https://entreprise.api.gouv.fr/v3/ministere_interieur/rna/associations/443170139/documents',
             json=DOCUMENTS_ASSOCIATION_RESPONSE,
         )
 
-- 
2.30.2

