From 5dffe74867362566e60bfa973010220f704e009d Mon Sep 17 00:00:00 2001 From: Chris Lenk Date: Tue, 24 Oct 2017 14:20:42 -0400 Subject: [PATCH] Clean up creator_of (renamed from created_by) --- stix2/environment.py | 13 +++++++++---- stix2/sources/__init__.py | 2 ++ stix2/sources/memory.py | 10 +++++++--- stix2/test/test_environment.py | 16 +++++++++++++--- 4 files changed, 31 insertions(+), 10 deletions(-) diff --git a/stix2/environment.py b/stix2/environment.py index bd50b7f..4919335 100644 --- a/stix2/environment.py +++ b/stix2/environment.py @@ -153,7 +153,7 @@ class Environment(object): return _parse(*args, **kwargs) parse.__doc__ = _parse.__doc__ - def created_by(self, obj): + def creator_of(self, obj): """Retrieve the Identity refered to by the object's `created_by_ref`. Args: @@ -161,8 +161,13 @@ class Environment(object): up. Returns: - The STIX object's creator. - """ + The STIX object's creator, or + None, if the object contains no `created_by_ref` property or the + object's creator cannot be found. + """ creator_id = obj.get('created_by_ref', '') - return self.get(creator_id) + if creator_id: + return self.get(creator_id) + else: + return None diff --git a/stix2/sources/__init__.py b/stix2/sources/__init__.py index 9d46ba9..49cb3f3 100644 --- a/stix2/sources/__init__.py +++ b/stix2/sources/__init__.py @@ -266,6 +266,8 @@ class CompositeDataSource(DataSource): # remove duplicate versions if len(all_data) > 0: all_data = deduplicate(all_data) + else: + return None # reduce to most recent version stix_obj = sorted(all_data, key=lambda k: k['modified'], reverse=True)[0] diff --git a/stix2/sources/memory.py b/stix2/sources/memory.py index 0d5901e..967b886 100644 --- a/stix2/sources/memory.py +++ b/stix2/sources/memory.py @@ -215,10 +215,14 @@ class MemorySource(DataSource): all_data = self.query(query=query, _composite_filters=_composite_filters) - # reduce to most recent version - stix_obj = sorted(all_data, key=lambda k: k['modified'])[0] + if all_data: + print(all_data) + # reduce to most recent version + stix_obj = sorted(all_data, key=lambda k: k['modified'])[0] - return stix_obj + return stix_obj + else: + return None def all_versions(self, stix_id, _composite_filters=None): """retrieve STIX objects from in-memory dict via STIX ID, all versions of it diff --git a/stix2/test/test_environment.py b/stix2/test/test_environment.py index 5758732..c669a33 100644 --- a/stix2/test/test_environment.py +++ b/stix2/test/test_environment.py @@ -193,8 +193,8 @@ def test_created_by(): env.add(identity) ind = env.create(stix2.Indicator, **INDICATOR_KWARGS) - creator = env.created_by(ind) - assert creator.id == identity.id + creator = env.creator_of(ind) + assert creator is identity def test_created_by_no_datasource(): @@ -204,5 +204,15 @@ def test_created_by_no_datasource(): ind = env.create(stix2.Indicator, **INDICATOR_KWARGS) with pytest.raises(AttributeError) as excinfo: - env.created_by(ind) + env.creator_of(ind) assert 'Environment has no data source' in str(excinfo.value) + + +def test_created_by_not_found(): + identity = stix2.Identity(**IDENTITY_KWARGS) + factory = stix2.ObjectFactory(created_by_ref=identity.id) + env = stix2.Environment(store=stix2.MemoryStore(), factory=factory) + + ind = env.create(stix2.Indicator, **INDICATOR_KWARGS) + creator = env.creator_of(ind) + assert creator is None