def test_get_buckets(self): # When no buckets have been set, it raises an error. assert_raises_regexp( CannotLoadConfiguration, 'have not been initialized and no database session', S3Uploader.get_bucket, S3Uploader.OA_CONTENT_BUCKET_KEY) # So let's use an ExternalIntegration to set some buckets. integration = self._external_integration( ExternalIntegration.S3, goal=ExternalIntegration.STORAGE_GOAL, username='******', password='******', settings={ S3Uploader.OA_CONTENT_BUCKET_KEY: 'banana', S3Uploader.BOOK_COVERS_BUCKET_KEY: 'bucket' }) # If an object from the database is given, the buckets # will be initialized, even though they hadn't been yet. identifier = self._identifier() result = S3Uploader.get_bucket(S3Uploader.OA_CONTENT_BUCKET_KEY, sessioned_object=identifier) eq_('banana', result) # Generating the S3Uploader from_config also gives us S3 buckets. S3Uploader.__buckets__ = S3Uploader.UNINITIALIZED_BUCKETS S3Uploader.from_config(self._db) eq_('bucket', S3Uploader.get_bucket(S3Uploader.BOOK_COVERS_BUCKET_KEY)) # Despite our new buckets, if a requested bucket isn't set, # an error ir raised. assert_raises_regexp(CannotLoadConfiguration, 'No S3 bucket found', S3Uploader.get_bucket, 'nonexistent_bucket_key')
def test_from_config(self): # If there's no configuration for S3, S3Uploader.from_config # raises an exception. assert_raises_regexp(CannotLoadConfiguration, 'Required S3 integration is not configured', S3Uploader.from_config, self._db) # If there is a configuration but it's misconfigured, an error # is raised. integration = self._external_integration( ExternalIntegration.S3, goal=ExternalIntegration.STORAGE_GOAL) assert_raises_regexp(CannotLoadConfiguration, 'without both access_key and secret_key', S3Uploader.from_config, self._db) # Otherwise, it builds just fine. integration.username = '******' integration.password = '******' uploader = S3Uploader.from_config(self._db) eq_(True, isinstance(uploader, S3Uploader)) # Well, unless there are multiple S3 integrations, and it # doesn't know which one to choose! duplicate = self._external_integration(ExternalIntegration.S3) duplicate.goal = ExternalIntegration.STORAGE_GOAL assert_raises_regexp(CannotLoadConfiguration, 'Multiple S3 ExternalIntegrations configured', S3Uploader.from_config, self._db)