~javiljoen/shopping-list

45013023d89b1c8c7cc3d5318e92838391e7108e — JA Viljoen 4 years ago 1de2f6a
Improve error message for bad requests
2 files changed, 12 insertions(+), 2 deletions(-)

M src/groceries/web/api.py
M tests/test_rest_server.py
M src/groceries/web/api.py => src/groceries/web/api.py +1 -1
@@ 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"])


M tests/test_rest_server.py => tests/test_rest_server.py +11 -1
@@ 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"
    }