Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_load_from_file_creates_intermediate_dirs(
self, mock_exists: mock.Mock, mock_makedirs: mock.Mock, mock_copy: mock.Mock) -> None:
mock_exists.return_value = False
storage = get_storage("file:///foo/bar/file")
storage.load_from_filename("input_file")
mock_exists.assert_called_with("/foo/bar")
mock_makedirs.assert_called_with("/foo/bar")
mock_copy.assert_called_with("input_file", "/foo/bar/file")
def test_save_to_filename_raises_internal_server_exception(self) -> None:
self.auth_failure = "500 Internal Server Error"
self.add_container_object("/v2.0/1234/CONTAINER", "/path/to/file.mp4", b"FOOBAR")
swift_uri = self._generate_storage_uri("/path/to/file.mp4")
storage_object = get_storage(swift_uri)
tmp_file = tempfile.NamedTemporaryFile()
with self.run_services():
with self.assertRaises(InternalServerError):
storage_object.save_to_filename(tmp_file.name)
def test_get_download_url_returns_signed_url_with_default_expiration(self) -> None:
mock_signed_url = self.mock_blob.generate_signed_url.return_value
storage = get_storage("gs://{}@bucketname/path/filename".format(self.credentials))
result = storage.get_download_url()
self.assertEqual(mock_signed_url, result)
self.assert_gets_bucket_with_credentials()
self.mock_bucket.blob.assert_called_once_with("path/filename")
self.mock_blob.generate_signed_url.assert_called_once_with(
expiration=datetime.timedelta(seconds=60),
response_disposition="attachment")
def test_delete_directory_raises_on_authentication_server_errors(self) -> None:
self.swift_service.add_handler("GET", "/v2.0/1234/CONTAINER", self.swift_container_handler)
self.auth_failure = "500 Internal Server Error"
swift_uri = self._generate_storage_uri("/path/to/files")
storage_object = get_storage(swift_uri)
with self.run_services():
with self.assertRaises(InternalServerError):
storage_object.delete_directory()
def test_local_storage_load_from_directory(self) -> None:
self.temp_directory = create_temp_nested_directory_with_files()
with TempDirectory() as temp_output:
temp_output_dir = temp_output.name
storage = get_storage("file://{0}/{1}".format(temp_output_dir, "tmp"))
storage.load_from_directory(self.temp_directory["temp_directory"]["path"])
destination_directory_path = os.path.join(
temp_output_dir, "tmp")
destination_input_one_path = os.path.join(
temp_output_dir, destination_directory_path,
self.temp_directory["temp_input_one"]["name"])
destination_input_two_path = os.path.join(
temp_output_dir, destination_directory_path,
self.temp_directory["temp_input_two"]["name"])
nested_temp_input_path = os.path.join(
temp_output_dir, destination_directory_path,
self.temp_directory["nested_temp_directory"]["path"],
self.temp_directory["nested_temp_input"]["name"])
def test_load_from_filename_puts_file_contents_at_object_endpoint(self) -> None:
tmp_file = tempfile.NamedTemporaryFile()
tmp_file.write(b"FOOBAR")
tmp_file.flush()
swift_uri = self._generate_storage_uri("/path/to/file.mp4")
storage_object = get_storage(swift_uri)
with self.expect_put_object("/v2.0/1234/CONTAINER", "/path/to/file.mp4", b"FOOBAR"):
with self.run_services():
storage_object.load_from_filename(tmp_file.name)
def test_save_to_filename(
self, mock_session_class: mock.Mock, mock_transfer_class: mock.Mock) -> None:
mock_session = mock_session_class.return_value
mock_s3 = mock_session.client.return_value
mock_transfer = mock_transfer_class.return_value
storage = get_storage(
"s3://access_key:access_secret@bucket/some/file?region=US_EAST")
storage.save_to_filename("destination/file")
mock_session_class.assert_called_with(
aws_access_key_id="access_key",
aws_secret_access_key="access_secret",
region_name="US_EAST")
mock_session.client.assert_called_with("s3")
mock_transfer_class.assert_called_with(mock_s3)
mock_transfer.download_file.assert_called_with("bucket", "some/file", "destination/file")
def test_load_from_filename_sends_guessed_content_type_from_extension(self) -> None:
tmp_file = tempfile.NamedTemporaryFile()
tmp_file.write(b"FOOBAR")
tmp_file.flush()
swift_uri = self._generate_storage_uri("/path/to/file.mp4")
storage_object = get_storage(swift_uri)
with self.expect_put_object("/v2.0/1234/CONTAINER", "/path/to/file.mp4", b"FOOBAR"):
with self.run_services():
storage_object.load_from_filename(tmp_file.name)
request = self.swift_service.assert_requested(
"PUT", "/v2.0/1234/CONTAINER/path/to/file.mp4")
request.assert_header_equals("Content-type", "video/mp4")
def test_load_from_file_puts_file_contents_at_object_endpoint(self) -> None:
temp = io.BytesIO(b"FOOBAR")
cloudfiles_uri = self._generate_storage_uri("/path/to/file.mp4", self.download_url_key)
storage_object = get_storage(cloudfiles_uri)
with self.use_local_identity_service():
with self.expect_put_object(
"/v2.0/MOSSO-TENANT/CONTAINER", "/path/to/file.mp4", b"FOOBAR"):
with self.run_services():
storage_object.load_from_file(temp)
def test_save_to_file_uses_default_region_when_one_is_not_provided(self) -> None:
self.add_container_object("/v2.0/MOSSO-TENANT/CONTAINER", "/path/to/file.mp4", b"FOOBAR")
temp = io.BytesIO()
cloudfiles_uri = self._generate_storage_uri("/path/to/file.mp4", self.download_url_key)
storage_object = get_storage(cloudfiles_uri)
with self.use_local_identity_service():
with self.run_services():
storage_object.save_to_file(temp)
self.swift_service.assert_requested_n_times(
"GET", "/v2.0/MOSSO-TENANT/CONTAINER/path/to/file.mp4", 1)
self.alt_cloudfiles_service.assert_requested_n_times(
"GET", "/v2.0/MOSSO-TENANT/CONTAINER/path/to/file.mp4", 0)