"""Tests for ows-payee connector.""" from unittest.mock import patch import pytest from src.connectors.ows_payee import OwsPayee from src.utils.error_handling import BankDetailsException from src.utils.error_handling import MISSING_BANK from src.utils.error_handling import MISSING_TAX from src.utils.error_handling import OwsServiceException from src.utils.error_handling import TaxDetailsException SERVICE = 'ows-payee' ACCOUNT_PAYEE_ID = 123 @patch('src.connectors.ows_service.OwsService.get') def test_get_tax_details(mock_owsservice, tax_details_fixture): """Test getting tax details.""" path = f'/account-payee/{ACCOUNT_PAYEE_ID}/tax-details' mock_owsservice.return_value = tax_details_fixture OwsPayee.get_tax_details(ACCOUNT_PAYEE_ID) mock_owsservice.assert_called_with(path, timeout=30.0) @patch('src.connectors.ows_service.OwsService.get') def test_get_tax_details_404_not_found(mock_owsservice): """Test getting tax details when the details are not found.""" path = f'/account-payee/{ACCOUNT_PAYEE_ID}/tax-details' status_code = 404 message = ( f'ows-payee error: {status_code} response from get /account-payee/123/tax-details: ' 'Payee tax details not found for account_payee_id:`123`' ) mock_owsservice.side_effect = OwsServiceException(message, status_code) with pytest.raises(TaxDetailsException) as excinfo: OwsPayee.get_tax_details(ACCOUNT_PAYEE_ID) mock_owsservice.assert_called_with(path, timeout=30.0) assert excinfo.value.message == message @patch('src.connectors.ows_service.OwsService.get') def test_get_tax_details_500_bad_gateway(mock_owsservice): """Test getting tax details when ows payee returns bad gateway.""" path = f'/account-payee/{ACCOUNT_PAYEE_ID}/tax-details' status_code = 500 message = ( f'ows-payee error: {status_code} response from get /account-payee/123/tax-details: ' 'Bad Gateway' ) mock_owsservice.side_effect = OwsServiceException(message, status_code) with pytest.raises(OwsServiceException) as excinfo: OwsPayee.get_tax_details(ACCOUNT_PAYEE_ID) mock_owsservice.assert_called_with(path, timeout=30.0) assert excinfo.value.message == message assert excinfo.value.status_code == status_code @patch('src.connectors.ows_service.OwsService.get') def test_get_tax_details_with_required(mock_owsservice): """Test getting tax details with a partially empty response.""" path = f'/account-payee/{ACCOUNT_PAYEE_ID}/tax-details' data = { 'account_payee_id': ACCOUNT_PAYEE_ID, 'vat_number': None, 'business_name': None, 'business_number': None, 'country_of_tax_residency_code': None, 'address': {'country_code': 'GBR'}, } mock_owsservice.return_value = data result = OwsPayee.get_tax_details(ACCOUNT_PAYEE_ID, ['address']) mock_owsservice.assert_called_with(path, timeout=30.0) assert result == data @patch('src.connectors.ows_service.OwsService.get') def test_get_tax_details_missing_required(mock_owsservice): """Test getting tax details with a partially empty response.""" path = f'/account-payee/{ACCOUNT_PAYEE_ID}/tax-details' mock_owsservice.return_value = { 'account_payee_id': ACCOUNT_PAYEE_ID, 'vat_number': None, 'business_name': None, 'business_number': None, 'country_of_tax_residency_code': None, 'address': {'country_code': 'GBR'}, } expected = 'ows-payee: Missing required tax details: vat_number' with pytest.raises(TaxDetailsException) as excinfo: OwsPayee.get_tax_details(ACCOUNT_PAYEE_ID, ['vat_number', 'address']) mock_owsservice.assert_called_with(path, timeout=30.0) assert excinfo.value.message == expected assert excinfo.value.failure_reason == MISSING_TAX @patch('src.connectors.ows_service.OwsService.get') def test_get_bank_details(mock_owsservice, bank_details_fixture): """Test getting bank details.""" path = f'/account-payee/{ACCOUNT_PAYEE_ID}/bank-details' mock_owsservice.return_value = bank_details_fixture OwsPayee.get_bank_details(ACCOUNT_PAYEE_ID) mock_owsservice.assert_called_with(path) @patch('src.connectors.ows_service.OwsService.get') def test_get_bank_details_404_not_found(mock_owsservice): """Test getting bank details when the details are not found.""" path = f'/account-payee/{ACCOUNT_PAYEE_ID}/bank-details' status_code = 404 message = ( f'ows-payee error: {status_code} response from' f' get /account-payee/123/bank-details: ' 'Payee bank details not found for account_payee_id:`123`' ) mock_owsservice.side_effect = OwsServiceException(message, status_code) with pytest.raises(BankDetailsException) as excinfo: OwsPayee.get_bank_details(ACCOUNT_PAYEE_ID) mock_owsservice.assert_called_with(path) assert excinfo.value.message == message @patch('src.connectors.ows_service.OwsService.get') def test_get_bank_details_500_bad_gateway(mock_owsservice): """Test getting bank details when ows payee returns bad gateway.""" path = f'/account-payee/{ACCOUNT_PAYEE_ID}/bank-details' status_code = 500 message = ( f'ows-payee error: {status_code} response from' f' get /account-payee/123/bank-details: ' 'Bad Gateway' ) mock_owsservice.side_effect = OwsServiceException(message, status_code) with pytest.raises(OwsServiceException) as excinfo: OwsPayee.get_bank_details(ACCOUNT_PAYEE_ID) mock_owsservice.assert_called_with(path) assert excinfo.value.message == message assert excinfo.value.status_code == status_code @patch('src.connectors.ows_service.OwsService.get') def test_get_bank_details_with_required(mock_owsservice): """Test getting bank details with a partially empty response.""" path = f'/account-payee/{ACCOUNT_PAYEE_ID}/bank-details' data = { 'account_payee_id': ACCOUNT_PAYEE_ID, 'type': 'INDIVIDUAL', 'contact': None, 'address': { 'country_code': 'US', 'address_1': 'asdf', 'address_2': 'dfgh', 'city': 'odesa', 'province': 'AZ', 'zip': '65000', }, 'payout_method': None, } mock_owsservice.return_value = data result = OwsPayee.get_bank_details(ACCOUNT_PAYEE_ID, ['address']) mock_owsservice.assert_called_with(path) assert result == data @patch('src.connectors.ows_service.OwsService.get') def test_get_bank_details_missing_required(mock_owsservice): """Test getting bank details with a partially empty response.""" path = f'/account-payee/{ACCOUNT_PAYEE_ID}/bank-details' mock_owsservice.return_value = { 'account_payee_id': ACCOUNT_PAYEE_ID, 'type': 'INDIVIDUAL', 'contact': None, 'address': { 'country_code': 'US', 'address_1': 'asdf', 'address_2': 'dfgh', 'city': 'odesa', 'province': 'AZ', 'zip': '65000', }, 'payout_method': None, } expected = 'ows-payee: Missing required bank details: contact' with pytest.raises(BankDetailsException) as excinfo: OwsPayee.get_bank_details(ACCOUNT_PAYEE_ID, ['contact', 'address']) mock_owsservice.assert_called_with(path) assert excinfo.value.message == expected assert excinfo.value.failure_reason == MISSING_BANK