From 66c0904313b56dae5b6912ecba9dc680f4d58e85 Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand <benjamin.bertrand@esss.se> Date: Wed, 20 Sep 2017 15:51:04 +0200 Subject: [PATCH] Raise 422 when creating item with invalid ICS id --- app/models.py | 3 ++- tests/functional/test_api.py | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/models.py b/app/models.py index 8466c0c..bbe3788 100644 --- a/app/models.py +++ b/app/models.py @@ -229,7 +229,8 @@ class Item(db.Model): def validate_ics_id(self, key, string): """Ensure the ICS id field matches the required format""" if string is not None: - assert ICS_ID_RE.fullmatch(string) is not None + if ICS_ID_RE.fullmatch(string) is None: + raise utils.InventoryError('ICS id shall match [A-Z]{3}[0-9]{3}', status_code=422) return string def to_dict(self): diff --git a/tests/functional/test_api.py b/tests/functional/test_api.py index 0f99c3a..61135ab 100644 --- a/tests/functional/test_api.py +++ b/tests/functional/test_api.py @@ -214,6 +214,13 @@ def test_create_item(client, user_token): check_items(response, (data, data, data2)) +def test_create_item_invalid_ics_id(client, user_token): + for ics_id in ('foo', 'AAB1234', 'AZ02', 'WS007', 'AAA01'): + data = {'serial_number': '123456', 'ics_id': ics_id} + response = post(client, '/api/items', data=data, token=user_token) + check_response_message(response, 'ICS id shall match [A-Z]{3}[0-9]{3}', 422) + + def test_get_item_fail(client, session, readonly_token): response = get(client, '/api/items/50', token=readonly_token) check_response_message(response, "Item id '50' not found", 404) -- GitLab