diff --git a/app/api/main.py b/app/api/main.py index 398afd88eeb9850ccbf16f58a1b430ef6ce657a5..3b6dcae41369516efa174190dccb8532f5e43868 100644 --- a/app/api/main.py +++ b/app/api/main.py @@ -68,7 +68,7 @@ def create_generic_model(model, mandatory_fields=('name',)): instance = model(**data) except TypeError as e: message = str(e).replace('__init__() got an ', '') - raise utils.CSEntryError(message) + raise utils.CSEntryError(message, status_code=422) except ValueError as e: raise utils.CSEntryError(str(e), status_code=422) db.session.add(instance) diff --git a/tests/functional/test_api.py b/tests/functional/test_api.py index ce1b072484d158282672ebd675496b2816f6f670..f7b671f2b2753ab331dd80bb7743e81546ecea74 100644 --- a/tests/functional/test_api.py +++ b/tests/functional/test_api.py @@ -185,6 +185,12 @@ def test_create_generic_model(endpoint, client, user_token): check_names(response, ('Foo', 'Bar')) +@pytest.mark.parametrize('endpoint', GENERIC_CREATE_ENDPOINTS) +def test_create_generic_model_invalid_param(endpoint, client, user_token): + response = post(client, f'/api/{endpoint}', data={'name': 'foo', 'hello': 'world'}, token=user_token) + check_response_message(response, "unexpected keyword argument 'hello'", 422) + + def test_create_item(client, user_token): # check that serial_number is mandatory response = post(client, '/api/items', data={}, token=user_token)