"""Test s3_file check.""" import flexmock from moto import mock_s3 from assets.connectors import s3 from assets.connectors import sentry from assets.constants import error from assets.models import s3_file @mock_s3 def test_check_s3_file_exists_no_bucket(): """Test s3 file check in unavailable bucket.""" (flexmock(sentry.sentry_client) .should_receive('captureException') .once()) test_bucket = 'bucket-doesnt-exist' test_file_key = 'unique_asset_name.jpg' result = s3_file.check_s3_file_exists(test_bucket, test_file_key) assert result.status == 404 assert result.errors['code'] == error.ERROR_CODE_NO_SUCH_BUCKET @mock_s3 def test_check_s3_file_exists_no_file_in_bucket(): """Test s3 file check of file that doesn't exist.""" (flexmock(sentry.sentry_client) .should_receive('captureException') .once()) conn = s3.connect_to_s3() test_bucket_name = 'test-bucket' conn.create_bucket(test_bucket_name) test_file_key = 'unique_asset_name.jpg' expected_error = ( 'unique_asset_name.jpg not found in bucket test-bucket!') result = s3_file.check_s3_file_exists(test_bucket_name, test_file_key) assert result.status == 404 assert result.errors['message'] == expected_error assert result.errors['code'] == error.ERROR_CODE_NOT_FOUND @mock_s3 def test_get_file_url_in_s3_success(): """Test s3 file check of valid file.""" conn = s3.connect_to_s3() test_bucket_name = 'test-bucket' test_bucket = conn.create_bucket(test_bucket_name) test_file_key = 'unique_asset_name.jpg' key = test_bucket.new_key(test_file_key) key.set_contents_from_string('asset_content') result = s3_file.check_s3_file_exists(test_bucket_name, test_file_key) assert result.status == 200 @mock_s3 def test_rename_s3_file_success(): """Test result when file copy and delete succeed.""" conn = s3.connect_to_s3() bucket_name = 'test-bucket' bucket = conn.create_bucket(bucket_name) original_path = 'old/path/file.jpg' new_path = 'new/path/file.jpg' asset_content = 'pretend that this is real jpg content' key = bucket.new_key(original_path) key.set_contents_from_string(asset_content) result = s3_file.rename_s3_file(bucket_name, original_path, new_path) retrieved_contents = bucket.get_key(new_path).get_contents_as_string() assert result.status == 200 assert retrieved_contents == asset_content.encode() assert bucket.get_key(original_path) is None @mock_s3 def test_rename_s3_file_target_exists(): """Test result when the target file already exists in s3.""" conn = s3.connect_to_s3() bucket_name = 'test-bucket' bucket = conn.create_bucket(bucket_name) original_path = 'old/path/file.jpg' new_path = 'new/path/file.jpg' asset_content = 'pretend that this is real jpg content' existing_content = 'pretend that this is real too, ok?' source_key = bucket.new_key(original_path) source_key.set_contents_from_string(asset_content) dest_key = bucket.new_key(new_path) dest_key.set_contents_from_string(existing_content) result = s3_file.rename_s3_file(bucket_name, original_path, new_path) retrieved_contents = bucket.get_key(new_path).get_contents_as_string() assert result.status == 200 assert retrieved_contents == asset_content.encode() @mock_s3 def test_rename_s3_file_not_found(): """Test result when no file is found at the original path.""" conn = s3.connect_to_s3() bucket_name = 'test-bucket' original_path = 'old/path/file.jpg' new_path = 'new/path/file.jpg' conn.create_bucket(bucket_name) result = s3_file.rename_s3_file(bucket_name, original_path, new_path) assert result.status == 404 @mock_s3 def test_rename_s3_file_nonexistent_bucket(): """Test result when the bucket does not exist.""" bucket_name = 'i-will-not-create-this-bucket' original_path = 'old/path/file.jpg' new_path = 'new/path/file.jpg' (flexmock(sentry.sentry_client) .should_receive('captureException') .once()) result = s3_file.rename_s3_file(bucket_name, original_path, new_path) assert result.status == 500 assert result.errors == { 'code': 'internal_error', 'message': 'The specified bucket does not exist'}