Skip to content
Snippets Groups Projects
Commit 66c09043 authored by Benjamin Bertrand's avatar Benjamin Bertrand
Browse files

Raise 422 when creating item with invalid ICS id

parent ef4fb50f
No related branches found
No related tags found
No related merge requests found
...@@ -229,7 +229,8 @@ class Item(db.Model): ...@@ -229,7 +229,8 @@ class Item(db.Model):
def validate_ics_id(self, key, string): def validate_ics_id(self, key, string):
"""Ensure the ICS id field matches the required format""" """Ensure the ICS id field matches the required format"""
if string is not None: 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 return string
def to_dict(self): def to_dict(self):
......
...@@ -214,6 +214,13 @@ def test_create_item(client, user_token): ...@@ -214,6 +214,13 @@ def test_create_item(client, user_token):
check_items(response, (data, data, data2)) 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): def test_get_item_fail(client, session, readonly_token):
response = get(client, '/api/items/50', token=readonly_token) response = get(client, '/api/items/50', token=readonly_token)
check_response_message(response, "Item id '50' not found", 404) check_response_message(response, "Item id '50' not found", 404)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment