Convert BOOL_PROPERTY to BooleanProperty.
parent
aa1ecfa235
commit
ef332a328b
|
|
@ -3,7 +3,7 @@
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from .base import _STIXBase
|
from .base import _STIXBase
|
||||||
from .properties import Property
|
from .properties import Property, BooleanProperty
|
||||||
from .utils import NOW
|
from .utils import NOW
|
||||||
|
|
||||||
ref_regex = ("^[a-z][a-z-]+[a-z]--[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}"
|
ref_regex = ("^[a-z][a-z-]+[a-z]--[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}"
|
||||||
|
|
@ -14,11 +14,6 @@ REF_PROPERTY = {
|
||||||
'error_msg': "{type} {field} values must consist of a valid STIX type name and a valid UUID, separated by '--'."
|
'error_msg': "{type} {field} values must consist of a valid STIX type name and a valid UUID, separated by '--'."
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOL_PROPERTY = {
|
|
||||||
'validate': (lambda x, val: isinstance(val, bool)),
|
|
||||||
'error_msg': "{type} {field} value must be a boolean."
|
|
||||||
}
|
|
||||||
|
|
||||||
COMMON_PROPERTIES = {
|
COMMON_PROPERTIES = {
|
||||||
# 'type' and 'id' should be defined on each individual type
|
# 'type' and 'id' should be defined on each individual type
|
||||||
'created': {
|
'created': {
|
||||||
|
|
@ -28,7 +23,7 @@ COMMON_PROPERTIES = {
|
||||||
'default': NOW,
|
'default': NOW,
|
||||||
},
|
},
|
||||||
'external_references': {},
|
'external_references': {},
|
||||||
'revoked': BOOL_PROPERTY,
|
'revoked': BooleanProperty(),
|
||||||
'created_by_ref': REF_PROPERTY
|
'created_by_ref': REF_PROPERTY
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -108,3 +108,12 @@ class IDProperty(Property):
|
||||||
|
|
||||||
def default(self):
|
def default(self):
|
||||||
return self.required_prefix + str(uuid.uuid4())
|
return self.required_prefix + str(uuid.uuid4())
|
||||||
|
|
||||||
|
|
||||||
|
class BooleanProperty(Property):
|
||||||
|
# TODO: Consider coercing some values (like the strings "true" and "false")
|
||||||
|
|
||||||
|
def validate(self, value):
|
||||||
|
if not isinstance(value, bool):
|
||||||
|
raise ValueError("must be a boolean value.")
|
||||||
|
return value
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from stix2.properties import Property, IDProperty, TypeProperty
|
from stix2.properties import Property, BooleanProperty, IDProperty, TypeProperty
|
||||||
|
|
||||||
|
|
||||||
def test_property():
|
def test_property():
|
||||||
|
|
@ -65,3 +65,14 @@ def test_id_property():
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
idprop.validate('not-my-type--90aaca8a-1110-5d32-956d-ac2f34a1bd8c')
|
idprop.validate('not-my-type--90aaca8a-1110-5d32-956d-ac2f34a1bd8c')
|
||||||
assert idprop.validate(idprop.default())
|
assert idprop.validate(idprop.default())
|
||||||
|
|
||||||
|
|
||||||
|
def test_boolean_property():
|
||||||
|
bool_prop = BooleanProperty()
|
||||||
|
|
||||||
|
assert bool_prop.validate(True) is not None
|
||||||
|
assert bool_prop.validate(False) is not None
|
||||||
|
for invalid in ('true', 'false', "T", "F", 1, 0):
|
||||||
|
print(invalid)
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
bool_prop.validate(invalid)
|
||||||
|
|
|
||||||
|
|
@ -185,7 +185,7 @@ def test_indicator_created_ref_invalid_format():
|
||||||
def test_indicator_revoked_invalid():
|
def test_indicator_revoked_invalid():
|
||||||
with pytest.raises(ValueError) as excinfo:
|
with pytest.raises(ValueError) as excinfo:
|
||||||
indicator = stix2.Indicator(revoked='false', **INDICATOR_KWARGS)
|
indicator = stix2.Indicator(revoked='false', **INDICATOR_KWARGS)
|
||||||
assert str(excinfo.value) == "Indicator revoked value must be a boolean."
|
assert str(excinfo.value) == "Invalid value for Indicator 'revoked': must be a boolean value."
|
||||||
|
|
||||||
|
|
||||||
def test_cannot_assign_to_indicator_attributes(indicator):
|
def test_cannot_assign_to_indicator_attributes(indicator):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue