"""Functional tests for subscribing to application webhooks events.""" from unittest.mock import MagicMock, patch import pytest from tests.testutils import transferwise_fixtures as fixtures @pytest.mark.parametrize( "name, trigger_on", [("test", "profile_verification"), ("test", "transfers_state")] ) @patch("collaborator.models.transferwise.requests") def test_subscribe_application_to_webhooks( mock_transferwise, name, trigger_on, test_client ): """Test subscribe application to webhooks.""" first_response = MagicMock() first_response.status_code = 200 first_response.json.return_value = fixtures.CLIENT_ACCESS_TOKEN_RESPONSE second_response = MagicMock() second_response.status_code = 201 second_response.json.return_value = fixtures.WEBHOOK_SUBSCRIPTION_RESPONSE mock_transferwise.post.side_effect = [first_response, second_response] mock_transferwise.post.return_value = second_response payload = {"name": name, "trigger_on": trigger_on} result = test_client.post( "/transferwise/application-webhooks-subscription", json=payload ) assert result.status_code == 200 assert result.json == fixtures.WEBHOOK_SUBSCRIPTION_RESPONSE @pytest.mark.parametrize( "name, trigger_on", [("test", "profile_verification"), ("test", "transfers_state")] ) @patch("collaborator.models.transferwise.requests") def test_subscribe_application_to_webhooks_error( mock_transferwise, name, trigger_on, test_client ): """Test subscribe application to webhooks error.""" first_response = MagicMock() first_response.status_code = 200 first_response.json.return_value = fixtures.CLIENT_ACCESS_TOKEN_RESPONSE second_response = MagicMock() second_response.status_code = 500 mock_transferwise.post.side_effect = [first_response, second_response] mock_transferwise.post.return_value = second_response payload = {"name": name, "trigger_on": trigger_on} result = test_client.post( "/transferwise/application-webhooks-subscription", json=payload ) assert result.status_code == 500 def test_subscribe_application_to_webhooks_missing_params(test_client): """Expect a 400 when a required field is absent.""" result = test_client.post( "/transferwise/application-webhooks-subscription", json={"name": "test"} ) assert result.status_code == 400