"""Tests for ows_royalties.py.""" from unittest.mock import patch import httpx import pytest from owsclient.test import OwsClientMock from file_upload_complete.connectors import ows_royalties from file_upload_complete.exception import OwsServiceException class TestFileUploadComplete: """Tests for file_upload_complete function.""" def test_file_upload_complete_success_200( self, ows_client_mock: OwsClientMock, ) -> None: """Test successful file upload complete with 200 status.""" mock_response = {'status': 'success', 'message': 'File processed'} file_key = 'test-file-key-123' ows_client_mock.post( 'ows-royalties', f'/file-upload/{file_key}/complete', ).mock( return_value=httpx.Response( 200, json=mock_response, ) ) response = ows_royalties.file_upload_complete(file_key) assert response == mock_response def test_file_upload_complete_success_201( self, ows_client_mock: OwsClientMock, ) -> None: """Test successful file upload complete with 201 status.""" mock_response = {'status': 'created'} file_key = 'test-file-key-456' ows_client_mock.post( 'ows-royalties', f'/file-upload/{file_key}/complete', ).mock( return_value=httpx.Response( 201, json=mock_response, ) ) response = ows_royalties.file_upload_complete(file_key) assert response == mock_response @patch('file_upload_complete.connectors.ows_royalties.logger') def test_file_upload_complete_failed_500( self, mock_logger, ows_client_mock: OwsClientMock, ) -> None: """Test failed file upload complete with 500 error.""" mock_response = { 'error': 'Internal server error', } file_key = 'test-file-key-789' ows_client_mock.post( 'ows-royalties', f'/file-upload/{file_key}/complete', ).mock( return_value=httpx.Response( 500, json=mock_response, ) ) with pytest.raises(OwsServiceException) as exc_info: ows_royalties.file_upload_complete(file_key) assert 'ows-royalties failure' in str(exc_info.value) assert 'Status 500' in str(exc_info.value) assert mock_logger.error.called @patch('file_upload_complete.connectors.ows_royalties.logger') def test_file_upload_complete_failed_404( self, mock_logger, ows_client_mock: OwsClientMock, ) -> None: """Test failed file upload complete with 404 not found.""" mock_response = {'error': 'File not found'} file_key = 'nonexistent-file-key' ows_client_mock.post( 'ows-royalties', f'/file-upload/{file_key}/complete', ).mock( return_value=httpx.Response( 404, json=mock_response, ) ) with pytest.raises(OwsServiceException) as exc_info: ows_royalties.file_upload_complete(file_key) assert 'Status 404' in str(exc_info.value) @patch('file_upload_complete.connectors.ows_royalties.logger') def test_file_upload_complete_non_json_error_response( self, mock_logger, ows_client_mock: OwsClientMock, ) -> None: """Test file upload complete with non-JSON error response.""" file_key = 'test-file-key' error_text = 'Internal Server Error' ows_client_mock.post( 'ows-royalties', f'/file-upload/{file_key}/complete', ).mock( return_value=httpx.Response( 500, text=error_text, headers={'content-type': 'text/plain'}, ) ) with pytest.raises(OwsServiceException): ows_royalties.file_upload_complete(file_key) # Verify logger was called with text content assert mock_logger.error.called @patch('file_upload_complete.connectors.ows_royalties.logger') def test_file_upload_complete_logs_info( self, mock_logger, ows_client_mock: OwsClientMock, ) -> None: """Test file upload complete logs appropriate info messages.""" mock_response = {'status': 'success'} file_key = 'test-file-key' ows_client_mock.post( 'ows-royalties', f'/file-upload/{file_key}/complete', ).mock( return_value=httpx.Response( 200, json=mock_response, ) ) ows_royalties.file_upload_complete(file_key) # Verify info logging was called assert mock_logger.info.called mock_logger.info.assert_called_with(f'Completing file upload: {file_key}') class TestQuarantineInfectedFileUpload: """Tests for quarantine_infected_file_upload function.""" def test_quarantine_infected_file_upload_success_200( self, ows_client_mock: OwsClientMock, ) -> None: """Test successful quarantine infected file upload with 200 status.""" mock_response = {'status': 'quarantined'} file_key = 'test-file-key-abc' ows_client_mock.post( 'ows-royalties', f'/file-upload/{file_key}/quarantine', ).mock( return_value=httpx.Response( 200, json=mock_response, ) ) response = ows_royalties.quarantine_infected_file_upload(file_key) assert response == mock_response def test_quarantine_infected_file_upload_success_201( self, ows_client_mock: OwsClientMock, ) -> None: """Test successful quarantine infected file upload with 201 status.""" mock_response = {'status': 'quarantined', 'id': '123'} file_key = 'test-file-key-def' ows_client_mock.post( 'ows-royalties', f'/file-upload/{file_key}/quarantine', ).mock( return_value=httpx.Response( 201, json=mock_response, ) ) response = ows_royalties.quarantine_infected_file_upload(file_key) assert response == mock_response @patch('file_upload_complete.connectors.ows_royalties.logger') def test_quarantine_infected_file_upload_failed_500( self, mock_logger, ows_client_mock: OwsClientMock, ) -> None: """Test failed quarantine infected file upload with 500 error.""" mock_response = { 'error': 'Failed to quarantine file upload.', } file_key = 'test-file-key-ghi' ows_client_mock.post( 'ows-royalties', f'/file-upload/{file_key}/quarantine', ).mock( return_value=httpx.Response( 500, json=mock_response, ) ) with pytest.raises(OwsServiceException) as exc_info: ows_royalties.quarantine_infected_file_upload(file_key) assert 'ows-royalties failure' in str(exc_info.value) assert 'Status 500' in str(exc_info.value) @patch('file_upload_complete.connectors.ows_royalties.logger') def test_quarantine_infected_file_upload_failed_400( self, mock_logger, ows_client_mock: OwsClientMock, ) -> None: """Test failed quarantine infected file upload with 400 bad request.""" mock_response = {'error': 'Invalid request'} file_key = 'invalid-file-key' ows_client_mock.post( 'ows-royalties', f'/file-upload/{file_key}/quarantine', ).mock( return_value=httpx.Response( 400, json=mock_response, ) ) with pytest.raises(OwsServiceException) as exc_info: ows_royalties.quarantine_infected_file_upload(file_key) assert 'Status 400' in str(exc_info.value) @patch('file_upload_complete.connectors.ows_royalties.logger') def test_quarantine_infected_file_upload_non_json_error_response( self, mock_logger, ows_client_mock: OwsClientMock, ) -> None: """Test quarantine infected file upload with non-JSON error response.""" file_key = 'test-file-key' error_text = 'Gateway Timeout' ows_client_mock.post( 'ows-royalties', f'/file-upload/{file_key}/quarantine', ).mock( return_value=httpx.Response( 504, text=error_text, headers={'content-type': 'text/html'}, ) ) with pytest.raises(OwsServiceException): ows_royalties.quarantine_infected_file_upload(file_key) # Verify logger was called assert mock_logger.error.called @patch('file_upload_complete.connectors.ows_royalties.logger') def test_quarantine_infected_file_upload_logs_messages( self, mock_logger, ows_client_mock: OwsClientMock, ) -> None: """Test quarantine infected file upload logs appropriate messages.""" mock_response = {'status': 'success'} file_key = 'test-file-key' ows_client_mock.post( 'ows-royalties', f'/file-upload/{file_key}/quarantine', ).mock( return_value=httpx.Response( 200, json=mock_response, ) ) ows_royalties.quarantine_infected_file_upload(file_key) # Verify logging was called assert mock_logger.info.call_count == 2 calls = [call.args[0] for call in mock_logger.info.call_args_list] assert any('Quarantine file upload' in call for call in calls) assert any('File upload quarantined successfully' in call for call in calls) class TestGetFileUploadConfig: """Tests for file_upload_config function.""" def test_file_upload_config_success_200( self, ows_client_mock: OwsClientMock, mock_file_upload_config ) -> None: """Test to get file_upload_config with 200 status.""" file_upload_config_id = mock_file_upload_config['file_upload_config_id'] mock_response = mock_file_upload_config ows_client_mock.get( 'ows-royalties', f'/file-upload-config/{file_upload_config_id}/', ).mock( return_value=httpx.Response( 200, json=mock_response, ) ) response = ows_royalties.get_file_upload_config(file_upload_config_id) assert response == mock_response @patch('file_upload_complete.connectors.ows_royalties.logger') def test_file_upload_config_failed_500( self, mock_logger, ows_client_mock: OwsClientMock, ) -> None: """Test to get file_upload_config with 500 error.""" mock_response = { 'error': 'Failed to cancel file upload.', } file_upload_config_id = 1 ows_client_mock.get( 'ows-royalties', f'/file-upload-config/1/', ).mock( return_value=httpx.Response( 500, json=mock_response, ) ) with pytest.raises(OwsServiceException) as exc_info: ows_royalties.get_file_upload_config(file_upload_config_id) assert 'ows-royalties failure' in str(exc_info.value) assert 'Status 500' in str(exc_info.value) mock_logger.error.assert_called_once()