diff --git a/app/models.py b/app/models.py index 8466c0c6bdd81b7fccc6b3b03e08fd756ae9c9b5..bbe378838e11ea183257bde5e0257739c583f9aa 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 0f99c3a248caca4a11060302b8a940cb64e9766f..61135aba478b596ad7a01e03f0cd9660d7fe594c 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)