diff --git a/app/inventory/forms.py b/app/inventory/forms.py index f146e451906988750f18b171f80d9db2ad41f2f0..be25524f4504ea1b4dd654ecf7e2df4087d41350 100644 --- a/app/inventory/forms.py +++ b/app/inventory/forms.py @@ -31,7 +31,10 @@ class ItemForm(CSEntryForm): "ICS id", validators=[ validators.InputRequired(), - validators.Regexp(ICS_ID_RE), + validators.Regexp( + ICS_ID_RE, + message="The ICS id shall be composed of 3 letters and 3 digits.", + ), Unique(models.Item, "ics_id"), ], ) diff --git a/app/validators.py b/app/validators.py index 642da1dc91cf3cf297b06cac938c46a49d4acc6a..afdba7fee538b0c9ea22d4723d8acef1946454dc 100644 --- a/app/validators.py +++ b/app/validators.py @@ -14,7 +14,7 @@ import re import sqlalchemy as sa from wtforms import ValidationError, SelectField -ICS_ID_RE = re.compile(r"[A-Z]{3}[0-9]{3}") +ICS_ID_RE = re.compile(r"^[A-Z]{3}[0-9]{3}$") HOST_NAME_RE = re.compile(r"^[a-z0-9\-]{2,20}$") INTERFACE_NAME_RE = re.compile(r"^[a-z0-9\-]{2,25}$") VLAN_NAME_RE = re.compile(r"^[A-Za-z0-9\-]{3,25}$") diff --git a/tests/functional/test_models.py b/tests/functional/test_models.py index c5014860fc836f950e643001545b6d3708c8a23d..377a1dc632e330657b23410deff12a639b1ceba6 100644 --- a/tests/functional/test_models.py +++ b/tests/functional/test_models.py @@ -799,3 +799,10 @@ def test_interface_name_length(db, host_factory, interface_factory): with pytest.raises(ValidationError) as excinfo: interface_factory(name=interface_name + "y", host=host1) assert r"Interface name shall match [a-z0-9\-]{2,25}" in str(excinfo.value) + + +@pytest.mark.parametrize("ics_id", ("123", "AA123", "AAA1234")) +def test_item_invalid_ics_id(db, item_factory, ics_id): + with pytest.raises(ValidationError) as excinfo: + item_factory(ics_id=ics_id) + assert r"ICS id shall match [A-Z]{3}[0-9]{3}" in str(excinfo.value) diff --git a/tests/functional/test_web.py b/tests/functional/test_web.py index 53c1c55c4428ef0a3898affb90156106710a5dbe..c4bdda2aaf8c452a7f6a53d502b36c30a768cd07 100644 --- a/tests/functional/test_web.py +++ b/tests/functional/test_web.py @@ -652,3 +652,17 @@ def test_create_network_no_vlan( assert network.vlan_name == vlan_name assert network.address == form["address"] assert network.vlan_id is None + + +def test_create_item_invalid_ics_id(logged_rw_client): + ics_id = "AAA1100" + form = { + "ics_id": ics_id, + "serial_number": "12345", + } + response = logged_rw_client.post( + f"/inventory/items/create", data=form, follow_redirects=True + ) + assert response.status_code == 200 + assert b"Register new item" in response.data + assert b"The ICS id shall be composed of 3 letters and 3 digits" in response.data