"""Functional tests for Account Payee 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, AccountPayeeFactory, \ AccountPayeeHistoryFactory @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_payee_history.authorize_many_accounts') def test_get_account_payee_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_payee_history by the given account_id.""" account = AccountFactory.create() account_payee = AccountPayeeFactory.create(account=account) history = AccountPayeeHistoryFactory.create( account=account, account_payee_id=account_payee.account_payee_id, payoneer_program_id=account_payee.payoneer_program_id, ) mock_authorize_many_accounts.return_value = authorize_many_accounts_return response = fixture_client.get( f'/account/{account.account_id}/account-payee-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_payee_history_id': history.account_payee_history_id, 'account_payee_id': account_payee.account_payee_id, 'account_id': account.account_id, 'payoneer_program_id': account_payee.payoneer_program_id, 'payoneer_payee_id': account_payee.payoneer_payee_id, 'payoneer_payee_name': account_payee.payoneer_payee_name, 'payoneer_iframe_url': account_payee.payoneer_iframe_url, 'payoneer_iframe_url_date': safe_format_date(account_payee.payoneer_iframe_url_date), 'payoneer_session_id': account_payee.payoneer_session_id, 'sap_vendor_id': account_payee.sap_vendor_id, 'reference_payment_type_id': account_payee.reference_payment_type_id, 'payment_description': history.payment_description, 'created_at': safe_format_date(history.created_at), 'last_modified': safe_format_date(history.last_modified) } def test_account_payee_history_order(fixture_client): """Test account_payee_history sorted by created_at.""" account = AccountFactory.create() account_payee = AccountPayeeFactory.create(account=account) history = [ AccountPayeeHistoryFactory.create( account=account, created_at=date, account_payee_id=account_payee.account_payee_id, payoneer_program_id=account_payee.payoneer_program_id, ) 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-payee-history' ) res_json = response.json account_payee_history_ids = [ history_item['account_payee_history_id'] for history_item in res_json ] account_payee_history_ids.sort(reverse=True) assert account_payee_history_ids == \ [history[2].account_payee_history_id, history[1].account_payee_history_id, history[0].account_payee_history_id] def test_get_account_payee_history_error(fixture_client): """Test to get an account_payee_history by the account_id that doesn't exist.""" response = fixture_client.get('/account/NOT_A_VALID_ID/account-payee-history') assert response.status_code == 404