def test_list_property_property_type_custom(): class TestObj(stix2.base._STIXBase): _type = "test" _properties = { "foo": StringProperty(), } p = ListProperty(EmbeddedObjectProperty(type=TestObj)) objs_custom = [ TestObj(foo="abc", bar=123, allow_custom=True), TestObj(foo="xyz"), ] assert p.clean(objs_custom) dicts_custom = [ { "foo": "abc", "bar": 123 }, { "foo": "xyz" }, ] # no opportunity to set allow_custom=True when using dicts with pytest.raises(ExtraPropertiesError): p.clean(dicts_custom)
def test_list_property_property_type(): p = ListProperty(StringProperty) result = p.clean(['abc', 'xyz'], False) assert result == (['abc', 'xyz'], False) with pytest.raises(ValueError): p.clean([], False)
def test_list_property_bad_value_type(): class TestObj(stix2.base._STIXBase): _type = "test" _properties = { "foo": StringProperty(), } list_prop = ListProperty(TestObj) with pytest.raises(ValueError): list_prop.clean([1])
def test_list_property_object_type(): class TestObj(stix2.base._STIXBase): _type = "test" _properties = { "foo": StringProperty(), } p = ListProperty(TestObj) objs = [TestObj(foo="abc"), TestObj(foo="xyz")] assert p.clean(objs) dicts = [{"foo": "abc"}, {"foo": "xyz"}] assert p.clean(dicts)
def test_list_property_object_type(): class TestObj(_STIXBase): _type = "test" _properties = { "foo": StringProperty(), } p = ListProperty(TestObj) objs = [TestObj(foo="abc"), TestObj(foo="xyz")] result = p.clean(objs, False) assert result == (objs, False) dicts = [{"foo": "abc"}, {"foo": "xyz"}] result = p.clean(dicts, False) assert result == (objs, False)
def test_list_property_property_type_custom(): class TestObj(_STIXBase): _type = "test" _properties = { "foo": StringProperty(), } p = ListProperty(EmbeddedObjectProperty(type=TestObj)) objs_custom = [ TestObj(foo="abc", bar=123, allow_custom=True), TestObj(foo="xyz"), ] result = p.clean(objs_custom, True) assert result == (objs_custom, True) with pytest.raises(CustomContentError): p.clean(objs_custom, False) dicts_custom = [ { "foo": "abc", "bar": 123 }, { "foo": "xyz" }, ] result = p.clean(dicts_custom, True) assert result == (objs_custom, True) with pytest.raises(ExtraPropertiesError): p.clean(dicts_custom, False)
def test_list_property(): p = ListProperty(StringProperty) assert p.clean(['abc', 'xyz']) with pytest.raises(ValueError): p.clean([])