"""Functional tests for LedgerAccount.""" from decimal import Decimal from typing import Any from unittest.mock import MagicMock, patch import pytest from flask.testing import FlaskClient from ledger.constants import error from tests.utils.factories import LedgerAccountFactory def test_get_current_balance(fixture_client, mock_event_fixtures): """Test to get the current balance of a account.""" LedgerAccountFactory.create(account_id=1, current_balance=Decimal('100.00')) LedgerAccountFactory.create(account_id=50, current_balance=Decimal('200.00')) res = fixture_client.get('/ledger-account/account/1/current-balance') assert res.status_code == 200 assert res.json == { 'currency_code': 'USD', 'currency_name': 'US Dollar', 'current_balance': '100.00', } def test_get_ledger_account_info(fixture_client, mock_event_fixtures): """Test to get ledger account information.""" ledger_account = LedgerAccountFactory.create( account_id=1, currency_code='USD', currency_amount=67614.28, previous_balance=0, current_balance=67614.28, contract_id=1, abacus_event_id=1, note='credit ledger item', ) res = fixture_client.get('/ledger-account/account/1') assert res.status_code == 200 assert res.json == [ { 'ledger_account_id': ledger_account.ledger_account_id, 'account_id': ledger_account.account_id, 'accounting_period_id': None, 'amount': str(ledger_account.currency_amount), 'contract_id': ledger_account.contract_id, 'currency_code': ledger_account.currency_code, 'currency_name': 'US Dollar', 'date': '2020-01-01', 'ending_balance': str(ledger_account.current_balance), 'opening_balance': str(ledger_account.previous_balance), 'statement_period_id': 1, 'transaction_type': 'take_reserves', } ] @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.pdp_authorize_resource') def test_get_ledger_account_info_paginated( mock_pdp_authorize_resource: MagicMock, authorize_account_return: bool, profile_type: str, profile_role: str, expected_status_code: int, fixture_client: FlaskClient, mock_event_fixtures: Any, ) -> None: """Test to get ledger account information.""" mock_pdp_authorize_resource.return_value = authorize_account_return ledger_account = LedgerAccountFactory.create( account_id=1, currency_code='USD', currency_amount=67614.28, previous_balance=0, current_balance=67614.28, contract_id=1, abacus_event_id=1, note='credit ledger item', ) res = fixture_client.get( '/ledger-accounts/account/1', 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['total_count'] == 1 assert res.json['items'] == [ { 'ledger_account_id': ledger_account.ledger_account_id, 'account_id': ledger_account.account_id, 'accounting_period_id': None, 'amount': str(ledger_account.currency_amount), 'contract_id': ledger_account.contract_id, 'currency_code': ledger_account.currency_code, 'currency_name': 'US Dollar', 'date': '2020-01-01', 'ending_balance': str(ledger_account.current_balance), 'opening_balance': str(ledger_account.previous_balance), 'statement_period_id': 1, 'transaction_type': 'take_reserves', } ] def test_get_ledger_accounts(fixture_client, mock_event_fixtures): """Test to get ledger accounts.""" LedgerAccountFactory.create( account_id=1, currency_code='USD', currency_amount=67614.28, previous_balance=0, current_balance=67614.28, note='credit ledger item', ) res = fixture_client.get('/ledger-account/accounting-period/1/accounts') assert res.status_code == 200 assert res.json['items'] == [ { 'abacus_event_id': 1, 'account_id': 1, 'contract_id': 1, 'currency_amount': '67614.28', 'ledger_account_id': 1, 'currency_code': 'USD', } ] assert res.json['total_count'] == 1