"""Functional tests for account_payment_term.""" from unittest.mock import MagicMock, patch from flask.testing import FlaskClient import pytest from abacus_account.constants import constants from abacus_account.constants import error from tests.utils.factories import AccountFactory from tests.utils.factories import AccountPaymentTermFactory def test_create_account_payment_term( reference_payment_entity_fixture, fixture_client ): """Test to create an account payment term.""" account = AccountFactory.create() post_body = { 'account_id': account.account_id, 'currency_code': 'USD', 'payment_minimum': '67.00', 'payment_schedule': constants.PAYMENT_SCHEDULE.SCHEDULE_30_DAYS_MONTH, 'payment_entity_id': 5, 'agreement_type_id': 1 } res = fixture_client.post('/account-payment-term/', json=post_body) assert res.status_code == 201 assert res.json == { **post_body, 'account_payment_term_id': res.json['account_payment_term_id'], 'payment_entity_id': 5 } def test_update_account_payment_term( reference_payment_entity_fixture, fixture_client ): """Test to update an account payment term.""" payment_entity_id = 5 account_payment_term = AccountPaymentTermFactory.create( payment_entity_id=payment_entity_id ) account_payment_term_id = account_payment_term.account_payment_term_id currency_code = 'AUD' res = fixture_client.put( f'/account-payment-term/{account_payment_term_id}', json=dict(currency_code=currency_code, payment_entity_id=payment_entity_id) ) assert res.status_code == 200 assert res.json == { 'account_payment_term_id': account_payment_term_id, 'account_id': account_payment_term.account_id, 'currency_code': currency_code, 'payment_minimum': str(account_payment_term.payment_minimum), 'payment_schedule': account_payment_term.payment_schedule, 'agreement_type_id': account_payment_term.agreement_type_id, 'payment_entity_id': payment_entity_id } def test_update_account_payment_term_error(fixture_client): """Test to update an account payment term that doesn't exist.""" account_payment_term_id = 1 currency_code = 'AUD' res = fixture_client.put( f'/account-payment-term/{account_payment_term_id}', json=dict(currency_code=currency_code) ) assert res.status_code == 404 assert res.json['message'] == \ error.ERROR_ENTITY_DOES_NOT_EXIST.format( object_type='AccountPaymentTerm', object_id=account_payment_term_id ) def test_update_account_payment_term_error_knr_account_edit_agreement_type( reference_payment_entity_fixture, fixture_client ): """Test to update an account payment term by .""" account_payment_term = AccountPaymentTermFactory.create() currency_code = 'USD' res = fixture_client.put( f'/account-payment-term/{account_payment_term.account_payment_term_id}', json=dict( currency_code=currency_code, payment_entity_id=4, agreement_type_id=3, ) ) assert res.status_code == 400 assert res.json['message'] == error.ERROR_KNR_ACCOUNT_CANT_MODIFY_AGREEMENT_TYPE.\ format(payment_term_id=account_payment_term.account_payment_term_id) @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_payment_term.authorize_many_accounts') def test_get_account_payment_term_by_account_id( mocker_authorize_many_accounts: MagicMock, reference_payment_entity_fixture, fixture_client: FlaskClient, authorize_many_accounts_return: bool, profile_type: str, profile_role: str, expected_status_code: int): """Test to get an account payment term by an account_id.""" account_payment_term = AccountPaymentTermFactory.create() account_id = account_payment_term.account_id mocker_authorize_many_accounts.return_value = authorize_many_accounts_return res = fixture_client.get( f'/account/{account_id}/account-payment-term/', 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' } ) assert res.status_code == expected_status_code if expected_status_code == 403: assert res.json['message'] == 'Unauthorized' assert res.json['code'] == error.ERROR_CODE_AUTHORIZATION else: assert res.json == { 'account_payment_term_id': account_payment_term.account_payment_term_id, 'account_id': account_id, 'currency_code': account_payment_term.currency_code, 'payment_entity_id': account_payment_term.payment_entity_id, 'payment_minimum': str(account_payment_term.payment_minimum), 'payment_schedule': account_payment_term.payment_schedule, 'agreement_type_id': account_payment_term.agreement_type_id } @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_payment_term.authorize_many_accounts') def test_get_account_payment_term_by_account_id_dataloaded( mock_authorize_many_accounts: MagicMock, reference_payment_entity_fixture, fixture_client: FlaskClient, authorize_many_accounts_return: bool, profile_type: str, profile_role: str, expected_status_code: int): """Test to get an account payment term by an account_id dataloaded.""" account_payment_term = AccountPaymentTermFactory.create() account_id = account_payment_term.account_id mock_authorize_many_accounts.return_value = authorize_many_accounts_return res = fixture_client.post( '/account/account-payment-term/dataloader', json=[account_id, 12345], 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' } ) assert res.status_code == expected_status_code if expected_status_code == 403: assert res.json['message'] == 'Unauthorized' assert res.json['code'] == error.ERROR_CODE_AUTHORIZATION else: assert res.json == [ { 'data': { 'account_payment_term_id': account_payment_term.account_payment_term_id, 'account_id': account_id, 'currency_code': account_payment_term.currency_code, 'payment_entity_id': account_payment_term.payment_entity_id, 'payment_minimum': str(account_payment_term.payment_minimum), 'payment_schedule': account_payment_term.payment_schedule, 'agreement_type_id': account_payment_term.agreement_type_id } }, { 'data': None } ] def test_get_payment_term_by_payment_term_id( reference_payment_entity_fixture, fixture_client ): """Test to get an account payment term by an account_payment_term_id.""" account_payment_term = AccountPaymentTermFactory.create() account_payment_term_id = account_payment_term.account_payment_term_id res = fixture_client.get(f'/account-payment-term/{account_payment_term_id}/') assert res.status_code == 200 assert res.json == { 'account_payment_term_id': account_payment_term.account_payment_term_id, 'account_id': account_payment_term.account_id, 'currency_code': account_payment_term.currency_code, 'payment_minimum': str(account_payment_term.payment_minimum), 'payment_schedule': account_payment_term.payment_schedule, 'agreement_type_id': account_payment_term.agreement_type_id, 'payment_entity_id': account_payment_term.payment_entity_id } def test_get_payment_term_by_payment_term_id_error(fixture_client): """Test getting account payment term that doesn't exist.""" account_payment_term_id = 1342 res = fixture_client.get(f'/account-payment-term/{account_payment_term_id}/') assert res.status_code == 404 assert res.json['message'] == \ error.ERROR_ENTITY_DOES_NOT_EXIST.format( object_type='AccountPaymentTerm', object_id=account_payment_term_id ) def test_get_account_payment_terms_snapshot_tsv( reference_payment_entity_fixture, fixture_client ): """Test to get account payment terms in tsv format.""" account_1 = AccountFactory.create(account_id=1) account_2 = AccountFactory.create(account_id=2) AccountPaymentTermFactory.create( account=account_1, ) AccountPaymentTermFactory.create( account=account_2, payment_minimum=555.55 ) result = fixture_client.post('/account-payment-terms/snapshot', json=[]) assert result.status_code == 200 assert result.data.decode() == 'account_payment_term_id\taccount_id\t' \ 'currency_code\tpayment_minimum\tpayment_entity_id\tpayment_schedule\t' \ 'agreement_type_id\n' \ '1\t1\tUSD\t35.00\t5\t30_days_after_month_end\t1\n' \ '2\t2\tUSD\t555.55\t5\t30_days_after_month_end\t1\n' def test_get_account_payment_terms_snapshot_tsv_filtered( reference_payment_entity_fixture, fixture_client ): """Test to get account payment terms in tsv format filtered by account ids.""" accounts = list() for account_id in range(1, 5): accounts.append(AccountFactory.create(account_id=account_id)) AccountPaymentTermFactory.create(account=accounts[0]) AccountPaymentTermFactory.create( account=accounts[1], payment_minimum=555.55 ) AccountPaymentTermFactory.create(account=accounts[2]) AccountPaymentTermFactory.create( account=accounts[3], payment_minimum=111 ) result = fixture_client.post('/account-payment-terms/snapshot', json=[1, 4]) assert result.status_code == 200 assert result.data.decode() == 'account_payment_term_id\taccount_id\t' \ 'currency_code\tpayment_minimum\tpayment_entity_id\tpayment_schedule\t' \ 'agreement_type_id\n' \ '1\t1\tUSD\t35.00\t5\t30_days_after_month_end\t1\n' \ '4\t4\tUSD\t111.00\t5\t30_days_after_month_end\t1\n'