"""Tests for TransferWise utils.""" import copy from datetime import datetime import json from typing import Optional from unittest.mock import MagicMock, patch from oto import status import pytest import requests from collaborator import config from collaborator.constants import transferwise_api as api from collaborator.constants import transferwise_error as error from collaborator.constants.transferwise_profile import TransferwiseProfileStatus from collaborator.models.rds.transferwise_profile_persister import ( TransferwiseProfilePersister, ) from collaborator.models.transferwise import Transferwise from collaborator.utils.error import OwsError from collaborator.utils.typing import Account, User from tests.testutils import db from tests.testutils import transferwise_fixtures as fixtures def _requests_response( data: Optional[dict] = None, status: int = status.OK ) -> requests.Response: """Create a mock requests-style Response object. Args: data (dict): Data contained in the response status (int): HTTP status code Returns: Response """ response = requests.Response() response.status_code = status if data: response._content = json.dumps(data).encode() return response def _requests_error_response( status: int = status.INTERNAL_ERROR, data: Optional[dict] = None ) -> requests.Response: """Create a mock requests-style error Response object. Args: error_code (str): Error code error_type (str): Error type status (int): HTTP status code Returns: Response """ response = requests.Response() if data: response._content = json.dumps(data).encode() response.status_code = status return response def test_transferwise_class_is_initialized_successfully(): """Test TransferWise class initialization.""" transferwise_base_class = Transferwise() assert transferwise_base_class.base_url == config.TRANSFERWISE_BASE_URL assert transferwise_base_class.client_id == config.TRANSFERWISE_CLIENT_ID assert transferwise_base_class.client_secret == config.TRANSFERWISE_CLIENT_SECRET @patch("collaborator.models.transferwise.requests") def test_get_request_no_access_token(mock_request): """Test get method.""" json_response = dict(account_id="123", profile_id="345") mock_request.get.return_value = _requests_response(json_response) headers = {"Content-Type": "application/json"} transferwise_client = Transferwise() api_path = "recipients?id=123" url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) result = transferwise_client._Transferwise__get(api_path) assert result.status_code == 200 assert result.json() == json_response mock_request.get.assert_called_with(url, headers=headers) @patch("collaborator.models.transferwise.requests") def test_get_request_no_access_token_supplied_headers(mock_request): """Test get method with supplied headers.""" json_response = dict(account_id="123", profile_id="345") mock_request.get.return_value = _requests_response(json_response) headers = {"Content-Type": "application/json", "My-Header": "123"} transferwise_client = Transferwise() api_path = "recipients?id=123" url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) result = transferwise_client._Transferwise__get( api_path, headers={"My-Header": "123"} ) assert result.status_code == 200 assert result.json() == json_response mock_request.get.assert_called_with(url, headers=headers) @db.test_schema_default_seed @patch("collaborator.models.transferwise.requests") def test_get_request_with_access_token(mock_request): """Test get method.""" json_response = dict(account_id="123", profile_id="345") mock_request.get.return_value = _requests_response(json_response) access_token = "abc-123" headers = { "Content-Type": "application/json", "Authorization": "Bearer {}".format(access_token), } transferwise_client = Transferwise(account=Account("vendor", 24601)) api_path = "recipients?id=123" url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) result = transferwise_client._Transferwise__get_with_profile_access_token(api_path) assert result.status_code == 200 assert result.json() == json_response mock_request.get.assert_called_with(url, headers=headers) @patch("collaborator.models.transferwise.requests") def test_get_request_with_basic_authentication(mock_request): """Test get method.""" json_response = dict(account_id="123", profile_id="345") mock_request.get.return_value = _requests_response(json_response) headers = {"Content-Type": "application/json"} transferwise_client = Transferwise() api_path = "recipients?id=123" url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) result = transferwise_client._Transferwise__get(api_path, basic_authentication=True) assert result.status_code == 200 assert result.json() == json_response mock_request.get.assert_called_with( url, headers=headers, auth=(config.TRANSFERWISE_CLIENT_ID, config.TRANSFERWISE_CLIENT_SECRET), ) @patch("collaborator.models.transferwise.requests") def test_get_request_with_basic_authentication_supplied_headers(mock_request): """Test get method.""" json_response = dict(account_id="123", profile_id="345") mock_request.get.return_value = _requests_response(json_response) headers = {"Content-Type": "application/json", "My-Header": "123"} transferwise_client = Transferwise() api_path = "recipients?id=123" url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) result = transferwise_client._Transferwise__get( api_path, basic_authentication=True, headers={"My-Header": "123"} ) assert result.status_code == 200 assert result.json() == json_response mock_request.get.assert_called_with( url, headers=headers, auth=(config.TRANSFERWISE_CLIENT_ID, config.TRANSFERWISE_CLIENT_SECRET), ) @patch("collaborator.models.transferwise.requests") def test_post_request_no_access_token(mock_request): """Test post method.""" json_response = dict(account_id="123", profile_id="345") mock_request.post.return_value = _requests_response(json_response) headers = {"Content-Type": "application/json"} transferwise_client = Transferwise() api_path = "recipients?id=123" url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) payload = dict(id=123, profile_id=345) result = transferwise_client._Transferwise__post(api_path, payload) assert result.status_code == 200 assert result.json() == json_response mock_request.post.assert_called_with(url, json=payload, headers=headers) @patch("collaborator.models.transferwise.requests") def test_post_request_no_access_token_supplied_headers(mock_request): """Test post method.""" json_response = dict(account_id="123", profile_id="345") mock_request.post.return_value = _requests_response(json_response) headers = {"Content-Type": "application/json", "My-Header": "123"} transferwise_client = Transferwise() api_path = "recipients?id=123" url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) payload = dict(id=123, profile_id=345) result = transferwise_client._Transferwise__post( api_path, payload, headers={"My-Header": "123"} ) assert result.status_code == 200 assert result.json() == json_response mock_request.post.assert_called_with(url, json=payload, headers=headers) @db.test_schema_default_seed @patch("collaborator.models.transferwise.requests") def test_post_request_with_access_token(mock_request): """Test post method.""" json_response = dict(account_id="24601", profile_id="345") mock_request.post.return_value = _requests_response(json_response) access_token = "abc-123" headers = { "Content-Type": "application/json", "Authorization": "Bearer {}".format(access_token), } transferwise_client = Transferwise(account=Account("vendor", 24601)) api_path = "recipients?id=123" url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) payload = dict(id=123, profile=345) result = transferwise_client._Transferwise__post( api_path, payload, access_token=access_token ) assert result.status_code == 200 assert result.json() == json_response mock_request.post.assert_called_with(url, json=payload, headers=headers) @patch("collaborator.models.transferwise.requests") def test_post_request_with_basic_authentication(mock_request): """Test post method.""" json_response = dict(account_id="123", profile_id="345") mock_request.post.return_value = _requests_response(json_response) headers = {"Content-Type": "application/x-www-form-urlencoded"} transferwise_client = Transferwise() api_path = "recipients?id=123" url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) payload = dict(id=123, profile_id=345) result = transferwise_client._Transferwise__post( api_path, payload, basic_authentication=True ) assert result.status_code == 200 assert result.json() == json_response mock_request.post.assert_called_with( url, data=payload, headers=headers, auth=(config.TRANSFERWISE_CLIENT_ID, config.TRANSFERWISE_CLIENT_SECRET), ) @patch("collaborator.models.transferwise.requests") def test_post_request_with_basic_authentication_supplied_headers(mock_request): """Test post method.""" json_response = dict(account_id="123", profile_id="345") mock_request.post.return_value = _requests_response(json_response) headers = {"Content-Type": "application/x-www-form-urlencoded", "My-Header": "123"} transferwise_client = Transferwise() api_path = "recipients?id=123" url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) payload = dict(id=123, profile_id=345) result = transferwise_client._Transferwise__post( api_path, payload, basic_authentication=True, headers={"My-Header": "123"} ) assert result.status_code == 200 assert result.json() == json_response mock_request.post.assert_called_with( url, data=payload, headers=headers, auth=(config.TRANSFERWISE_CLIENT_ID, config.TRANSFERWISE_CLIENT_SECRET), ) @db.test_schema_default_seed @patch("collaborator.models.transferwise.requests") def test_patch_request_with_access_token(mock_request): """Test patch method with access token.""" json_response = dict(account_id="24601", profile_id="345") mock_request.patch.return_value = _requests_response(json_response) access_token = "abc-123" headers = { "Content-Type": "application/json", "Authorization": "Bearer {}".format(access_token), } transferwise_client = Transferwise(account=Account("vendor", 24601)) api_path = "recipients?id=123" url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) payload = dict(id=123, profile=345) result = transferwise_client._Transferwise__patch( api_path, payload, access_token=access_token ) assert result.status_code == 200 assert result.json() == json_response mock_request.patch.assert_called_with(url, json=payload, headers=headers) @patch("collaborator.models.transferwise.requests") def test_patch_request_with_basic_authentication(mock_request): """Test patch method with basic authentication.""" json_response = dict(account_id="123", profile_id="345") mock_request.patch.return_value = _requests_response(json_response) headers = {"Content-Type": "application/x-www-form-urlencoded"} transferwise_client = Transferwise() api_path = "recipients?id=123" url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) payload = dict(id=123, profile_id=345) result = transferwise_client._Transferwise__patch( api_path, payload, basic_authentication=True ) assert result.status_code == 200 assert result.json() == json_response mock_request.patch.assert_called_with( url, data=payload, headers=headers, auth=(config.TRANSFERWISE_CLIENT_ID, config.TRANSFERWISE_CLIENT_SECRET), ) @patch("collaborator.models.transferwise.requests") def test_delete_request_no_access_token(mock_request): """Test delete method.""" mock_request.delete.return_value = _requests_response() headers = {"Content-Type": "application/json"} transferwise_client = Transferwise() api_path = "accounts/123" url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) result = transferwise_client._Transferwise__delete(api_path) assert result.status_code == 200 mock_request.delete.assert_called_with(url, headers=headers) @db.test_schema_default_seed @patch("collaborator.models.transferwise.requests") def test_delete_request_with_access_token(mock_request): """Test delete method.""" mock_request.delete.return_value = _requests_response() access_token = "abc-123" headers = { "Content-Type": "application/json", "Authorization": "Bearer {}".format(access_token), } transferwise_client = Transferwise(account=Account("vendor", 24601)) recipient_id = 1 api_path = "{}/{}".format(api.ACCOUNTS, recipient_id) url = "{}{}".format(config.TRANSFERWISE_BASE_URL, api_path) result = transferwise_client._Transferwise__delete(api_path, has_access_token=True) assert result.status_code == 200 mock_request.delete.assert_called_with(url, headers=headers) @db.test_schema_default_seed @patch("collaborator.models.transferwise.requests") def test_delete_request_with_basic_authentication(mock_request): """Test delete method.""" mock_request.delete.return_value = _requests_response() headers = {"Content-Type": "application/x-www-form-urlencoded"} transferwise_client = Transferwise() recipient_id = 1 api_path = "{}/{}".format(api.ACCOUNTS, recipient_id) url = "{}{}".format(config.TRANSFERWISE_BASE_URL, api_path) result = transferwise_client._Transferwise__delete( api_path, basic_authentication=True ) assert result.status_code == 200 mock_request.delete.assert_called_with( url, headers=headers, auth=(config.TRANSFERWISE_CLIENT_ID, config.TRANSFERWISE_CLIENT_SECRET), ) @patch("collaborator.models.transferwise.requests") def test_refresh_account_requirements(mock_request): """Test refresh account requirements.""" mock_request.post.return_value = _requests_response( fixtures.ACCOUNT_REQUIREMENTS_RESPONSE ) headers = {"Content-Type": "application/json", "Accept-Minor-Version": "1"} target = "USD" source = "USD" transferwise_client = Transferwise() api_path = api.ACCOUNT_REQUIREMENTS.format(source=source, target=target) url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) payload = fixtures.REFRESH_ACCOUNT_REQUIREMENTS result = transferwise_client.refresh_account_requirements( "USD", "USD", fixtures.REFRESH_ACCOUNT_REQUIREMENTS ) assert result == fixtures.ACCOUNT_REQUIREMENTS_RESPONSE mock_request.post.assert_called_with(url, json=payload, headers=headers) @patch("collaborator.models.transferwise.requests") def test_refresh_account_requirements_error(mock_request): """Test refresh account requirements with error response.""" mock_request.post.return_value = _requests_error_response() headers = {"Content-Type": "application/json", "Accept-Minor-Version": "1"} target = "USD" source = "USD" transferwise_client = Transferwise() api_path = api.ACCOUNT_REQUIREMENTS.format(source=source, target=target) url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) payload = fixtures.REFRESH_ACCOUNT_REQUIREMENTS with pytest.raises(OwsError) as err: transferwise_client.refresh_account_requirements( "USD", "USD", fixtures.REFRESH_ACCOUNT_REQUIREMENTS ) assert err.value.status == 500 assert err.value.code == error.TRANSFERWISE_ACCOUNT_REQUIREMENTS_ERROR mock_request.post.assert_called_with(url, json=payload, headers=headers) @patch("collaborator.models.transferwise.requests") def test_get_account_requirements(mock_request): """Test fetching account requirements.""" mock_request.get.return_value = _requests_response( fixtures.ACCOUNT_REQUIREMENTS_RESPONSE ) headers = {"Content-Type": "application/json", "Accept-Minor-Version": "1"} target = "USD" source = "USD" transferwise_client = Transferwise() api_path = api.ACCOUNT_REQUIREMENTS.format(source=source, target=target) url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) result = transferwise_client.get_account_requirements("USD", "USD") assert result == fixtures.ACCOUNT_REQUIREMENTS_RESPONSE mock_request.get.assert_called_with(url, headers=headers) @patch("collaborator.models.transferwise.requests") def test_get_account_requirements_error(mock_request): """Test fetching account requirements with error response.""" mock_request.get.return_value = _requests_error_response() headers = {"Content-Type": "application/json", "Accept-Minor-Version": "1"} target = "USD" source = "USD" transferwise_client = Transferwise() api_path = api.ACCOUNT_REQUIREMENTS.format(source=source, target=target) url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) with pytest.raises(OwsError) as err: transferwise_client.get_account_requirements("USD", "USD") assert err.value.status == 500 assert err.value.code == error.TRANSFERWISE_ACCOUNT_REQUIREMENTS_ERROR mock_request.get.assert_called_with(url, headers=headers) @patch("collaborator.models.transferwise.requests") def test_validate_field(mock_request): """Test fetching account requirements.""" mock_request.get.return_value = _requests_response( fixtures.FIELD_VALIDATION_RESPONSE ) headers = {"Content-Type": "application/json"} validator = "sort-code" query_string_params = "sortCode=231470" transferwise_client = Transferwise() api_path = api.FIELD_VALIDATOR.format( validator=validator, query_string_params=query_string_params ) url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) result = transferwise_client.get_field_validation(validator, query_string_params) assert result == fixtures.FIELD_VALIDATION_RESPONSE mock_request.get.assert_called_with(url, headers=headers) @patch("collaborator.models.transferwise.requests") def test_validate_field_error(mock_request): """Test fetching account requirements with error response.""" mock_request.get.return_value = _requests_error_response() headers = {"Content-Type": "application/json"} validator = "sort-code" query_string_params = "sortCode=231470" transferwise_client = Transferwise() api_path = api.FIELD_VALIDATOR.format( validator=validator, query_string_params=query_string_params ) url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) with pytest.raises(OwsError) as err: transferwise_client.get_field_validation(validator, query_string_params) assert err.value.status == 500 assert err.value.code == error.TRANFERWISE_FIELD_VALIDATION_ERROR mock_request.get.assert_called_with(url, headers=headers) @pytest.mark.parametrize( ("code", "redirect_uri"), [ ("abcd-1234", "https://example.com/callback"), ("loltest", "https://coolwebsite.com/callback"), ], ) @patch("collaborator.models.transferwise.requests") def test_create_oauth_token(mock_request, code, redirect_uri): """Test creating an OAuth token.""" oath_reponse = {"token": "yes"} mock_request.post.return_value = _requests_response(oath_reponse) transferwise_client = Transferwise() result = transferwise_client.create_oauth_token(code, redirect_uri) assert result == oath_reponse mock_request.post.assert_called_with( config.TRANSFERWISE_BASE_URL + api.OAUTH_TOKEN, data={ "code": code, "grant_type": "authorization_code", "client_id": transferwise_client.client_id, "redirect_uri": redirect_uri, }, headers={"Content-Type": "application/x-www-form-urlencoded"}, auth=(transferwise_client.client_id, transferwise_client.client_secret), ) @patch("collaborator.models.transferwise.requests") def test_create_oauth_token_error(mock_request): """Test failing to create an OAuth token.""" mock_request.post.return_value = _requests_error_response() transferwise_client = Transferwise() with pytest.raises(OwsError) as err: transferwise_client.create_oauth_token("code", "redirect_uri") assert err.value.status == 500 assert err.value.code == error.OAUTH_TOKEN_ERROR @db.test_schema_default_seed @patch("collaborator.models.transferwise.requests") def test_create_recipient(mock_request): """Test creating recipient.""" message = dict(id=1) mock_request.post.return_value = _requests_response(message, 201) transferwise_client = Transferwise(account=Account("vendor", 24601)) payload = {"some_field": 1} result = transferwise_client.create_recipient(payload) assert result == message headers = {"Content-Type": "application/json", "Authorization": "Bearer abc-123"} api_path = api.ACCOUNTS url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) mock_request.post.assert_called_with(url, headers=headers, json=payload) @db.test_schema_default_seed @patch("collaborator.models.transferwise.call_datadog_with_event") @patch("collaborator.models.transferwise.requests") def test_create_recipient_failed(mock_request, mock_call_datadog_with_event): """Test creating recipient failed.""" mock_request.post.return_value = _requests_error_response(422) transferwise_client = Transferwise(account=Account("vendor", 24601)) payload = {"some_field": 1} with pytest.raises(OwsError) as err: transferwise_client.create_recipient(payload) assert err.value.status == 422 assert err.value.code == error.TRANSFERWISE_POST_REQUEST_ERROR headers = {"Content-Type": "application/json", "Authorization": "Bearer abc-123"} api_path = api.ACCOUNTS url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) mock_request.post.assert_called_with(url, headers=headers, json=payload) mock_call_datadog_with_event.assert_called_with( title="Transferwise_request_error for vendor 24601", text="subscribe_to_application_event_error", tags=[ "transferwise", "transferwise api", "transferwise api error", "application:flask", "service:test-ows-collaborator", "environment:test", ], ) @db.test_schema_default_seed @patch("collaborator.models.transferwise.requests") def test_create_recipient_retry_logic(mock_request): """Test creating recipient failed.""" message = dict(id=1) mock_request.post.side_effect = [ _requests_error_response(401), _requests_response(message, 201), ] oauth_token_response = { "access_token": "new_access_token", "refresh_token": "123-abc", } transferwise_client = Transferwise(account=Account("vendor", 24601)) transferwise_client.refresh_oauth_token = MagicMock() transferwise_client.refresh_oauth_token.return_value = oauth_token_response headers_with_new_access_token = { "Content-Type": "application/json", "Authorization": "Bearer new_access_token", } api_path = api.ACCOUNTS url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) payload = {"some_field": 1} result = transferwise_client.create_recipient(payload) assert result == message assert mock_request.post.call_count == 2 mock_request.post.assert_called_with( url, headers=headers_with_new_access_token, json=payload ) @db.test_schema_default_seed @patch("collaborator.models.transferwise.requests") def test_create_recipient_retry_logic_error(mock_request): """Test creating recipient failed.""" mock_request.post.return_value = _requests_error_response(401) transferwise_client = Transferwise(account=Account("vendor", 24601)) transferwise_client.refresh_profile_access_token = MagicMock() transferwise_client.refresh_profile_access_token.side_effect = OwsError( "failol", status=422, code="norefresh" ) headers = {"Content-Type": "application/json", "Authorization": "Bearer abc-123"} api_path = api.ACCOUNTS url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) payload = {"some_field": 1} with pytest.raises(OwsError) as err: transferwise_client.create_recipient(payload) assert err.value.status == 422 assert err.value.code == "norefresh" mock_request.post.assert_called_with(url, headers=headers, json=payload) @db.test_schema_default_seed @patch("collaborator.models.transferwise.requests") def test_delete_recipient(mock_request): """Test deleting recipient.""" mock_request.delete.return_value = _requests_response() transferwise_client = Transferwise(account=Account("vendor", 24601)) recipient_id = 1 transferwise_client.delete_recipient(recipient_id) headers = {"Content-Type": "application/json", "Authorization": "Bearer abc-123"} api_path = "{}/{}".format(api.ACCOUNTS, recipient_id) url = "{}{}".format(config.TRANSFERWISE_BASE_URL, api_path) mock_request.delete.assert_called_with(url, headers=headers) @db.test_schema_default_seed @patch("collaborator.models.transferwise.call_datadog_with_event") @patch("collaborator.models.transferwise.requests") def test_delete_recipient_failed(mock_request, mock_call_datadog_with_event): """Test deleting recipient failed.""" mock_request.delete.return_value = _requests_error_response(422) transferwise_client = Transferwise(account=Account("vendor", 24601)) recipient_id = 1 with pytest.raises(OwsError) as err: transferwise_client.delete_recipient(recipient_id) assert err.value.status == 422 assert err.value.code == error.TRANSFERWISE_DELETE_REQUEST_ERROR headers = {"Content-Type": "application/json", "Authorization": "Bearer abc-123"} api_path = "{}/{}".format(api.ACCOUNTS, recipient_id) url = "{}{}".format(config.TRANSFERWISE_BASE_URL, api_path) mock_request.delete.assert_called_with(url, headers=headers) mock_call_datadog_with_event.assert_called_with( title="Transferwise_request_error for vendor 24601", text="", tags=[ "transferwise", "transferwise api", "transferwise api error", "application:flask", "service:test-ows-collaborator", "environment:test", ], ) @db.test_schema_default_seed @patch("collaborator.models.transferwise.requests") def test_delete_recipient_retry_logic(mock_request): """Test deleting recipient retry logic.""" recipient_id = 1 mock_request.delete.side_effect = [ _requests_error_response(401), _requests_response(), ] oauth_token_response = { "access_token": "new_access_token", "refresh_token": "123-abc", } transferwise_client = Transferwise(account=Account("vendor", 24601)) transferwise_client.refresh_oauth_token = MagicMock() transferwise_client.refresh_oauth_token.return_value = oauth_token_response headers_with_new_access_token = { "Content-Type": "application/json", "Authorization": "Bearer new_access_token", } api_path = "{}/{}".format(api.ACCOUNTS, recipient_id) url = "{}{}".format(config.TRANSFERWISE_BASE_URL, api_path) transferwise_client.delete_recipient(recipient_id) assert mock_request.delete.call_count == 2 mock_request.delete.assert_called_with(url, headers=headers_with_new_access_token) @db.test_schema_default_seed @patch("collaborator.models.transferwise.requests") def test_delete_recipient_retry_logic_error(mock_request): """Test deleting recipient retry logic failed.""" mock_request.delete.return_value = _requests_error_response(401) transferwise_client = Transferwise(account=Account("vendor", 24601)) recipient_id = 1 transferwise_client.refresh_profile_access_token = MagicMock() transferwise_client.refresh_profile_access_token.side_effect = OwsError( "failol", status=422, code="norefresh" ) headers = {"Content-Type": "application/json", "Authorization": "Bearer abc-123"} api_path = "{}/{}".format(api.ACCOUNTS, recipient_id) url = "{}{}".format(config.TRANSFERWISE_BASE_URL, api_path) with pytest.raises(OwsError) as err: transferwise_client.delete_recipient(recipient_id) assert err.value.status == 422 assert err.value.code == "norefresh" assert mock_request.delete.call_count == 1 mock_request.delete.assert_called_with(url, headers=headers) @patch("collaborator.models.transferwise.requests") def test_oauth_token(mock_request): """Test refresh account requirements.""" mock_request.post.return_value = _requests_response( fixtures.REFRESH_ACCESS_TOKEN_RESPONSE ) headers = {"Content-Type": "application/x-www-form-urlencoded"} transferwise_client = Transferwise() api_path = api.OAUTH_TOKEN url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) refresh_token = "1d0ec7b9-b569-426d-a18d-8dead5b6a3cc" payload = {"grant_type": "refresh_token", "refresh_token": refresh_token} result = transferwise_client.refresh_oauth_token(refresh_token) assert result == fixtures.REFRESH_ACCESS_TOKEN_RESPONSE mock_request.post.assert_called_with( url, data=payload, headers=headers, auth=(config.TRANSFERWISE_CLIENT_ID, config.TRANSFERWISE_CLIENT_SECRET), ) @patch("collaborator.models.transferwise.requests") def test_oauth_token_error(mock_request): """Test refresh account requirements.""" mock_request.post.return_value = _requests_error_response() headers = {"Content-Type": "application/x-www-form-urlencoded"} transferwise_client = Transferwise() api_path = api.OAUTH_TOKEN url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) refresh_token = "1d0ec7b9-b569-426d-a18d-8dead5b6a3cc" payload = {"grant_type": "refresh_token", "refresh_token": refresh_token} with pytest.raises(OwsError) as err: transferwise_client.refresh_oauth_token(refresh_token) assert err.value.status == 500 assert err.value.code == error.OAUTH_TOKEN_ERROR mock_request.post.assert_called_with( url, data=payload, headers=headers, auth=(config.TRANSFERWISE_CLIENT_ID, config.TRANSFERWISE_CLIENT_SECRET), ) @patch( "collaborator.models.rds.transferwise_profile_persister." "TransferwiseProfilePersister.soft_delete_profile" ) @patch("collaborator.models.rds.transferwise_profile_persister.datetime") @patch("collaborator.models.transferwise.requests") def test_oauth_token_error_refresh_token( mock_request, datetime_mock, mock_soft_delete, mock_account ): """Test refresh account requirements.""" datetime_mock.now.return_value = datetime(2021, 1, 4, 15, 26, 24, 348286) mock_request.post.return_value = _requests_error_response( status=400, data={ "error": "invalid_grant", "error_description": "Invalid refresh token: 3bae9b1d-9", }, ) headers = {"Content-Type": "application/x-www-form-urlencoded"} transferwise_client = Transferwise(account=mock_account) api_path = api.OAUTH_TOKEN url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) refresh_token = "1d0ec7b9-b569-426d-a18d-8dead5b6a3cc" payload = {"grant_type": "refresh_token", "refresh_token": refresh_token} with pytest.raises(OwsError) as err: transferwise_client.refresh_oauth_token(refresh_token) assert err.value.status == 400 assert err.value.code == error.OAUTH_TOKEN_ERROR mock_request.post.assert_called_with( url, data=payload, headers=headers, auth=(config.TRANSFERWISE_CLIENT_ID, config.TRANSFERWISE_CLIENT_SECRET), ) mock_soft_delete.assert_called_with( mock_account, None, user=User(type="system", id=0) ) def test_refresh_profile_access_token(mocker, mock_account): """Test refresh profile access token.""" refresh_token = "refresh_token_in_da_house" new_token = "i_am_the_new_token" new_refresh_token = "refresh_token_in_da_outhouse" profile_id = 76 profile_credentials = {"profile_id": profile_id, "refresh_token": refresh_token} expected_result = { "access_token": new_token, "id": 99, "profile_id": profile_id, "refresh_token": refresh_token, "subaccount_id": None, "vendor_id": 24601, } mocker.patch.object( Transferwise, "refresh_oauth_token", return_value={"access_token": new_token, "refresh_token": new_refresh_token}, ) mocker.patch.object( TransferwiseProfilePersister, "update_active_profile", return_value={ "id": 99, "profile_id": profile_id, "vendor_id": 24601, "subaccount_id": None, "access_token": new_token, "refresh_token": refresh_token, }, ) transferwise_client = Transferwise(account=mock_account) result = transferwise_client.refresh_profile_access_token(profile_credentials) assert result == expected_result TransferwiseProfilePersister.update_active_profile.assert_called_with( mock_account, None, profile_id, new_token, new_refresh_token, None ) @patch("collaborator.models.transferwise.requests") def test_oauth_token_with_client_access_token(mock_request): """Test refresh account requirements.""" mock_request.post.return_value = _requests_response( fixtures.CLIENT_ACCESS_TOKEN_RESPONSE ) headers = {"Content-Type": "application/x-www-form-urlencoded"} transferwise_client = Transferwise() api_path = api.OAUTH_TOKEN url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) payload = {"grant_type": "client_credentials"} result = transferwise_client.refresh_oauth_token(fetch_client_credentials=True) assert result == fixtures.CLIENT_ACCESS_TOKEN_RESPONSE mock_request.post.assert_called_with( url, data=payload, headers=headers, auth=(config.TRANSFERWISE_CLIENT_ID, config.TRANSFERWISE_CLIENT_SECRET), ) @patch("collaborator.models.transferwise.requests") def test_oauth_token_with_client_access_token_error(mock_request): """Test refresh account requirements.""" mock_request.post.return_value = _requests_error_response() headers = {"Content-Type": "application/x-www-form-urlencoded"} transferwise_client = Transferwise() api_path = api.OAUTH_TOKEN url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) payload = {"grant_type": "client_credentials"} with pytest.raises(OwsError) as err: transferwise_client.refresh_oauth_token(fetch_client_credentials=True) assert err.value.status == 500 assert err.value.code == error.OAUTH_TOKEN_ERROR mock_request.post.assert_called_with( url, data=payload, headers=headers, auth=(config.TRANSFERWISE_CLIENT_ID, config.TRANSFERWISE_CLIENT_SECRET), ) @patch("collaborator.models.transferwise.requests") def test_subscribe_application_to_event(mock_request, mocker): """Test subscribe application to event.""" mocker.patch.object( Transferwise, "refresh_oauth_token", return_value=fixtures.CLIENT_ACCESS_TOKEN_RESPONSE, ) mock_request.post.return_value = _requests_response( fixtures.WEBHOOK_SUBSCRIPTION_RESPONSE, 201 ) access_token = "be69d566-971e-4e15-9648-85a486195863" headers = { "Content-Type": "application/json", "Authorization": "Bearer {}".format(access_token), } transferwise_client = Transferwise() client_key = "TRANSFERWISE_CLIENT_ID" api_path = api.WEBHOOK_APPLICATION_SUBSCRIPTION.format(client_key=client_key) url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) name = "test hook" trigger_on = "profile_verification" payload = { "name": name, "trigger_on": "profiles#verification-state-change", "delivery": {"version": "2.0.0", "url": config.WEBHOOK_DELIVERY_URL}, } result = transferwise_client.subscribe_application_to_event(name, trigger_on) assert result == fixtures.WEBHOOK_SUBSCRIPTION_RESPONSE mock_request.post.assert_called_with(url, json=payload, headers=headers) @patch("collaborator.models.transferwise.call_datadog_with_event") @patch("collaborator.models.transferwise.requests") def test_subscribe_application_to_event_error( mock_request, mock_call_datadog_with_event, mocker ): """Test subscribe application to event.""" mocker.patch.object( Transferwise, "refresh_oauth_token", return_value=fixtures.CLIENT_ACCESS_TOKEN_RESPONSE, ) mock_request.post.return_value = _requests_error_response() access_token = "be69d566-971e-4e15-9648-85a486195863" headers = { "Content-Type": "application/json", "Authorization": "Bearer {}".format(access_token), } transferwise_client = Transferwise() client_key = "TRANSFERWISE_CLIENT_ID" api_path = api.WEBHOOK_APPLICATION_SUBSCRIPTION.format(client_key=client_key) url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) name = "test hook" trigger_on = "profile_verification" payload = { "name": name, "trigger_on": "profiles#verification-state-change", "delivery": {"version": "2.0.0", "url": config.WEBHOOK_DELIVERY_URL}, } with pytest.raises(OwsError) as err: transferwise_client.subscribe_application_to_event(name, trigger_on) assert err.value.status == 500 assert err.value.code == error.APPLICATION_SUBSCRIPTION_ERROR mock_request.post.assert_called_with(url, json=payload, headers=headers) mock_call_datadog_with_event.assert_called_with( title="Transferwise_request_error occurred when subscribing application for events.", text="subscribe_to_application_event_error", tags=[ "transferwise", "transferwise api", "transferwise api error", "application:flask", "service:test-ows-collaborator", "environment:test", ], ) @db.test_schema_default_seed @patch("collaborator.models.transferwise.requests") def test_create_quote(mock_request): """Test create quote.""" message = fixtures.QUOTE_RESPONSE mock_request.post.return_value = _requests_response(message, 201) transferwise_client = Transferwise(account=Account("vendor", 24601)) payload = fixtures.QUOTE_REQUESTED_PARAMS final_message = { "recipient_id": payload["recipient_id"], "collaborator_id": payload["collaborator_id"], **message, } result = transferwise_client.create_quote(payload) assert result == final_message headers = {"Content-Type": "application/json", "Authorization": "Bearer abc-123"} api_path = api.QUOTES.format(profile_id="123fffa33") url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) mock_request.post.assert_called_with(url, headers=headers, json=payload) @db.test_schema_default_seed @patch("collaborator.models.transferwise.call_datadog_with_event") @patch("collaborator.models.transferwise.requests") def test_create_quote_error(mock_request, mock_call_datadog_with_event): """Test creating quote failed.""" mock_request.post.return_value = _requests_error_response(422) transferwise_client = Transferwise(account=Account("vendor", 24601)) payload = fixtures.QUOTE_REQUESTED_PARAMS with pytest.raises(OwsError) as err: transferwise_client.create_quote(payload) assert err.value.status == 422 assert err.value.code == error.TRANSFERWISE_POST_REQUEST_ERROR headers = {"Content-Type": "application/json", "Authorization": "Bearer abc-123"} api_path = api.QUOTES.format(profile_id="123fffa33") url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) mock_request.post.assert_called_with(url, headers=headers, json=payload) mock_call_datadog_with_event.assert_called_with( title="Transferwise_request_error for vendor 24601", text="", tags=[ "transferwise", "transferwise api", "transferwise api error", "application:flask", "service:test-ows-collaborator", "environment:test", ], ) @db.test_schema_default_seed @patch("collaborator.models.transferwise.requests") @pytest.mark.parametrize( "message", [ {"profileId": 12345}, {"profileId": 54321}, ], ) def test_ping_api(request_mock, message): """Test ping api.""" profile_id = 12345 request_mock.get.return_value = _requests_response(message) transferwise_client = Transferwise(account=Account("vendor", 24601)) transferwise_client.ping_api(profile_id) headers = {"Content-Type": "application/json", "Authorization": "Bearer abc-123"} api_path = api.PROFILE.format(profile_id=profile_id) url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) request_mock.get.assert_called_with(url, headers=headers) @db.test_schema_default_seed @pytest.mark.parametrize("quote_uuid", ["abcd-1234", "1234-abcd"]) @patch("collaborator.models.transferwise.requests") def test_get_quote(request_mock, quote_uuid, mock_account): """Test getting a quote.""" request_mock.get.return_value = _requests_response(fixtures.QUOTE_RESPONSE) transferwise_client = Transferwise(mock_account) result = transferwise_client.get_quote(quote_uuid, profile_id=1234) assert result == fixtures.QUOTE_RESPONSE request_mock.get.assert_called_with( f"https://api.wise-sandbox.com/v3/profiles/1234/quotes/{quote_uuid}", headers={"Content-Type": "application/json", "Authorization": "Bearer abc-123"}, ) @db.test_schema_default_seed @patch("collaborator.models.transferwise.call_datadog_with_event") @patch("collaborator.models.transferwise.requests") def test_get_quote_error(request_mock, mock_call_datadog_with_event, mock_account): """Test getting a quote.""" request_mock.get.return_value = _requests_error_response(status.NOT_FOUND) transferwise_client = Transferwise(mock_account) with pytest.raises(OwsError) as err: transferwise_client.get_quote("abcd-1234", 1234) assert err.value.status == status.NOT_FOUND assert err.value.code == error.TRANSFERWISE_GET_REQUEST_ERROR mock_call_datadog_with_event.assert_called_with( title="Transferwise_request_error for vendor 24601", text="", tags=[ "transferwise", "transferwise api", "transferwise api error", "application:flask", "service:test-ows-collaborator", "environment:test", ], ) @db.test_schema_default_seed @patch("collaborator.models.transferwise.requests") def test_create_batch(mock_request): """Test create batch.""" message = fixtures.BATCH_RESPONSE mock_request.post.return_value = _requests_response(message, 201) transferwise_client = Transferwise(account=Account("vendor", 24601)) profile_id = 1111 payload = copy.deepcopy(fixtures.BATCH_TRANSFER_REQUESTED_PARAMS) result = transferwise_client.create_batch(profile_id, payload) assert result == message headers = {"Content-Type": "application/json", "Authorization": "Bearer abc-123"} api_path = api.BATCH.format(profile_id=profile_id) url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) mock_request.post.assert_called_with(url, headers=headers, json=payload) @db.test_schema_default_seed @patch("collaborator.models.transferwise.call_datadog_with_event") @patch("collaborator.models.transferwise.requests") def test_create_batch_error(mock_request, mock_call_datadog_with_event): """Test create batch with error.""" mock_request.post.return_value = _requests_error_response(422) transferwise_client = Transferwise(account=Account("vendor", 24601)) profile_id = 1111 payload = fixtures.BATCH_REQUESTED_PARAMS with pytest.raises(OwsError) as err: transferwise_client.create_batch(profile_id, payload) assert err.value.status == 422 assert err.value.code == error.TRANSFERWISE_POST_REQUEST_ERROR headers = {"Content-Type": "application/json", "Authorization": "Bearer abc-123"} api_path = api.BATCH.format(profile_id=profile_id) url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) mock_request.post.assert_called_with(url, headers=headers, json=payload) mock_call_datadog_with_event.assert_called_with( title="Transferwise_request_error for vendor 24601", text="", tags=[ "transferwise", "transferwise api", "transferwise api error", "application:flask", "service:test-ows-collaborator", "environment:test", ], ) @db.test_schema_default_seed @patch("collaborator.models.transferwise.requests") def test_create_batch_transfer(mock_request): """Test create batch transfer.""" message = fixtures.BATCH_TRANSFER_RESPONSE mock_request.post.return_value = _requests_response(message, 201) transferwise_client = Transferwise(account=Account("vendor", 24601)) profile_id = 12341234 payload = copy.deepcopy(fixtures.BATCH_TRANSFER_REQUESTED_PARAMS) batch_id = "54a6bc09-cef9-49a8-9041-f1f0c654cd88" result = transferwise_client.create_batch_transfer(payload, profile_id, batch_id) assert result == message headers = {"Content-Type": "application/json", "Authorization": "Bearer abc-123"} api_path = api.BATCH_TRANSFER.format(profile_id=profile_id, batch_group_id=batch_id) url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) mock_request.post.assert_called_with(url, headers=headers, json=payload) @db.test_schema_default_seed @patch("collaborator.models.transferwise.call_datadog_with_event") @patch("collaborator.models.transferwise.requests") def test_create_batch_transfer_error(mock_request, mock_call_datadog_with_event): """Test create batch transfer error.""" tw_error = { "errors": [ { "code": "quote.duplicate", "message": "Cannot use one quote to create 2 transfers", } ] } mock_request.post.return_value = _requests_error_response(422, tw_error) transferwise_client = Transferwise(account=Account("vendor", 24601)) profile_id = 12341234 payload = copy.deepcopy(fixtures.BATCH_TRANSFER_REQUESTED_PARAMS) batch_id = "54a6bc09-cef9-49a8-9041-f1f0c654cd88" expected_error_payload = { "called_with_params": payload, "code": "quote.duplicate", "message": "Cannot use one quote to create 2 transfers", } with pytest.raises(OwsError) as err: transferwise_client.create_batch_transfer(payload, profile_id, batch_id) assert err.value.status == 422 assert err.value.code == error.TRANSFERWISE_POST_REQUEST_ERROR assert err.value.message == expected_error_payload headers = {"Content-Type": "application/json", "Authorization": "Bearer abc-123"} api_path = api.BATCH_TRANSFER.format(profile_id=profile_id, batch_group_id=batch_id) url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) mock_request.post.assert_called_with(url, headers=headers, json=payload) mock_call_datadog_with_event.assert_called_with( title="Transferwise_request_error for vendor 24601", text=json.dumps( { "errors": [ { "code": "quote.duplicate", "message": "Cannot use one quote to create 2 transfers", } ] } ), tags=[ "transferwise", "transferwise api", "transferwise api error", "application:flask", "service:test-ows-collaborator", "environment:test", ], ) @db.test_schema_default_seed @patch("collaborator.models.transferwise.requests") def test_get_batch_state(mock_request): """Test get batch state.""" message = fixtures.BATCH_RESPONSE mock_request.get.return_value = _requests_response(message) transferwise_client = Transferwise(account=Account("vendor", 24601)) profile_id = 12341234 batch_id = "54a6bc09-cef9-49a8-9041-f1f0c654cd88" result = transferwise_client.get_batch_state(profile_id, batch_id) assert result == message headers = {"Content-Type": "application/json", "Authorization": "Bearer abc-123"} api_path = api.BATCH_STATE.format(profile_id=profile_id, batch_group_id=batch_id) url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) mock_request.get.assert_called_with(url, headers=headers) @db.test_schema_default_seed @patch("collaborator.models.transferwise.call_datadog_with_event") @patch("collaborator.models.transferwise.requests") def test_get_batch_state_error(mock_request, mock_call_datadog_with_event): """Test get batch state error.""" mock_request.get.return_value = _requests_error_response(422) transferwise_client = Transferwise(account=Account("vendor", 24601)) profile_id = 12341234 batch_id = "54a6bc09-cef9-49a8-9041-f1f0c654cd88" with pytest.raises(OwsError) as err: transferwise_client.get_batch_state(profile_id, batch_id) assert err.value.status == 422 assert err.value.code == error.TRANSFERWISE_GET_REQUEST_ERROR headers = {"Content-Type": "application/json", "Authorization": "Bearer abc-123"} api_path = api.BATCH_STATE.format(profile_id=profile_id, batch_group_id=batch_id) url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) mock_request.get.assert_called_with(url, headers=headers) mock_call_datadog_with_event.assert_called_with( title="Transferwise_request_error for vendor 24601", text="", tags=[ "transferwise", "transferwise api", "transferwise api error", "application:flask", "service:test-ows-collaborator", "environment:test", ], ) @db.test_schema_default_seed @patch("collaborator.models.transferwise.requests") def test_modify_batch_state_as_complete(mock_request): """Test modify batch state as complete.""" message = fixtures.BATCH_COMPLETED_RESPONSE mock_request.patch.return_value = _requests_response(message) transferwise_client = Transferwise(account=Account("vendor", 24601)) profile_id = 12341234 batch_id = "54a6bc09-cef9-49a8-9041-f1f0c654cd88" payload = {"status": "COMPLETED", "version": 1} result = transferwise_client.modify_batch_state(profile_id, batch_id) assert result == message headers = {"Content-Type": "application/json", "Authorization": "Bearer abc-123"} api_path = api.BATCH_STATE.format(profile_id=profile_id, batch_group_id=batch_id) url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) mock_request.patch.assert_called_with(url, headers=headers, json=payload) @db.test_schema_default_seed @patch("collaborator.models.transferwise.requests") def test_modify_batch_state_as_cancelled(mock_request): """Test modify batch state as cancelled.""" message = fixtures.BATCH_CANCELLED_RESPONSE mock_request.patch.return_value = _requests_response(message) transferwise_client = Transferwise(account=Account("vendor", 24601)) profile_id = 12341234 batch_id = "54a6bc09-cef9-49a8-9041-f1f0c654cd88" payload = {"status": "CANCELLED", "version": 1} result = transferwise_client.modify_batch_state( profile_id, batch_id, complete_batch=False ) assert result == message headers = {"Content-Type": "application/json", "Authorization": "Bearer abc-123"} api_path = api.BATCH_STATE.format(profile_id=profile_id, batch_group_id=batch_id) url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) mock_request.patch.assert_called_with(url, headers=headers, json=payload) @db.test_schema_default_seed @patch("collaborator.models.transferwise.call_datadog_with_event") @patch("collaborator.models.transferwise.requests") def test_modify_batch_state_error(mock_request, mock_call_datadog_with_event): """Test modify batch state error.""" mock_request.patch.return_value = _requests_error_response(422) transferwise_client = Transferwise(account=Account("vendor", 24601)) profile_id = 12341234 batch_id = "54a6bc09-cef9-49a8-9041-f1f0c654cd88" payload = {"status": "CANCELLED", "version": 1} with pytest.raises(OwsError) as err: transferwise_client.modify_batch_state( profile_id, batch_id, complete_batch=False ) assert err.value.status == 422 assert err.value.code == error.TRANSFERWISE_PATCH_REQUEST_ERROR headers = {"Content-Type": "application/json", "Authorization": "Bearer abc-123"} api_path = api.BATCH_STATE.format(profile_id=profile_id, batch_group_id=batch_id) url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) mock_request.patch.assert_called_with(url, headers=headers, json=payload) mock_call_datadog_with_event.assert_called_with( title="Transferwise_request_error for vendor 24601", text="", tags=[ "transferwise", "transferwise api", "transferwise api error", "application:flask", "service:test-ows-collaborator", "environment:test", ], ) @db.test_schema_default_seed @patch("collaborator.models.transferwise.requests") def test_get_batch_group(mock_request): """Test get batch group.""" message = fixtures.BATCH_COMPLETED_RESPONSE mock_request.get.return_value = _requests_response(message) transferwise_client = Transferwise(account=Account("vendor", 24601)) profile_id = 12341234 batch_id = "54a6bc09-cef9-49a8-9041-f1f0c654cd88" result = transferwise_client.get_batch_group(profile_id, batch_id) assert result == message headers = {"Content-Type": "application/json", "Authorization": "Bearer abc-123"} api_path = api.BATCH_STATE.format(profile_id=profile_id, batch_group_id=batch_id) url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) mock_request.get.assert_called_with(url, headers=headers) @db.test_schema_default_seed @patch("collaborator.models.transferwise.requests") def test_get_batch_group_error(mock_request): """Test get batch group error.""" mock_request.get.return_value = _requests_error_response(500) transferwise_client = Transferwise(account=Account("vendor", 24601)) profile_id = 12341234 batch_id = "54a6bc09-cef9-49a8-9041-f1f0c654cd88" with pytest.raises(OwsError) as error_info: transferwise_client.get_batch_group(profile_id, batch_id) assert error_info.value.status == 500 headers = {"Content-Type": "application/json", "Authorization": "Bearer abc-123"} api_path = api.BATCH_STATE.format(profile_id=profile_id, batch_group_id=batch_id) url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) mock_request.get.assert_called_with(url, headers=headers) @db.test_schema_default_seed @patch("collaborator.models.transferwise.requests") def test_simulate_transfer_processing(mock_request, mock_account): """Test simulate_transfer_processing.""" message = fixtures.BATCH_TRANSFER_RESPONSE mock_request.get.return_value = _requests_response(message) transferwise_client = Transferwise(account=mock_account) transfer_id = 468956 transfer_status = "processing" result = transferwise_client.simulate_transfer_processing( transfer_id, transfer_status ) assert result == message headers = {"Content-Type": "application/json", "Authorization": "Bearer abc-123"} api_path = api.SIMULATE_TRANSFER_PROCESSING.format( transfer_id=transfer_id, transfer_status=transfer_status ) url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) mock_request.get.assert_called_with(url, headers=headers) @db.test_schema_default_seed @patch("collaborator.models.transferwise.requests") def test_simulate_transfer_processing_error(mock_request, mock_account): """Test simulate_transfer_processing with error.""" mock_request.get.return_value = _requests_error_response(500) transferwise_client = Transferwise(account=mock_account) transfer_id = 468956 transfer_status = "processing" with pytest.raises(OwsError) as error_info: transferwise_client.simulate_transfer_processing(transfer_id, transfer_status) assert error_info.value.status == 500 headers = {"Content-Type": "application/json", "Authorization": "Bearer abc-123"} api_path = api.SIMULATE_TRANSFER_PROCESSING.format( transfer_id=transfer_id, transfer_status=transfer_status ) url = "{0}{1}".format(config.TRANSFERWISE_BASE_URL, api_path) mock_request.get.assert_called_with(url, headers=headers) @db.test_schema_default_seed @patch("collaborator.models.transferwise.requests") @pytest.mark.parametrize( "message, expected_status", [ ( { "routes": [ { "current_status": "verified", "source_currency": "USD", } ] }, TransferwiseProfileStatus.VERIFIED, ), ( { "routes": [ { "current_status": "not_verified", "source_currency": "USD", } ] }, TransferwiseProfileStatus.UNVERIFIED, ), ], ) def test_get_profile_verification_status(request_mock, message, expected_status): """Test getting the profile verification status.""" request_mock.post.return_value = _requests_response(message) transferwise_client = Transferwise() result = transferwise_client.get_profile_verification_status( 12345, "USD", "abcd-1234" ) assert result == expected_status @db.test_schema_default_seed @patch("collaborator.models.transferwise.g") @patch("collaborator.models.transferwise.requests") def test_get_profile_verification_status_bad_status(request_mock, g_mock, mock_account): """Test getting the profile verification status with a weird status.""" request_mock.post.return_value = _requests_response( {"routes": [{"current_status": "🐙", "source_currency": "LVL"}]} ) transferwise_client = Transferwise(mock_account) result = transferwise_client.get_profile_verification_status( 12345, "LVL", "abcd-1234" ) assert result == TransferwiseProfileStatus.ERROR g_mock.log.error.assert_called_with("Unhandled profile verification status: 🐙") @db.test_schema_default_seed @patch("collaborator.models.transferwise.g") @patch("collaborator.models.transferwise.requests") def test_get_profile_verification_status_error(request_mock, g_mock, mock_account): """Test failing to get the profile verification status.""" request_mock.post.return_value = _requests_error_response( status.BAD_REQUEST, {"fail": "lol"} ) transferwise_client = Transferwise(mock_account) result = transferwise_client.get_profile_verification_status( 12345, "USD", "abcd-1234" ) assert result is TransferwiseProfileStatus.ERROR g_mock.log.error.assert_called_with( 'Unable to get profile verification status: {"fail": "lol"}' ) @pytest.mark.parametrize( "response_data, expected", [ ( [ { "id": "70c0bf93-c9c3-4dae-b899-dd04f6f1c8c1", "name": "profile", "delivery": {"version": "2.0.0", "url": "https://test"}, "trigger_on": "profiles#verification-state-change", "created_by": {"type": "application", "id": "theorchard"}, "created_at": "2020-07-06T17:13:15Z", "scope": {"domain": "application", "id": "theorchard"}, "request_headers": [], } ], ["profiles#verification-state-change"], ), ( [{"trigger_on": "a"}, {"trigger_on": "b"}], ["a", "b"], ), ], ) @patch("collaborator.models.transferwise.requests") def test_get_application_subscriptions(requests_mock, response_data, expected): """Test getting application subscriptions.""" requests_mock.get.return_value = _requests_response(response_data) transferwise_client = Transferwise() result = transferwise_client.get_application_subscriptions() assert result == expected @patch("collaborator.models.transferwise.requests") def test_get_application_subscriptions_error(requests_mock): """Test failing to get application subscriptions.""" requests_mock.get.return_value = _requests_error_response( status.UNAUTHORIZED, {"fail": "lol"} ) transferwise_client = Transferwise() with pytest.raises(OwsError) as error_info: transferwise_client.get_application_subscriptions() assert error_info.value.status == status.UNAUTHORIZED @pytest.mark.parametrize( "source, target, uri", [ ( "USD", "USD", "v1/account-requirements?source=USD&target=USD&targetAmount=50000", ), ( "USD", "JPY", "v1/account-requirements?source=USD&target=JPY&targetAmount=50000&addressRequired=true", ), ( "JPY", "JPY", "v1/account-requirements?source=JPY&target=JPY&targetAmount=50000&addressRequired=true", ), ( "JPY", "USD", "v1/account-requirements?source=JPY&target=USD&targetAmount=50000&addressRequired=true", ), ], ) def test_get_account_requirements_uri(source, target, uri): """Test get_account_requirements_uri.""" klass = Transferwise() assert uri == klass.get_account_requirements_uri(source, target)