"""Test ows-abacus-state requests.""" from http import HTTPStatus import httpx from owsclient.test import OwsClientMock import pytest from src.exceptions import OwsAbacusStateException from src.ows_abacus_state import ( banking_details_rejected, get_banking_details_review_status, set_payment_eligibility_error, ) def test_get_banking_details_review_status_success( ows_client_mock: OwsClientMock, ) -> None: """Test successfully getting banking_details_review action_status.""" account_payee_id = 1 mock_states_response = [ { 'abacus_state_id': 10, 'action_name': 'payment_eligibility', 'action_status': 'pending', }, { 'abacus_state_id': 11, 'action_name': 'banking_details_review', 'action_status': 'completed', }, ] ows_client_mock.get( 'ows-abacus-state', f'/abacus-state/account_payee/{account_payee_id}', ).mock(return_value=httpx.Response(HTTPStatus.OK, json=mock_states_response)) result = get_banking_details_review_status(account_payee_id) assert result == 'completed' def test_get_banking_details_review_status_not_found( ows_client_mock: OwsClientMock, ) -> None: """Test getting banking_details_review when no such state exists.""" account_payee_id = 1 mock_states_response = [ { 'abacus_state_id': 10, 'action_name': 'payment_eligibility', 'action_status': 'pending', }, ] ows_client_mock.get( 'ows-abacus-state', f'/abacus-state/account_payee/{account_payee_id}', ).mock(return_value=httpx.Response(HTTPStatus.OK, json=mock_states_response)) result = get_banking_details_review_status(account_payee_id) assert result is None def test_get_banking_details_review_status_empty_list( ows_client_mock: OwsClientMock, ) -> None: """Test getting banking_details_review when no states exist at all.""" account_payee_id = 1 ows_client_mock.get( 'ows-abacus-state', f'/abacus-state/account_payee/{account_payee_id}', ).mock(return_value=httpx.Response(HTTPStatus.OK, json=[])) result = get_banking_details_review_status(account_payee_id) assert result is None def test_get_banking_details_review_status_failure( ows_client_mock: OwsClientMock, ) -> None: """Test failure getting abacus states.""" account_payee_id = 1 ows_client_mock.get( 'ows-abacus-state', f'/abacus-state/account_payee/{account_payee_id}', ).mock(return_value=httpx.Response(HTTPStatus.BAD_REQUEST)) with pytest.raises( OwsAbacusStateException, match=f'ERROR in GET /abacus-state/account_payee/{account_payee_id}', ): get_banking_details_review_status(account_payee_id) def test_set_payment_eligibility_error_success( ows_client_mock: OwsClientMock, ) -> None: """Test successfully setting payment_eligibility state to error.""" account_payee_id = 1 abacus_state_id = 10 message = 'Failed to move program' mock_states_response = [ { 'abacus_state_id': abacus_state_id, 'action_name': 'payment_eligibility', 'action_status': 'pending', }, { 'abacus_state_id': 11, 'action_name': 'banking_details_review', 'action_status': 'completed', }, ] ows_client_mock.get( 'ows-abacus-state', f'/abacus-state/account_payee/{account_payee_id}', ).mock(return_value=httpx.Response(HTTPStatus.OK, json=mock_states_response)) ows_client_mock.put( 'ows-abacus-state', f'/abacus-state/{abacus_state_id}', json={'action_status': 'error', 'message': message}, ).mock(return_value=httpx.Response(HTTPStatus.OK)) set_payment_eligibility_error(account_payee_id, message) def test_set_payment_eligibility_error_no_state( ows_client_mock: OwsClientMock, ) -> None: """Test error when no payment_eligibility state exists.""" account_payee_id = 1 mock_states_response = [ { 'abacus_state_id': 11, 'action_name': 'banking_details_review', 'action_status': 'completed', }, ] ows_client_mock.get( 'ows-abacus-state', f'/abacus-state/account_payee/{account_payee_id}', ).mock(return_value=httpx.Response(HTTPStatus.OK, json=mock_states_response)) with pytest.raises( OwsAbacusStateException, match='No payment_eligibility state found for account_payee_id=1', ): set_payment_eligibility_error(account_payee_id, 'Failed to move program') def test_set_payment_eligibility_error_empty_list( ows_client_mock: OwsClientMock, ) -> None: """Test error when no states exist at all.""" account_payee_id = 1 ows_client_mock.get( 'ows-abacus-state', f'/abacus-state/account_payee/{account_payee_id}', ).mock(return_value=httpx.Response(HTTPStatus.OK, json=[])) with pytest.raises( OwsAbacusStateException, match='No payment_eligibility state found for account_payee_id=1', ): set_payment_eligibility_error(account_payee_id, 'Failed to move program') def test_set_payment_eligibility_error_get_failure( ows_client_mock: OwsClientMock, ) -> None: """Test failure fetching states for set_payment_eligibility_error.""" account_payee_id = 1 ows_client_mock.get( 'ows-abacus-state', f'/abacus-state/account_payee/{account_payee_id}', ).mock(return_value=httpx.Response(HTTPStatus.BAD_REQUEST)) with pytest.raises( OwsAbacusStateException, match=f'ERROR in GET /abacus-state/account_payee/{account_payee_id}', ): set_payment_eligibility_error(account_payee_id, 'Failed to move program') def test_set_payment_eligibility_error_put_failure( ows_client_mock: OwsClientMock, ) -> None: """Test failure on PUT when setting payment_eligibility error.""" account_payee_id = 1 abacus_state_id = 10 message = 'Failed to move program' mock_states_response = [ { 'abacus_state_id': abacus_state_id, 'action_name': 'payment_eligibility', 'action_status': 'pending', }, ] ows_client_mock.get( 'ows-abacus-state', f'/abacus-state/account_payee/{account_payee_id}', ).mock(return_value=httpx.Response(HTTPStatus.OK, json=mock_states_response)) ows_client_mock.put( 'ows-abacus-state', f'/abacus-state/{abacus_state_id}', json={'action_status': 'error', 'message': message}, ).mock(return_value=httpx.Response(HTTPStatus.BAD_REQUEST, json={'error': 'test'})) with pytest.raises( OwsAbacusStateException, match='Failed to set payment eligibility error:', ): set_payment_eligibility_error(account_payee_id, message) def test_banking_details_rejected_when_banking_review_rejected( ows_client_mock: OwsClientMock, ) -> None: """Returns True when banking_details_review is rejected.""" account_payee_id = 1 mock_states_response = [ { 'abacus_state_id': 10, 'action_name': 'banking_details_review', 'action_status': 'rejected', }, { 'abacus_state_id': 11, 'action_name': 'payment_eligibility', 'action_status': 'completed', }, ] ows_client_mock.get( 'ows-abacus-state', f'/abacus-state/account_payee/{account_payee_id}', ).mock(return_value=httpx.Response(HTTPStatus.OK, json=mock_states_response)) assert banking_details_rejected(account_payee_id) is True def test_banking_details_rejected_when_payment_eligibility_rejected( ows_client_mock: OwsClientMock, ) -> None: """Returns True when payment_eligibility is rejected.""" account_payee_id = 1 mock_states_response = [ { 'abacus_state_id': 10, 'action_name': 'banking_details_review', 'action_status': 'completed', }, { 'abacus_state_id': 11, 'action_name': 'payment_eligibility', 'action_status': 'rejected', }, ] ows_client_mock.get( 'ows-abacus-state', f'/abacus-state/account_payee/{account_payee_id}', ).mock(return_value=httpx.Response(HTTPStatus.OK, json=mock_states_response)) assert banking_details_rejected(account_payee_id) is True def test_banking_details_rejected_returns_false_when_no_rejected_states( ows_client_mock: OwsClientMock, ) -> None: """Returns False when neither banking_details_review nor payment_eligibility are rejected.""" account_payee_id = 1 mock_states_response = [ { 'abacus_state_id': 10, 'action_name': 'banking_details_review', 'action_status': 'completed', }, { 'abacus_state_id': 11, 'action_name': 'payment_eligibility', 'action_status': 'pending', }, ] ows_client_mock.get( 'ows-abacus-state', f'/abacus-state/account_payee/{account_payee_id}', ).mock(return_value=httpx.Response(HTTPStatus.OK, json=mock_states_response)) assert banking_details_rejected(account_payee_id) is False def test_banking_details_rejected_ignores_other_actions_even_if_rejected( ows_client_mock: OwsClientMock, ) -> None: """Returns False when unrelated actions are rejected but relevant ones are not.""" account_payee_id = 1 mock_states_response = [ { 'abacus_state_id': 10, 'action_name': 'some_other_action', 'action_status': 'rejected', }, { 'abacus_state_id': 11, 'action_name': 'banking_details_review', 'action_status': 'completed', }, ] ows_client_mock.get( 'ows-abacus-state', f'/abacus-state/account_payee/{account_payee_id}', ).mock(return_value=httpx.Response(HTTPStatus.OK, json=mock_states_response)) assert banking_details_rejected(account_payee_id) is False def test_banking_details_rejected_get_failure_raises_exception( ows_client_mock: OwsClientMock, ) -> None: """Raises OwsAbacusStateException when fetching states fails.""" account_payee_id = 1 ows_client_mock.get( 'ows-abacus-state', f'/abacus-state/account_payee/{account_payee_id}', ).mock(return_value=httpx.Response(HTTPStatus.BAD_REQUEST)) with pytest.raises( OwsAbacusStateException, match=f'ERROR in GET /abacus-state/account_payee/{account_payee_id}', ): banking_details_rejected(account_payee_id)