"""Tests for statement_period_adjustment_file handlers.""" from decimal import Decimal from unittest.mock import call, patch from abacus_common_logic.test_utils.helpers import get_json_body, get_message from owsresponse import response from royalties.constants import error from royalties.schemas import StatementPeriodAdjustmentFileDetailSchema from royalties.tests.utils.factories import StatementPeriodAdjustmentFileFactory @patch('royalties.blueprints.statement_period_adjustment_file.logic') class TestStatementPeriodAdjustmentFileHandlers: """Tests for statement_period_adjustment_file handlers.""" def test_create_statement_period_adjustment_file_success( self, mock_logic, fixture_client ): """Create statement period adjustment file.""" json_body = { 'file_name': 'test_file.csv', 'valid_file_location': 'valid/location', 'invalid_file_location': 'invalid/location', 'valid_row_count': 1, 'invalid_row_count': 2, 'total_file_amount_multicurrency': '3.000000000001', 'total_rounded_amount_multicurrency': '4.02', 'md5sum': 'b6579ec2950296ed6a04f08f67f64422', 'error_type': 'content_error', } expected_logic_params = { **json_body, 'statement_period_id': 1, 'total_file_amount_multicurrency': Decimal( json_body['total_file_amount_multicurrency'] ), 'total_rounded_amount_multicurrency': Decimal( json_body['total_rounded_amount_multicurrency'] ), } res_body = { **json_body, 'statement_period_id': 1, 'statement_period_adjustment_file_id': 1, } mock_response = response.Response(message=res_body, status=201) mock_logic.create_statement_period_adjustment_file.return_value = mock_response res = fixture_client.post('/statement-period/1/adjustment-file', json=json_body) assert res.status_code == 201 assert get_json_body(res) == res_body mock_logic.create_statement_period_adjustment_file.assert_called_once_with( **expected_logic_params ) def test_create_statement_period_adjustment_file_failure_validation( self, mock_logic, fixture_client ): """Create statement period adjustment file.""" json_body = { 'valid_file_location': 'valid/location', 'invalid_file_location': 'invalid/location', 'valid_row_count': 1, 'invalid_row_count': 2, 'total_file_amount_multicurrency': '3.000000000001', 'total_rounded_amount_multicurrency': '4.02', 'md5sum': 'b6579ec2950296ed6a04f08f67f64422', 'error_type': 'content_error', } res = fixture_client.post('/statement-period/1/adjustment-file', json=json_body) assert res.status_code == 400 message = get_message(res) assert error.ERROR_FIELD_MISSING in message['file_name'] assert not mock_logic.create_statement_period_adjustment_file.called def test_update_statement_period_adjustment_file_success( self, mock_logic, fixture_client ): """Update statement period adjustment file.""" statement_period_adjustment_file = StatementPeriodAdjustmentFileFactory.create() json_body = { 'valid_file_location': 'valid/location', 'invalid_file_location': 'invalid/location', 'valid_row_count': 1, 'invalid_row_count': 2, 'total_file_amount_multicurrency': '3.000000000002', 'total_rounded_amount_multicurrency': '4.03', 'md5sum': 'b6579ec2950296ed6a04f08f67f64422', 'error_type': 'content_error', } expected_logic_params = { **json_body, 'total_file_amount_multicurrency': Decimal( json_body['total_file_amount_multicurrency'] ), 'total_rounded_amount_multicurrency': Decimal( json_body['total_rounded_amount_multicurrency'] ), } res_body = { **json_body, 'statement_period_id': 1, 'statement_period_adjustment_file_id': 1, } mock_response = response.Response(message=res_body, status=201) mock_logic.update_statement_period_adjustment_file.return_value = mock_response res = fixture_client.put( f'/statement-period/1/adjustment-file/' f'{statement_period_adjustment_file.statement_period_adjustment_file_id}', json=json_body, ) assert res.status_code == 201 assert get_json_body(res) == res_body assert mock_logic.update_statement_period_adjustment_file.call_args_list == [ call(statement_period_adjustment_file, **expected_logic_params) ] def test_update_statement_period_adjustment_file_failure_validation( self, mock_logic, fixture_client ): """Update statement period adjustment file.""" statement_period_adjustment_file = StatementPeriodAdjustmentFileFactory.create() json_body = { 'valid_row_count': 'wrong_value', } res = fixture_client.put( f'/statement-period/1/adjustment-file/' f'{statement_period_adjustment_file.statement_period_adjustment_file_id}', json=json_body, ) assert res.status_code == 400 message = get_message(res) assert 'valid_row_count' in message assert not mock_logic.update_statement_period_adjustment_file.called def test_list_statement_period_adjustment_files_success( self, mock_logic, fixture_client ): """List statement period adjustment files.""" statement_period_adjustment_file = StatementPeriodAdjustmentFileFactory.create() statement_period_adjustment_file2 = ( StatementPeriodAdjustmentFileFactory.create() ) res = fixture_client.get( f'/statement-period/{statement_period_adjustment_file.statement_period_id}' f'/adjustment-files', ) assert res.status_code == 200 assert ( statement_period_adjustment_file.statement_period_id != statement_period_adjustment_file2.statement_period_id ) assert get_json_body(res)['items'] == [ StatementPeriodAdjustmentFileDetailSchema().dump( statement_period_adjustment_file ) ] assert get_json_body(res)['total_count'] == 1 def test_delete_statement_period_adjustment_file(self, mock_logic, fixture_client): """Delete statement period adjustment file.""" statement_period_adjustment_file = StatementPeriodAdjustmentFileFactory.create() mock_logic.delete_statement_period_adjustment_file.return_value = ( response.Response(status=204) ) res = fixture_client.delete( f'/statement-period/1/adjustment-file/' f'{statement_period_adjustment_file.statement_period_adjustment_file_id}' ) assert res.status_code == 204 assert mock_logic.delete_statement_period_adjustment_file.call_args_list == [ call(statement_period_adjustment_file) ] def test_get_abacus_adjustments_template_file(self, mock_logic, fixture_client): """Get abacus adjustments template file.""" mock_logic.get_adjustments_template_xlsx.return_value = response.Response( status=200 ) res = fixture_client.get('/abacus-adjustments/download/template') assert res.status_code == 200 mock_logic.get_adjustments_template_xlsx.assert_called_once() def test_get_abacus_adjustment_invalid_report_file( self, mock_logic, fixture_client ): """Get abacus adjustments invalid report file.""" mock_logic.get_adjustment_file_invalid_report.return_value = response.Response( status=200 ) res = fixture_client.get('/statement-period-adjustment-file/123/download/error') assert res.status_code == 200 mock_logic.get_adjustment_file_invalid_report.assert_called_once() def test_get_abacus_adjustment_valid_report_file(self, mock_logic, fixture_client): """Get abacus adjustments valid report file.""" mock_logic.get_adjustment_file_valid_report.return_value = response.Response( status=200 ) res = fixture_client.get( '/statement-period-adjustment-file/123/download/report' ) assert res.status_code == 200 mock_logic.get_adjustment_file_valid_report.assert_called_once() def test_get_abacus_adjustment_invalid_report_file_failure( self, mock_logic, fixture_client ): """Get abacus adjustments invalid report file failure.""" mock_logic.get_adjustment_file_invalid_report.return_value = None res = fixture_client.get( '/statement-period-adjustment-file/123456/download/error' ) assert res.status_code == 400 response_data = (res.data).decode('utf-8') assert ( error.ERROR_STATEMENT_PERIOD_INVALID_REPORT_NOT_FOUND.format( statement_period_adjustment_file_id=123456 ) in response_data ) mock_logic.get_adjustment_file_invalid_report.assert_called_once() def test_get_abacus_adjustment_valid_report_file_failure( self, mock_logic, fixture_client ): """Get abacus adjustments valid report file failure.""" mock_logic.get_adjustment_file_valid_report.return_value = None res = fixture_client.get( '/statement-period-adjustment-file/123456/download/report' ) assert res.status_code == 400 response_data = (res.data).decode('utf-8') assert ( error.ERROR_STATEMENT_PERIOD_VALID_REPORT_NOT_FOUND.format( statement_period_adjustment_file_id=123456 ) in response_data ) mock_logic.get_adjustment_file_valid_report.assert_called_once() def test_get_statement_period_adjustment_files(self, mock_logic, fixture_client): """Test to get statement period statement files.""" mock_logic.get_statement_period_adjustment_files.return_value = ( response.Response(message='OK', status=200) ) res = fixture_client.get('/statement-period-adjustment-files') assert res.status_code == 200 def test_get_statement_period_adjustment_file_users( self, mock_logic, fixture_client ): """Test to get a list of statement_period_adjustment_file users.""" mock_result = [ {'created_by': 'd5ca8ac3-7e51-4793-8775-50d11282504c'}, {'created_by': 'e5ca8bc3-7e52-4793-8775-50d11282504c'}, ] mock_logic.get_statement_period_adjustment_file_users.return_value = ( response.Response(message=mock_result, status=200) ) res = fixture_client.get( '/statement-period-adjustment-file/users/uploaded-file' ) assert res.status_code == 200 assert res.json == mock_result def test_validate_adjustments( self, mock_logic, fixture_client, adjustments_fixture ): """Test validating a list of manual adjustments.""" statement_period_id = 1 mock_result = {} mock_logic.validate_adjustments.return_value = response.Response( message=mock_result, status=200 ) res = fixture_client.post( '/statement-period-adjustment-file/validate-adjustments', json={ 'statement_period_id': statement_period_id, 'adjustments': adjustments_fixture, }, ) assert res.status_code == 200 assert res.json == mock_result # `statement_period_id` is required res = fixture_client.post( '/statement-period-adjustment-file/validate-adjustments', json={'adjustments': adjustments_fixture}, ) assert res.status_code == 422 # `adjustments` is required res = fixture_client.post( '/statement-period-adjustment-file/validate-adjustments', json={'statement_period_id': statement_period_id}, ) assert res.status_code == 422 # IDs should be sent as strings res = fixture_client.post( '/statement-period-adjustment-file/validate-adjustments', json={ 'statement_period_id': statement_period_id, 'adjustments': [{**adjustments_fixture[0], 'account_id': 1}], }, ) assert res.status_code == 422 def test_get_in_progress_auto_generated_adjustments( self, mock_logic, fixture_client ): """Test to get in-progress auto generated adjustments.""" statement_period_id = 1 mock_result = { 'batch_type': 'auto', 'status': 'generating', 'statement_period_id': statement_period_id, 'statement_period_adjustment_file_id': 1, } mock_logic.get_in_progress_auto_generated_adjustments.return_value = ( response.Response(message=mock_result, status=200) ) res = fixture_client.get( f'/statement-period/{statement_period_id}/adjustments/auto-generation/progress' ) assert res.status_code == 200 assert res.json == mock_result