"""Integration tests for ledger_account.""" import requests from tests.integration.conftest import ( LEDGER_API_BASE_URL, ows_abacus_event_api_client, ows_ledger_api_client, ows_royalties_api_client, ) from tests.integration.utils.generic_helper import ( generate_ledger_event, generate_post_ledger_account_body, generate_random_string, ) def _setup(basic_headers) -> None: """Set up test data for the tests.""" ows_ledger_client = ows_ledger_api_client(basic_headers) ows_royalties_client = ows_royalties_api_client(basic_headers) ows_abacus_client = ows_abacus_event_api_client(basic_headers) account_id = 1 contract_name = 'orcd_autotest_{}'.format(generate_random_string(16)) contract = ows_royalties_client.generate_contract_body( contract_name, 'distribution' ) contract.update(account_id=account_id) response_post_contract = ows_royalties_client.post_contract(contract) contract_id = response_post_contract.json()['contract_id'] response_get_accounting_runs = ows_royalties_client.get_accounting_periods() accounting_runs = response_get_accounting_runs.json()['items'] open_period = next( (item for item in accounting_runs if item['closed_date'] is None), None ) accounting_period_id = open_period['accounting_period_id'] _ = ows_royalties_client.get_accounting_period_runs(accounting_period_id) ledger_event = generate_ledger_event( accounting_period_id, target_type='accounting_period' ) response_post_abacus_event = ows_abacus_client.post_abacus_event(ledger_event) abacus_event_id = response_post_abacus_event.json()['abacus_event_id'] post_ledger_account_body = [ generate_post_ledger_account_body(abacus_event_id, account_id, contract_id) ] _ = ows_ledger_client.post_ledger_bulk(post_ledger_account_body) def test_get_ledger_account_info_paginated__authorized( basic_headers, auth_headers, create_run_controller, create_accounting_run, create_account, ) -> None: """Integration test for /ledger-accounts/account/.""" _setup(basic_headers) account_id = 1 response = requests.get( f'{LEDGER_API_BASE_URL}/ledger-accounts/account/{account_id}', headers=auth_headers, ) assert response.status_code == 200, response.text assert response.json() == { 'items': [ { 'ledger_account_id': 1, 'account_id': 1, 'accounting_period_id': 1, 'amount': '-50.50', 'contract_id': 1, 'currency_code': 'USD', 'currency_name': 'US Dollar', 'date': '2020-10-31', 'ending_balance': '-50.50', 'opening_balance': '0.00', 'statement_period_id': 300, 'transaction_type': 'commit_royalties', } ], 'total_count': 1, } def test_get_ledger_account_info_paginated__unauthorized( basic_headers, unauthorized_headers, create_run_controller, create_accounting_run, create_account, ) -> None: """Integration test for /ledger-accounts/account/ for an unauthorized user.""" account_id = 1 response = requests.get( f'{LEDGER_API_BASE_URL}/ledger-accounts/account/{account_id}', headers=unauthorized_headers, ) assert response.status_code == 403, response.text assert response.json() == {'code': 'authorization_error', 'message': 'Unauthorized'}