Projet

Général

Profil

0004-caluire-axel-child_info-endpoint-53825.patch

Lauréline Guérin, 07 mai 2021 14:48

Télécharger (5 ko)

Voir les différences:

Subject: [PATCH 4/4] caluire-axel: child_info endpoint (#53825)

 functests/caluire_axel/test_caluire_axel.py | 10 ++++
 passerelle/contrib/caluire_axel/models.py   | 20 ++++++++
 tests/test_caluire_axel.py                  | 56 +++++++++++++++++++++
 3 files changed, 86 insertions(+)
functests/caluire_axel/test_caluire_axel.py
38 38
    assert res['err'] == 0
39 39
    print('\n')
40 40

  
41
    for child in data['data']['MEMBRE']:
42
        print("GET child info")
43
        url = conn + '/child_info?NameID=%s&idpersonne=%s' % (name_id, child['IDENT'])
44
        resp = requests.get(url)
45
        resp.raise_for_status()
46
        res = resp.json()
47
        pprint.pprint(res)
48
        assert res['err'] == 0
49
        print('\n')
50

  
41 51
    print("Deleting link")
42 52
    url = conn + '/unlink?NameID=%s' % name_id
43 53
    resp = requests.post(url)
passerelle/contrib/caluire_axel/models.py
178 178
        family_data = self.get_family_data(link.family_id)
179 179
        return {'data': family_data.get('MEMBRE', [])}
180 180

  
181
    @endpoint(
182
        display_category=_('Family account'),
183
        display_order=5,
184
        description=_("Get information about a child"),
185
        perm='can_access',
186
        parameters={
187
            'NameID': {'description': _('Publik ID')},
188
            'idpersonne': {'description': _('Child ID')},
189
        },
190
    )
191
    def child_info(self, request, idpersonne, NameID):
192
        link = self.get_link(NameID)
193
        family_data = self.get_family_data(link.family_id)
194

  
195
        for child in family_data.get('MEMBRE', []):
196
            if child['IDENT'] == idpersonne:
197
                return {'data': child}
198

  
199
        raise APIError('Child not found', err_code='not-found')
200

  
181 201

  
182 202
class Link(models.Model):
183 203
    resource = models.ForeignKey(CaluireAxel, on_delete=models.CASCADE)
tests/test_caluire_axel.py
434 434
    assert resp.json['data'][2]['text'] == 'Enfant 3 CALUIRE TEST'
435 435
    assert resp.json['data'][3]['id'] == '59509'
436 436
    assert resp.json['data'][3]['text'] == 'Enfant 5 CALUIRE TEST'
437

  
438

  
439
def test_child_info_endpoint_axel_error(app, resource):
440
    Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
441
    with mock.patch('passerelle.contrib.caluire_axel.schemas.get_famille_individus') as operation:
442
        operation.side_effect = AxelError('FooBar')
443
        resp = app.get('/caluire-axel/test/child_info?NameID=yyy&idpersonne=zzz')
444
    assert resp.json['err_desc'] == "Axel error: FooBar"
445
    assert resp.json['err'] == 'error'
446

  
447

  
448
def test_child_info_endpoint_no_result(app, resource):
449
    resp = app.get('/caluire-axel/test/child_info?NameID=yyy&idpersonne=zzz')
450
    assert resp.json['err_desc'] == "Person not found"
451
    assert resp.json['err'] == 'not-found'
452

  
453
    Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
454
    filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/family_info.xml')
455
    with open(filepath) as xml:
456
        content = xml.read()
457
    with mock_getdata(content, 'GetFamilleIndividus'):
458
        resp = app.get('/caluire-axel/test/child_info?NameID=yyy&idpersonne=zzz')
459
    assert resp.json['err_desc'] == "Child not found"
460
    assert resp.json['err'] == 'not-found'
461

  
462

  
463
def test_child_info_endpoint(app, resource):
464
    Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
465
    filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/family_info.xml')
466
    with open(filepath) as xml:
467
        content = xml.read()
468
    with mock_getdata(content, 'GetFamilleIndividus'):
469
        resp = app.get('/caluire-axel/test/child_info?NameID=yyy&idpersonne=50632')
470
    assert resp.json['err'] == 0
471
    assert set(resp.json['data'].keys()) == set(
472
        [
473
            'ADRESSE',
474
            'CIVILITE',
475
            'FAMILLE',
476
            'GARDEALTERNEE',
477
            'IDENT',
478
            'MAIL',
479
            'NAISSANCE',
480
            'NOM',
481
            'NOMJF',
482
            'PAI',
483
            'PRENOM',
484
            'SEXE',
485
            'TELFIXE',
486
            'TELPORTABLE',
487
            'id',
488
            'text',
489
        ]
490
    )
491
    assert resp.json['data']['id'] == '50632'
492
    assert resp.json['data']['text'] == 'Enfant 1 CALUIRE TEST'
437
-