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}-"
"[0-9a-fA-F]{12}$")
ERROR_INVALID_ID = (
"not a valid STIX identifier, must match <object-type>--<UUIDv4>"
)
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 <object-type>--<guid>.")
raise ValueError(ERROR_INVALID_ID)
return value

View File

@ -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():

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.prop_name == "object_refs"
assert excinfo.value.reason == "must match <object-type>--<guid>."
assert str(excinfo.value) == "Invalid value for Report 'object_refs': must match <object-type>--<guid>."
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", [