From 74cb9a5276fff2492c3318cbb0acd1df505188fd Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Tue, 7 Apr 2020 13:27:12 +0200
Subject: [PATCH] api_entreprise: add 'exercices' endpoint (#41416)

---
 passerelle/apps/api_entreprise/models.py | 14 ++++++++
 tests/test_api_entreprise.py             | 41 +++++++++++++++++++++++-
 2 files changed, 54 insertions(+), 1 deletion(-)

diff --git a/passerelle/apps/api_entreprise/models.py b/passerelle/apps/api_entreprise/models.py
index 020c0fdc..dbd89ad2 100644
--- a/passerelle/apps/api_entreprise/models.py
+++ b/passerelle/apps/api_entreprise/models.py
@@ -306,6 +306,20 @@ class APIEntreprise(BaseResource):
     def etablissements(self, request, siret, **kwargs):
         return self.get('etablissements/%s/' % siret, **kwargs)
 
+    @endpoint(perm='can_access',
+              methods=['get'],
+              pattern=r'(?P<siret>\w+)/$',
+              example_pattern='{siret}/',
+              description_get=_('Get firms\'s financial year informations'),
+              parameters={
+                  'siret': SIRET_PARAM,
+                  'object': OBJECT_PARAM,
+                  'context': CONTEXT_PARAM,
+                  'recipient': RECIPIENT_PARAM,
+              })
+    def exercices(self, request, siret, **kwargs):
+        return self.get('exercices/%s/' % siret, **kwargs)
+
     @endpoint(perm='can_access',
               pattern=r'(?P<siren>\w+)/$',
               example_pattern='{siren}/',
diff --git a/tests/test_api_entreprise.py b/tests/test_api_entreprise.py
index ecd49c19..b33edcd9 100644
--- a/tests/test_api_entreprise.py
+++ b/tests/test_api_entreprise.py
@@ -245,6 +245,27 @@ ETABLISSEMENT_EFFECTIFS_MENSUELS_ACOSS_COVID = {
 }
 
 
+EXERCICES_RESPONSE = {
+    "exercices": [
+        {
+            "ca": "648374448",
+            "date_fin_exercice": "2016-12-31T00:00:00+01:00",
+            "date_fin_exercice_timestamp": 1483138800
+        },
+        {
+            "ca": "491463386",
+            "date_fin_exercice": "2015-12-31T00:00:00+01:00",
+            "date_fin_exercice_timestamp": 1451516400
+        },
+        {
+            "ca": "473899061",
+            "date_fin_exercice": "2014-12-31T00:00:00+01:00",
+            "date_fin_exercice_timestamp": 1419980400
+        }
+    ]
+}
+
+
 DOCUMENT_ASSOCIATION_RESPONSE = "binary content"
 
 REQUEST_PARAMS = {'context': 'MSP', 'object': 'demand', 'recipient': 'siret'}
@@ -301,6 +322,12 @@ def etablisssment_effectifs_mensuels_acoss_covid(url, request):
     return response(200, ETABLISSEMENT_EFFECTIFS_MENSUELS_ACOSS_COVID, request=request)
 
 
+@urlmatch(netloc='^entreprise.api.gouv.fr$',
+          path='^/v2/exercices')
+def api_entreprise_exercices(url, request):
+    return response(200, EXERCICES_RESPONSE, request=request)
+
+
 @urlmatch(netloc='^entreprise.api.gouv.fr$')
 def api_entreprise_error_500(url, request):
     return response(500, 'bad error happened', request=request)
@@ -328,7 +355,8 @@ def mock_api_entreprise():
             api_entreprise_etablissements, api_entreprise_entreprises, api_entreprise_associations,
             api_entreprise_extraits_rcs, api_entreprise_associations, api_entreprise_documents_associations,
             api_entreprise_document_association, effectifs_annuels_acoss_covid,
-            entreprise_effectifs_mensuels_acoss_covid, etablisssment_effectifs_mensuels_acoss_covid):
+            entreprise_effectifs_mensuels_acoss_covid, etablisssment_effectifs_mensuels_acoss_covid,
+            api_entreprise_exercices):
         yield None
 
 
@@ -510,6 +538,17 @@ def test_etablissment_effectifs_mensuels_acoss_covid(app, resource, mock_api_ent
     assert data['effectifs_mensuels'] == 100.5
 
 
+def test_exercices(app, resource, mock_api_entreprise, freezer):
+    response = app.get('/api-entreprise/test/exercices/418166096/', params=REQUEST_PARAMS)
+    data = response.json['data']
+    assert len(data['exercices']) == 3
+    exercice = data['exercices'][0]
+    assert exercice['ca'] == '648374448'
+    assert exercice['date_fin_exercice'] == '2016-12-31T00:00:00+01:00'
+    assert exercice['date_fin_exercice_timestamp'] == 1483138800
+    assert exercice['date_fin_exercice_datetime'] == '2016-12-30T23:00:00Z'
+
+
 def test_error_500(app, resource, mock_api_entreprise):
     with HTTMock(api_entreprise_error_500):
         response = app.get('/api-entreprise/test/entreprises/443170139/',
-- 
2.20.1

