Update test modules

stix2.0
Emmanuelle Vargas-Gonzalez 2017-11-17 08:50:40 -05:00
parent e6a8b555d3
commit c03ecb5230
2 changed files with 53 additions and 6 deletions

View File

@ -94,7 +94,7 @@ def test_custom_property_in_bundled_object():
assert '"x_foo": "bar"' in str(bundle) assert '"x_foo": "bar"' in str(bundle)
def test_custom_marking_no_init(): def test_custom_marking_no_init_1():
@stix2.CustomMarking('x-new-obj', [ @stix2.CustomMarking('x-new-obj', [
('property1', stix2.properties.StringProperty(required=True)), ('property1', stix2.properties.StringProperty(required=True)),
]) ])
@ -104,6 +104,8 @@ def test_custom_marking_no_init():
no = NewObj(property1='something') no = NewObj(property1='something')
assert no.property1 == 'something' assert no.property1 == 'something'
def test_custom_marking_no_init_2():
@stix2.CustomMarking('x-new-obj2', [ @stix2.CustomMarking('x-new-obj2', [
('property1', stix2.properties.StringProperty(required=True)), ('property1', stix2.properties.StringProperty(required=True)),
]) ])
@ -122,6 +124,15 @@ class NewType(object):
def __init__(self, property2=None, **kwargs): def __init__(self, property2=None, **kwargs):
if property2 and property2 < 10: if property2 and property2 < 10:
raise ValueError("'property2' is too small.") raise ValueError("'property2' is too small.")
if "property3" in kwargs and not isinstance(kwargs.get("property3"), int):
raise TypeError("Must be integer!")
def test_custom_object_raises_exception():
with pytest.raises(TypeError) as excinfo:
NewType(property1='something', property3='something', allow_custom=True)
assert str(excinfo.value) == "Must be integer!"
def test_custom_object_type(): def test_custom_object_type():
@ -137,7 +148,7 @@ def test_custom_object_type():
assert "'property2' is too small." in str(excinfo.value) assert "'property2' is too small." in str(excinfo.value)
def test_custom_object_no_init(): def test_custom_object_no_init_1():
@stix2.sdo.CustomObject('x-new-obj', [ @stix2.sdo.CustomObject('x-new-obj', [
('property1', stix2.properties.StringProperty(required=True)), ('property1', stix2.properties.StringProperty(required=True)),
]) ])
@ -147,6 +158,8 @@ def test_custom_object_no_init():
no = NewObj(property1='something') no = NewObj(property1='something')
assert no.property1 == 'something' assert no.property1 == 'something'
def test_custom_object_no_init_2():
@stix2.sdo.CustomObject('x-new-obj2', [ @stix2.sdo.CustomObject('x-new-obj2', [
('property1', stix2.properties.StringProperty(required=True)), ('property1', stix2.properties.StringProperty(required=True)),
]) ])
@ -190,23 +203,36 @@ class NewObservable():
def __init__(self, property2=None, **kwargs): def __init__(self, property2=None, **kwargs):
if property2 and property2 < 10: if property2 and property2 < 10:
raise ValueError("'property2' is too small.") raise ValueError("'property2' is too small.")
if "property3" in kwargs and not isinstance(kwargs.get("property3"), int):
raise TypeError("Must be integer!")
def test_custom_observable_object(): def test_custom_observable_object_1():
no = NewObservable(property1='something') no = NewObservable(property1='something')
assert no.property1 == 'something' assert no.property1 == 'something'
def test_custom_observable_object_2():
with pytest.raises(stix2.exceptions.MissingPropertiesError) as excinfo: with pytest.raises(stix2.exceptions.MissingPropertiesError) as excinfo:
NewObservable(property2=42) NewObservable(property2=42)
assert excinfo.value.properties == ['property1'] assert excinfo.value.properties == ['property1']
assert "No values for required properties" in str(excinfo.value) assert "No values for required properties" in str(excinfo.value)
def test_custom_observable_object_3():
with pytest.raises(ValueError) as excinfo: with pytest.raises(ValueError) as excinfo:
NewObservable(property1='something', property2=4) NewObservable(property1='something', property2=4)
assert "'property2' is too small." in str(excinfo.value) assert "'property2' is too small." in str(excinfo.value)
def test_custom_observable_object_no_init(): def test_custom_observable_raises_exception():
with pytest.raises(TypeError) as excinfo:
NewObservable(property1='something', property3='something', allow_custom=True)
assert str(excinfo.value) == "Must be integer!"
def test_custom_observable_object_no_init_1():
@stix2.observables.CustomObservable('x-new-observable', [ @stix2.observables.CustomObservable('x-new-observable', [
('property1', stix2.properties.StringProperty()), ('property1', stix2.properties.StringProperty()),
]) ])
@ -216,6 +242,8 @@ def test_custom_observable_object_no_init():
no = NewObs(property1='something') no = NewObs(property1='something')
assert no.property1 == 'something' assert no.property1 == 'something'
def test_custom_observable_object_no_init_2():
@stix2.observables.CustomObservable('x-new-obs2', [ @stix2.observables.CustomObservable('x-new-obs2', [
('property1', stix2.properties.StringProperty()), ('property1', stix2.properties.StringProperty()),
]) ])
@ -374,6 +402,15 @@ class NewExtension():
def __init__(self, property2=None, **kwargs): def __init__(self, property2=None, **kwargs):
if property2 and property2 < 10: if property2 and property2 < 10:
raise ValueError("'property2' is too small.") raise ValueError("'property2' is too small.")
if "property3" in kwargs and not isinstance(kwargs.get("property3"), int):
raise TypeError("Must be integer!")
def test_custom_extension_raises_exception():
with pytest.raises(TypeError) as excinfo:
NewExtension(property1='something', property3='something', allow_custom=True)
assert str(excinfo.value) == "Must be integer!"
def test_custom_extension(): def test_custom_extension():
@ -453,7 +490,7 @@ def test_custom_extension_empty_properties():
assert "'properties' must be a dict!" in str(excinfo.value) assert "'properties' must be a dict!" in str(excinfo.value)
def test_custom_extension_no_init(): def test_custom_extension_no_init_1():
@stix2.observables.CustomExtension(stix2.DomainName, 'x-new-extension', { @stix2.observables.CustomExtension(stix2.DomainName, 'x-new-extension', {
'property1': stix2.properties.StringProperty(required=True), 'property1': stix2.properties.StringProperty(required=True),
}) })
@ -463,6 +500,8 @@ def test_custom_extension_no_init():
ne = NewExt(property1="foobar") ne = NewExt(property1="foobar")
assert ne.property1 == "foobar" assert ne.property1 == "foobar"
def test_custom_extension_no_init_2():
@stix2.observables.CustomExtension(stix2.DomainName, 'x-new-ext2', { @stix2.observables.CustomExtension(stix2.DomainName, 'x-new-ext2', {
'property1': stix2.properties.StringProperty(required=True), 'property1': stix2.properties.StringProperty(required=True),
}) })

View File

@ -187,7 +187,8 @@ def test_parse_marking_definition(data):
]) ])
class NewMarking(object): class NewMarking(object):
def __init__(self, property2=None, **kwargs): def __init__(self, property2=None, **kwargs):
return if "property3" in kwargs and not isinstance(kwargs.get("property3"), int):
raise TypeError("Must be integer!")
def test_registered_custom_marking(): def test_registered_custom_marking():
@ -208,6 +209,13 @@ def test_registered_custom_marking():
assert marking_def.definition_type == "x-new-marking-type" assert marking_def.definition_type == "x-new-marking-type"
def test_registered_custom_marking_raises_exception():
with pytest.raises(TypeError) as excinfo:
NewMarking(property1='something', property3='something', allow_custom=True)
assert str(excinfo.value) == "Must be integer!"
def test_not_registered_marking_raises_exception(): def test_not_registered_marking_raises_exception():
with pytest.raises(ValueError) as excinfo: with pytest.raises(ValueError) as excinfo:
# Used custom object on purpose to demonstrate a not-registered marking # Used custom object on purpose to demonstrate a not-registered marking