astregs: store linked association label (#36706)

This commit is contained in:
Serghei Mihai 2019-10-06 14:54:02 +02:00
parent 7b82aae51c
commit e153f5c6c8
3 changed files with 60 additions and 9 deletions

View File

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.20 on 2019-10-06 13:01
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('astregs', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='link',
name='association_label',
field=models.CharField(max_length=128, null=True),
)
]

View File

@ -463,14 +463,8 @@ class AstreGS(BaseResource):
return {'data': []}
data = []
for link in Link.objects.filter(resource=self, name_id=NameID):
r = self.call('RechercheTiersDetails', 'liste',
Criteres={'numeroTiers': link.association_id}
)
if r.liste:
# # ugly API, it always returns a list of only one element
item = r.liste.EnregRechercheTiersDetailsReturn[0]
data.append({'id': link.association_id,
'text': '%s - %s' % (item.Numero_SIRET, item.Nom_enregistrement)})
data.append({'id': link.association_id,
'text': link.get_label()})
return {'data': data}
@ -648,7 +642,23 @@ class Link(models.Model):
resource = models.ForeignKey(AstreGS)
name_id = models.CharField(max_length=32)
association_id = models.CharField(max_length=32)
association_label = models.CharField(max_length=128, null=True)
created = models.DateTimeField(auto_now_add=True)
def get_label(self):
r = self.resource.call('RechercheTiersDetails', 'liste',
Criteres={'numeroTiers': self.association_id}
)
if r.liste:
# ugly API, it always returns a list of only one element
item = r.liste.EnregRechercheTiersDetailsReturn[0]
self.association_label = '%s - %s' % (item.Numero_SIRET, item.Nom_enregistrement)
self.save()
else:
self.resource.logger.error('No data found for association %s', self.association_id)
return self.association_label
class Meta:
unique_together = ('resource', 'name_id', 'association_id')

View File

@ -1,8 +1,10 @@
# -*- coding: utf-8 -*-
import os
import logging
import mock
import pytest
import re
from requests.exceptions import ConnectionError
from requests import Request
@ -69,6 +71,12 @@ def recherche_tiers_details_wsdl():
def recherche_tiers_details_result():
return get_xml_file('RechercheTiersDetails.xml')
@pytest.fixture
def recherche_tiers_details_empty_result():
content = get_xml_file('RechercheTiersDetails.xml')
return re.sub('<ns1:liste>.*</ns1:liste>', '<ns1:liste></ns1:liste>',
content)
@pytest.fixture
def tiers_creation_response():
return get_xml_file('TiersCreationResponse.xml')
@ -190,16 +198,29 @@ def test_unlink_user_from_association(mocked_post, mocked_get, connector, app):
@mock.patch('passerelle.utils.Request.get')
@mock.patch('passerelle.utils.Request.post')
def test_list_user_associations(mocked_post, mocked_get, recherche_tiers_details_wsdl,
recherche_tiers_details_result, connector, app):
recherche_tiers_details_result, recherche_tiers_details_empty_result,
connector, app, caplog):
mocked_get.return_value = mock.Mock(content=recherche_tiers_details_wsdl)
mocked_post.return_value = mock.Mock(content=recherche_tiers_details_result, status_code=200,
headers={'Content-Type': 'text/xml'})
resp = app.get('/astregs/test/link', params={'association_id': '42', 'NameID': 'user_name_id'})
resp = app.get('/astregs/test/links', params={'NameID': 'user_name_id'})
mocked_get.assert_called()
mocked_post.assert_called()
assert resp.json['data']
assert resp.json['data'][0]['id'] == '42'
assert resp.json['data'][0]['text'] == '50043390900016 - ASSOCIATION OMNISPORTS DES MONTS D AZUR'
mocked_post.return_value = mock.Mock(content=recherche_tiers_details_empty_result, status_code=200,
headers={'Content-Type': 'text/xml'})
resp = app.get('/astregs/test/links', params={'NameID': 'user_name_id'})
assert resp.json['data']
assert resp.json['data'][0]['id'] == '42'
assert resp.json['data'][0]['text'] == '50043390900016 - ASSOCIATION OMNISPORTS DES MONTS D AZUR'
assert len(caplog.records)
assert caplog.records[-1].levelno == logging.ERROR
assert caplog.records[-1].message == 'No data found for association 42'
@mock.patch('passerelle.utils.Request.get', side_effect=search_wsdl_side_effect)
@mock.patch('passerelle.utils.Request.post')