From 10707873af96ece1cce6684521f17545a4d173bb Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Mon, 6 Apr 2020 15:30:11 +0200
Subject: [PATCH 2/2] api_entreprise: add covid endpoints (#41315)

---
 passerelle/apps/api_entreprise/models.py | 95 ++++++++++++++++++++++++
 tests/test_api_entreprise.py             | 91 ++++++++++++++++++++++-
 2 files changed, 184 insertions(+), 2 deletions(-)

diff --git a/passerelle/apps/api_entreprise/models.py b/passerelle/apps/api_entreprise/models.py
index 81562552..4672eb0e 100644
--- a/passerelle/apps/api_entreprise/models.py
+++ b/passerelle/apps/api_entreprise/models.py
@@ -323,6 +323,101 @@ class APIEntreprise(BaseResource):
     def entreprises(self, request, siren, **kwargs):
         return self.get('entreprises/%s/' % siren, **kwargs)
 
+    @endpoint(perm='can_access',
+              pattern=r'(?P<siren>\w+)/$',
+              example_pattern='{siren}/',
+              description=_('Get firm\'s related numbers'),
+              parameters={
+                  'siren': {
+                      'description': _('firm SIREN number'),
+                      'example_value': '44317013900036',
+                  },
+                  'object': {
+                      'description': _('request object: form number, file identifier...'),
+                      'example_value': '42'
+                  },
+                  'context': {
+                      'description': _('request context: MPS, APS...'),
+                      'example_value': 'APS'
+                  },
+                  'recipient': {
+                      'description': _('request recipient: usually customer number'),
+                      'example_value': '44317013900036'
+                  }
+              })
+    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+)/$',
+              example_pattern='{siren}/',
+              description=_('Get firm\'s related monthly numbers'),
+              parameters={
+                  'year': {
+                      'description': _('requested year'),
+                      'example_value': '2019',
+                  },
+                  'month': {
+                      'description': _('requested month'),
+                      'example_value': '02',
+                  },
+                  'siren': {
+                      'description': _('firm SIREN number'),
+                      'example_value': '44317013900036',
+                  },
+                  'object': {
+                      'description': _('request object: form number, file identifier...'),
+                      'example_value': '42'
+                  },
+                  'context': {
+                      'description': _('request context: MPS, APS...'),
+                      'example_value': 'APS'
+                  },
+                  'recipient': {
+                      'description': _('request recipient: usually customer number'),
+                      'example_value': '44317013900036'
+                  }
+              })
+    def entreprise_effectifs_mensuels_acoss_covid(self, request, year, month, siren, **kwargs):
+        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+)/$',
+              example_pattern='{siren}/',
+              description=_('Get firm\'s related monthly numbers'),
+              parameters={
+                  'year': {
+                      'description': _('requested year'),
+                      'example_value': '2019',
+                  },
+                  'month': {
+                      'description': _('requested month'),
+                      'example_value': '02',
+                  },
+                  'siren': {
+                      'description': _('firm SIRET number'),
+                      'example_value': '44317013900036',
+                  },
+                  'object': {
+                      'description': _('request object: form number, file identifier...'),
+                      'example_value': '42'
+                  },
+                  'context': {
+                      'description': _('request context: MPS, APS...'),
+                      'example_value': 'APS'
+                  },
+                  'recipient': {
+                      'description': _('request recipient: usually customer number'),
+                      'example_value': '44317013900036'
+                  }
+              })
+    def etablissement_effectifs_mensuels_acoss_covid(self, request, year, month, siret, **kwargs):
+        return self.get(
+            'effectifs_mensuels_acoss_covid/%s/%s/etablissement/%s/' % (year, month, siret), **kwargs
+        )
+
     @endpoint(perm='can_access',
               methods=['get'],
               pattern=r'(?P<siret>\w+)/$',
diff --git a/tests/test_api_entreprise.py b/tests/test_api_entreprise.py
index f498c010..71b78b1a 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
 
 
@@ -415,6 +460,19 @@ def test_extraits_rcs(app, resource, mock_api_entreprise, freezer):
     assert data['date_immatriculation_datetime'] == '1998-03-26T23:00:00Z'
 
 
+def test_extraits_rcs(app, resource, mock_api_entreprise, freezer):
+    response = app.get('/api-entreprise/test/extraits_rcs/443170139/',
+                       params=REQUEST_PARAMS)
+    assert 'data' in response.json
+    data = response.json['data']
+
+    assert data['siren'] == '418166096'
+    assert data['date_extrait'] == '21 AVRIL 2017'
+    assert data['date_immatriculation_timestamp'] == 890953200
+    assert data['date_immatriculation'] == '1998-03-27'
+    assert data['date_immatriculation_datetime'] == '1998-03-26T23:00:00Z'
+
+
 def test_document_association(app, resource, mock_api_entreprise, freezer):
     response = app.get('/api-entreprise/test/documents_associations/443170139/',
                        params=REQUEST_PARAMS)
@@ -436,6 +494,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

