From c03ecb5230645d7e6bf27cfa032b94863d43a3eb Mon Sep 17 00:00:00 2001 From: Emmanuelle Vargas-Gonzalez Date: Fri, 17 Nov 2017 08:50:40 -0500 Subject: [PATCH] Update test modules --- stix2/test/test_custom.py | 49 +++++++++++++++++++++++++++++++++---- stix2/test/test_markings.py | 10 +++++++- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/stix2/test/test_custom.py b/stix2/test/test_custom.py index ecee1cd..7c1832b 100644 --- a/stix2/test/test_custom.py +++ b/stix2/test/test_custom.py @@ -94,7 +94,7 @@ def test_custom_property_in_bundled_object(): 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', [ ('property1', stix2.properties.StringProperty(required=True)), ]) @@ -104,6 +104,8 @@ def test_custom_marking_no_init(): no = NewObj(property1='something') assert no.property1 == 'something' + +def test_custom_marking_no_init_2(): @stix2.CustomMarking('x-new-obj2', [ ('property1', stix2.properties.StringProperty(required=True)), ]) @@ -122,6 +124,15 @@ class NewType(object): def __init__(self, property2=None, **kwargs): if property2 and property2 < 10: 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(): @@ -137,7 +148,7 @@ def test_custom_object_type(): 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', [ ('property1', stix2.properties.StringProperty(required=True)), ]) @@ -147,6 +158,8 @@ def test_custom_object_no_init(): no = NewObj(property1='something') assert no.property1 == 'something' + +def test_custom_object_no_init_2(): @stix2.sdo.CustomObject('x-new-obj2', [ ('property1', stix2.properties.StringProperty(required=True)), ]) @@ -190,23 +203,36 @@ class NewObservable(): def __init__(self, property2=None, **kwargs): if property2 and property2 < 10: 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') assert no.property1 == 'something' + +def test_custom_observable_object_2(): with pytest.raises(stix2.exceptions.MissingPropertiesError) as excinfo: NewObservable(property2=42) assert excinfo.value.properties == ['property1'] assert "No values for required properties" in str(excinfo.value) + +def test_custom_observable_object_3(): with pytest.raises(ValueError) as excinfo: NewObservable(property1='something', property2=4) 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', [ ('property1', stix2.properties.StringProperty()), ]) @@ -216,6 +242,8 @@ def test_custom_observable_object_no_init(): no = NewObs(property1='something') assert no.property1 == 'something' + +def test_custom_observable_object_no_init_2(): @stix2.observables.CustomObservable('x-new-obs2', [ ('property1', stix2.properties.StringProperty()), ]) @@ -374,6 +402,15 @@ class NewExtension(): def __init__(self, property2=None, **kwargs): if property2 and property2 < 10: 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(): @@ -453,7 +490,7 @@ def test_custom_extension_empty_properties(): 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', { 'property1': stix2.properties.StringProperty(required=True), }) @@ -463,6 +500,8 @@ def test_custom_extension_no_init(): ne = NewExt(property1="foobar") assert ne.property1 == "foobar" + +def test_custom_extension_no_init_2(): @stix2.observables.CustomExtension(stix2.DomainName, 'x-new-ext2', { 'property1': stix2.properties.StringProperty(required=True), }) diff --git a/stix2/test/test_markings.py b/stix2/test/test_markings.py index 456bf92..d2271f0 100644 --- a/stix2/test/test_markings.py +++ b/stix2/test/test_markings.py @@ -187,7 +187,8 @@ def test_parse_marking_definition(data): ]) class NewMarking(object): 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(): @@ -208,6 +209,13 @@ def test_registered_custom_marking(): 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(): with pytest.raises(ValueError) as excinfo: # Used custom object on purpose to demonstrate a not-registered marking