"""Test for TransferWise Profile Persister.""" from unittest.mock import patch from oto import status import pytest from collaborator.constants import error from collaborator.models.rds.transferwise_profile_persister import ( TransferwiseProfilePersister, ) from collaborator.utils import logging from collaborator.utils.error import OwsError from collaborator.utils.typing import Account from tests.testutils import db @db.test_schema_default_seed @pytest.mark.parametrize( "profile_id, subaccount_id, access_token, refresh_token," "verification_status, expected_result", [ ( 1, None, "abcdef-1234", "foo-bar-1234", "VERIFIED", { "id": 4, "profile_id": 1, "vendor_id": 24601, "subaccount_id": None, "status": "VERIFIED", "access_token": "abcdef-1234", "refresh_token": "foo-bar-1234", "refresh_token_created_at": "now", "active": True, "deleted_by": None, "deleted_date": None, }, ), ( 2, 1234, "abcdef-1234", "foo-bar-1234", "UNVERIFIED", { "id": 4, "profile_id": 2, "vendor_id": 24601, "subaccount_id": 1234, "status": "UNVERIFIED", "access_token": "abcdef-1234", "refresh_token": "foo-bar-1234", "refresh_token_created_at": "now", "active": True, "deleted_by": None, "deleted_date": None, }, ), ], ) def test_create_profile( profile_id, subaccount_id, access_token, refresh_token, verification_status, expected_result, mock_account, ): """Test creating a TransferWise profile.""" result = TransferwiseProfilePersister.create_profile( mock_account, subaccount_id, profile_id, access_token, refresh_token, verification_status, ) # It's hard to override the default field, so just replacing it for now result["refresh_token_created_at"] = "now" assert result == expected_result @db.test_schema_default_seed @patch.object(logging, "log_event") @pytest.mark.parametrize( "account, subaccount_id, previous, updated", [ ( Account("vendor", "24601"), None, { "id": 1, "profile_id": 12341234, "vendor_id": 24601, "subaccount_id": None, "access_token": "abc-123", "refresh_token": "123-abc", "refresh_token_created_at": "2006-05-04T03:02:01", "status": "UNVERIFIED", "active": True, "deleted_by": None, "deleted_date": None, }, { "id": 1, "profile_id": 12341234, "vendor_id": 24601, "subaccount_id": None, "access_token": "abc-123", "refresh_token": "123-abc", "refresh_token_created_at": "2006-05-04T03:02:01", "status": "UNVERIFIED", "active": False, "deleted_by": "oa:24601", "deleted_date": "now", }, ), ( Account("vendor", "6650"), 1234, { "id": 2, "profile_id": 12341235, "vendor_id": 6650, "subaccount_id": 1234, "access_token": "abc-456", "refresh_token": "456-abc", "refresh_token_created_at": "2010-09-08T07:06:05", "status": "UNVERIFIED", "active": True, "deleted_by": None, "deleted_date": None, }, { "id": 2, "profile_id": 12341235, "vendor_id": 6650, "subaccount_id": 1234, "access_token": "abc-456", "refresh_token": "456-abc", "refresh_token_created_at": "2010-09-08T07:06:05", "status": "UNVERIFIED", "active": False, "deleted_by": "oa:24601", "deleted_date": "now", }, ), ], ) def test_soft_delete_profile( log_event_mock, account, subaccount_id, previous, updated, mock_user ): """Test soft deleting a profile.""" result = TransferwiseProfilePersister.soft_delete_profile( account, subaccount_id, mock_user ) result["deleted_date"] = "now" assert result == updated log_event_mock.assert_called_with( "update", "transferwise_profile", updated["id"], previous, updated, mock_user ) @db.test_schema_default_seed @pytest.mark.parametrize( "account, subaccount_id", [(Account("vendor", "24601"), 1234), (Account("vendor", "1234"), None)], ) def test_soft_delete_profile_not_found(account, subaccount_id, mock_user): """Test soft deleting a profile that doesn't exist.""" with pytest.raises(OwsError) as err: TransferwiseProfilePersister.soft_delete_profile( account, subaccount_id, mock_user ) assert err.value.status == status.NOT_FOUND @db.test_schema_default_seed @pytest.mark.parametrize( "account, subaccount_id, profile_id, access_token, refresh_token," "verification_status, expected_result", [ ( Account("vendor", "24601"), None, 98, "abcdef-1234", "foo-bar-1234", "VERIFIED", { "id": 1, "profile_id": 98, "vendor_id": 24601, "subaccount_id": None, "status": "VERIFIED", "access_token": "abcdef-1234", "refresh_token": "foo-bar-1234", "refresh_token_created_at": "2006-05-04T03:02:01", "active": True, "deleted_by": None, "deleted_date": None, }, ), ( Account("vendor", "6650"), 1234, 99, "abcdef-1234", "foo-bar-1234", None, { "id": 2, "profile_id": 99, "vendor_id": 6650, "subaccount_id": 1234, "status": "UNVERIFIED", "access_token": "abcdef-1234", "refresh_token": "foo-bar-1234", "refresh_token_created_at": "2010-09-08T07:06:05", "active": True, "deleted_by": None, "deleted_date": None, }, ), ], ) def test_update_profile( profile_id, account, subaccount_id, access_token, refresh_token, verification_status, expected_result, ): """Test updating existing TransferWise profiles.""" result = TransferwiseProfilePersister.update_active_profile( account, subaccount_id, profile_id, access_token, refresh_token, verification_status, ) assert result == expected_result @db.test_schema_default_seed def test_update_profile_not_found(): """Test updating an existing TransferWise profile that doesn't exist.""" account = Account("vendor", "99999") subaccount_id = None profile_id = 12341234 access_token = "access" refresh_token = "refresh" verification_status = None with pytest.raises(OwsError) as error_info: TransferwiseProfilePersister.update_active_profile( account, subaccount_id, profile_id, access_token, refresh_token, verification_status, ) assert error_info.value.status == status.NOT_FOUND assert error_info.value.code == error.ERROR_CODE_PROFILE_NOT_FOUND assert error_info.value.message == error.ERROR_MESSAGE_PROFILE_NOT_FOUND @db.test_schema_default_seed @pytest.mark.parametrize( "account, subaccount_id, expected_result", [ ( Account("vendor", "24601"), None, { "id": 1, "profile_id": 12341234, "vendor_id": 24601, "subaccount_id": None, "access_token": "abc-123", "refresh_token": "123-abc", "status": "UNVERIFIED", "refresh_token_created_at": "2006-05-04T03:02:01", "active": True, "deleted_by": None, "deleted_date": None, }, ), ( Account("vendor", "6650"), 1234, { "id": 2, "profile_id": 12341235, "vendor_id": 6650, "subaccount_id": 1234, "access_token": "abc-456", "refresh_token": "456-abc", "status": "UNVERIFIED", "refresh_token_created_at": "2010-09-08T07:06:05", "active": True, "deleted_by": None, "deleted_date": None, }, ), ], ) def test_get_profile(account, subaccount_id, expected_result): """Test getting a profile.""" result = TransferwiseProfilePersister.get_active_profile(account, subaccount_id) assert result == expected_result @db.test_schema_default_seed @pytest.mark.parametrize( "account, subaccount_id", [ (Account("vendor", "24601"), 1234), (Account("vendor", "6650"), None), (Account("vendor", "1234"), None), ], ) def test_get_profile_not_found(account, subaccount_id): """Test getting a profile which does not exist.""" with pytest.raises(OwsError) as error_info: TransferwiseProfilePersister.get_active_profile(account, subaccount_id) assert error_info.value.status == status.NOT_FOUND assert error_info.value.code == error.ERROR_CODE_PROFILE_NOT_FOUND assert error_info.value.message == error.ERROR_MESSAGE_PROFILE_NOT_FOUND @db.test_schema_default_seed @patch.object(logging, "log_event") def test_update_profile_status(log_event_mock): """Test updating the profile status.""" expected_result = [ { "id": 1, "profile_id": 12341234, "vendor_id": 24601, "subaccount_id": None, "status": "VERIFIED", "access_token": "abc-123", "refresh_token": "123-abc", "refresh_token_created_at": "2006-05-04T03:02:01", "active": True, "deleted_by": None, "deleted_date": None, }, { "id": 3, "profile_id": 12341234, "vendor_id": 1234, "subaccount_id": None, "status": "VERIFIED", "access_token": "abc-123", "refresh_token": "123-abc", "refresh_token_created_at": "2006-05-04T03:02:01", "active": False, "deleted_by": None, "deleted_date": None, }, ] result = TransferwiseProfilePersister.update_profile_status(12341234, "VERIFIED") assert result == expected_result assert log_event_mock.call_count == len(expected_result) @db.test_schema_default_seed @pytest.mark.parametrize("profile_id", [999999, 987987]) @patch.object(logging, "log_event") def test_update_profile_status_not_found(log_event_mock, profile_id): """Test updating profile status when the profile isn't found.""" with pytest.raises(OwsError) as error_info: TransferwiseProfilePersister.update_profile_status(profile_id, "VERIFIED") assert error_info.value.status == status.NOT_FOUND assert error_info.value.code == error.ERROR_CODE_PROFILE_NOT_FOUND assert error_info.value.message == error.ERROR_MESSAGE_PROFILE_NOT_FOUND log_event_mock.assert_not_called()