"""Tests for payee's KYC notifications handlers.""" from http import HTTPStatus from unittest.mock import patch from payee.constants.error import ERROR_DONT_HAVE_PERMISSIONS from tests.utils.common import convert_object_to_dict, remove_keys from tests.utils.factories import ( AccountPayeeFactory, AccountPayeeKycNotificationFactory, PayeeFactory, PayeeKycNotificationFactory, ) @patch('payee.utils.permissions.authorization_backend.is_authorized', return_value=True) @patch('payee.blueprints.payee_kyc_notification.AccountPayeeKycNotification') def test_get_payee_latest_kyc_notification( mocked_model, mock_perm, fixture_client, mock_headers ): """Test getting KYC notification data.""" kyc_notification = AccountPayeeKycNotificationFactory.build() mocked_model.get_latest_by_payee_id.return_value = kyc_notification result = fixture_client.get( f'/account-payee/{kyc_notification.account_payee_id}/kyc-notification', headers=mock_headers, ) assert result.status_code == HTTPStatus.OK mocked_model.get_latest_by_payee_id.assert_called_once_with( kyc_notification.account_payee_id ) assert result.json == convert_object_to_dict( kyc_notification, remove_fields=('deleted_at', 'deleted_by') ) @patch( 'payee.utils.permissions.authorization_backend.is_authorized', return_value=False ) @patch('payee.blueprints.payee_kyc_notification.AccountPayeeKycNotification') def test_get_payee_latest_kyc_notification_failure_permissions( mocked_model, mock_perm, fixture_client, mock_headers ): """Test getting KYC notification data failure permissions.""" kyc_notification = AccountPayeeKycNotificationFactory.build() result = fixture_client.get( f'/account-payee/{kyc_notification.account_payee_id}/kyc-notification', headers=mock_headers, ) assert result.status_code == HTTPStatus.UNAUTHORIZED assert result.json == {'error': ERROR_DONT_HAVE_PERMISSIONS} assert mocked_model.get_latest_by_payee_id.call_count == 0 @patch('payee.utils.permissions.authorization_backend.is_authorized', return_value=True) @patch('payee.blueprints.payee_kyc_notification.AccountPayeeKycNotification') def test_get_payee_latest_kyc_notification_not_found( mocked_model, mock_perm, fixture_client, mock_headers ): """Test getting KYC notification data failure not found.""" account_payee_id = 133 mocked_model.get_latest_by_payee_id.return_value = None result = fixture_client.get( f'/account-payee/{account_payee_id}/kyc-notification', headers=mock_headers, ) assert result.status_code == HTTPStatus.OK assert result.json == {} mocked_model.get_latest_by_payee_id.assert_called_once_with(account_payee_id) @patch('payee.logic.payee.Payee') @patch('payee.logic.payee.AccountPayee') @patch('payee.blueprints.payee_kyc_notification.AccountPayeeKycNotification') def test_create_account_payee_kyc_notification( mocked_model, account_payee_mock, payee_mock, fixture_client, mock_config, mock_headers, ): """Test creating payee's KYC_notification.""" kyc_notification = AccountPayeeKycNotificationFactory.build() mocked_model.create.return_value = kyc_notification kyc_notification = convert_object_to_dict(kyc_notification) payee_mock.get_by_id.return_value = None account_payee_mock.get_by_id.return_value = AccountPayeeFactory.build( account_payee_id=kyc_notification['account_payee_id'] ) request_data = remove_keys( dict(kyc_notification), remove_fields=( 'account_payee_kyc_notification_id', 'account_payee_id', 'created_at', 'last_modified', 'created_by', 'last_modified_by', 'deleted_at', 'deleted_by', ), ) result = fixture_client.post( f'/account-payee/{kyc_notification["account_payee_id"]}/kyc-notification', headers=mock_headers, json=request_data, ) assert result.status_code == HTTPStatus.CREATED mocked_model.create.assert_called_once_with( account_payee_id=kyc_notification['account_payee_id'], **request_data ) assert result.json == { 'type': 'account_payee', **remove_keys(kyc_notification, remove_fields=('deleted_at', 'deleted_by')), } @patch('payee.logic.payee.Payee') @patch('payee.logic.payee.AccountPayee') @patch('payee.blueprints.payee_kyc_notification.AccountPayeeKycNotification') def test_delete_account_payee_kyc_notifications( mocked_model, account_payee_mock, payee_mock, fixture_client, mock_config, mock_headers, ): """Test deleting payee's KYC_notification.""" account_payee_id = 142 mocked_model.soft_delete_by_payee_id.return_value = 3 payee_mock.get_by_id.return_value = None account_payee_mock.get_by_id.return_value = AccountPayeeFactory.build( account_payee_id=account_payee_id ) result = fixture_client.delete( f'/account-payee/{account_payee_id}/kyc-notification', headers=mock_headers, ) assert result.status_code == HTTPStatus.NO_CONTENT mocked_model.soft_delete_by_payee_id.assert_called_once_with(account_payee_id) assert result.text == '' @patch('payee.logic.payee.Payee') @patch('payee.logic.payee.AccountPayee') @patch('payee.blueprints.payee_kyc_notification.PayeeKycNotification') def test_create_payee_kyc_notification( mocked_model, account_payee_mock, payee_mock, fixture_client, mock_config, mock_headers, ): """Test creating payee's KYC_notification.""" kyc_notification = PayeeKycNotificationFactory.build() mocked_model.create.return_value = kyc_notification kyc_notification = convert_object_to_dict(kyc_notification) account_payee_mock.get_by_id.return_value = None payee_mock.get_by_id.return_value = PayeeFactory.build( payee_id=kyc_notification['payee_id'] ) request_data = remove_keys( dict(kyc_notification), remove_fields=( 'payee_kyc_notification_id', 'payee_id', 'created_at', 'last_modified', 'created_by', 'last_modified_by', 'deleted_at', 'deleted_by', ), ) result = fixture_client.post( f'/payee/{kyc_notification["payee_id"]}/kyc-notification', headers=mock_headers, json=request_data, ) assert result.status_code == HTTPStatus.CREATED mocked_model.create.assert_called_once_with( payee_id=kyc_notification['payee_id'], **request_data ) assert result.json == { 'type': 'payee', **remove_keys(kyc_notification, remove_fields=('deleted_at', 'deleted_by')), } @patch('payee.logic.payee.Payee') @patch('payee.logic.payee.AccountPayee') @patch('payee.blueprints.payee_kyc_notification.PayeeKycNotification') def test_delete_payee_kyc_notifications( mocked_model, account_payee_mock, payee_mock, fixture_client, mock_config, mock_headers, ): """Test deleting payee's KYC_notification.""" payee_id = 142 mocked_model.soft_delete_by_payee_id.return_value = 3 account_payee_mock.get_by_id.return_value = None payee_mock.get_by_id.return_value = PayeeFactory.build(payee_id=payee_id) result = fixture_client.delete( f'/payee/{payee_id}/kyc-notification', headers=mock_headers, ) assert result.status_code == HTTPStatus.NO_CONTENT mocked_model.soft_delete_by_payee_id.assert_called_once_with(payee_id) assert result.text == '' @patch('payee.utils.permissions.authorization_backend.is_authorized', return_value=True) @patch('payee.blueprints.payee_kyc_notification.logic') def test_get_generic_payee_latest_kyc_notification( mocked_logic, mock_perm, fixture_client, mock_headers ): """Test getting KYC notification data.""" kyc_notification = PayeeKycNotificationFactory.build() response_notifcation = convert_object_to_dict( kyc_notification, remove_fields=( 'payee_kyc_notification_id', 'payee_id', 'created_at', 'last_modified', 'created_by', 'last_modified_by', 'deleted_at', 'deleted_by', ), ) mocked_logic.get_payee_kyc_notification_dataloader.return_value = { 'payee_kyc_notifications': [{'data': response_notifcation}] } result = fixture_client.post( f'/payee/kyc-notification/dataloader', headers=mock_headers, json={'payee_ids': [kyc_notification.payee_id]}, ) assert result.status_code == HTTPStatus.OK mocked_logic.get_payee_kyc_notification_dataloader.assert_called_once_with( [kyc_notification.payee_id] ) assert result.json == {'payee_kyc_notifications': [{'data': response_notifcation}]}