"""Functional tests for Account Tax Info History.""" import datetime from unittest.mock import MagicMock, patch from abacus_common_logic.utils.dates import safe_format_date from flask.testing import FlaskClient import pytest from abacus_account.constants import error from tests.utils.factories import AccountFactory, AccountTaxInfoFactory, \ AccountTaxInfoHistoryFactory @pytest.mark.parametrize( ( 'authorize_many_accounts_return', 'profile_type', 'profile_role', 'expected_status_code', ), [ pytest.param( None, 'AbacusProfile', 'administrator', 200, id='standlone check passes' ), pytest.param( True, 'Account360Profile', 'account360', 200, id='pdp check authorized' ), pytest.param( False, 'Account360Profile', 'account360', 403, id='pdp check not authorized' ) ] ) @patch('abacus_account.blueprints.account_tax_info_history.authorize_many_accounts') def test_get_account_tax_info_history_by_account_id( mock_authorize_many_accounts: MagicMock, fixture_client: FlaskClient, authorize_many_accounts_return: bool, profile_type: str, profile_role: str, expected_status_code: int): """Test to get an account_tax_info_history by the given account_id.""" account = AccountFactory.create() account_tax_info = AccountTaxInfoFactory.create(account=account) history = AccountTaxInfoHistoryFactory.create( account=account, account_tax_info=account_tax_info) mock_authorize_many_accounts.return_value = authorize_many_accounts_return response = fixture_client.get( f'/account/{account.account_id}/account-tax-info-history', headers={ 'Content-Type': 'application/json', 'Orchard-Identity-Id': 'd5ca8ac3-7e51-4793-8775-50d11282504c', 'Orchard-Profile-Id': '35109', 'Orchard-Profile-Type': profile_type, 'Orchard-Roles': profile_role, 'Orchard-Requestor-Service': 'graphql-abacus' } ) res_json = response.json assert response.status_code == expected_status_code if expected_status_code == 403: assert res_json['code'] == error.ERROR_CODE_AUTHORIZATION assert res_json['message'] == 'Unauthorized' else: history_item = res_json[0] assert history_item == { 'account_tax_info_history_id': history.account_tax_info_history_id, 'account_tax_info_id': account_tax_info.account_tax_info_id, 'account_id': account.account_id, 'country_of_tax_residence': account_tax_info.country_of_tax_residence, 'is_sba_signed': account_tax_info.is_sba_signed, 'is_vat_exempt': account_tax_info.is_vat_exempt, 'is_tax_treaty_claimed': account_tax_info.is_tax_treaty_claimed, 'tax_employment_type': account_tax_info.tax_employment_type, 'certificate_of_residence_expiration_date': account_tax_info.certificate_of_residence_expiration_date, 'is_wht_applicable': account_tax_info.is_wht_applicable, 'is_resident_of_spanish_islands': account_tax_info.is_resident_of_spanish_islands, 'wht_rate_override': account_tax_info.wht_rate_override, 'created_at': safe_format_date(history.created_at), 'last_modified': safe_format_date(history.last_modified) } def test_account_tax_info_history_order(fixture_client): """Test account_tax_info_history sorted by created_at.""" account = AccountFactory.create() AccountTaxInfoFactory.create(account=account) history = [ AccountTaxInfoHistoryFactory.create(account=account, created_at=date) for date in ( datetime.date(2022, 6, 1), datetime.date(2022, 4, 1), datetime.date(2022, 2, 1) ) ] response = fixture_client.get( f'/account/{account.account_id}/account-tax-info-history' ) res_json = response.json account_tax_info_history_ids = [ history_item['account_tax_info_history_id'] for history_item in res_json ] account_tax_info_history_ids.sort(reverse=True) assert account_tax_info_history_ids == \ [history[2].account_tax_info_history_id, history[1].account_tax_info_history_id, history[0].account_tax_info_history_id] def test_get_account_tax_info_history_error(fixture_client): """Test to get an account_tax_info_history by the account_id that doesn't exist.""" response = fixture_client.get('/account/NOT_A_VALID_ID/account-tax-info-history') assert response.status_code == 404