"""Tests for Handlers.""" import json from unittest.mock import MagicMock from unittest.mock import patch from owsresponse import response import pytest from asset_file_details import api from asset_file_details import handlers from asset_file_details.constants.error import ERROR_CODE_INVALID_PHYSICAL_LOCATION_ID from asset_file_details.constants.error import ERROR_MESSAGE_INVALID_PHYSICAL_LOCATION_ID from asset_file_details.logic import file_details \ as file_details_logic from asset_file_details.logic import flac_file_details \ as flac_file_details_logic @pytest.fixture def fixture_client(): """Create an api test client fixture.""" return api.app.test_client() @patch('asset_file_details.handlers.g', spec=['log']) def test_exception_handler(mock_g): """Verify exception_Handler returns 500 status code and json payload.""" message = ( 'The server encountered an internal error ' 'and was unable to complete your request.') mock_error = MagicMock() server_response = handlers.exception_handler(mock_error) mock_g.log.exception.assert_called_with(mock_error) # assert status code is 500 assert server_response.status_code == 500 # assert json payload response_message = json.loads(server_response.data.decode()) assert response_message['message'] == message assert response_message['code'] == response.error.ERROR_CODE_INTERNAL_ERROR def test_get_flac_file_details_success(mocker, fixture_client): """Test successful GET request for getting flac file details.""" mocker.patch.object( flac_file_details_logic, 'get_flac_file_details', return_value=response.Response( status=200, message='success' ) ) result = fixture_client.get( '/flac-file-details/upc/1234567890/cd/1/track_id/1') assert result.status_code == 200 def test_get_flac_file_details_bad_params(fixture_client): """Test GET request for getting flac file details with missing params.""" result = fixture_client.get('/flac-file-details/upc/1234567890') assert result.status_code == 404 def test_get_wav_file_details_success(mocker, fixture_client): """Test successful GET request for getting wav file details.""" mocked_get_file_details_by_format = mocker.patch.object( file_details_logic, 'get_file_details_by_format', return_value=response.Response( status=200, message='success' ) ) result = fixture_client.get( '/file-details/format/wav/upc/1234567890') mocked_get_file_details_by_format.assert_called_with('wav', '1234567890', '1') assert result.status_code == 200 def test_get_wav_file_details_with_physical_location_id_success(mocker, fixture_client): """Test successful GET request for getting wav file details.""" mocked_get_file_details_by_format = mocker.patch.object( file_details_logic, 'get_file_details_by_format', return_value=response.Response( status=200, message='success' ) ) result = fixture_client.get( '/file-details/format/wav/upc/1234567890?physical_location_id=2') mocked_get_file_details_by_format.assert_called_with('wav', '1234567890', '2') assert result.status_code == 200 def test_wrong_file_format_validation(mocker, fixture_client): """Test a failed GET request for getting wav file details.""" result = fixture_client.get( '/file-details/format/wav1/upc/1234567890') assert result.status_code == 400 def test_get_file_details_by_format_invalid_physical_location_id(fixture_client): """Test invalid physical location id for get_file_details_by_format endpoint.""" expected = { 'code': ERROR_CODE_INVALID_PHYSICAL_LOCATION_ID, 'message': ERROR_MESSAGE_INVALID_PHYSICAL_LOCATION_ID } result = fixture_client.get('/file-details/format/wav/upc/12345?physical_location_id=5') assert result.json == expected assert result.status_code == 400