cti-python-stix2/stix2/test/test_properties.py

174 lines
3.7 KiB
Python
Raw Normal View History

2017-02-24 16:28:53 +01:00
import pytest
from stix2.properties import (BooleanProperty, IDProperty, IntegerProperty,
ListProperty, Property, ReferenceProperty,
StringProperty, TimestampProperty, TypeProperty)
2017-04-11 18:10:55 +02:00
from .constants import FAKE_TIME
2017-02-24 16:28:53 +01:00
def test_property():
p = Property()
assert p.required is False
2017-04-17 21:13:11 +02:00
def test_basic_clean():
2017-02-24 16:28:53 +01:00
class Prop(Property):
2017-04-17 21:13:11 +02:00
def clean(self, value):
2017-02-24 16:28:53 +01:00
if value == 42:
return value
else:
raise ValueError("Must be 42")
p = Prop()
2017-04-17 21:13:11 +02:00
assert p.clean(42) == 42
2017-02-24 16:28:53 +01:00
with pytest.raises(ValueError):
2017-04-17 21:13:11 +02:00
p.clean(41)
2017-02-24 16:28:53 +01:00
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-04-17 21:13:11 +02:00
assert p.clean("2.0")
with pytest.raises(ValueError):
2017-04-17 21:13:11 +02:00
assert p.clean("x") is False
with pytest.raises(ValueError):
2017-04-17 21:13:11 +02:00
assert p.clean(2.0) is False
2017-02-24 16:28:53 +01:00
assert p.default() == "2.0"
2017-04-17 21:13:11 +02:00
assert p.clean(p.default())
def test_list_property():
p = ListProperty(StringProperty)
2017-04-17 21:13:11 +02:00
assert p.clean(['abc', 'xyz'])
with pytest.raises(ValueError):
2017-04-17 21:13:11 +02:00
p.clean([])
def test_string_property():
prop = StringProperty()
2017-04-17 21:13:11 +02:00
assert prop.clean('foobar')
assert prop.clean(1)
assert prop.clean([1, 2, 3])
def test_type_property():
prop = TypeProperty('my-type')
2017-04-17 21:13:11 +02:00
assert prop.clean('my-type')
with pytest.raises(ValueError):
2017-04-17 21:13:11 +02:00
prop.clean('not-my-type')
assert prop.clean(prop.default())
2017-02-24 16:28:53 +01:00
def test_id_property():
idprop = IDProperty('my-type')
2017-04-17 21:13:11 +02:00
assert idprop.clean('my-type--90aaca8a-1110-5d32-956d-ac2f34a1bd8c')
2017-04-06 19:08:48 +02:00
with pytest.raises(ValueError) as excinfo:
2017-04-17 21:13:11 +02:00
idprop.clean('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:
2017-04-17 21:13:11 +02:00
idprop.clean('my-type--foo')
2017-04-25 16:03:37 +02:00
assert str(excinfo.value) == "must have a valid UUID after the prefix."
2017-04-06 19:08:48 +02:00
2017-04-17 21:13:11 +02:00
assert idprop.clean(idprop.default())
2017-04-18 15:19:38 +02:00
@pytest.mark.parametrize("value", [
2,
-1,
3.14,
False,
])
def test_integer_property_valid(value):
int_prop = IntegerProperty()
assert int_prop.clean(value) is not None
@pytest.mark.parametrize("value", [
"something",
StringProperty(),
])
def test_integer_property_invalid(value):
int_prop = IntegerProperty()
with pytest.raises(ValueError):
int_prop.clean(value)
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):
bool_prop = BooleanProperty()
2017-04-17 21:13:11 +02:00
assert bool_prop.clean(value) is not None
2017-04-10 16:18:54 +02:00
@pytest.mark.parametrize("value", [
'abc',
['false'],
{'true': 'true'},
2,
-1,
])
def test_boolean_property_invalid(value):
bool_prop = BooleanProperty()
with pytest.raises(ValueError):
2017-04-17 21:13:11 +02:00
bool_prop.clean(value)
def test_reference_property():
ref_prop = ReferenceProperty()
2017-04-17 21:13:11 +02:00
assert ref_prop.clean("my-type--3a331bfe-0566-55e1-a4a0-9a2cd355a300")
with pytest.raises(ValueError):
2017-04-17 21:13:11 +02:00
ref_prop.clean("foo")
2017-04-11 18:10:55 +02:00
@pytest.mark.parametrize("value", [
'2017-01-01T12:34:56Z',
'2017-01-01 12:34:56',
'Jan 1 2017 12:34:56',
])
def test_timestamp_property_valid(value):
ts_prop = TimestampProperty()
2017-04-17 21:13:11 +02:00
assert ts_prop.clean(value) == FAKE_TIME
2017-04-11 18:10:55 +02:00
def test_timestamp_property_invalid():
ts_prop = TimestampProperty()
with pytest.raises(ValueError):
2017-04-17 21:13:11 +02:00
ts_prop.clean(1)
2017-04-11 18:10:55 +02:00
with pytest.raises(ValueError):
2017-04-17 21:13:11 +02:00
ts_prop.clean("someday sometime")