GH-188: Update error message for invalid IDs.

stix2.0
Greg Back 2018-07-05 11:25:48 -05:00
parent a2e2407025
commit 25409b2099
3 changed files with 16 additions and 11 deletions

View File

@ -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}-" "[89abAB][0-9a-fA-F]{3}-"
"[0-9a-fA-F]{12}$") "[0-9a-fA-F]{12}$")
ERROR_INVALID_ID = (
"not a valid STIX identifier, must match <object-type>--<UUIDv4>"
)
class Property(object): class Property(object):
"""Represent a property of STIX data type. """Represent a property of STIX data type.
@ -183,7 +187,7 @@ class IDProperty(Property):
if not value.startswith(self.required_prefix): if not value.startswith(self.required_prefix):
raise ValueError("must start with '{0}'.".format(self.required_prefix)) raise ValueError("must start with '{0}'.".format(self.required_prefix))
if not ID_REGEX.match(value): if not ID_REGEX.match(value):
raise ValueError("must have a valid UUID after the prefix.") raise ValueError(ERROR_INVALID_ID)
return value return value
def default(self): def default(self):
@ -331,7 +335,7 @@ class ReferenceProperty(Property):
if not value.startswith(self.type): if not value.startswith(self.type):
raise ValueError("must start with '{0}'.".format(self.type)) raise ValueError("must start with '{0}'.".format(self.type))
if not ID_REGEX.match(value): if not ID_REGEX.match(value):
raise ValueError("must match <object-type>--<guid>.") raise ValueError(ERROR_INVALID_ID)
return value return value

View File

@ -4,12 +4,13 @@ import pytest
from stix2 import CustomObject, EmailMIMEComponent, ExtensionsProperty, TCPExt from stix2 import CustomObject, EmailMIMEComponent, ExtensionsProperty, TCPExt
from stix2.exceptions import AtLeastOnePropertyError, DictionaryKeyError from stix2.exceptions import AtLeastOnePropertyError, DictionaryKeyError
from stix2.properties import (BinaryProperty, BooleanProperty, from stix2.properties import (ERROR_INVALID_ID, BinaryProperty,
DictionaryProperty, EmbeddedObjectProperty, BooleanProperty, DictionaryProperty,
EnumProperty, FloatProperty, HashesProperty, EmbeddedObjectProperty, EnumProperty,
HexProperty, IDProperty, IntegerProperty, FloatProperty, HashesProperty, HexProperty,
ListProperty, Property, ReferenceProperty, IDProperty, IntegerProperty, ListProperty,
StringProperty, TimestampProperty, TypeProperty) Property, ReferenceProperty, StringProperty,
TimestampProperty, TypeProperty)
from . import constants from . import constants
@ -143,7 +144,7 @@ def test_id_property_wrong_type():
def test_id_property_not_a_valid_hex_uuid(value): def test_id_property_not_a_valid_hex_uuid(value):
with pytest.raises(ValueError) as excinfo: with pytest.raises(ValueError) as excinfo:
ID_PROP.clean(value) 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(): def test_id_property_default():

View File

@ -87,8 +87,8 @@ def test_report_example_objects_in_object_refs_with_bad_id():
assert excinfo.value.cls == stix2.Report assert excinfo.value.cls == stix2.Report
assert excinfo.value.prop_name == "object_refs" assert excinfo.value.prop_name == "object_refs"
assert excinfo.value.reason == "must match <object-type>--<guid>." assert excinfo.value.reason == stix2.properties.ERROR_INVALID_ID
assert str(excinfo.value) == "Invalid value for Report 'object_refs': must match <object-type>--<guid>." assert str(excinfo.value) == "Invalid value for Report 'object_refs': " + stix2.properties.ERROR_INVALID_ID
@pytest.mark.parametrize("data", [ @pytest.mark.parametrize("data", [