Projet

Général

Profil

0001-atos_genesys-make-search-endpoint-a-datasource-of-co.patch

Benjamin Dauvergne, 28 mai 2019 21:42

Télécharger (10,3 ko)

Voir les différences:

Subject: [PATCH] atos_genesys: make search endpoint a datasource of contact
 informations (#33492)

The datasource is only provisionned if the search find one and only one
dossier, otherwise it's empty.
 passerelle/apps/atos_genesys/models.py | 98 +++++++++++++++++++++----
 tests/test_atos_genesys.py             | 99 +++++++++++++++++++++++---
 2 files changed, 173 insertions(+), 24 deletions(-)
passerelle/apps/atos_genesys/models.py
269 269
        d['DROITS'] = droits
270 270
        # create CIVILITE
271 271
        for identification in d.get('IDENTIFICATION', []):
272
            sexe = identification['SEXE']
273
            identification['CIVILITE'] = {'M': u'Monsieur', 'F': u'Madame'}.get(sexe)
272
            sexe = identification.get('SEXE', '')
273
            identification['CIVILITE'] = {'M': u'Monsieur', 'F': u'Madame'}.get(sexe, '')
274 274
        return d
275 275

  
276 276
    @endpoint(name='dossiers',
......
363 363
              })
364 364
    def search(self, request, first_name, last_name, date_of_birth):
365 365
        try:
366
            date_of_birth = datetime.datetime.strptime(date_of_birth, '%Y-%m-%d')
366
            date_of_birth = datetime.datetime.strptime(date_of_birth, '%Y-%m-%d').date()
367 367
        except (ValueError, TypeError):
368 368
            raise APIError('invalid date_of_birth: %r' % date_of_birth)
369 369
        beneficiaires = self.call_cherche_beneficiaire(
370 370
            prenom=first_name,
371 371
            nom=last_name,
372 372
            dob=date_of_birth)
373
        data = []
374
        dossiers = []
375
        # get dossiers of found beneficiaries
373 376
        for beneficiaire in beneficiaires:
374
            ref_per = beneficiaire.get('REF_PER')
375
            if not ref_per:
377
            id_per = beneficiaire.get('ID_PER')
378
            if not id_per:
379
                self.logger.warning('no ID_PER')
380
                continue
381
            try:
382
                dob = beneficiaire['DATE_NAISSANCE']
383
            except KeyError:
384
                self.logger.warning('id_per %s: no DATE_NAISSANCE', id_per)
385
                continue
386
            try:
387
                dob = datetime.datetime.strptime(dob, '%d/%m/%Y').date()
388
            except (ValueError, TypeError):
389
                self.logger.warning('id_per %s: invalid DATE_NAISSANCE', id_per)
376 390
                continue
377
            dossier = self.call_select_usager_by_ref(ref_per)
378
            identification = dossier['IDENTIFICATION'][0]
379
            beneficiaire['ID_PER'] = identification['ID_PER']
380
            beneficiaire['TEL_FIXE'] = identification.get('TEL_FIXE', '')
381
            beneficiaire['TEL_MOBILE'] = identification.get('TEL_MOBILE', '')
382
            beneficiaire['MAIL'] = identification.get('MAIL', '')
383
        return {'data': beneficiaires}
391
            if dob != date_of_birth:
392
                self.logger.debug('ignoring id_per %s different dob %s != %s', id_per, dob, date_of_birth)
393
                continue
394
            dossier = self.call_select_usager(id_per)
395
            try:
396
                identification = dossier['IDENTIFICATION'][0]
397
            except KeyError:
398
                self.logger.warning('id_per %s: dossier is empty', id_per)
399
                continue
400
            assert identification['ID_PER'] == id_per
401
            dossiers.append(dossier)
402

  
403
        # there must be only one
404
        if len(dossiers) == 0:
405
            raise APIError('not-found')
406
        if len(dossiers) > 1:
407
            raise APIError('too-many')
408

  
409
        # get contact informations
410
        identification = dossiers[0]['IDENTIFICATION'][0]
411
        id_per = identification['ID_PER']
412
        nom = identification.get('NOM', '')
413
        prenom = identification.get('PRENOM', '')
414
        nom_naissance = identification.get('NOM_NAISSANCE', '')
415
        tel1 = ''.join(c for c in identification.get('TEL_MOBILE', '') if c.isdigit())
416
        tel2 = ''.join(c for c in identification.get('TEL_FIXE', '') if c.isdigit())
417
        email = identification.get('MAIL', '').strip()
418
        if tel1:
419
            data.append({
420
                'id': 'tel1',
421
                'text': tel1[:2] + '*****' + tel1[-3:],
422
                'phone': tel1,
423

  
424
                'id_per': id_per,
425
                'nom': nom,
426
                'prenom': prenom,
427
                'nom_naissance': nom_naissance,
428
            })
429
        if tel2:
430
            data.append({
431
                'id': 'tel2',
432
                'text': tel2[:2] + '*****' + tel2[-3:],
433
                'phone': tel2,
434

  
435
                'id_per': id_per,
436
                'nom': nom,
437
                'prenom': prenom,
438
                'nom_naissance': nom_naissance,
439
            })
440
        if email:
441
            data.append({
442
                'id': 'email1',
443
                'text': email[:2] + '***@***' + email[-3:],
444
                'email': email,
445

  
446
                'id_per': id_per,
447
                'nom': nom,
448
                'prenom': prenom,
449
                'nom_naissance': nom_naissance,
450
            })
451
        if len(data) == 0:
452
            self.logger.debug('id_per %s: no contact information, ignored', id_per)
453
            raise APIError('no-contacts')
454

  
455
        return {'data': data}
384 456

  
385 457
    @endpoint(name='link-by-id-per',
386 458
              methods=['post'],
387 459
              description=_('Create link with an extranet account'),
388 460
              perm='can_access',
389 461
              parameters={
390
                  'NameID':{
462
                  'NameID': {
391 463
                      'description': _('Publik NameID'),
392 464
                      'example_value': 'xyz24d934',
393 465
                  },
tests/test_atos_genesys.py
243 243
    <REF_PER>951858</REF_PER>
244 244
    <LIEU_NAIS>ANTIBES (006)</LIEU_NAIS>
245 245
    <PAY_NAIS>FRANCE</PAY_NAIS>
246
    <ID_PER>1234</ID_PER>
246 247
  </ROW>
247 248
</ROWSET>
248 249
</return>'''
249 250

  
250 251
    with utils.mock_url(FAKE_URL + 'WSUsagerPublik/services/PublikService/chercheBeneficiaire', RESPONSE_SEARCH):
251
        with utils.mock_url(FAKE_URL + 'WSUsagerPublik/services/PublikService/selectUsagerByRef',
252
        with utils.mock_url(FAKE_URL + 'WSUsagerPublik/services/PublikService/selectUsager',
252 253
                            RESPONSE_SELECT_USAGER):
253 254
            response = app.get(url + '?' + urlencode({
254 255
                'first_name': 'John',
......
256 257
                'date_of_birth': '1925-01-01',
257 258
            }))
258 259
    assert response.json['err'] == 0
259
    assert len(response.json['data']) == 1
260
    data = response.json['data'][0]
261
    assert data['REF_PER'] == '951858'
262
    assert data['ID_PER'] == '1234'
263
    assert data['NOMPER'] == 'John'
264
    assert data['PRENOMPER'] == 'Doe'
265
    assert data['DATE_NAISSANCE'] == '01/01/1925'
266
    assert data['TEL_FIXE'] == '06.44.44.44.44'
267
    assert data['TEL_MOBILE'] == '06.55.55.55.55'
268
    assert data['MAIL'] == 'test@sirus.fr'
260
    assert len(response.json['data']) == 3
261
    data = response.json['data']
262
    assert data == [
263
        {
264
            u'id': u'tel1',
265
            u'id_per': u'1234',
266
            u'nom': u'DOE',
267
            u'nom_naissance': u'TEST',
268
            u'phone': u'0655555555',
269
            u'prenom': u'John',
270
            u'text': u'06*****555'},
271
        {
272
            u'id': u'tel2',
273
            u'id_per': u'1234',
274
            u'nom': u'DOE',
275
            u'nom_naissance': u'TEST',
276
            u'phone': u'0644444444',
277
            u'prenom': u'John',
278
            u'text': u'06*****444'
279
        },
280
        {
281
            u'email': u'test@sirus.fr',
282
            u'id': u'email1',
283
            u'id_per': u'1234',
284
            u'nom': u'DOE',
285
            u'nom_naissance': u'TEST',
286
            u'prenom': u'John',
287
            u'text': u'te***@***.fr'
288
        }
289
    ]
290

  
291
    with utils.mock_url(FAKE_URL + 'WSUsagerPublik/services/PublikService/chercheBeneficiaire', RESPONSE_SEARCH):
292
        with utils.mock_url(FAKE_URL + 'WSUsagerPublik/services/PublikService/selectUsager',
293
                            RESPONSE_SELECT_USAGER):
294
            response = app.get(url + '?' + urlencode({
295
                'first_name': 'John',
296
                'last_name': 'Doe',
297
                'date_of_birth': '1925-01-02',
298
            }))
299
            assert response.json['err'] == 1
300
            assert response.json['err_desc'] == 'not-found'
301

  
302
    RESPONSE_SEARCH_TOO_MANY = '''<?xml version="1.0" encoding="UTF-8"?><return><ROWSET>
303
  <ROW num="1">
304
    <NOMPER>John</NOMPER>
305
    <PRENOMPER>Doe</PRENOMPER>
306
    <DATE_NAISSANCE>01/01/1925</DATE_NAISSANCE>
307
    <REF_PER>951858</REF_PER>
308
    <LIEU_NAIS>ANTIBES (006)</LIEU_NAIS>
309
    <PAY_NAIS>FRANCE</PAY_NAIS>
310
    <ID_PER>1234</ID_PER>
311
  </ROW>
312
  <ROW num="2">
313
    <NOMPER>Johnny</NOMPER>
314
    <PRENOMPER>Doe</PRENOMPER>
315
    <DATE_NAISSANCE>01/01/1925</DATE_NAISSANCE>
316
    <REF_PER>951858</REF_PER>
317
    <LIEU_NAIS>ANTIBES (006)</LIEU_NAIS>
318
    <PAY_NAIS>FRANCE</PAY_NAIS>
319
    <ID_PER>1234</ID_PER>
320
  </ROW>
321
</ROWSET>
322
</return>'''
323

  
324
    RESPONSE_SELECT_USAGER_NO_CONTACTS = '''<?xml version="1.0"?>
325
    <return><ROWSET>
326
      <ROW num="1">
327
        <IDENTIFICATION>
328
          <IDENTIFICATION_ROW num="1">
329
            <ID_PER>1234</ID_PER>
330
          </IDENTIFICATION_ROW>
331
        </IDENTIFICATION>
332
      </ROW>
333
    </ROWSET></return>'''
334

  
335
    with utils.mock_url(FAKE_URL + 'WSUsagerPublik/services/PublikService/chercheBeneficiaire',
336
                        RESPONSE_SEARCH):
337
        with utils.mock_url(FAKE_URL + 'WSUsagerPublik/services/PublikService/selectUsager',
338
                            RESPONSE_SELECT_USAGER_NO_CONTACTS):
339
            response = app.get(url + '?' + urlencode({
340
                'first_name': 'John',
341
                'last_name': 'Doe',
342
                'date_of_birth': '1925-01-01',
343
            }))
344
            assert response.json['err'] == 1
345
            assert response.json['err_desc'] == 'no-contacts'
269 346

  
270 347

  
271 348
def test_ws_link_by_id_per(app, genesys):
272
-