"""Functional tests for PaymentHoldHistory.""" import datetime from unittest.mock import MagicMock, patch from flask.testing import FlaskClient import pytest from abacus_account.constants import error from tests.utils.factories import AccountFactory, PaymentHoldFactory, \ PaymentHoldHistoryFactory @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.payment_hold_history.authorize_many_accounts') def test_get_payment_hold_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): """Tests that we get the payment hold history for a given account ID.""" account = AccountFactory.create() payment_hold = PaymentHoldFactory.create(account=account) payment_hold_history = PaymentHoldHistoryFactory.create( account=account, payment_hold=payment_hold) mock_authorize_many_accounts.return_value = authorize_many_accounts_return response = fixture_client.get( f'/account/{account.account_id}/payment-hold-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: current_hold = res_json[0] assert current_hold == { 'payment_hold_id': payment_hold.payment_hold_id, 'account_id': account.account_id, 'is_on_hold': payment_hold.is_on_hold, 'start_date': payment_hold.start_date.isoformat(), 'created_at': payment_hold.created_at.strftime('%Y-%m-%d'), 'created_by': payment_hold.created_by, 'reason': payment_hold.reason, 'last_modified': payment_hold.last_modified.strftime('%Y-%m-%d'), 'last_modified_by': payment_hold.last_modified_by, } historical_hold = res_json[1] assert historical_hold == { 'payment_hold_history_id': payment_hold_history.payment_hold_history_id, 'payment_hold_id': payment_hold.payment_hold_id, 'account_id': account.account_id, 'is_on_hold': payment_hold.is_on_hold, 'start_date': payment_hold.start_date.isoformat(), 'end_date': payment_hold_history.end_date.isoformat(), 'created_at': payment_hold.created_at.strftime('%Y-%m-%d'), 'created_by': payment_hold.created_by, 'last_modified': payment_hold.last_modified.strftime('%Y-%m-%d'), 'last_modified_by': payment_hold.last_modified_by, 'reason': payment_hold.reason } def test_payment_hold_history_order(fixture_client): """Tests that payment hold history is sorted by created_at, most recent first.""" account = AccountFactory.create() PaymentHoldFactory.create(account=account) history_items = [ PaymentHoldHistoryFactory.create(account=account, created_at=date) for date in ( datetime.date(2019, 6, 1), datetime.date(2019, 4, 1), datetime.date(2019, 2, 1) ) ] response = fixture_client.get(f'/account/{account.account_id}/payment-hold-history') res_json = response.json res_json.pop(0) payment_hold_history_ids = [ history_item['payment_hold_history_id'] for history_item in res_json ] payment_hold_history_ids.sort(reverse=True) assert payment_hold_history_ids == \ [history_items[2].payment_hold_history_id, history_items[1].payment_hold_history_id, history_items[0].payment_hold_history_id] def test_get_payment_hold_history_error(fixture_client): """Tests that we get an error if an unrecognized account ID is passed in.""" response = fixture_client.get('account/NOT_A_VALID_ID/payment-hold-history') assert response.status_code == 404