Add created_by() function to Environment
parent
09ef9353fc
commit
e1e368c0d2
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue