write some tests to check new MarkingDefinition constraints

pull/1/head
Emmanuelle Vargas-Gonzalez 2021-01-15 16:01:36 -05:00
parent b696f14560
commit 367b485fcd
2 changed files with 31 additions and 10 deletions

View File

@ -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)

View File

@ -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,
)