"""Functional tests for the tax reset api.""" from http import HTTPStatus from unittest.mock import patch from uuid import uuid4 from owsrequest.constants.headers import ORCHARD_REQUESTOR_SERVICE import pytest from payee.config import Config from payee.constants.error import ( ERROR_DONT_HAVE_PERMISSIONS, ERROR_NO_VALID_IDENTITY_IN_CONTEXT, ERROR_NOT_PERMITTED_IDENTITY, ) from payee.logic.secure_document import ( get_secure_document_revision, save_secure_document_details_by_payee_id, ) from payee.logic.tax_details import get_tax_details_version from payee.models.account_tax_info import AccountTaxInfo from payee.models.reports_tax_info import ReportsTaxInfo from payee.models.tax_details import TaxDetailsDocument from payee.models.tax_form import TaxFormInfo @patch('payee.connectors.secure_data.manager.uuid4') def test_tax_reset_success_details( mock_uuid4, fixture_client, mock_headers, payoneer_program_fixtures, account_fixtures, account_payee_fixtures, account_tax_info_fixtures, mock_jwt_auth, ) -> None: """Functional: tax reset succeeds.""" mock_jwt_auth.identity = Config.TAX_FORM_SERVICE_IDENTITIES[0] account_payee_id = 1 mock_version = str(uuid4()) tax_details = { 'vat_number': 'GB821018768', 'local_tax_id': '821018768', 'country_of_tax_residency_code': 'USA', 'address_1': 'down the lane1', 'address_2': 'down the lane2', 'province': 'New York', 'city': 'Brooklyn', 'zip': '80021', 'country_code': 'UKR', 'business_name': 'business_name', 'business_number': 'business_number', } mock_uuid4.return_value = mock_version save_secure_document_details_by_payee_id( account_payee_id, TaxDetailsDocument, **tax_details ) assert get_tax_details_version(account_payee_id, '0') is not None res = fixture_client.delete( f'/account-payee/{account_payee_id}/tax-reset', headers={ORCHARD_REQUESTOR_SERVICE: 'graphql-abacus', **mock_headers}, ) assert res.status_code == HTTPStatus.NO_CONTENT assert get_tax_details_version(account_payee_id, '0') is None tax_info = AccountTaxInfo.get_by_id(1) assert tax_info.country_of_tax_residence is None assert tax_info.is_sba_signed is False assert tax_info.is_tax_treaty_claimed is False assert tax_info.tax_employment_type is None assert tax_info.certificate_of_residence_expiration_date is None assert tax_info.is_wht_applicable is True assert tax_info.is_resident_of_spanish_islands is None history_id = next( reversed(tax_info.account_tax_info_history) ).account_tax_info_history_id reports = ReportsTaxInfo.query.filter_by( account_tax_info_history_id=history_id ).all() assert len(reports) == 1 assert reports[0].tax_type == 'details' assert reports[0].revision_id == mock_version @patch('payee.connectors.secure_data.manager.uuid4') def test_tax_reset_success_form( mock_uuid4, fixture_client, mock_headers, payoneer_program_fixtures, account_fixtures, account_payee_fixtures, account_tax_info_fixtures, account_payee_tax_form_info, tax_form_info_secure_documents, mock_jwt_auth, ) -> None: """Functional: tax reset succeeds for tax form case.""" mock_jwt_auth.identity = Config.TAX_FORM_SERVICE_IDENTITIES[0] account_payee_id = 1 mock_version = str(uuid4()) mock_uuid4.return_value = mock_version tax_form_info = TaxFormInfo.query.filter_by( account_payee_id=account_payee_id ).first() res = fixture_client.delete( f'/account-payee/{account_payee_id}/tax-reset', headers={ORCHARD_REQUESTOR_SERVICE: 'graphql-abacus', **mock_headers}, ) assert res.status_code == HTTPStatus.NO_CONTENT, res.text assert ( get_secure_document_revision( account_payee_id, tax_form_info.tax_form_document_class, '0' ) is None ) tax_info = AccountTaxInfo.get_by_id(1) assert tax_info.country_of_tax_residence is None assert tax_info.is_sba_signed is False assert tax_info.is_tax_treaty_claimed is False assert tax_info.tax_employment_type is None assert tax_info.certificate_of_residence_expiration_date is None assert tax_info.is_wht_applicable is True assert tax_info.is_resident_of_spanish_islands is None tax_form_info = TaxFormInfo.query.filter_by( account_payee_id=account_payee_id ).first() assert tax_form_info.deleted_at is not None history_id = next( reversed(tax_info.account_tax_info_history) ).account_tax_info_history_id reports = ReportsTaxInfo.query.filter_by( account_tax_info_history_id=history_id ).all() assert len(reports) == 1 assert reports[0].tax_type == 'form' assert reports[0].revision_id == mock_version def test_tax_reset_failure_permissions( fixture_client, mock_account360_headers, faker, ) -> None: """Functional: tax reset fails for finance user without permissions.""" account_payee_id = faker.pyint() res = fixture_client.delete( f'/account-payee/{account_payee_id}/tax-reset', headers={ ORCHARD_REQUESTOR_SERVICE: 'graphql-abacus', # just some other headers, that are not required abacus headers **mock_account360_headers, }, ) assert res.status_code == HTTPStatus.FORBIDDEN assert res.json == {'code': 'authorization_error', 'message': 'access denied'}