From 25409b2099c8c56771aeaa82b3ae9abb08989a27 Mon Sep 17 00:00:00 2001 From: Greg Back Date: Thu, 5 Jul 2018 11:25:48 -0500 Subject: [PATCH] GH-188: Update error message for invalid IDs. --- stix2/properties.py | 8 ++++++-- stix2/test/test_properties.py | 15 ++++++++------- stix2/test/test_report.py | 4 ++-- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/stix2/properties.py b/stix2/properties.py index 871e8fd..1fc0e7c 100644 --- a/stix2/properties.py +++ b/stix2/properties.py @@ -25,6 +25,10 @@ ID_REGEX = re.compile("^[a-z0-9][a-z0-9-]+[a-z0-9]--" # object type "[89abAB][0-9a-fA-F]{3}-" "[0-9a-fA-F]{12}$") +ERROR_INVALID_ID = ( + "not a valid STIX identifier, must match --" +) + class Property(object): """Represent a property of STIX data type. @@ -183,7 +187,7 @@ class IDProperty(Property): if not value.startswith(self.required_prefix): raise ValueError("must start with '{0}'.".format(self.required_prefix)) if not ID_REGEX.match(value): - raise ValueError("must have a valid UUID after the prefix.") + raise ValueError(ERROR_INVALID_ID) return value def default(self): @@ -331,7 +335,7 @@ class ReferenceProperty(Property): if not value.startswith(self.type): raise ValueError("must start with '{0}'.".format(self.type)) if not ID_REGEX.match(value): - raise ValueError("must match --.") + raise ValueError(ERROR_INVALID_ID) return value diff --git a/stix2/test/test_properties.py b/stix2/test/test_properties.py index 6280c57..1106bcb 100644 --- a/stix2/test/test_properties.py +++ b/stix2/test/test_properties.py @@ -4,12 +4,13 @@ import pytest from stix2 import CustomObject, EmailMIMEComponent, ExtensionsProperty, TCPExt from stix2.exceptions import AtLeastOnePropertyError, DictionaryKeyError -from stix2.properties import (BinaryProperty, BooleanProperty, - DictionaryProperty, EmbeddedObjectProperty, - EnumProperty, FloatProperty, HashesProperty, - HexProperty, IDProperty, IntegerProperty, - ListProperty, Property, ReferenceProperty, - StringProperty, TimestampProperty, TypeProperty) +from stix2.properties import (ERROR_INVALID_ID, BinaryProperty, + BooleanProperty, DictionaryProperty, + EmbeddedObjectProperty, EnumProperty, + FloatProperty, HashesProperty, HexProperty, + IDProperty, IntegerProperty, ListProperty, + Property, ReferenceProperty, StringProperty, + TimestampProperty, TypeProperty) from . import constants @@ -143,7 +144,7 @@ def test_id_property_wrong_type(): def test_id_property_not_a_valid_hex_uuid(value): with pytest.raises(ValueError) as excinfo: ID_PROP.clean(value) - assert str(excinfo.value) == "must have a valid UUID after the prefix." + assert str(excinfo.value) == ERROR_INVALID_ID def test_id_property_default(): diff --git a/stix2/test/test_report.py b/stix2/test/test_report.py index b38ab40..9999877 100644 --- a/stix2/test/test_report.py +++ b/stix2/test/test_report.py @@ -87,8 +87,8 @@ def test_report_example_objects_in_object_refs_with_bad_id(): assert excinfo.value.cls == stix2.Report assert excinfo.value.prop_name == "object_refs" - assert excinfo.value.reason == "must match --." - assert str(excinfo.value) == "Invalid value for Report 'object_refs': must match --." + assert excinfo.value.reason == stix2.properties.ERROR_INVALID_ID + assert str(excinfo.value) == "Invalid value for Report 'object_refs': " + stix2.properties.ERROR_INVALID_ID @pytest.mark.parametrize("data", [