Skip to content
Snippets Groups Projects
Commit 3beedccd authored by Augustin Wenger's avatar Augustin Wenger Committed by sandcha
Browse files

Adds a function to check fields within the request.

parent e8acc292
No related branches found
No related tags found
1 merge request!19Ajoute un endpoint /dotations
from Simulation_engine.simulate_dotations import simulate
# Checks whether all dictionnaries in the model exist in the target dict.
# Returns True or False, and the problematic field
def check_keys_dict(dict_to_check, model):
for k, v in model.items():
if k not in dict_to_check: # key does not exist
return False, k
if isinstance(v, dict):
if not isinstance(dict_to_check[k], dict): # should be a dict but is not
return False, k
res_child, node_err = check_keys_dict(dict_to_check[k], v)
if not res_child:
return False, ".".join([k, node_err])
return True, "All required keys are in the checked dictionary"
def check_request_body(request_body):
required_dict = {"reforme": {"dotations": {"montants": {"dgf": None}, "communes": None}}}
result, errorfield = check_keys_dict(request_body, required_dict)
if not result:
return {"Error": "Missing required '{}' field in request body.".format(errorfield)}, 400
class Dotations(object):
......@@ -7,8 +31,9 @@ class Dotations(object):
request_body = params["body"]
# vérifier le format
if "dotations" not in request_body:
return {"Error": "Missing 'dotations' field in request body."}, 400
check_result = check_request_body(request_body)
if check_result is not None:
return check_result
# calculer
simulation_result = simulate(request_body)
......
......@@ -14,7 +14,12 @@ def test_dotations_request_body_error(client, headers):
def test_dotations(client, headers):
request = {
"dotations": {}
"reforme": {
"dotations": {
"montants": {"dgf": 16},
"communes": {}
}
}
}
response_function = partial(client.post, "dotations", headers=headers)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment