"""Test ledger_account_contract handlers.""" from unittest.mock import MagicMock, patch import pytest from flask.testing import FlaskClient from owsresponse import response from werkzeug.datastructures import ImmutableMultiDict from ledger.constants import error @pytest.mark.parametrize( ( 'authorize_account_return', 'profile_type', 'profile_role', 'expected_status_code', ), [ pytest.param( None, 'AbacusProfile', 'administrator', 200, id='standalone check passes' ), pytest.param( True, 'Account360Profile', 'account360', 200, id='pdp check authorized' ), pytest.param( False, 'Account360Profile', 'account360', 403, id='pdp check not authorized' ), ], ) @patch('ledger.blueprints.ledger_account_contract.pdp_authorize_resource') @patch('ledger.blueprints.ledger_account_contract.logic') def test_get_payable_balance_by_account( mock_logic: MagicMock, mock_pdp_authorize_resource: MagicMock, authorize_account_return: bool, profile_type: str, profile_role: str, expected_status_code: int, fixture_client: FlaskClient, ) -> None: """Test get payable balance by account_id.""" mock_pdp_authorize_resource.return_value = authorize_account_return account_id = 123 mock_payable_balance = { 'account_id': account_id, 'currency_code': 'EUR', 'payable_balance': '3000.00', } mock_logic.get_payable_balance_by_account.return_value = response.Response( message=mock_payable_balance, status=200 ) res = fixture_client.get( f'/ledger-account-contract/account/{account_id}/payable-balance', 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 = res.json assert res.status_code == expected_status_code if expected_status_code == 403: assert res_json['code'] == error.ERROR_CODE_AUTHORIZATION assert res_json['message'] == 'Unauthorized' else: assert res_json == mock_payable_balance mock_logic.get_payable_balance_by_account.assert_called_once_with(account_id) @pytest.mark.parametrize( ( 'authorize_account_return', 'profile_type', 'profile_role', 'expected_status_code', ), [ pytest.param( None, 'AbacusProfile', 'administrator', 200, id='standalone check passes' ), pytest.param( True, 'Account360Profile', 'account360', 200, id='pdp check authorized' ), pytest.param( False, 'Account360Profile', 'account360', 403, id='pdp check not authorized' ), ], ) @patch('ledger.blueprints.ledger_account_contract.pdp_authorize_resource') @patch('ledger.blueprints.ledger_account_contract.logic') def test_get_ledger_list_by_account_id( mock_logic: MagicMock, mock_pdp_authorize_resource: MagicMock, authorize_account_return: bool, profile_type: str, profile_role: str, expected_status_code: int, fixture_client: FlaskClient, ) -> None: """Test getting list of ledgers by account_id.""" mock_pdp_authorize_resource.return_value = authorize_account_return account_id = 123 mock_ledger_items = [ { 'ledger_account_contract_id': 1, 'account_id': account_id, 'accounting_period_id': 1, 'amount': '320260.99', 'contract_id': 44, 'currency_code': 'USD', 'date': '2020-05-07', 'ending_balance': '320260.99', 'opening_balance': '0.00', 'transaction_type': 'accounting_run', } ] mock_logic.get_ledger_list_by_account_id.return_value = response.Response( message=mock_ledger_items, status=200 ) res = fixture_client.get( f'/ledger-account-contracts/account/{account_id}', 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 = res.json assert res.status_code == expected_status_code if expected_status_code == 403: assert res_json['code'] == error.ERROR_CODE_AUTHORIZATION assert res_json['message'] == 'Unauthorized' mock_logic.get_ledger_list_by_account_id.assert_not_called() else: assert res.json == mock_ledger_items mock_logic.get_ledger_list_by_account_id.assert_called_once_with( account_id, ImmutableMultiDict([]) )