From 367b485fcd6dd96b1277430c16eafc5cf22ba96e Mon Sep 17 00:00:00 2001 From: Emmanuelle Vargas-Gonzalez Date: Fri, 15 Jan 2021 16:01:36 -0500 Subject: [PATCH] write some tests to check new MarkingDefinition constraints --- stix2/test/v21/test_marking_definition.py | 28 ++++++++++++++++++++++- stix2/v21/common.py | 13 ++++------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/stix2/test/v21/test_marking_definition.py b/stix2/test/v21/test_marking_definition.py index 232bdf2..7b4fc62 100644 --- a/stix2/test/v21/test_marking_definition.py +++ b/stix2/test/v21/test_marking_definition.py @@ -1,7 +1,7 @@ import pytest -from stix2 import exceptions +from stix2 import exceptions, parse from stix2.v21 import ( TLP_AMBER, TLP_GREEN, TLP_RED, TLP_WHITE, MarkingDefinition, TLPMarking, ) @@ -143,3 +143,29 @@ def test_unknown_tlp_marking(): definition_type='tlp', definition=TLPMarking(tlp='gray'), ) + + +def test_marking_definition_missing_definition(): + my_favorite_marking = { + 'type': 'marking-definition', + 'spec_version': '2.1', + 'id': 'marking-definition--f9dbe89c-0030-4a9d-8b78-0dcd0a0de874', + 'name': 'This is the name of my favorite Marking', + 'definition_type': 'foobar' + } + with pytest.raises(exceptions.PropertyPresenceError): + parse(my_favorite_marking) + + +def test_marking_definition_missing_definition_type(): + my_favorite_marking = { + 'type': 'marking-definition', + 'spec_version': '2.1', + 'id': 'marking-definition--f9dbe89c-0030-4a9d-8b78-0dcd0a0de874', + 'name': 'This is the name of my favorite Marking', + 'definition': { + 'some_type': 'foobar' + } + } + with pytest.raises(exceptions.InvalidValueError): + parse(my_favorite_marking) diff --git a/stix2/v21/common.py b/stix2/v21/common.py index 308cdf2..bf3aa0f 100644 --- a/stix2/v21/common.py +++ b/stix2/v21/common.py @@ -226,16 +226,11 @@ class MarkingDefinition(_STIXBase21, _MarkingsMixin): definition = self.get("definition") definition_type = self.get("definition_type") extensions = self.get("extensions") - if not extensions and not definition: + + if not (definition_type and definition) and not extensions: raise PropertyPresenceError( - "MarkingDefinition objects must have the property 'definition' " - "if 'extensions' is not present", - MarkingDefinition, - ) - if not extensions and not definition_type: - raise PropertyPresenceError( - "MarkingDefinition objects must have the property 'definition_type' " - "if 'extensions' is not present", + "MarkingDefinition objects must have the properties " + "'definition_type' and 'definition' if 'extensions' is not present", MarkingDefinition, )