2017-02-24 16:28:53 +01:00
|
|
|
import pytest
|
|
|
|
|
2017-04-07 01:17:32 +02:00
|
|
|
from stix2.properties import (Property, BooleanProperty, ListProperty,
|
|
|
|
StringProperty, TypeProperty, IDProperty,
|
|
|
|
ReferenceProperty)
|
2017-02-24 16:28:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_property():
|
|
|
|
p = Property()
|
|
|
|
|
|
|
|
assert p.required is False
|
|
|
|
|
|
|
|
|
|
|
|
def test_basic_validate():
|
|
|
|
class Prop(Property):
|
|
|
|
|
|
|
|
def validate(self, value):
|
|
|
|
if value == 42:
|
|
|
|
return value
|
|
|
|
else:
|
|
|
|
raise ValueError("Must be 42")
|
|
|
|
|
|
|
|
p = Prop()
|
|
|
|
|
|
|
|
assert p.validate(42) == 42
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
p.validate(41)
|
|
|
|
|
|
|
|
|
|
|
|
def test_default_field():
|
|
|
|
class Prop(Property):
|
|
|
|
|
|
|
|
def default(self):
|
|
|
|
return 77
|
|
|
|
|
|
|
|
p = Prop()
|
|
|
|
|
|
|
|
assert p.default() == 77
|
|
|
|
|
|
|
|
|
|
|
|
def test_fixed_property():
|
|
|
|
p = Property(fixed="2.0")
|
|
|
|
|
2017-02-24 17:46:21 +01:00
|
|
|
assert p.validate("2.0")
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
assert p.validate("x") is False
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
assert p.validate(2.0) is False
|
2017-02-24 16:28:53 +01:00
|
|
|
|
|
|
|
assert p.default() == "2.0"
|
2017-02-24 17:46:21 +01:00
|
|
|
assert p.validate(p.default())
|
|
|
|
|
|
|
|
|
2017-04-07 01:17:32 +02:00
|
|
|
def test_list_property():
|
|
|
|
p = ListProperty(StringProperty)
|
|
|
|
|
|
|
|
assert p.validate(['abc', 'xyz'])
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
p.validate([])
|
|
|
|
|
|
|
|
|
|
|
|
def test_string_property():
|
|
|
|
prop = StringProperty()
|
|
|
|
|
|
|
|
assert prop.validate('foobar')
|
|
|
|
assert prop.validate(1)
|
|
|
|
assert prop.validate([1, 2, 3])
|
|
|
|
|
|
|
|
|
2017-02-24 17:46:21 +01:00
|
|
|
def test_type_property():
|
|
|
|
prop = TypeProperty('my-type')
|
|
|
|
|
|
|
|
assert prop.validate('my-type')
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
prop.validate('not-my-type')
|
|
|
|
assert prop.validate(prop.default())
|
2017-02-24 16:28:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_id_property():
|
|
|
|
idprop = IDProperty('my-type')
|
|
|
|
|
2017-02-24 17:20:24 +01:00
|
|
|
assert idprop.validate('my-type--90aaca8a-1110-5d32-956d-ac2f34a1bd8c')
|
2017-04-06 19:08:48 +02:00
|
|
|
with pytest.raises(ValueError) as excinfo:
|
2017-02-24 17:20:24 +01:00
|
|
|
idprop.validate('not-my-type--90aaca8a-1110-5d32-956d-ac2f34a1bd8c')
|
2017-04-06 19:08:48 +02:00
|
|
|
assert str(excinfo.value) == "must start with 'my-type--'."
|
|
|
|
with pytest.raises(ValueError) as excinfo:
|
|
|
|
idprop.validate('my-type--foo')
|
|
|
|
assert str(excinfo.value) == "must have a valid version 4 UUID after the prefix."
|
|
|
|
|
2017-02-24 16:28:53 +01:00
|
|
|
assert idprop.validate(idprop.default())
|
2017-03-22 00:33:43 +01:00
|
|
|
|
|
|
|
|
2017-04-10 16:18:54 +02:00
|
|
|
@pytest.mark.parametrize("value", [
|
|
|
|
True,
|
|
|
|
False,
|
|
|
|
'True',
|
|
|
|
'False',
|
|
|
|
'true',
|
|
|
|
'false',
|
|
|
|
'TRUE',
|
|
|
|
'FALSE',
|
|
|
|
'T',
|
|
|
|
'F',
|
|
|
|
't',
|
|
|
|
'f',
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
])
|
|
|
|
def test_boolean_property_valid(value):
|
2017-03-22 00:33:43 +01:00
|
|
|
bool_prop = BooleanProperty()
|
|
|
|
|
2017-04-10 16:18:54 +02:00
|
|
|
assert bool_prop.validate(value) is not None
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("value", [
|
|
|
|
'abc',
|
|
|
|
['false'],
|
|
|
|
{'true': 'true'},
|
|
|
|
2,
|
|
|
|
-1,
|
|
|
|
])
|
|
|
|
def test_boolean_property_invalid(value):
|
|
|
|
bool_prop = BooleanProperty()
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
bool_prop.validate(value)
|
2017-03-22 00:44:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_reference_property():
|
|
|
|
ref_prop = ReferenceProperty()
|
|
|
|
|
|
|
|
assert ref_prop.validate("my-type--3a331bfe-0566-55e1-a4a0-9a2cd355a300")
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
ref_prop.validate("foo")
|