"""Test Account Tax Info handlers.""" from datetime import date from unittest.mock import call, MagicMock, patch from flask.testing import FlaskClient from owsresponse import response from owsresponse.adaptors.flask import flaskify import pytest @patch('abacus_account.blueprints.account_tax_info.logic') def test_get_account_tax_info_list_success( mock_logic, fixture_client, faker ): """Test to get get_account_tax_info_list.""" mock_response = response.Response(message={'result': 'ok'}, status=200) mock_logic.get_account_tax_info_list.return_value = mock_response limit = faker.pyint(1, 100) offset = faker.pyint(0, 10) account_ids = faker.pylist(allowed_types=[int]) res = fixture_client.post( f'/accounts/account-tax-info?' f'limit={limit}&offset={offset}&' f'certificate_of_residence_expiration_date_start=2023-01-01&' f'certificate_of_residence_expiration_date_end=2023-12-31', json=account_ids ) assert res.status_code == 200 assert mock_logic.get_account_tax_info_list.call_args_list == [ call( limit=limit, offset=offset, account_ids=account_ids, certificate_of_residence_expiration_date_start=date(2023,1,1), certificate_of_residence_expiration_date_end=date(2023,12,31) ) ] @patch('abacus_account.blueprints.account_tax_info.logic') def test_get_account_tax_info_list_failure_validation( mock_logic, fixture_client, faker ): """Test to get get_account_tax_info_list validations errors.""" mock_response = response.Response(message={'result': 'ok'}, status=200) mock_logic.get_account_tax_info_list.return_value = mock_response res = fixture_client.post( '/accounts/account-tax-info?limit=1abc&offset=2efg' ) assert res.status_code == 415 assert not mock_logic.get_account_tax_info_list.called mock_logic.reset_mock() res = fixture_client.post( '/accounts/account-tax-info', json=['1a', '2b', '3c'] ) assert res.status_code == 400 assert not mock_logic.get_account_tax_info_list.called @pytest.mark.parametrize( ( 'profile_type', 'profile_role', 'authorize_return', 'permissions_return', 'expected_status', ), [ pytest.param( 'MoneyhubProfile', 'administrator', None, True, 200, id='Standalone check OK, Permissions check OK' ), pytest.param( 'MoneyhubProfile', 'administrator', 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_tax_info.ows_client') @patch('abacus_account.blueprints.account_tax_info.permissions_authorize_many_accounts') @patch('abacus_account.blueprints.account_tax_info.authorize_many_accounts') @patch('abacus_account.logic.account_tax_info.get_account_tax_info_by_account_id') def test_get_account_tax_info_by_account_id( mock_logic: 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 GET endpoint to get account tax info by account id.""" mock_response = response.Response(message={ 'account_tax_info_id': 123, 'account_id': 567, 'country_of_tax_residence': 'usa', 'is_sba_signed': True, 'is_vat_exempt': False, 'is_tax_treaty_claimed': False }, status=200) mock_logic.return_value = mock_response mock_authorize_many_accounts.return_value = authorize_return mock_permissions_authorize_many_accounts.return_value = permissions_return account_id = 123456789 res = fixture_client.get( f'/account/{account_id}/account-tax-info/', 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: assert res.json == mock_response.message mock_logic.assert_called_once_with(account_id) if authorize_return is not None: mock_authorize_many_accounts.assert_called_once_with([account_id]) 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', [account_id] ) @pytest.mark.parametrize( ( 'profile_type', 'profile_role', 'authorize_return', 'permissions_return', 'expected_status', ), [ pytest.param( 'MoneyhubProfile', 'administrator', None, True, 200, id='Standalone check OK, Permissions check OK' ), pytest.param( 'MoneyhubProfile', 'administrator', 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_tax_info.ows_client') @patch('abacus_account.blueprints.account_tax_info.permissions_authorize_many_accounts') @patch('abacus_account.blueprints.account_tax_info.authorize_many_accounts') @patch('abacus_account.blueprints.account_tax_info.ItemView.get') def test_get_account_tax_info( mock_logic: 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 GET endpoint to get account tax info by account tax info ids.""" account_tax_info_id = 123 account_id = 567 mock_response = flaskify(response.Response(message={ 'account_tax_info_id': account_tax_info_id, 'account_id': account_id, 'country_of_tax_residence': 'usa', 'is_sba_signed': True, 'is_vat_exempt': False, 'is_tax_treaty_claimed': False }, status=200)) mock_logic.return_value = mock_response mock_authorize_many_accounts.return_value = authorize_return mock_permissions_authorize_many_accounts.return_value = permissions_return res = fixture_client.get( f'/account-tax-info/{account_tax_info_id}', 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: assert res.json == mock_response.json mock_logic.assert_called_once_with(account_tax_info_id) if authorize_return is not None: mock_authorize_many_accounts.assert_called_once_with([account_id]) 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', [account_id] ) @pytest.mark.parametrize( ( 'profile_type', 'profile_role', 'permissions_return', 'expected_status', ), [ pytest.param( 'DocumentsProfile', 'payee_management', True, 200, id='Standalone check OK, Permissions check OK' ), pytest.param( 'DocumentsProfile', 'payee_management', False, 403, id='Standalone check OK, Permissions check not OK' ), ], ) @patch('abacus_account.blueprints.account_tax_info.ows_client') @patch('abacus_account.blueprints.account_tax_info.permissions_authorize_many_accounts') @patch('abacus_account.blueprints.account_tax_info.ItemView.get') @patch('abacus_account.blueprints.account_tax_info.ItemView.put') def test_put_account_tax_info( mock_logic_put: MagicMock, mock_logic_get: MagicMock, mock_permissions_authorize_many_accounts: MagicMock, mock_ows_client: MagicMock, fixture_client: FlaskClient, profile_type: str, profile_role: str, permissions_return: bool | None, expected_status: int ): """Test put account tax info handler.""" account_tax_info_id = 123 account_id = 456 mock_response = flaskify(response.Response(message={ 'account_tax_info_id': account_tax_info_id, 'account_id': account_id, 'country_of_tax_residence': 'usa', 'is_sba_signed': True, 'is_vat_exempt': False, 'is_tax_treaty_claimed': False }, status=200)) mock_logic_get.return_value = mock_response mock_logic_put.return_value = mock_response mock_permissions_authorize_many_accounts.return_value = permissions_return res = fixture_client.put( f'/account-tax-info/{account_tax_info_id}', headers={ 'Orchard-Requestor-Service': 'graphql-abacus', 'Orchard-Profile-Type': profile_type, 'Orchard-Profile-Id': '1234', 'Orchard-Roles': profile_role, 'Orchard-Identity-Id': '1234' }, data={ 'country_of_tax_residence': 'usa', } ) assert res.status_code == expected_status if expected_status == 200: mock_logic_put.assert_called_once_with(account_tax_info_id) assert res.json == mock_response.json mock_permissions_authorize_many_accounts.assert_called_once_with( mock_ows_client, profile_type, '1234', [account_id] )