Move check to when custom observable is defined

Catch properties named "_ref" or "_refs" that aren't
ObjectReferenceProperty when the custom observable is defined, not when
it is instantiated.
stix2.1
clenk 2017-08-30 15:33:28 -04:00
parent 0cd75e3fba
commit 0e658255a8
3 changed files with 41 additions and 20 deletions

View File

@ -205,18 +205,14 @@ class _Observable(_STIXBase):
try: try:
allowed_types = prop.contained.valid_types allowed_types = prop.contained.valid_types
except AttributeError: except AttributeError:
try: allowed_types = prop.valid_types
allowed_types = prop.valid_types
except AttributeError: try:
raise ValueError("'%s' is named like an object reference property but " ref_type = self._STIXBase__valid_refs[ref]
"is not an ObjectReferenceProperty or a ListProperty " except TypeError:
"containing ObjectReferenceProperty." % prop_name) raise ValueError("'%s' must be created with _valid_refs as a dict, not a list." % self.__class__.__name__)
if allowed_types: if allowed_types:
try:
ref_type = self._STIXBase__valid_refs[ref]
except TypeError:
raise ValueError("'%s' must be created with _valid_refs as a dict, not a list." % self.__class__.__name__)
if ref_type not in allowed_types: if ref_type not in allowed_types:
raise InvalidObjRefError(self.__class__, prop_name, "object reference '%s' is of an invalid type '%s'" % (ref, ref_type)) raise InvalidObjRefError(self.__class__, prop_name, "object reference '%s' is of an invalid type '%s'" % (ref, ref_type))

View File

@ -766,6 +766,16 @@ def CustomObservable(type='x-custom-observable', properties={}):
_properties = { _properties = {
'type': TypeProperty(_type), 'type': TypeProperty(_type),
} }
# Check properties ending in "_ref/s" are ObjectReferenceProperties
for prop_name, prop in properties.items():
if prop_name.endswith('_ref') and prop.__class__.__name__ != 'ObjectReferenceProperty':
raise ValueError("'%s' is named like an object reference property but "
"is not an ObjectReferenceProperty." % prop_name)
elif (prop_name.endswith('_refs') and (prop.__class__.__name__ != 'ListProperty'
or prop.contained.__class__.__name__ != 'ObjectReferenceProperty')):
raise ValueError("'%s' is named like an object reference property but "
"is not a ListProperty containing ObjectReferenceProperty." % prop_name)
_properties.update(properties) _properties.update(properties)
def __init__(self, **kwargs): def __init__(self, **kwargs):

View File

@ -153,20 +153,35 @@ def test_custom_observable_object():
def test_custom_observable_object_invalid_ref_property(): def test_custom_observable_object_invalid_ref_property():
@stix2.observables.CustomObservable('x-new-obs', {
'property1': stix2.properties.StringProperty(required=True),
'property_ref': stix2.properties.StringProperty(),
})
class NewObs():
pass
with pytest.raises(ValueError) as excinfo: with pytest.raises(ValueError) as excinfo:
NewObs(_valid_refs={'1': 'file'}, @stix2.observables.CustomObservable('x-new-obs', {
property1='something', 'property_ref': stix2.properties.StringProperty(),
property_ref='1') })
class NewObs():
pass
assert "is named like an object reference property but is not an ObjectReferenceProperty" in str(excinfo.value) assert "is named like an object reference property but is not an ObjectReferenceProperty" in str(excinfo.value)
def test_custom_observable_object_invalid_refs_property():
with pytest.raises(ValueError) as excinfo:
@stix2.observables.CustomObservable('x-new-obs', {
'property_refs': stix2.properties.StringProperty(),
})
class NewObs():
pass
assert "is named like an object reference property but is not a ListProperty containing ObjectReferenceProperty" in str(excinfo.value)
def test_custom_observable_object_invalid_refs_list_property():
with pytest.raises(ValueError) as excinfo:
@stix2.observables.CustomObservable('x-new-obs', {
'property_refs': stix2.properties.ListProperty(stix2.properties.StringProperty),
})
class NewObs():
pass
assert "is named like an object reference property but is not a ListProperty containing ObjectReferenceProperty" in str(excinfo.value)
def test_custom_observable_object_invalid_valid_refs(): def test_custom_observable_object_invalid_valid_refs():
@stix2.observables.CustomObservable('x-new-obs', { @stix2.observables.CustomObservable('x-new-obs', {
'property1': stix2.properties.StringProperty(required=True), 'property1': stix2.properties.StringProperty(required=True),