Projet

Général

Profil

0001-api_entreprise-store-default-recipient-in-model-3443.patch

Serghei Mihai, 01 juillet 2019 16:00

Télécharger (4,21 ko)

Voir les différences:

Subject: [PATCH] api_entreprise: store default recipient in model (#34439)

And let it be overriden in endpoint calls.
 .../migrations/0002_auto_20190701_1357.py     | 23 +++++++++++++++++++
 passerelle/apps/api_entreprise/models.py      |  5 +++-
 tests/test_api_entreprise.py                  |  8 +++++--
 3 files changed, 33 insertions(+), 3 deletions(-)
 create mode 100644 passerelle/apps/api_entreprise/migrations/0002_auto_20190701_1357.py
passerelle/apps/api_entreprise/migrations/0002_auto_20190701_1357.py
1
# -*- coding: utf-8 -*-
2
# Generated by Django 1.11.20 on 2019-07-01 11:57
3
from __future__ import unicode_literals
4

  
5
from django.db import migrations, models
6

  
7

  
8
class Migration(migrations.Migration):
9

  
10
    dependencies = [
11
        ('api_entreprise', '0001_initial'),
12
    ]
13

  
14
    operations = [
15
        migrations.AddField(
16
            model_name='apientreprise',
17
            name='recipient',
18
            field=models.CharField(default='', max_length=1024, verbose_name='Recipient',
19
                                   help_text='default value'
20
            ),
21
            preserve_default=False,
22
        ),
23
    ]
passerelle/apps/api_entreprise/models.py
71 71

  
72 72
    url = models.URLField(_('API URL'), max_length=256, default='https://entreprise.api.gouv.fr/v2/')
73 73
    token = models.CharField(max_length=1024, verbose_name=_('API token'))
74
    recipient = models.CharField(max_length=1024, verbose_name=_('Recipient'), blank=False,
75
                                 help_text=_('default value'))
74 76

  
75 77
    category = _('Business Process Connectors')
76 78

  
......
79 81

  
80 82
    def get(self, path, **kwargs):
81 83
        params = {'token': self.token}
82
        for param in ('context', 'object', 'recipient'):
84
        for param in ('context', 'object'):
83 85
            if not kwargs.get(param):
84 86
                raise WrongParameter([param], [])
85 87
            params[param] = kwargs[param]
88
        params['recipient'] = kwargs.get('recipient') or self.recipient
86 89
        url = urljoin(self.url, path)
87 90
        try:
88 91
            response = self.requests.get(url, data=params)
tests/test_api_entreprise.py
290 290
        slug='test',
291 291
        title='API Entreprise',
292 292
        description='API Entreprise',
293
        token='83c68bf0b6013c4daf3f8213f7212aa5')
293
        token='83c68bf0b6013c4daf3f8213f7212aa5',
294
        recipient='recipient')
294 295

  
295 296

  
296 297
@mock.patch('passerelle.utils.Request.get')
......
299 300
    response = app.get('/api-entreprise/test/documents_associations/443170139/', status=400)
300 301
    assert response.json['err_class'] == 'passerelle.views.WrongParameter'
301 302
    assert response.json['err_desc'] == "missing parameters: 'context'."
302
    params = {'context': 'Custom context', 'object': 'Custom object', 'recipient': 'Custom recipient'}
303
    params = {'context': 'Custom context', 'object': 'Custom object'}
303 304
    response = app.get('/api-entreprise/test/documents_associations/443170139/', params=params)
304 305
    assert mocked_get.call_args[1]['data']['context'] == 'Custom context'
305 306
    assert mocked_get.call_args[1]['data']['object'] == 'Custom object'
307
    assert mocked_get.call_args[1]['data']['recipient'] == 'recipient'
308
    params['recipient'] = 'Custom recipient'
309
    response = app.get('/api-entreprise/test/documents_associations/443170139/', params=params)
306 310
    assert mocked_get.call_args[1]['data']['recipient'] == 'Custom recipient'
307 311

  
308 312

  
309
-