From 0d3f80f2fec627ca00e72d031c15d38f2352e1a6 Mon Sep 17 00:00:00 2001 From: = Date: Tue, 15 May 2018 15:41:46 -0400 Subject: [PATCH] removing taxii 503 error test as not reproducible in Travis environment --- stix2/datastore/taxii.py | 57 ++++++++++++++---------------- stix2/test/test_datastore_taxii.py | 9 ----- 2 files changed, 27 insertions(+), 39 deletions(-) diff --git a/stix2/datastore/taxii.py b/stix2/datastore/taxii.py index e3f4e45..776baf7 100644 --- a/stix2/datastore/taxii.py +++ b/stix2/datastore/taxii.py @@ -9,6 +9,13 @@ from stix2.datastore import DataSink, DataSource, DataStoreMixin from stix2.datastore.filters import Filter, FilterSet, apply_common_filters from stix2.utils import deduplicate +try: + from taxii2client import ValidationError + _taxii2_client = True +except ImportError: + _taxii2_client = False + + TAXII_FILTERS = ['added_after', 'id', 'type', 'version'] @@ -51,27 +58,22 @@ class TAXIICollectionSink(DataSink): """ def __init__(self, collection, allow_custom=False): super(TAXIICollectionSink, self).__init__() - try: - # we have to execute .can_write first in isolation because the - # attribute access could trigger a taxii2client.ValidationError which - # we catch here as a ValueError (its parent class). Later, we need to - # have the ability to also raise a different ValueError based on the - # value of .can_write - writeable = collection.can_write + if not _taxii2_client: + raise ImportError("taxii2client library is required for usage of TAXIICollectionSink") - except (HTTPError, ValueError) as e: + try: + if collection.can_write: + self.collection = collection + else: + raise ValueError("The TAXII Collection object provided does not have write access" + " to the underlying linked Collection resource") + + except (HTTPError, ValidationError) as e: e.message = ("The underlying TAXII Collection resource defined in the supplied TAXII" " Collection object provided could not be reached. TAXII Collection Error: " + e.message) raise - if writeable: - # now past taxii2client possible exceptions, check value for local exceptions - self.collection = collection - else: - raise ValueError("The TAXII Collection object provided does not have write access" - " to the underlying linked Collection resource") - self.allow_custom = allow_custom def add(self, stix_data, version=None): @@ -131,27 +133,22 @@ class TAXIICollectionSource(DataSource): """ def __init__(self, collection, allow_custom=True): super(TAXIICollectionSource, self).__init__() - try: - # we have to execute .can_read first in isolation because the - # attribute access could trigger a taxii2client.ValidationError which - # we catch here as a ValueError (its parent class). Later, we need to - # have the ability to also raise a different ValueError based on the - # value of .can_read - writeable = collection.can_read + if not _taxii2_client: + raise ImportError("taxii2client library is required for usage of TAXIICollectionSource") - except (HTTPError, ValueError) as e: + try: + if collection.can_read: + self.collection = collection + else: + raise ValueError("The TAXII Collection object provided does not have read access" + " to the underlying linked Collection resource") + + except (HTTPError, ValidationError) as e: e.message = ("The underlying TAXII Collection resource defined in the supplied TAXII" " Collection object provided could not be reached. TAXII Collection Error: " + e.message) raise - if writeable: - # now past taxii2client possible exceptions, check value for local exceptions - self.collection = collection - else: - raise ValueError("The TAXII Collection object provided does not have read access" - " to the underlying linked Collection resource") - self.allow_custom = allow_custom def get(self, stix_id, version=None, _composite_filters=None): diff --git a/stix2/test/test_datastore_taxii.py b/stix2/test/test_datastore_taxii.py index c753afc..af8fecd 100644 --- a/stix2/test/test_datastore_taxii.py +++ b/stix2/test/test_datastore_taxii.py @@ -339,15 +339,6 @@ def test_can_write_error(collection_no_rw_access): assert "Collection object provided does not have write access" in str(excinfo.value) -def test_bad_collection(): - """this triggers a real connectivity issue (HTTPError: 503 ServerError) """ - with pytest.raises(HTTPError) as excinfo: - mock = MockTAXIICollectionEndpoint("http://doenstexist118482.org", verify=False) - TAXIICollectionStore(mock) - assert "Collection object provided could not be reached. TAXII Collection Error:" in str(excinfo.value.message) - assert "HTTPError" in str(excinfo.type) - - def test_get_404(collection): """a TAXIICollectionSource.get() call that receives an HTTP 404 response code from the taxii2client should be be returned as None.