ants: normalize and check identifiant_predemande on push (#83552)

This commit is contained in:
Benjamin Dauvergne 2023-11-15 17:53:05 +01:00
parent 57edcdf88f
commit 98c1546c48
5 changed files with 49 additions and 13 deletions

View File

@ -76,7 +76,7 @@
"saint-didier",
"Plateforme X"
],
"identifiant_predemande": "abcd",
"identifiant_predemande": "abCD123456",
"date": "2023-04-03T10:15:00Z",
"gestion_url": "",
"annulation_url": "",

View File

@ -1,6 +1,7 @@
# ANTS-Hub - Copyright (C) Entr'ouvert
import logging
import re
import requests
from django.db.models import F, Q
@ -26,7 +27,17 @@ def get_api_optimisation_url():
)
IDENTIFIANT_PREDEMANDE_RE = re.compile(r'^[A-Z0-9]{10}$')
def push_rdv(rdv):
identifiant_predemande = rdv.identifiant_predemande.upper()
if not IDENTIFIANT_PREDEMANDE_RE.match(identifiant_predemande):
logger.warning(
'rdv(%s) of lieu %s not pushed, wrong format of identifiant_predemande.', rdv, rdv.lieu
)
return True
# swagger : https://api-coordination.rendezvouspasseport.ants.gouv.fr/docs#/Appointments%20-%20%C3%A9diteurs/add_appointment_api_appointments_post
auth_token = get_api_optimisation_auth_token()
if auth_token is None:
@ -35,7 +46,7 @@ def push_rdv(rdv):
try:
if rdv.canceled is None:
params = [
('application_id', rdv.identifiant_predemande),
('application_id', identifiant_predemande),
('management_url', rdv.gestion_url),
('meeting_point', rdv.lieu.nom),
('meeting_point_id', str(rdv.lieu.id)),
@ -51,7 +62,7 @@ def push_rdv(rdv):
return response.json()['success'] == 'true'
else:
params = [
('application_id', rdv.identifiant_predemande),
('application_id', identifiant_predemande),
('meeting_point', rdv.lieu.nom),
('meeting_point_id', str(rdv.lieu.id)),
('appointment_date', localtime(rdv.date).strftime('%Y-%m-%d %H:%M:%S')),
@ -64,6 +75,12 @@ def push_rdv(rdv):
)
response.raise_for_status()
return response.json()['rowcount'] == 1
except requests.HTTPError as e:
if e.response.status_code == 422:
raise ANTSError(str(e), e.response.json())
raise ANTSError(
str(e),
)
except (requests.RequestException, ValueError, KeyError, TypeError) as e:
raise ANTSError(str(e))

View File

@ -8,7 +8,6 @@ import functools
import json
import logging
import os
import re
import time
import jsonschema
@ -21,7 +20,7 @@ from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View
from ants_hub.api.ants import ANTSError, get_status_of_predemandes
from ants_hub.api.ants import IDENTIFIANT_PREDEMANDE_RE, ANTSError, get_status_of_predemandes
from ants_hub.data.models import (
Collectivite,
Horaire,
@ -418,8 +417,6 @@ class RendezVousDisponibleView(View):
rendez_vous_disponibles = csrf_exempt(authenticate(RendezVousDisponibleView.as_view()))
IDENTIFIANT_PREDEMANDE_RE = re.compile(r'^[A-Z0-9]{10}$')
class PredemandesView(View):
def get_autres_demandes(self, identifiant_predemandes):

View File

@ -236,7 +236,7 @@ class TestEndpoints:
response = django_app.get(
'/api/ants/searchApplicationIds',
params={
'application_ids': ['abcd', '123456'],
'application_ids': ['abCD123456', '123456'],
},
)
assert response.json == {
@ -250,7 +250,7 @@ class TestEndpoints:
'meeting_point': 'Mairie',
}
],
'abcd': [
'abCD123456': [
{
'cancel_url': 'http://testserver/rdv/saint-didier-1/mairie-1/2023-04-03T12:15:00+02:00/'
'annulation/7cace277-9157-4fbc-9705-45522984805d/',
@ -275,7 +275,7 @@ class TestEndpoints:
'management_url': 'https://monrdv.fr/gestion/',
}
],
'abcd': [
'abCD123456': [
{
'cancel_url': 'https://saint-didier.fr/mairie/annulation/',
'management_url': 'https://saint-didier/gestion/',
@ -399,7 +399,7 @@ class TestAPIV2Push:
'fragment': '',
}
assert called_qs == [
('application_id', 'abcd'),
('application_id', 'ABCD123456'),
('management_url', ''),
('meeting_point', 'Mairie'),
('meeting_point_id', '1'),
@ -434,7 +434,7 @@ class TestAPIV2Push:
'fragment': '',
}
assert called_qs == [
('application_id', 'abcd'),
('application_id', 'ABCD123456'),
('meeting_point', 'Mairie'),
('meeting_point_id', '1'),
('appointment_date', '2023-04-03 12:15:00'),
@ -456,10 +456,21 @@ class TestAPIV2Push:
'json': '',
'status': 200,
},
{
'json': {'detail': []},
'status': 422,
},
{
'status': 500,
},
],
ids=[
'JSON is empty dict',
'JSON is null',
'JSON is empty string',
'HTTP 422 with details',
'HTTP 500',
],
)
def test_post_error(self, response_kwargs, db, freezer):
post_response = responses.add(
@ -493,10 +504,21 @@ class TestAPIV2Push:
'json': '',
'status': 200,
},
{
'json': {'detail': []},
'status': 422,
},
{
'status': 500,
},
],
ids=[
'JSON is empty dict',
'JSON is null',
'JSON is empty string',
'HTTP 422 with details',
'HTTP 500',
],
)
def test_delete_error(self, response_kwargs, db, freezer):
post_response = responses.add(

View File

@ -116,7 +116,7 @@ class TestLogged:
'',
'3 avril 2023 12:15',
'Mairie / 2 rue du four / Saint-Didier',
'abcd',
'abCD123456',
'3 avril 2023 12:16',
'3 avril 2023 12:16',
],