"""Tests for account_payee handlers.""" from unittest.mock import MagicMock, patch from flask.testing import FlaskClient from owsresponse import response import pytest @patch('abacus_account.blueprints.account_payee.logic') def test_create_account_payee(mock_logic, fixture_client): """Test for the account_payee creation handler.""" post_data = { 'account_id': 1 } mock_response = response.Response(message='ok', status=201) mock_logic.create_account_payee.return_value = mock_response res = fixture_client.post('/account-payee', json=post_data) assert res.status_code == 201 mock_logic.create_account_payee.assert_called_once_with( account_id=1 ) @patch('abacus_account.blueprints.account_payee.logic') def test_get_account_payee_by_account_id(mock_logic, fixture_client): """Test for the account_payee fetching handler.""" mock_response = response.Response(message={'result': 'ok'}, status=200) mock_logic.get_account_payee_by_account_id.return_value = mock_response res = fixture_client.get('/account/111/account-payee') assert res.status_code == 200 mock_logic.get_account_payee_by_account_id.assert_called_once_with(111) @pytest.mark.parametrize( ( 'permissions_return', 'expected_status', ), [ pytest.param( True, 200, id='Permissions check OK' ), pytest.param( False, 403, id='Permissions check not OK' ), ], ) @patch('abacus_account.blueprints.account_payee.ows_client') @patch('abacus_account.blueprints.account_payee.permissions_authorize_many_accounts') @patch('abacus_account.blueprints.account_payee.logic') def test_permissions_get_account_payee_by_account_id( mock_logic: MagicMock, mock_permissions_authorize_many_accounts: MagicMock, mock_ows_client: MagicMock, fixture_client: FlaskClient, permissions_return: bool | None, expected_status: int ): """Test the permissions check for the get account payee by account id handler.""" mock_response = response.Response(message={'result': 'ok'}, status=200) mock_logic.get_account_payee_by_account_id.return_value = mock_response mock_permissions_authorize_many_accounts.return_value = permissions_return account_id = 123 res = fixture_client.get( f'/account/{account_id}/account-payee', headers={ 'Orchard-Requestor-Service': 'graphql-abacus', 'Orchard-Profile-Type': 'DocumentsProfile', 'Orchard-Profile-Id': '1234', 'Orchard-Roles': 'payee_management', 'Orchard-Identity-Id': '1234' } ) assert res.status_code == expected_status if expected_status == 200: mock_logic.get_account_payee_by_account_id.assert_called_once_with(account_id) assert res.json == mock_response.message mock_permissions_authorize_many_accounts.assert_called_once_with( mock_ows_client, 'DocumentsProfile', '1234', [account_id] ) @pytest.mark.parametrize( ( 'profile_type', 'profile_role', 'authorize_return', 'permissions_return', 'expected_status', ), [ pytest.param( 'DocumentsProfile', 'payee_management', None, True, 200, id='Standalone check OK, Permissions check OK' ), pytest.param( 'DocumentsProfile', 'payee_management', None, False, 403, id='Standalone check OK, Permissions check not OK' ), pytest.param( 'Account360Profile', 'account360', True, True, 200, id='Standalone check not OK, PDP check OK' ), pytest.param( 'Account360Profile', 'account360', False, True, 403, id='Standalone check not OK, PDP check not OK' ), ], ) @patch('abacus_account.blueprints.account_payee.ows_client') @patch('abacus_account.blueprints.account_payee.permissions_authorize_many_accounts') @patch('abacus_account.blueprints.account_payee.authorize_many_accounts') @patch('abacus_account.blueprints.account_payee.get_payees_by_ids') def test_dataloader_account_payees_by_ids( mock_get_payees: MagicMock, mock_authorize_many_accounts: MagicMock, mock_permissions_authorize_many_accounts: MagicMock, mock_ows_client: MagicMock, fixture_client: FlaskClient, profile_type: str, profile_role: str, authorize_return: bool | None, permissions_return: bool | None, expected_status: int ): """Test for the get account payees dataloader handler.""" mock_get_payees.return_value = [{ 'account_id': 1, 'account_payee_id': 1, 'reference_payment_type_id': None, }] if authorize_return is not None: mock_authorize_many_accounts.return_value = authorize_return mock_permissions_authorize_many_accounts.return_value = permissions_return post_data = [1, 2] res = fixture_client.post( '/account-payee/dataloader', json=post_data, headers={ 'Orchard-Requestor-Service': 'graphql-abacus', 'Orchard-Profile-Type': profile_type, 'Orchard-Profile-Id': '1234', 'Orchard-Roles': profile_role, 'Orchard-Identity-Id': '1234' } ) assert res.status_code == expected_status if expected_status == 200: mock_get_payees.assert_called_once_with(post_data) assert res.json == {'items': [ { 'data': { 'account_id': 1, 'account_payee_id': 1, 'reference_payment_type_id': None, } }, { 'data': None } ]} if authorize_return is not None: mock_authorize_many_accounts.assert_called_once_with([1]) else: mock_authorize_many_accounts.assert_not_called() if authorize_return is not False: mock_permissions_authorize_many_accounts.assert_called_once_with( mock_ows_client, profile_type, '1234', [1] ) @pytest.mark.parametrize( ( 'profile_type', 'profile_role', 'authorize_return', 'permissions_return', 'expected_status', ), [ pytest.param( 'DocumentsProfile', 'payee_management', None, True, 200, id='Standalone check OK, Permissions check OK' ), pytest.param( 'DocumentsProfile', 'payee_management', None, False, 403, id='Standalone check OK, Permissions check not OK' ), pytest.param( 'Account360Profile', 'account360', True, True, 200, id='Standalone check not OK, PDP check OK' ), pytest.param( 'Account360Profile', 'account360', False, True, 403, id='Standalone check not OK, PDP check not OK' ), ], ) @patch('abacus_account.blueprints.account_payee.ows_client') @patch('abacus_account.blueprints.account_payee.permissions_authorize_many_accounts') @patch('abacus_account.blueprints.account_payee.authorize_many_accounts') @patch('abacus_account.blueprints.account_payee.get_payees_by_account_ids') def test_dataloader_account_payees_by_account_ids( mock_get_payees: MagicMock, mock_authorize_many_accounts: MagicMock, mock_permissions_authorize_many_accounts: MagicMock, mock_ows_client: MagicMock, fixture_client: FlaskClient, profile_type: str, profile_role: str, authorize_return: bool | None, permissions_return: bool | None, expected_status: int ): """Test for the POST /account-payee/dataloader/account handler.""" mock_response = [ { 'account_id': 1, 'payoneer_payee_id': 1, }, {} ] mock_get_payees.return_value = mock_response if authorize_return is not None: mock_authorize_many_accounts.return_value = authorize_return mock_permissions_authorize_many_accounts.return_value = permissions_return post_data = [1, 2] res = fixture_client.post( '/account-payee/dataloader/account', json=post_data, headers={ 'Orchard-Requestor-Service': 'graphql-abacus', 'Orchard-Profile-Type': profile_type, 'Orchard-Profile-Id': '1234', 'Orchard-Roles': profile_role, 'Orchard-Identity-Id': '1234' } ) assert res.status_code == expected_status if expected_status == 200: mock_get_payees.assert_called_once_with(post_data) assert res.json == {'items': [ { 'data': { 'account_id': 1, 'payoneer_payee_id': 1, } }, { 'data': None } ]} if authorize_return is not None: mock_authorize_many_accounts.assert_called_once_with([1, 2]) else: mock_authorize_many_accounts.assert_not_called() if authorize_return is not False: mock_permissions_authorize_many_accounts.assert_called_once_with( mock_ows_client, profile_type, '1234', [1, 2] )