Projet

Général

Profil

0002-api_entreprise-add-endpoint-to-get-last-document-of-.patch

Serghei Mihai, 05 septembre 2019 15:20

Télécharger (4,83 ko)

Voir les différences:

Subject: [PATCH 2/2] api_entreprise: add endpoint to get last document of
 given type (#35361)

 passerelle/apps/api_entreprise/models.py | 35 ++++++++++++++++++++++++
 tests/test_api_entreprise.py             | 24 ++++++++++++++--
 2 files changed, 57 insertions(+), 2 deletions(-)
passerelle/apps/api_entreprise/models.py
208 208
            return HttpResponse(response, content_type='application/pdf')
209 209
        raise Http404('document not found')
210 210

  
211
    @endpoint(name='document_association',
212
              pattern=r'(?P<association_id>\w+)/get-last/$',
213
              example_pattern='{association_id}/get-last/',
214
              description=_('Get association\'s last document of type'),
215
              parameters={
216
                  'association_id': {
217
                      'description': _('association SIREN or RNA/WALDEC number'),
218
                      'example_value': '44317013900036',
219
                  },
220
                  'document_type': {
221
                      'description': _('document type'),
222
                      'example_value': 'Statuts',
223
                  },
224
                  'object': {
225
                      'Description': _('request object: form number, file identifier...'),
226
                      'example_value': '42'
227
                  },
228
                  'context': {
229
                      'description': _('request context: MPS, APS...'),
230
                      'example_value': 'APS'
231
                  },
232
                  'recipient': {
233
                      'description': _('request recipient: usually customer number'),
234
                      'example_value': '44317013900036'
235
                  }
236
              })
237
    def get_last_document_of_type(self, request, association_id, document_type, **kwargs):
238
        document = None
239
        resp = self.get('documents_associations/%s/' % association_id, **kwargs)
240
        documents = [item for item in resp['data'].get('documents', []) if item.get('type') == document_type]
241
        if documents:
242
            documents.sort(key=lambda doc: doc['timestamp'])
243
            document = documents[-1]
244
        return {'data': document}
245

  
211 246
    @endpoint(perm='can_access',
212 247
              pattern=r'(?P<siren>\w+)/$',
213 248
              example_pattern='{siren}/',
tests/test_api_entreprise.py
214 214
      "url": "https://apientreprise.fr/attestations/40ab0b07d434d0417e8997ce7c5afbef/recepisse_association.pdf",
215 215
      "timestamp": "1500667325"
216 216
    },
217
    {
218
      "timestamp": "1337158058",
219
      "url": "https://apientreprise.fr/attestations/40ab0b07d434d0417e8997ce7c5afbef/attestation_document_association.pdf",
220
      "type": "Statuts"
221
    },
217 222
  ]
218 223
}
219 224

  
......
374 379
                       params=REQUEST_PARAMS)
375 380
    assert 'data' in response.json
376 381
    data = response.json['data']
377
    assert len(data) == 2
382
    assert len(data) == 3
378 383
    for document in data:
379 384
        assert 'id' in document
380 385
        assert 'text' in document
......
383 388
        assert 'datetime' in document
384 389

  
385 390

  
391
def test_associations_last_document_of_type_endpoint(app, resource, mock_api_entreprise):
392
    params = REQUEST_PARAMS
393
    params['document_type'] = 'Statuts'
394
    response = app.get('/api-entreprise/test/document_association/443170139/get-last/',
395
                       params=params)
396
    assert 'data' in response.json
397
    data = response.json['data']
398
    assert data['timestamp'] == '1500660325'
399

  
400
    params['document_type'] = 'Liste des dirigeants'
401
    response = app.get('/api-entreprise/test/document_association/443170139/get-last/',
402
                       params=params)
403
    assert not response.json['data']
404

  
405

  
386 406
def test_extraits_rcs(app, resource, mock_api_entreprise, freezer):
387 407
    response = app.get('/api-entreprise/test/extraits_rcs/443170139/',
388 408
                       params=REQUEST_PARAMS)
......
401 421
                       params=REQUEST_PARAMS)
402 422
    assert 'data' in response.json
403 423
    data = response.json['data']
404
    assert len(data) == 2
424
    assert len(data) == 3
405 425
    document = data[0]
406 426
    assert 'url' in document
407 427
    resp = app.get(document['url'],
408
-