From c29671e3f704e351241e74f69528e8b27f768ba9 Mon Sep 17 00:00:00 2001
From: Benjamin Bertrand <benjamin.bertrand@esss.se>
Date: Mon, 13 Nov 2017 22:50:41 +0100
Subject: [PATCH] Test TypeError in create_generic_model

---
 app/api/main.py              | 2 +-
 tests/functional/test_api.py | 6 ++++++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/app/api/main.py b/app/api/main.py
index 398afd8..3b6dcae 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 ce1b072..f7b671f 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)
-- 
GitLab