From 47ec39ff57384d05f7819c1468a3fa31af9a3206 Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Mon, 6 Apr 2020 16:54:15 +0200
Subject: [PATCH 3/3] api_entreprise: add covid endpoints (#41315)

---
 passerelle/apps/api_entreprise/models.py | 57 +++++++++++++++++
 tests/test_api_entreprise.py             | 78 +++++++++++++++++++++++-
 2 files changed, 133 insertions(+), 2 deletions(-)

diff --git a/passerelle/apps/api_entreprise/models.py b/passerelle/apps/api_entreprise/models.py
index 01c8a55f..0a4d2d8a 100644
--- a/passerelle/apps/api_entreprise/models.py
+++ b/passerelle/apps/api_entreprise/models.py
@@ -139,6 +139,11 @@ class APIEntreprise(BaseResource):
         'example_value': 'APS'
     }
 
+    MONTH_PARAM = {
+        'description': _('requested month'),
+        'example_value': '02',
+    }
+
     OBJECT_PARAM = {
         'description': _('request object: form number, file identifier...'),
         'example_value': '42'
@@ -159,6 +164,11 @@ class APIEntreprise(BaseResource):
         'example_value': '44317013900036'
     }
 
+    YEAR_PARAM = {
+        'description': _('requested year'),
+        'example_value': '2019',
+    }
+
     @endpoint(perm='can_access',
               pattern=r'(?P<association_id>\w+)/$',
               example_pattern='{association_id}/',
@@ -295,3 +305,50 @@ class APIEntreprise(BaseResource):
               })
     def etablissements(self, request, siret, **kwargs):
         return self.get('etablissements/%s/' % siret, **kwargs)
+
+    @endpoint(perm='can_access',
+              pattern=r'(?P<siren>\w+)/$',
+              example_pattern='{siren}/',
+              description=_('Get firm\'s related numbers'),
+              parameters={
+                  'siren': SIREN_PARAM,
+                  'object': OBJECT_PARAM,
+                  'context': CONTEXT_PARAM,
+                  'recipient': RECIPIENT_PARAM,
+              })
+    def effectifs_annuels_acoss_covid(self, request, siren, **kwargs):
+        return self.get('effectifs_annuels_acoss_covid/%s/' % siren, **kwargs)
+
+    @endpoint(perm='can_access',
+              pattern=r'(?P<year>\w+)/(?P<month>\w+)/(?P<siren>\w+)/$',
+              description=_('Get firm\'s related monthly numbers'),
+              parameters={
+                  'year': YEAR_PARAM,
+                  'month': MONTH_PARAM,
+                  'siren': SIREN_PARAM,
+                  'object': OBJECT_PARAM,
+                  'context': CONTEXT_PARAM,
+                  'recipient': RECIPIENT_PARAM,
+              })
+    def entreprise_effectifs_mensuels_acoss_covid(self, request, year, month, siren, **kwargs):
+        month = month.zfill(2)
+        return self.get(
+            'effectifs_mensuels_acoss_covid/%s/%s/entreprise/%s/' % (year, month, siren), **kwargs
+        )
+
+    @endpoint(perm='can_access',
+              pattern=r'(?P<year>\w+)/(?P<month>\w+)/(?P<siret>\w+)/$',
+              description=_('Get firm\'s related monthly numbers'),
+              parameters={
+                  'year': YEAR_PARAM,
+                  'month': MONTH_PARAM,
+                  'siret': SIRET_PARAM,
+                  'object': OBJECT_PARAM,
+                  'context': CONTEXT_PARAM,
+                  'recipient': RECIPIENT_PARAM,
+              })
+    def etablissement_effectifs_mensuels_acoss_covid(self, request, year, month, siret, **kwargs):
+        month = month.zfill(2)
+        return self.get(
+            'effectifs_mensuels_acoss_covid/%s/%s/etablissement/%s/' % (year, month, siret), **kwargs
+        )
diff --git a/tests/test_api_entreprise.py b/tests/test_api_entreprise.py
index f498c010..ecd49c19 100644
--- a/tests/test_api_entreprise.py
+++ b/tests/test_api_entreprise.py
@@ -221,6 +221,30 @@ DOCUMENTS_ASSOCIATION_RESPONSE = {
   ]
 }
 
+
+EFFECTIFS_ANNUELS_ACOSS_COVID = {
+    "siren": "418166096",
+    "annee": "2019",
+    "effectifs_annuels": 100.5
+}
+
+
+ENTREPRISE_EFFECTIFS_MENSUELS_ACOSS_COVID = {
+    "siren": "418166096",
+    "annee": "2019",
+    "mois": "02",
+    "effectifs_mensuels": 100.5
+}
+
+
+ETABLISSEMENT_EFFECTIFS_MENSUELS_ACOSS_COVID = {
+    "siret": "41816609600051",
+    "annee": "2019",
+    "mois": "02",
+    "effectifs_mensuels": 100.5
+}
+
+
 DOCUMENT_ASSOCIATION_RESPONSE = "binary content"
 
 REQUEST_PARAMS = {'context': 'MSP', 'object': 'demand', 'recipient': 'siret'}
@@ -259,6 +283,24 @@ def api_entreprise_document_association(url, request):
     return response(200, DOCUMENT_ASSOCIATION_RESPONSE, request=request)
 
 
+@urlmatch(netloc='^entreprise.api.gouv.fr$',
+          path='^/v2/effectifs_annuels_acoss_covid/')
+def effectifs_annuels_acoss_covid(url, request):
+    return response(200, EFFECTIFS_ANNUELS_ACOSS_COVID, request=request)
+
+
+@urlmatch(netloc='^entreprise.api.gouv.fr$',
+          path='^/v2/effectifs_mensuels_acoss_covid/.*/entreprise')
+def entreprise_effectifs_mensuels_acoss_covid(url, request):
+    return response(200, ENTREPRISE_EFFECTIFS_MENSUELS_ACOSS_COVID, request=request)
+
+
+@urlmatch(netloc='^entreprise.api.gouv.fr$',
+          path='^/v2/effectifs_mensuels_acoss_covid/.*/etablissement')
+def etablisssment_effectifs_mensuels_acoss_covid(url, request):
+    return response(200, ETABLISSEMENT_EFFECTIFS_MENSUELS_ACOSS_COVID, request=request)
+
+
 @urlmatch(netloc='^entreprise.api.gouv.fr$')
 def api_entreprise_error_500(url, request):
     return response(500, 'bad error happened', request=request)
@@ -282,8 +324,11 @@ def api_entreprise_error_not_found(url, request):
 
 @pytest.yield_fixture
 def mock_api_entreprise():
-    with HTTMock(api_entreprise_etablissements, api_entreprise_entreprises, api_entreprise_associations, api_entreprise_extraits_rcs,
-                 api_entreprise_associations, api_entreprise_documents_associations, api_entreprise_document_association):
+    with HTTMock(
+            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):
         yield None
 
 
@@ -436,6 +481,35 @@ def test_document_association(app, resource, mock_api_entreprise, freezer):
     resp = app.get(document['url'], status=404)
 
 
+def test_effectifs_annuels_acoss_covid(app, resource, mock_api_entreprise, freezer):
+    response = app.get('/api-entreprise/test/effectifs_annuels_acoss_covid/418166096/',
+                       params=REQUEST_PARAMS)
+    data = response.json['data']
+    assert data['siren'] == '418166096'
+    assert data['annee'] == '2019'
+    assert data['effectifs_annuels'] == 100.5
+
+
+def test_entreprise_effectifs_mensuels_acoss_covid(app, resource, mock_api_entreprise, freezer):
+    response = app.get('/api-entreprise/test/entreprise_effectifs_mensuels_acoss_covid/2019/02/418166096/',
+                       params=REQUEST_PARAMS)
+    data = response.json['data']
+    assert data['siren'] == '418166096'
+    assert data['annee'] == '2019'
+    assert data['mois'] == '02'
+    assert data['effectifs_mensuels'] == 100.5
+
+
+def test_etablissment_effectifs_mensuels_acoss_covid(app, resource, mock_api_entreprise, freezer):
+    response = app.get('/api-entreprise/test/etablissement_effectifs_mensuels_acoss_covid/2019/02/418166096/',
+                       params=REQUEST_PARAMS)
+    data = response.json['data']
+    assert data['siret'] == '41816609600051'
+    assert data['annee'] == '2019'
+    assert data['mois'] == '02'
+    assert data['effectifs_mensuels'] == 100.5
+
+
 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

