"""Test s3_file check.""" from unittest.mock import MagicMock from botocore.exceptions import ClientError import pytest from podcast.models import s3 from podcast.utils import exc def test_check_s3_file_exists_no_bucket(monkeypatch): """Test s3 model returns 404 for unavailable destination.""" test_bucket = 'bucket-doesnt-exist' test_file_key = 'unique_asset_name.jpg' mock_response = { 'Error': {'Code': '404', 'Message': 'Not Found'}, 'ResponseMetadata': { 'HTTPStatusCode': 404, } } mock_client = MagicMock() mock_client.get_s3_client().head_object.side_effect = ClientError(mock_response, 'head') monkeypatch.setattr(s3, 's3', mock_client) with pytest.raises(exc.OwsError) as err: s3.check_s3_file_exists(test_bucket, test_file_key) assert err.value.status == 404 assert err.value.message == '404: error looking for unique_asset_name.jpg in bucket bucket-doesnt-exist, Not Found' def test_check_s3_file_exists_success(monkeypatch): """Test s3 model returns 200 for available file.""" test_bucket = 'bucket-exist' test_file_key = 'unique_asset_name.jpg' mock_response = { 'ResponseMetadata': { 'HTTPStatusCode': 200, } } mock_client = MagicMock() mock_client.get_s3_client().head_object.return_value = mock_response monkeypatch.setattr(s3, 's3', mock_client) s3.check_s3_file_exists(test_bucket, test_file_key) def test_copy_s3_file_success(monkeypatch): """Test copy s3 file from one bucket to another success.""" test_source_bucket = 'source-bucket' test_destination_bucket = 'destination-bucket' test_file_key = 'unique_asset_name.wav' mock_client = MagicMock() mock_client.get_s3_client().copy.return_value = None monkeypatch.setattr(s3, 's3', mock_client) result = s3.copy_s3_file(test_source_bucket, test_file_key, test_destination_bucket, test_file_key) assert result is None def test_copy_s3_file_failure(monkeypatch): """Test copy s3 file from one bucket to another failure.""" test_source_bucket = 'source-bucket' test_destination_bucket = 'destination-bucket' test_file_key = 'unique_asset_name2.wav' mock_response = { 'Error': {'Code': '403', 'Message': 'Forbidden'}, 'ResponseMetadata': { 'HTTPStatusCode': 403, } } mock_client = MagicMock() mock_client.get_s3_client().copy.side_effect = ClientError(mock_response, 'copy') monkeypatch.setattr(s3, 's3', mock_client) with pytest.raises(exc.OwsError) as err: s3.copy_s3_file(test_source_bucket, test_file_key, test_destination_bucket, test_file_key) assert err.value.status == 403 assert err.value.message == '403: error while copying file: unique_asset_name2.wav from ' \ 'input bucket: source-bucket to output bucket: destination-bucket, Forbidden'