diff --git a/api/tests/unit/webhooks/test_unit_webhooks.py b/api/tests/unit/webhooks/test_unit_webhooks.py index 9ef69bcd2561..b9ede94112e5 100644 --- a/api/tests/unit/webhooks/test_unit_webhooks.py +++ b/api/tests/unit/webhooks/test_unit_webhooks.py @@ -392,6 +392,49 @@ def test_send_test_webhook__200_response_from_webhook__returns_correct_response( assert response_json["detail"] == "Webhook test successful" +@pytest.mark.parametrize( + "external_api_response_status", + [ + 201, + 202, + 204, + ], +) +def test_send_test_webhook__various_2xx_status_codes__returns_success( + mocker: MockerFixture, + admin_client: APIClient, + external_api_response_status: int, + organisation: Organisation, +) -> None: + # Given + webhook_url = "http://test.webhook.com" + mock_post = mocker.patch("requests.post") + mock_response = MagicMock() + mock_response.status_code = external_api_response_status + mock_response.text = "success" + mock_post.return_value = mock_response + + url = reverse("api-v1:webhooks:webhooks-test") + + data = { + "webhook_url": webhook_url, + "secret": "some-secret", + "scope": {"type": "organisation", "id": organisation.id}, + } + + # When + response = admin_client.post( + url, data=json.dumps(data), content_type="application/json" + ) + + # Then + assert response.status_code == 200 + mock_post.assert_called_once() + response_json = response.json() + assert response_json["status"] == external_api_response_status + assert response_json["detail"] == "Webhook test successful" + + @pytest.mark.parametrize( "external_api_response_status, external_api_error_text, expected_final_status", [ @@ -434,10 +477,10 @@ def test_send_test_webhook__various_error_status_codes__returns_correct_response mock_post.assert_called_once() response_json = response.json() assert response_json["status"] == external_api_response_status - assert response_json["detail"] == "Webhook returned invalid status" + assert response_json["detail"] == "Webhook returned error status" assert ( response_json["body"] - == "Please check the webhook endpoint to validate it returns a 200 OK." + == f"Webhook returned HTTP {external_api_response_status}." ) diff --git a/api/webhooks/views.py b/api/webhooks/views.py index cfb756537eb2..bf467c467e90 100644 --- a/api/webhooks/views.py +++ b/api/webhooks/views.py @@ -47,11 +47,11 @@ def test(self, request: Request) -> Response: else WebhookType.ENVIRONMENT ) response = send_test_request_to_webhook(webhook_url, secret, webhook_type) - if response.status_code != 200: + if not response.ok: return Response( { - "detail": "Webhook returned invalid status", - "body": "Please check the webhook endpoint to validate it returns a 200 OK.", + "detail": "Webhook returned error status", + "body": f"Webhook returned HTTP {response.status_code}.", "status": response.status_code, }, status=status.HTTP_400_BAD_REQUEST,