Skip to content

Commit

Permalink
Add a deterministic ID unit test with an embedded object
Browse files Browse the repository at this point in the history
  • Loading branch information
chisholm committed Jun 2, 2020
1 parent f49e230 commit 4d44d26
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions stix2/test/v21/test_deterministic_ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import stix2.base
import stix2.canonicalization.Canonicalize
from stix2.properties import (
BooleanProperty, DictionaryProperty, ExtensionsProperty, FloatProperty,
IDProperty, IntegerProperty, ListProperty, StringProperty,
TimestampProperty, TypeProperty,
BooleanProperty, DictionaryProperty, EmbeddedObjectProperty,
ExtensionsProperty, FloatProperty, IDProperty, IntegerProperty,
ListProperty, StringProperty, TimestampProperty, TypeProperty,
)
import stix2.v21.base

Expand Down Expand Up @@ -151,3 +151,40 @@ class SomeSCO(stix2.v21.base._Observable):
actual_uuid5 = _uuid_from_id(sco["id"])

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

0 comments on commit 4d44d26

Please sign in to comment.