Commit d4945cc6 authored by David Foucher's avatar David Foucher

Change HTTP error 422 to 400 for strict HTTP compatibilty

See info:
https://stackoverflow.com/questions/16133923/400-vs-422-response-to-post-of-data/20215807#20215807
parent a9932315
Pipeline #3431 failed with stages
in 4 minutes and 58 seconds
...@@ -285,7 +285,7 @@ async def test_simulate_error_triggers_log(client): ...@@ -285,7 +285,7 @@ async def test_simulate_error_triggers_log(client):
log_path = Path(os.environ["TREFLE_LOG_DIR"]) / "trefle-simulate.log" log_path = Path(os.environ["TREFLE_LOG_DIR"]) / "trefle-simulate.log"
log_path.write_text("") log_path.write_text("")
resp = await client.post("/financement", body=body) resp = await client.post("/financement", body=body)
assert resp.status == 422 assert resp.status == 400
lines = log_path.read_text().splitlines() lines = log_path.read_text().splitlines()
assert len(lines) == 1 assert len(lines) == 1
log_data = json.loads(lines[0]) log_data = json.loads(lines[0])
...@@ -323,7 +323,7 @@ async def test_simulate_endpoint_with_empty_idcc(client): ...@@ -323,7 +323,7 @@ async def test_simulate_endpoint_with_empty_idcc(client):
"beneficiaire.entreprise.idcc": None, "beneficiaire.entreprise.idcc": None,
}, },
) )
assert resp.status == HTTPStatus.UNPROCESSABLE_ENTITY assert resp.status == HTTPStatus.BAD_REQUEST
assert json.loads(resp.body) == { assert json.loads(resp.body) == {
"beneficiaire.entreprise.idcc": "Ce champ est obligatoire" "beneficiaire.entreprise.idcc": "Ce champ est obligatoire"
} }
...@@ -345,7 +345,7 @@ async def test_simulate_endpoint_with_invalid_idcc_format(client): ...@@ -345,7 +345,7 @@ async def test_simulate_endpoint_with_invalid_idcc_format(client):
"beneficiaire.entreprise.idcc": "foobar", "beneficiaire.entreprise.idcc": "foobar",
}, },
) )
assert resp.status == HTTPStatus.UNPROCESSABLE_ENTITY assert resp.status == HTTPStatus.BAD_REQUEST
assert json.loads(resp.body) == { assert json.loads(resp.body) == {
"beneficiaire.entreprise.idcc": "Valeur d'IDCC inconnue: `foobar`" "beneficiaire.entreprise.idcc": "Valeur d'IDCC inconnue: `foobar`"
} }
...@@ -367,7 +367,7 @@ async def test_simulate_endpoint_with_unknown_idcc(client): ...@@ -367,7 +367,7 @@ async def test_simulate_endpoint_with_unknown_idcc(client):
"beneficiaire.entreprise.idcc": 1234567, "beneficiaire.entreprise.idcc": 1234567,
}, },
) )
assert resp.status == HTTPStatus.UNPROCESSABLE_ENTITY assert resp.status == HTTPStatus.BAD_REQUEST
assert json.loads(resp.body) == { assert json.loads(resp.body) == {
"beneficiaire.entreprise.idcc": "Valeur d'IDCC inconnue: `1234567`" "beneficiaire.entreprise.idcc": "Valeur d'IDCC inconnue: `1234567`"
} }
...@@ -391,7 +391,7 @@ async def test_simulate_endpoint_with_invalid_naf(client, naf): ...@@ -391,7 +391,7 @@ async def test_simulate_endpoint_with_invalid_naf(client, naf):
"beneficiaire.entreprise.idcc": "1486", "beneficiaire.entreprise.idcc": "1486",
}, },
) )
assert resp.status == HTTPStatus.UNPROCESSABLE_ENTITY assert resp.status == HTTPStatus.BAD_REQUEST
assert json.loads(resp.body) == { assert json.loads(resp.body) == {
"beneficiaire.entreprise.naf": ( "beneficiaire.entreprise.naf": (
f"`{naf}` n'est pas au format " "^\\d{2}\\.?\\d{2}[a-zA-Z]$" f"`{naf}` n'est pas au format " "^\\d{2}\\.?\\d{2}[a-zA-Z]$"
...@@ -416,7 +416,7 @@ async def test_simulate_endpoint_with_invalid_insee(client, insee): ...@@ -416,7 +416,7 @@ async def test_simulate_endpoint_with_invalid_insee(client, insee):
"beneficiaire.entreprise.idcc": 2706, "beneficiaire.entreprise.idcc": 2706,
}, },
) )
assert resp.status == HTTPStatus.UNPROCESSABLE_ENTITY assert resp.status == HTTPStatus.BAD_REQUEST
assert json.loads(resp.body) == { assert json.loads(resp.body) == {
"beneficiaire.entreprise.commune": ( "beneficiaire.entreprise.commune": (
f"`{insee}` n'est pas au format " "^(2[AB]|[0-9]{2})[0-9]{3}$" f"`{insee}` n'est pas au format " "^(2[AB]|[0-9]{2})[0-9]{3}$"
...@@ -440,7 +440,7 @@ async def test_simulate_endpoint_with_unknown_departement(client): ...@@ -440,7 +440,7 @@ async def test_simulate_endpoint_with_unknown_departement(client):
"beneficiaire.entreprise.idcc": 2706, "beneficiaire.entreprise.idcc": 2706,
}, },
) )
assert resp.status == HTTPStatus.UNPROCESSABLE_ENTITY assert resp.status == HTTPStatus.BAD_REQUEST
assert json.loads(resp.body) == { assert json.loads(resp.body) == {
"beneficiaire.entreprise.commune": "Valeur invalide: `99001`" "beneficiaire.entreprise.commune": "Valeur invalide: `99001`"
} }
...@@ -458,7 +458,7 @@ async def test_simulate_endpoint_with_invalid_data(client): ...@@ -458,7 +458,7 @@ async def test_simulate_endpoint_with_invalid_data(client):
"beneficiaire.entreprise.idcc": 2706, "beneficiaire.entreprise.idcc": 2706,
}, },
) )
assert resp.status == HTTPStatus.UNPROCESSABLE_ENTITY assert resp.status == HTTPStatus.BAD_REQUEST
assert "application/json" in resp.headers["Content-Type"] assert "application/json" in resp.headers["Content-Type"]
validator = ResponseValidator(spec) validator = ResponseValidator(spec)
...@@ -498,7 +498,7 @@ async def test_simulate_endpoint_with_invalid_formation_id(client, mock_get): ...@@ -498,7 +498,7 @@ async def test_simulate_endpoint_with_invalid_formation_id(client, mock_get):
mock_get(status_code=404) mock_get(status_code=404)
resp = await client.post("/financement", body=body) resp = await client.post("/financement", body=body)
assert resp.status == HTTPStatus.UNPROCESSABLE_ENTITY assert resp.status == HTTPStatus.BAD_REQUEST
assert json.loads(resp.body) == { assert json.loads(resp.body) == {
"error": "Error with id `1234`: `No formation found`" "error": "Error with id `1234`: `No formation found`"
} }
...@@ -616,7 +616,7 @@ async def test_authentification_with_bad_matching_pattern(patch_authorisations, ...@@ -616,7 +616,7 @@ async def test_authentification_with_bad_matching_pattern(patch_authorisations,
patch_authorisations([auth]) patch_authorisations([auth])
resp = await client.post("/authentification", body=body) resp = await client.post("/authentification", body=body)
assert resp.status == HTTPStatus.UNPROCESSABLE_ENTITY assert resp.status == HTTPStatus.BAD_REQUEST
async def test_authentification_with_bad_email(patch_authorisations, client, mock_get): async def test_authentification_with_bad_email(patch_authorisations, client, mock_get):
......
...@@ -69,7 +69,7 @@ async def simulate_(request, response): ...@@ -69,7 +69,7 @@ async def simulate_(request, response):
except DataError as err: except DataError as err:
error = {err.key: err.error} error = {err.key: err.error}
log_simulate(context, errors=error) log_simulate(context, errors=error)
raise HttpError(HTTPStatus.UNPROCESSABLE_ENTITY, error) raise HttpError(HTTPStatus.BAD_REQUEST, error)
eligible = request.query.bool("eligible", None) eligible = request.query.bool("eligible", None)
if eligible is not None: if eligible is not None:
...@@ -106,7 +106,7 @@ async def remuneration_(request, response): ...@@ -106,7 +106,7 @@ async def remuneration_(request, response):
except DataError as err: except DataError as err:
error = {err.key: err.error} error = {err.key: err.error}
log_simulate(context, errors=error) log_simulate(context, errors=error)
raise HttpError(HTTPStatus.UNPROCESSABLE_ENTITY, error) raise HttpError(HTTPStatus.BAD_REQUEST, error)
# TODO: explain only for financement see routine.py check_remuneration # TODO: explain only for financement see routine.py check_remuneration
# explain = request.query.bool("explain", False) # explain = request.query.bool("explain", False)
...@@ -188,7 +188,7 @@ async def authent(request, response): ...@@ -188,7 +188,7 @@ async def authent(request, response):
try: try:
data = request.json data = request.json
except ValueError as err: except ValueError as err:
raise HttpError(HTTPStatus.UNPROCESSABLE_ENTITY, message=err) raise HttpError(HTTPStatus.BAD_REQUEST, message=err)
date = datetime.datetime.today().strftime('%y%m%d') date = datetime.datetime.today().strftime('%y%m%d')
authsuccess = False authsuccess = False
...@@ -209,7 +209,7 @@ async def authent(request, response): ...@@ -209,7 +209,7 @@ async def authent(request, response):
+ 'non authorisé à modifier le fichier \'' + 'non authorisé à modifier le fichier \''
+ data.get('file') + '\'.') + data.get('file') + '\'.')
except re.error: except re.error:
raise HttpError(HTTPStatus.UNPROCESSABLE_ENTITY, raise HttpError(HTTPStatus.BAD_REQUEST,
'Le motif de fichier autorisé est invalide.') 'Le motif de fichier autorisé est invalide.')
else: else:
continue continue
...@@ -277,4 +277,4 @@ async def source_save(request, response): ...@@ -277,4 +277,4 @@ async def source_save(request, response):
except NotModifiedError: except NotModifiedError:
raise HttpError(HTTPStatus.NOT_MODIFIED, message="Aucune modification apportée") raise HttpError(HTTPStatus.NOT_MODIFIED, message="Aucune modification apportée")
except ValueError as err: except ValueError as err:
raise HttpError(HTTPStatus.UNPROCESSABLE_ENTITY, message=err) raise HttpError(HTTPStatus.BAD_REQUEST, message=err)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment