toulouse-foederis: normalize phone number (#77528)
gitea/passerelle/pipeline/head This commit looks good Details

This commit is contained in:
Corentin Sechet 2023-09-11 18:34:41 +02:00 committed by Corentin Sechet
parent fd1c591ab3
commit 62e452c31e
3 changed files with 38 additions and 1 deletions

View File

@ -16,6 +16,7 @@
import base64
import phonenumbers
import requests
from django.core.files.base import ContentFile
from django.db import models, transaction
@ -28,6 +29,7 @@ from django.utils.translation import gettext_lazy as _
from passerelle.base.models import BaseResource, HTTPResource
from passerelle.utils.api import endpoint
from passerelle.utils.json import datasource_array_schema, datasource_schema, response_schema
from passerelle.utils.jsonresponse import APIError
ATTACHMENT_SCHEMA = {
'$schema': 'http://json-schema.org/draft-04/schema#',
@ -587,6 +589,15 @@ class Resource(BaseResource, HTTPResource):
return None
return int(id)
phone = post_data.get('phone', None)
if phone is not None:
try:
parsed_phone = phonenumbers.parse(phone, 'FR')
except phonenumbers.NumberParseException:
raise APIError(_('Couldn\'t recognize provided phone number.'))
formatted_phone = f'+{parsed_phone.country_code} {parsed_phone.national_number}'
request_data = {
'type_de_candidature': post_data.get('type', 'E'),
'annonce': _get_id('announce_id'),
@ -612,7 +623,7 @@ class Resource(BaseResource, HTTPResource):
'adresse_ligne_2': post_data.get('address_complement', None),
'code_postal': post_data.get('zip', None),
'ville': post_data.get('city', None),
'telephone': post_data.get('phone', None),
'telephone': formatted_phone,
'email': post_data.get('email', None),
'date_debut_contrat': post_data.get('contract_start_date', None),
'date_fin_contrat': post_data.get('contract_end_date', None),

View File

@ -174,6 +174,7 @@ setup(
'roman',
'cryptography',
'xmltodict',
'phonenumbers',
],
cmdclass={
'build': build,

View File

@ -516,6 +516,31 @@ class TestEndpoints:
assert response.json['data']['application_id'] == 42
@pytest.mark.parametrize(
'phone_number,prefix',
[('+33 6 36 65 65 65', '33'), ('06 36 65 65 65', '33'), ('+596 6 36 65 65 65', '596')],
)
def test_create_application_phone_format(self, resource, app, phone_number, prefix):
@httmock.urlmatch(path=r'^.*/data/candidature$')
def handler(url, request):
payload = json.loads(request.body)
assert payload['telephone'] == f'+{prefix} 636656565'
return httmock.response(200, json.dumps({'code': 200, 'results': [{'id': 42}]}))
with httmock.HTTMock(handler):
response = app.post_json(
'/toulouse-foederis/foederis/create-application',
params={'phone': phone_number},
)
assert response.json['data']['application_id'] == 42
response = app.post_json(
'/toulouse-foederis/foederis/create-application',
params={'phone': 'mille sabords'},
)
assert response.json['err'] == 1
assert 'Couldn\'t recognize provided phone number' in response.json['err_desc']
def test_attach_file(self, resource, app):
@httmock.urlmatch(path=r'^.*/data/candidature/424242/fields/cv$')
def handler(url, request):