def test_resource_path_altered_correctly(self, mock_settings,
                                             mock_service_account,
                                             mock_storage, mock_os_exists):

        resources = Resource.objects.filter(owner=self.regular_user_1)
        r = resources[0]
        original_path = r.path
        owner_uuid = self.regular_user_1.pk
        expected_basename = '{uuid}.{name}'.format(
            uuid=str(r.pk), name=os.path.basename(original_path))

        os.environ['STORAGE_BUCKET_NAME'] = DUMMY_BUCKETNAME
        storage_backend = GoogleBucketStorage()
        mock_bucket = mock.MagicMock()
        mock_upload_blob = mock.MagicMock()
        storage_backend.get_or_create_bucket = mock.MagicMock()
        storage_backend.get_or_create_bucket.return_value = mock_bucket
        storage_backend.upload_blob = mock_upload_blob
        path = storage_backend.store(r)

        mock_os_exists.return_value = True

        mock_upload_blob.assert_called()
        storage_backend.get_or_create_bucket.assert_called()
        expected_destination = os.path.join( GoogleBucketStorage.BUCKET_PREFIX, \
            DUMMY_BUCKETNAME, \
            Resource.USER_RESOURCE_STORAGE_DIRNAME, \
            str(owner_uuid), expected_basename)
        self.assertEqual(path, expected_destination)
    def test_bucket_transfer_call_case3(self, mock_settings,
                                        mock_service_account, mock_storage,
                                        mock_os_exists):
        '''
        If an analysis is performed remotely (so that files are located in 
        bucket storage) and the storage backend is also bucket-based, we need to 
        perform an inter-bucket transfer.

        That interbucket method can fail for various reasons. If an unexpected
        error occurs, the interbucket transfer function will raise a general
        Exception
        '''
        resources = Resource.objects.filter(owner=self.regular_user_1)
        r = resources[0]
        original_path = r.path
        owner_uuid = self.regular_user_1.pk
        expected_basename = '{uuid}.{name}'.format(
            uuid=str(r.pk), name=os.path.basename(original_path))

        os.environ['STORAGE_BUCKET_NAME'] = DUMMY_BUCKETNAME
        mock_settings.STORAGE_CREDENTIALS = '/some/dummy/path'
        storage_backend = GoogleBucketStorage()
        mock_bucket = mock.MagicMock()
        mock_upload_blob = mock.MagicMock()
        mock_interbucket_transfer = mock.MagicMock()
        storage_backend.get_or_create_bucket = mock.MagicMock()
        storage_backend.get_or_create_bucket.return_value = mock_bucket
        storage_backend.upload_blob = mock_upload_blob
        mock_interbucket_transfer.side_effect = [Exception('!!!')]
        storage_backend.perform_interbucket_transfer = mock_interbucket_transfer

        # If this is False, then the Resource does not exist on the local filesystem.
        # This is what triggers the alternative behavior of performing an interbucket
        # transfer
        mock_os_exists.return_value = False

        with self.assertRaises(Exception) as ex:
            storage_backend.store(r)

        mock_upload_blob.assert_not_called()
        mock_interbucket_transfer.assert_called()
        storage_backend.get_or_create_bucket.assert_called()
    def test_bucket_transfer_call(self, mock_settings, mock_service_account,
                                  mock_storage, mock_os_exists):
        '''
        If an analysis is performed remotely (so that files are located in 
        bucket storage) and the storage backend is also bucket-based, we need to 
        perform an inter-bucket transfer. Test that the proper calls are made
        '''
        resources = Resource.objects.filter(owner=self.regular_user_1)
        r = resources[0]
        original_path = r.path
        owner_uuid = self.regular_user_1.pk
        expected_basename = '{uuid}.{name}'.format(
            uuid=str(r.pk), name=os.path.basename(original_path))

        os.environ['STORAGE_BUCKET_NAME'] = DUMMY_BUCKETNAME
        mock_settings.STORAGE_CREDENTIALS = '/some/dummy/path'
        storage_backend = GoogleBucketStorage()
        mock_bucket = mock.MagicMock()
        mock_upload_blob = mock.MagicMock()
        mock_interbucket_transfer = mock.MagicMock()
        storage_backend.get_or_create_bucket = mock.MagicMock()
        storage_backend.get_or_create_bucket.return_value = mock_bucket
        storage_backend.upload_blob = mock_upload_blob
        storage_backend.perform_interbucket_transfer = mock_interbucket_transfer

        # If this is False, then the Resource does not exist on the local filesystem.
        # This is what triggers the alternative behavior of performing an interbucket
        # transfer
        mock_os_exists.return_value = False

        path = storage_backend.store(r)

        mock_upload_blob.assert_not_called()
        mock_interbucket_transfer.assert_called()
        storage_backend.get_or_create_bucket.assert_called()
        expected_destination = os.path.join( GoogleBucketStorage.BUCKET_PREFIX, \
            DUMMY_BUCKETNAME, \
            Resource.USER_RESOURCE_STORAGE_DIRNAME, \
            str(owner_uuid), expected_basename)
        self.assertEqual(path, expected_destination)