From 45013023d89b1c8c7cc3d5318e92838391e7108e Mon Sep 17 00:00:00 2001 From: JA Viljoen Date: Thu, 6 Aug 2020 10:52:52 +0200 Subject: [PATCH] Improve error message for bad requests --- src/groceries/web/api.py | 2 +- tests/test_rest_server.py | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/groceries/web/api.py b/src/groceries/web/api.py index e527013..81e6824 100644 --- a/src/groceries/web/api.py +++ b/src/groceries/web/api.py @@ -42,7 +42,7 @@ def create_item(): try: item = Item(**request.json) except TypeError: - abort(400, description="Missing fields in request") + abort(400, description="Invalid fields in request") grocery_list = GroceryList(current_app.config["BACKEND"]) diff --git a/tests/test_rest_server.py b/tests/test_rest_server.py index 83e503d..c5f8111 100644 --- a/tests/test_rest_server.py +++ b/tests/test_rest_server.py @@ -99,7 +99,17 @@ def test_create_item_with_missing_fields(client): response = client.post("/api/items/", json={"name": "turkey"}) assert response.status_code == 400 assert response.get_json() == { - "error": "400 Bad Request: Missing fields in request" + "error": "400 Bad Request: Invalid fields in request" + } + + +def test_create_item_with_extra_fields(client): + response = client.post( + "/api/items/", json={"name": "turkey", "origin": "new world"} + ) + assert response.status_code == 400 + assert response.get_json() == { + "error": "400 Bad Request: Invalid fields in request" } -- 2.45.2