Add a deterministic ID unit test with an embedded object

pull/1/head
Michael Chisholm 2020-06-02 19:55:36 -04:00
parent fa981affa9
commit c94feae623
1 changed files with 40 additions and 3 deletions

View File

@ -7,9 +7,9 @@ import six
import stix2.base import stix2.base
import stix2.canonicalization.Canonicalize import stix2.canonicalization.Canonicalize
from stix2.properties import ( from stix2.properties import (
BooleanProperty, DictionaryProperty, ExtensionsProperty, FloatProperty, BooleanProperty, DictionaryProperty, EmbeddedObjectProperty,
IDProperty, IntegerProperty, ListProperty, StringProperty, ExtensionsProperty, FloatProperty, IDProperty, IntegerProperty,
TimestampProperty, TypeProperty, ListProperty, StringProperty, TimestampProperty, TypeProperty,
) )
import stix2.v21.base import stix2.v21.base
@ -151,3 +151,40 @@ def test_json_incompatible_timestamp_value():
actual_uuid5 = _uuid_from_id(sco["id"]) actual_uuid5 = _uuid_from_id(sco["id"])
assert actual_uuid5 == expected_uuid5 assert actual_uuid5 == expected_uuid5
def test_embedded_object():
class SubObj(stix2.base._STIXBase):
_type = "sub-object"
_properties = OrderedDict((
('value', StringProperty()),
))
class SomeSCO(stix2.v21.base._Observable):
_type = "some-sco"
_properties = OrderedDict((
('type', TypeProperty(_type, spec_version='2.1')),
('id', IDProperty(_type, spec_version='2.1')),
(
'extensions', ExtensionsProperty(
spec_version='2.1', enclosing_type=_type,
),
),
('sub_obj', EmbeddedObjectProperty(type=SubObj)),
))
_id_contributing_properties = ['sub_obj']
sub_obj = SubObj(value="foo")
sco = SomeSCO(sub_obj=sub_obj)
obj = {
"sub_obj": {
"value": "foo",
},
}
can_json = stix2.canonicalization.Canonicalize.canonicalize(obj, utf8=False)
expected_uuid5 = _make_uuid5(can_json)
actual_uuid5 = _uuid_from_id(sco["id"])
assert actual_uuid5 == expected_uuid5