Add created_by() function to Environment

stix2.0
Chris Lenk 2017-10-20 09:13:04 -04:00
parent 09ef9353fc
commit e1e368c0d2
2 changed files with 36 additions and 0 deletions

View File

@ -152,3 +152,17 @@ class Environment(object):
def parse(self, *args, **kwargs):
return _parse(*args, **kwargs)
parse.__doc__ = _parse.__doc__
def created_by(self, obj):
"""Retrieve the Identity refered to by the object's `created_by_ref`.
Args:
obj: The STIX object whose `created_by_ref` property will be looked
up.
Returns:
The STIX object's creator.
"""
creator_id = obj.get('created_by_ref', '')
return self.get(creator_id)

View File

@ -184,3 +184,25 @@ def test_parse_malware():
assert mal.modified == FAKE_TIME
assert mal.labels == ['ransomware']
assert mal.name == "Cryptolocker"
def test_created_by():
identity = stix2.Identity(**IDENTITY_KWARGS)
factory = stix2.ObjectFactory(created_by_ref=identity.id)
env = stix2.Environment(store=stix2.MemoryStore(), factory=factory)
env.add(identity)
ind = env.create(stix2.Indicator, **INDICATOR_KWARGS)
creator = env.created_by(ind)
assert creator.id == identity.id
def test_created_by_no_datasource():
identity = stix2.Identity(**IDENTITY_KWARGS)
factory = stix2.ObjectFactory(created_by_ref=identity.id)
env = stix2.Environment(factory=factory)
ind = env.create(stix2.Indicator, **INDICATOR_KWARGS)
with pytest.raises(AttributeError) as excinfo:
env.created_by(ind)
assert 'Environment has no data source' in str(excinfo.value)