greco: add tests (#51569)

This commit is contained in:
Nicolas Roche 2021-03-02 12:56:43 +01:00 committed by Frédéric Péters
parent 34d441dd8e
commit c137e7be68
10 changed files with 376 additions and 3 deletions

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<getMailResponse xmlns="http://mairies.services.external.coheris.com">
<getMailReturn>
<iddemande>MDFGDZRF</iddemande>
<idgreco>538593</idgreco>
<motifsrejet>Aucune réponse trouvée</motifsrejet>
<etat xsi:nil="true"/>
<message>
<bcc xsi:nil="true"/>
<cc xsi:nil="true"/>
<content xsi:nil="true"/>
<date xsi:nil="true"/>
<subject xsi:nil="true"/>
<to xsi:nil="true"/>
</message>
</getMailReturn>
</getMailResponse>
</soapenv:Body>
</soapenv:Envelope>

View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<getMailResponse xmlns="http://mairies.services.external.coheris.com">
<getMailReturn>
<iddemande>DWPQGCTS</iddemande>
<idgreco>538554</idgreco>
<motifsrejet/>
<etat xsi:nil="true"/>
<message>
<bcc>
<item/>
</bcc>
<cc>
<item/>
</cc>
<content>&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Arial;"&gt; Référence de votre demande, à rappeler lors de tout échange : 538554&lt;br /&gt;Code de suivi toodego (ou numéro logiciel externe) : DWPQGCTS&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Bonjour Monsieur BAUD,&lt;br /&gt;&lt;br /&gt;La Métropole de Lyon a enregistré votre demande le 09/02/2021.&lt;br /&gt;&lt;br /&gt;Adresse concernée : 79 RUE MOLIERE LYON 3&lt;br /&gt;&lt;br /&gt;Votre demande concerne le domaine de compétences suivant : Voirie.&lt;br /&gt;&lt;br /&gt;Nous vous informons que notre service Voirie s\'est rendu sur place, mais n\'a constaté aucun dysfonctionnement.&lt;br /&gt;En cas de nouvelle anomalie, nous vous invitons à reprendre contact avec nous.&lt;br /&gt;&lt;br /&gt;Nous vous remercions d\'avoir contacté la Métropole de Lyon et restons à votre disposition pour toute question complémentaire.&lt;br /&gt;&lt;br /&gt;Bien cordialement,&lt;br /&gt;&lt;br /&gt;Votre conseiller Métropole de Lyon&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;----&lt;br /&gt;&lt;br /&gt;&lt;u&gt;Nous contacter&lt;/u&gt;&lt;br /&gt;Centre d\'information et de contact&lt;br /&gt;- par téléphone : 04 78 63 40 00, du lundi au vendredi, de 9h00 à 12h00 et de 13h30 à 16h00&lt;br /&gt;- via notre site internet : &lt;a href="https://www.grandlyon.com/services/contacter-la-metropole-de-lyon.html"&gt;https://www.grandlyon.com/services/contacter-la-metropole-de-lyon.html&lt;/a&gt;&lt;br /&gt;- par mail : &lt;a href="mailto:centredecontacts@grandlyon.com"&gt;centredecontacts@grandlyon.com&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Vous avez utilisé l\'application toodego pour faire votre demande : suivez son traitement dans &lt;a href="https://www.toodego.com/connect/"&gt;www.toodego.com&lt;/a&gt;, avec votre code de suivi : DWPQGCTS&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;em&gt;Merci de ne pas utiliser la fonction REPONDRE de votre messagerie.&lt;/em&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="font-size: xx-small;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;em&gt;Les informations à caractère personnel recueillies dans le cadre de votre demande sont enregistrées dans un fichier informatisé par la Métropole de Lyon pour la gestion de la relation usager. Elles sont conservées pendant 2 ans et sont destinées aux services de la Métropole de Lyon, ainsi qu\'à ses partenaires intervenant dans la relation usager. Conformément à la loi Informatique et libertés, vous pouvez exercer vos droits d\'accès aux données vous concernant et les faire rectifier en vous adressant par courrier postal à : Métropole de Lyon - Direction des Affaires Juridiques et de la Commande Publique - 20, rue du Lac - CS 33569 - 69505 Lyon Cedex 03. &lt;/em&gt; &lt;/span&gt;&lt;/span&gt;</content>
<date>10/02/2021 10:27:47</date>
<subject>Métropole de Lyon : réponse à votre demande n° 538554</subject>
<to>
<item>gbaud@grandlyon.com</item>
</to>
</message>
</getMailReturn>
</getMailResponse>
</soapenv:Body>
</soapenv:Envelope>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<creerResponse xmlns="http://mairies.services.external.coheris.com">
<creerReturn>
<iddemande>MDFGDZRF</iddemande>
<idgreco xsi:nil="true"/>
<motifsrejet>Beneficiaire: Adresse mail incorrecte\r\n</motifsrejet>
<etat xsi:nil="true"/>
<application>69999</application>
</creerReturn>
</creerResponse>
</soapenv:Body>
</soapenv:Envelope>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<creerResponse xmlns="http://mairies.services.external.coheris.com">
<creerReturn>
<iddemande>MDFGDZRF</iddemande>
<idgreco>538593</idgreco>
<motifsrejet/>
<etat>1 - Créée</etat>
<application>69999</application>
</creerReturn>
</creerResponse>
</soapenv:Body>
</soapenv:Envelope>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<communicationTestResponse xmlns="http://mairies.services.external.coheris.com">
<communicationTestReturn>com.coheris.external.services.mairies.action.TestCommunicationAction.execute(...)\n[ping]</communicationTestReturn>
</communicationTestResponse>
</soapenv:Body>
</soapenv:Envelope>

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<consulterResponse xmlns="http://mairies.services.external.coheris.com">
<consulterReturn>
<iddemande>AAAAAARF</iddemande>
<idgreco>538999</idgreco>
<motifsrejet>La demande n\'existe pas\r\n</motifsrejet>
<etat xsi:nil="true"/>
<commentaireReponse xsi:nil="true"/>
<comptesRendus xsi:nil="true"/>
<dateResolutionEffective xsi:nil="true"/>
<dateResolutionPrevue xsi:nil="true"/>
<groupeResponsable xsi:nil="true"/>
<suiteDonnee xsi:nil="true"/>
<idOrganismeExterne xsi:nil="true"/>
<nomOrganisme xsi:nil="true"/>
</consulterReturn>
</consulterResponse>
</soapenv:Body>
</soapenv:Envelope>

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<consulterResponse xmlns="http://mairies.services.external.coheris.com">
<consulterReturn>
<iddemande>MDFGDZRF</iddemande>
<idgreco>538593</idgreco>
<motifsrejet/>
<etat>1 - Créée</etat>
<commentaireReponse xsi:nil="true"/>
<comptesRendus xsi:nil="true"/>
<dateResolutionEffective xsi:nil="true"/>
<dateResolutionPrevue>2021-03-16T14:14:23.000Z</dateResolutionPrevue>
<groupeResponsable>DRTU/SPS/CIC-Centre info et contact</groupeResponsable>
<suiteDonnee xsi:nil="true"/>
<idOrganismeExterne xsi:nil="true"/>
<nomOrganisme xsi:nil="true"/>
</consulterReturn>
</consulterResponse>
</soapenv:Body>
</soapenv:Envelope>

View File

@ -0,0 +1,6 @@
{
"access_token" : "8f4de6a5-16fb-3e91-862e-bbac5195aef7",
"expires_in" : 54,
"scope" : "am_application_scope default",
"token_type" : "Bearer"
}

View File

@ -1,11 +1,27 @@
# Copyright (C) 2016 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from base64 import b64encode
import os
import pytest
import mock
from requests import Response
import utils
from django.urls import reverse
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse
from django.utils.encoding import force_str
from passerelle.base.models import ApiUser, AccessRight
from passerelle.contrib.greco.models import Greco
@ -13,7 +29,73 @@ from passerelle.contrib.greco.models import Greco
pytestmark = pytest.mark.django_db
WSDL_FILENAME = os.path.join(os.path.dirname(__file__), 'data', 'greco.wsdl')
TEST_BASE_DIR = os.path.join(os.path.dirname(__file__), 'data', 'greco')
WSDL_FILENAME = os.path.join(TEST_BASE_DIR, 'greco.wsdl')
def get_xml_file(filename):
with open(os.path.join(TEST_BASE_DIR, "%s.xml" % filename), 'rb') as desc:
return desc.read()
def get_json_file(filename):
with open(os.path.join(TEST_BASE_DIR, "%s.json" % filename)) as desc:
return desc.read()
def fake_xml_response(basename):
return utils.FakedResponse(
content=get_xml_file(basename),
status_code=200,
headers={'Content-Type': 'text/xml'},
)
TOKEN = utils.FakedResponse(
content=get_json_file('token'),
status_code=200,
headers={'Content-Type': 'application/json'},
)
PING = fake_xml_response('ping')
CREATE_OK = fake_xml_response('create_ok')
CREATE_KO = fake_xml_response('create_ko')
STATUS_OK = fake_xml_response('status_ok')
STATUS_KO = fake_xml_response('status_ko')
ANSWER_OK = fake_xml_response('answer_ok')
ANSWER_KO = fake_xml_response('answer_ko')
CREATE_PAYLOAD = {
'datecreation': '2021-03-02T14:44:38',
'datedepot': '2021-03-02T14:44:38',
'domaineobjettype': '1426',
'iddemande': 'MDFGDZRF',
'mediareponse': 'Mail',
'priorite': 'Normal',
'transmetteur_service': 'GNM',
'description': 'Nom de lusager\xa0:\n admin admin\n '
+ 'Carte\xa0:\n 45.75399700317313;4.846451997582336\nNuméro\xa0:\n '
+ '48\n Nom de la voie\xa0:\n Cours Gambetta\n Code postal\xa0:\n '
+ '69007\nCommune\xa0:\n Lyon\nVotre demande concerne :\xa0:\n '
+ 'Un nouvel espace cyclable, bande, piste...\n'
+ 'Votre demande\xa0:\n Une nouvelle piste cyclable, svp.\n',
'beneficiaire_typetiers': 'Particulier',
'beneficiaire_email': 'jdoe@example.fr',
'beneficiaire_civilite': 'Monsieur',
'beneficiaire_prenom': 'Jhon',
'beneficiaire_nom': 'Doe',
'beneficiaire_telephone': '',
'beneficiaire_mobile': '0612345678',
'beneficiaire_numerovoie': None,
'beneficiaire_voie': None,
'beneficiaire_complement': None,
'beneficiaire_codepostal': None,
'beneficiaire_commune': None,
'localisation_numerovoie': '48',
'localisation_voie': 'Cours Gambetta',
'localisation_codefuvvoie': '27679',
'localisation_commune': 'lyon 7',
'localisation_complement': None,
}
@pytest.fixture
@ -53,3 +135,162 @@ def test_greco_ping(mocked_post, app, conn):
resp = app.get(url).json
assert mocked_post.call_count == 2 # token + communcationTest (ping -> 503)
assert resp['err'] == 'transport-error-503'
@mock.patch('passerelle.utils.Request.post', side_effect=(TOKEN, PING))
def test_greco_ping_ok(mocked_post, app, conn):
url = reverse('generic-endpoint', kwargs={'connector': 'greco', 'endpoint': 'ping', 'slug': conn.slug})
url += '?apikey=grecokey'
resp = app.get(url)
assert not resp.json['err']
assert (
resp.json['data']
== 'com.coheris.external.services.mairies.action.TestCommunicationAction.execute(...)\\n[ping]'
)
@mock.patch('passerelle.utils.Request.post', side_effect=(TOKEN, CREATE_OK))
def test_greco_create_ok(mocked_post, app, conn):
url = reverse('generic-endpoint', kwargs={'connector': 'greco', 'endpoint': 'create', 'slug': conn.slug})
url += '?apikey=grecokey'
resp = app.post_json(url, params=CREATE_PAYLOAD)
assert not resp.json['err']
assert resp.json['data'] == {
'iddemande': 'MDFGDZRF',
'idgreco': '538593',
'motifsrejet': None,
'etat': '1 - Créée',
'application': '69999',
}
@mock.patch('passerelle.utils.Request.post', side_effect=(TOKEN, CREATE_KO))
def test_greco_create_ko(mocked_post, app, conn):
url = reverse('generic-endpoint', kwargs={'connector': 'greco', 'endpoint': 'create', 'slug': conn.slug})
url += '?apikey=grecokey'
payload = CREATE_PAYLOAD
payload['beneficiaire_email'] = 'jdoe@localhost'
resp = app.post_json(url, params=payload)
assert not resp.json['err']
assert resp.json['data'] == {
'iddemande': 'MDFGDZRF',
'idgreco': None,
'motifsrejet': 'Beneficiaire: Adresse mail incorrecte\\r\\n',
'etat': None,
'application': '69999',
}
@mock.patch('passerelle.utils.Request.post', side_effect=(TOKEN, CREATE_OK))
def test_greco_create_enclosed(mocked_post, app, conn):
url = reverse('generic-endpoint', kwargs={'connector': 'greco', 'endpoint': 'create', 'slug': conn.slug})
url += '?apikey=grecokey'
payload = CREATE_PAYLOAD
payload['iddemande'] = ('MDFGDARF',)
payload['piece_jointe1'] = {
'content': force_str(b64encode(b'my picture content')),
'filename': 'foo.png',
'content_type': 'image/png',
}
resp = app.post_json(url, params=payload)
# RFC 2045 MIME multipart boundaries
assert force_str(mocked_post.call_args[1]['data']).count('==\r\n') == 2
assert force_str(mocked_post.call_args[1]['data']).count('\r\n--==') == 2
# substitute binary parts
assert 'Content-ID: <foo.png>' in force_str(mocked_post.call_args[1]['data'])
assert not resp.json['err']
@mock.patch('passerelle.utils.Request.post', side_effect=(TOKEN, STATUS_OK))
def test_greco_status_ok(mocked_post, app, conn):
url = reverse('generic-endpoint', kwargs={'connector': 'greco', 'endpoint': 'status', 'slug': conn.slug})
url += '?apikey=grecokey'
resp = app.get(url, params={'idgreco': '538593', 'iddemande': 'MDFGDZRF'})
assert not resp.json['err']
assert resp.json['data'] == {
'iddemande': 'MDFGDZRF',
'idgreco': '538593',
'motifsrejet': None,
'etat': '1 - Créée',
'commentaireReponse': None,
'comptesRendus': None,
'dateResolutionEffective': None,
'dateResolutionPrevue': '2021-03-16T14:14:23Z',
'groupeResponsable': 'DRTU/SPS/CIC-Centre info et contact',
'suiteDonnee': None,
'idOrganismeExterne': None,
'nomOrganisme': None,
}
@mock.patch('passerelle.utils.Request.post', side_effect=(TOKEN, STATUS_KO))
def test_greco_status_ko(mocked_post, app, conn):
url = reverse('generic-endpoint', kwargs={'connector': 'greco', 'endpoint': 'status', 'slug': conn.slug})
url += '?apikey=grecokey'
resp = app.get(url, params={'idgreco': '538999', 'iddemande': 'AAAAAARF'})
assert not resp.json['err']
assert resp.json['data'] == {
'iddemande': 'AAAAAARF',
'idgreco': '538999',
'motifsrejet': "La demande n\\'existe pas\\r\\n",
'etat': None,
'commentaireReponse': None,
'comptesRendus': None,
'dateResolutionEffective': None,
'dateResolutionPrevue': None,
'groupeResponsable': None,
'suiteDonnee': None,
'idOrganismeExterne': None,
'nomOrganisme': None,
}
@mock.patch('passerelle.utils.Request.post', side_effect=(TOKEN, ANSWER_OK))
def test_greco_answer_ok(mocked_post, app, conn):
url = reverse('generic-endpoint', kwargs={'connector': 'greco', 'endpoint': 'answer', 'slug': conn.slug})
url += '?apikey=grecokey'
resp = app.get(url, params={'idgreco': '538554', 'iddemande': 'DWPQGCTS'})
assert not resp.json['err']
data = resp.json['data']
assert len(data['message']['content']) == 2433
data['message']['content'] = '...'
assert data == {
'iddemande': 'DWPQGCTS',
'idgreco': '538554',
'motifsrejet': None,
'etat': None,
'message': {
'bcc': {'item': []},
'cc': {'item': []},
'content': '...',
'date': '10/02/2021 10:27:47',
'subject': 'Métropole de Lyon : réponse à votre demande n° 538554',
'to': {'item': ['gbaud@grandlyon.com']},
},
}
@mock.patch('passerelle.utils.Request.post', side_effect=(TOKEN, ANSWER_KO))
def test_greco_answer_ko(mocked_post, app, conn):
url = reverse('generic-endpoint', kwargs={'connector': 'greco', 'endpoint': 'answer', 'slug': conn.slug})
url += '?apikey=grecokey'
resp = app.get(url, params={'idgreco': '538593', 'iddemande': 'MDFGDZRF'})
assert not resp.json['err']
assert resp.json['data'] == {
'iddemande': 'MDFGDZRF',
'idgreco': '538593',
'motifsrejet': 'Aucune réponse trouvée',
'etat': None,
'message': {'bcc': None, 'cc': None, 'content': None, 'date': None, 'subject': None, 'to': None},
}