"""Test KYC notification logic.""" from unittest import mock import pytest from src.logic import kyc_notification from src.models.ows_abacus_account import AccountPayeeUpdateModel from src.models.payee import Payee, PayeeEntityType from tests.unit.factories import ( AccountPayeeFactory, AccountPaymentTermFactory, ReferencePaymentEntityFactory, ) @pytest.mark.parametrize( 'set_states_result,is_paychex', ((True, False), (True, True), (False, False)) ) @mock.patch('src.logic.kyc_notification.is_feature_enabled', return_value=False) @mock.patch('src.logic.kyc_notification.update_account_payee') @mock.patch('src.logic.kyc_notification.get_account_payee') @mock.patch('src.logic.kyc_notification.set_states') @mock.patch('src.logic.kyc_notification.delete_kyc_notifications') def test_handle_kyc_approved( mocked_delete_kyc_notifications: mock.Mock, mocked_set_states: mock.Mock, mocked_get_account_payee: mock.Mock, mocked_update_account_payee: mock.Mock, mocked_is_feature_enabled: mock.Mock, set_states_result: bool, is_paychex: bool, ) -> None: """Test handle_kyc_approved.""" payee = Payee(payee_type=PayeeEntityType.account_payee, payee_id='145') mocked_set_states.return_value = set_states_result mocked_get_account_payee.return_value = AccountPayeeFactory.build( reference_payment_type_id=10 if is_paychex else 9 ) kyc_notification.handle_kyc_approved(payee) mocked_get_account_payee.assert_called_once_with(int(payee.payee_id)) mocked_set_states.assert_called_once_with( payee, 'KYC APPROVED', banking_details_review_status='approved', payment_eligibility_status='running', ) if set_states_result: mocked_delete_kyc_notifications.assert_called_once_with(payee) else: mocked_delete_kyc_notifications.assert_not_called() if set_states_result and is_paychex: mocked_update_account_payee.assert_called_once_with( int(payee.payee_id), AccountPayeeUpdateModel(reference_payment_type_id=8), ) else: mocked_update_account_payee.assert_not_called() @pytest.mark.parametrize( 'set_states_result,is_paychex', ((True, False), (True, True), (False, False)) ) @mock.patch('src.logic.kyc_notification.get_reference_payment_entity') @mock.patch('src.logic.kyc_notification.get_account_payment_term') @mock.patch('src.logic.kyc_notification.is_feature_enabled', return_value=True) @mock.patch('src.logic.kyc_notification.update_account_payee') @mock.patch('src.logic.kyc_notification.get_account_payee') @mock.patch('src.logic.kyc_notification.set_states') @mock.patch('src.logic.kyc_notification.delete_kyc_notifications') def test_handle_kyc_approved_awal( mocked_delete_kyc_notifications: mock.Mock, mocked_set_states: mock.Mock, mocked_get_account_payee: mock.Mock, mocked_update_account_payee: mock.Mock, mocked_is_feature_enabled: mock.Mock, mocked_get_account_payment_term: mock.Mock, mocked_get_reference_payment_entity: mock.Mock, set_states_result: bool, is_paychex: bool, ) -> None: """Test handle_kyc_approved with AWAL payee and FF enabled.""" payee = Payee(payee_type=PayeeEntityType.account_payee, payee_id='145') account_payee = AccountPayeeFactory.build( reference_payment_type_id=10 if is_paychex else 9 ) mocked_set_states.return_value = set_states_result mocked_get_account_payee.return_value = account_payee mocked_get_account_payment_term.return_value = AccountPaymentTermFactory.build( payment_entity_id=1 ) mocked_get_reference_payment_entity.return_value = ( ReferencePaymentEntityFactory.build(payment_entity_name='AWAL-UK') ) kyc_notification.handle_kyc_approved(payee) mocked_get_account_payee.assert_called_once_with(int(payee.payee_id)) mocked_get_account_payment_term.assert_called_once_with(account_payee.account_id) mocked_set_states.assert_called_once_with( payee, 'KYC APPROVED', banking_details_review_status='approved', payment_eligibility_status='approved', ) if set_states_result: mocked_delete_kyc_notifications.assert_called_once_with(payee) else: mocked_delete_kyc_notifications.assert_not_called() if set_states_result and is_paychex: mocked_update_account_payee.assert_called_once_with( int(payee.payee_id), AccountPayeeUpdateModel(reference_payment_type_id=8), ) else: mocked_update_account_payee.assert_not_called() @mock.patch('src.logic.kyc_notification.get_reference_payment_entity') @mock.patch('src.logic.kyc_notification.get_account_payment_term') @mock.patch('src.logic.kyc_notification.is_feature_enabled', return_value=True) @mock.patch('src.logic.kyc_notification.update_account_payee') @mock.patch('src.logic.kyc_notification.get_account_payee') @mock.patch('src.logic.kyc_notification.set_states') @mock.patch('src.logic.kyc_notification.delete_kyc_notifications') def test_handle_kyc_approved_non_awal_with_ff_enabled( mocked_delete_kyc_notifications: mock.Mock, mocked_set_states: mock.Mock, mocked_get_account_payee: mock.Mock, mocked_update_account_payee: mock.Mock, mocked_is_feature_enabled: mock.Mock, mocked_get_account_payment_term: mock.Mock, mocked_get_reference_payment_entity: mock.Mock, ) -> None: """Test handle_kyc_approved with non-AWAL payee and FF enabled.""" payee = Payee(payee_type=PayeeEntityType.account_payee, payee_id='145') account_payee = AccountPayeeFactory.build(reference_payment_type_id=9) mocked_set_states.return_value = True mocked_get_account_payee.return_value = account_payee mocked_get_account_payment_term.return_value = AccountPaymentTermFactory.build( payment_entity_id=5 ) mocked_get_reference_payment_entity.return_value = ( ReferencePaymentEntityFactory.build(payment_entity_name='ORCHARD-UK') ) kyc_notification.handle_kyc_approved(payee) mocked_set_states.assert_called_once_with( payee, 'KYC APPROVED', banking_details_review_status='approved', payment_eligibility_status='running', ) @mock.patch('src.logic.kyc_notification.get_account_payment_term') @mock.patch('src.logic.kyc_notification.is_feature_enabled', return_value=True) @mock.patch('src.logic.kyc_notification.update_account_payee') @mock.patch('src.logic.kyc_notification.get_account_payee') @mock.patch('src.logic.kyc_notification.set_states') @mock.patch('src.logic.kyc_notification.delete_kyc_notifications') def test_handle_kyc_approved_no_payment_entity_with_ff_enabled( mocked_delete_kyc_notifications: mock.Mock, mocked_set_states: mock.Mock, mocked_get_account_payee: mock.Mock, mocked_update_account_payee: mock.Mock, mocked_is_feature_enabled: mock.Mock, mocked_get_account_payment_term: mock.Mock, ) -> None: """Test handle_kyc_approved with no payment_entity_id and FF enabled.""" payee = Payee(payee_type=PayeeEntityType.account_payee, payee_id='145') account_payee = AccountPayeeFactory.build(reference_payment_type_id=9) mocked_set_states.return_value = True mocked_get_account_payee.return_value = account_payee mocked_get_account_payment_term.return_value = AccountPaymentTermFactory.build( payment_entity_id=None ) kyc_notification.handle_kyc_approved(payee) mocked_set_states.assert_called_once_with( payee, 'KYC APPROVED', banking_details_review_status='approved', payment_eligibility_status='running', ) @pytest.mark.parametrize( 'reason,set_states_result', (('some_reason', True), (None, False)) ) @mock.patch('src.logic.kyc_notification.set_states') @mock.patch('src.logic.kyc_notification.delete_kyc_notifications') def test_handle_kyc_declined( mocked_delete_kyc_notifications: mock.Mock, mocked_set_states: mock.Mock, reason: str | None, set_states_result: bool, ) -> None: """Test handle_kyc_declined.""" payee = Payee(payee_type=PayeeEntityType.account_payee, payee_id='153') mocked_set_states.return_value = set_states_result kyc_notification.handle_kyc_declined(payee, reason) mocked_set_states.assert_called_once_with( payee, reason or 'KYC DECLINED', banking_details_review_status='rejected', ) if set_states_result: mocked_delete_kyc_notifications.assert_called_once_with(payee) else: mocked_delete_kyc_notifications.assert_not_called() @pytest.mark.parametrize( 'file_upload_link,requirement_id,sub_requirement_id,set_states_result', ( ('test_link', 'test_req', 'test_sub_req', True), ('test_lnk', None, None, False), ), ) @mock.patch('src.logic.kyc_notification.set_states') @mock.patch('src.logic.kyc_notification.delete_kyc_notifications') @mock.patch('src.logic.kyc_notification.create_kyc_notification') def test_handle_kyc_notification( mocked_create_kyc_notification: mock.Mock, mocked_delete_kyc_notifications: mock.Mock, mocked_set_states: mock.Mock, file_upload_link: str, requirement_id: str | None, sub_requirement_id: str | None, set_states_result: bool, ) -> None: """Test handle_kyc_notification.""" payee = Payee(payee_type=PayeeEntityType.account_payee, payee_id='343') mocked_set_states.return_value = set_states_result kyc_notification.handle_kyc_notification( payee, file_upload_link, requirement_id, sub_requirement_id ) mocked_set_states.assert_called_once_with( payee, 'KYC NOTIFICATION', banking_details_review_status='running' ) if set_states_result: mocked_delete_kyc_notifications.assert_called_once_with(payee) mocked_create_kyc_notification.assert_called_once_with( payee=payee, file_upload_link=file_upload_link, requirement_id=requirement_id, sub_requirement_id=sub_requirement_id, ) else: mocked_delete_kyc_notifications.assert_not_called() mocked_create_kyc_notification.assert_not_called() @pytest.mark.parametrize( ( 'file_upload_link,requirement_type_id,sub_requirement_status_id' ',possible_sub_requirement_types,entity_reference_id,entity_reference_type_id,set_states_result' ), ( ('test_link', 5, 100, '1,7,101', 'uuid4_id', 386, True), ('test_lnk', None, None, None, None, None, False), ), ) @mock.patch('src.logic.kyc_notification.set_states') @mock.patch('src.logic.kyc_notification.delete_kyc_notifications') @mock.patch('src.logic.kyc_notification.create_kyc_notification') def test_handle_kyc_requirement_reopen( mocked_create_kyc_notification: mock.Mock, mocked_delete_kyc_notifications: mock.Mock, mocked_set_states: mock.Mock, file_upload_link: str, requirement_type_id: int | None, sub_requirement_status_id: int | None, possible_sub_requirement_types: str | None, entity_reference_id: str | None, entity_reference_type_id: int | None, set_states_result: bool, ) -> None: """Test handle_kyc_requirement_reopen.""" payee = Payee(payee_type=PayeeEntityType.account_payee, payee_id='343') mocked_set_states.return_value = set_states_result kyc_notification.handle_kyc_requirement_reopen( payee, file_upload_link, requirement_type_id, sub_requirement_status_id, possible_sub_requirement_types, entity_reference_id, entity_reference_type_id, ) mocked_set_states.assert_called_once_with( payee, 'KYC RE-OPENED', banking_details_review_status='running', ) if set_states_result: mocked_delete_kyc_notifications.assert_called_once_with(payee) mocked_create_kyc_notification.assert_called_once_with( payee=payee, file_upload_link=file_upload_link, requirement_type_id=requirement_type_id, sub_requirement_status_id=sub_requirement_status_id, possible_sub_requirement_types=possible_sub_requirement_types, entity_reference_id=entity_reference_id, entity_reference_type_id=entity_reference_type_id, ) else: mocked_delete_kyc_notifications.assert_not_called() mocked_create_kyc_notification.assert_not_called()