"""PaymentHoldProcessor tests.""" import logging from unittest.mock import MagicMock, patch from faker import Faker import pytest from src.connectors.exceptions import OwsPayeeException from src.models import ( Address, BankFieldDetail, Company, Contact, PayeeDetails, PayoutMethod, ) from src.processors.banking_details.post_banking_details import ( PostBankingDetailsProcessor, ) from src.processors.base import LogEntry from src.processors.exceptions import ProcessingError from tests.unit.factories import PayeeDetailsFactory from tests.utils import create_sample_csv_buffer class TestPostBankingDetailsProcessor: """PostBankingDetailsProcessor test suite.""" def test_process(self, faker: Faker) -> None: """Test process method.""" processor = PostBankingDetailsProcessor(faker.pystr(), faker.pystr()) processor._load_data = MagicMock() # type: ignore processor._post_banking_details = MagicMock() # type: ignore processor.process() assert processor._load_data.called assert processor._post_banking_details.called @patch('src.processors.banking_details.post_banking_details.ows_abacus_account') def test_load_data_success( self, mock_account: MagicMock, ) -> None: """Test loading data from CSV file success.""" buffer = create_sample_csv_buffer( ( ( 'vendorId', 'payeeType', 'firstName', 'lastName', 'dateOfBirth', 'email', 'companyName', 'address1', 'address2', 'city', 'state', 'country', 'postal_code', 'bankAccountType', 'bankCountry', 'currency', 'BankName', 'OtherBankName', 'AccountName', 'AccountNumber', 'Swift', 'IBAN', 'AccountType', 'OtherAccountType', 'RoutingNumber', 'SortCode', 'BIC', 'BankNumber', 'AccountNameEnglish', 'BIN', 'PassportNumber', 'StorefrontURL', 'State', 'IdNumber', 'BankCode', 'BSB', 'IdType', 'AccountTaxNumber', 'BranchName', 'AccountHolderCitizenship', 'CityCode', 'BranchCode', 'Address', 'Suffix', 'CNIC', 'IncorporationNumber', 'ForeignIDNumber', 'TIN', 'ProviceCode', 'SNIC', ), ( '123', 'Individual', 'John', 'Doe', '1/1/90', 'john.doe@example.com', None, '123 Main St', 'Apt 4B', 'New York', 'NY', 'CO', '10001', 'Personal', 'US', 'USD', 'HSBC BANK ARGENTINA SA', None, None, None, None, None, None, None, 'Checking', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, ), ( '456', 'Company', None, None, None, None, 'MegaCorp.', '123 Main St', None, 'Brasília', None, 'BR', '10001', 'Company', 'BR', 'USD', 'Bank Name', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, ), ) ) account_id_to_payee_id_mapping = {123: 15, 456: 17, 341: 25} mock_account.get_payees_by_accounts.return_value = ( account_id_to_payee_id_mapping ) processor = PostBankingDetailsProcessor('', '') processor._input_file_buffer = buffer processor._load_data() assert processor._data == [ PayeeDetails( account_payee_id=15, type='INDIVIDUAL', contact=Contact( first_name='John', last_name='Doe', date_of_birth='1/1/90', email='john.doe@example.com', ), company=Company(name=''), address=Address( country_code='CO', address_1='123 Main St', address_2='Apt 4B', city='New York', province='NY', zip='10001', ), payout_method=PayoutMethod( bank_account_type='PERSONAL', country='US', currency='USD', bank_field_details=[ BankFieldDetail( name='BankName', value='HSBC BANK ARGENTINA SA' ), BankFieldDetail(name='RoutingNumber', value='Checking'), ], ), ), PayeeDetails( account_payee_id=17, type='COMPANY', contact=Contact( first_name='', last_name='', date_of_birth='', email='' ), company=Company(name='MegaCorp.'), address=Address( country_code='BR', address_1='123 Main St', address_2='', city='Brasília', province='', zip='10001', ), payout_method=PayoutMethod( bank_account_type='COMPANY', country='BR', currency='USD', bank_field_details=[ BankFieldDetail(name='BankName', value='Bank Name') ], ), ), ] def test_load_data_failure_no_file(self) -> None: """Test loading data from CSV file failure.""" processor = PostBankingDetailsProcessor('', '') with pytest.raises(ProcessingError, match='Unable to read the file'): processor._load_data() @patch('src.processors.banking_details.post_banking_details.ows_payee') @patch('src.processors.banking_details.post_banking_details.logger') def test_post_banking_details_success( self, mock_logger: MagicMock, mock_ows_payee: MagicMock, faker: Faker ) -> None: processor = PostBankingDetailsProcessor(faker.pystr(), faker.pystr()) payee = PayeeDetailsFactory.build() processor._data = [payee] processor._post_banking_details() mock_ows_payee.save_bank_details.assert_called_once_with(payee) mock_logger.error.assert_not_called() @patch('src.processors.banking_details.post_banking_details.ows_payee') def test_post_banking_details_failure( self, mock_ows_payee: MagicMock, faker: Faker ) -> None: processor = PostBankingDetailsProcessor(faker.pystr(), faker.pystr()) payee = PayeeDetailsFactory.build() processor._data = [payee] mock_ows_payee.save_bank_details.side_effect = OwsPayeeException( f'exception for {payee.account_payee_id}' ) processor._post_banking_details() mock_ows_payee.save_bank_details.assert_called_once_with(payee) assert processor._logs == { logging.ERROR: [ LogEntry(f'ows-payee failure: exception for {payee.account_payee_id}') ] }