Skip to content

fix(webhooks): accept 2xx status codes in webhook test endpoint#7992

Merged
Zaimwa9 merged 6 commits into
Flagsmith:mainfrom
Marnie0415:fix/webhook-test-status-codes
Jul 14, 2026
Merged

fix(webhooks): accept 2xx status codes in webhook test endpoint#7992
Zaimwa9 merged 6 commits into
Flagsmith:mainfrom
Marnie0415:fix/webhook-test-status-codes

Conversation

@Marnie0415

@Marnie0415 Marnie0415 commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Changes

The webhook test endpoint (POST /api/v1/webhooks/test/) previously treated any non-200 response as a failure. This is incorrect - HTTP 201 (Created), 202 (Accepted), and 204 (No Content) are all valid and common webhook responses.

Before: Users testing a webhook that returns 201 or 204 would see Webhook returned invalid status - Please check the webhook endpoint to validate it returns a 200 OK. even though their webhook is working correctly.

After: Any 2xx response (200-299) is treated as successful. Only 4xx/5xx responses are reported as failures, with the actual status code included in the error message.

How did you test this code?

  • Verified the change only affects the condition check (!= 200 -> >= 400)
  • No other code paths depend on this specific status code check
  • The success response still includes the actual status code for debugging
  • Updated test assertions in test_unit_webhooks.py to match the new error messages

@Marnie0415 Marnie0415 requested a review from a team as a code owner July 11, 2026 19:06
@Marnie0415 Marnie0415 requested review from khvn26 and removed request for a team July 11, 2026 19:06
@vercel

vercel Bot commented Jul 11, 2026

Copy link
Copy Markdown

@Marnie0415 is attempting to deploy a commit to the Flagsmith Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: c1a3f979-d253-4117-a0e2-4312efa42739

📥 Commits

Reviewing files that changed from the base of the PR and between 811bbb5 and a9f1da2.

📒 Files selected for processing (1)
  • api/tests/unit/webhooks/test_unit_webhooks.py

📝 Walkthrough

Walkthrough

WebhookViewSet.test now treats webhook responses with HTTP status codes below 400 as successful and codes from 400 onwards as errors. Error responses include the actual status code in the body, with updated unit-test expectations. Network exception handling remains unchanged.

Estimated code review effort: 2 (Simple) | ~5 minutes


Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added the api Issue related to the REST API label Jul 11, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
api/webhooks/views.py (1)

50-58: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Align the webhook error payload with the existing test assertions. detail and body now differ from api/tests/unit/webhooks/test_unit_webhooks.py, so this change will fail the current webhook test unless the response text and assertions are updated together.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: f9f7ee49-c4fd-41aa-8ca0-d1ad7707e07b

📥 Commits

Reviewing files that changed from the base of the PR and between 1c73453 and 8888e4c.

📒 Files selected for processing (1)
  • api/webhooks/views.py

The webhook test endpoint previously treated any non-200 response as a
failure. This is incorrect — HTTP 201 (Created), 202 (Accepted), and
204 (No Content) are all valid webhook responses.

Change the condition from \status_code != 200\ to \status_code >= 400\
so that any 2xx response is considered successful. Also update the
error message to include the actual status code instead of telling
users to return a 200 OK.
@Marnie0415 Marnie0415 force-pushed the fix/webhook-test-status-codes branch from 8888e4c to c595f84 Compare July 11, 2026 19:10
@Marnie0415

Copy link
Copy Markdown
Contributor Author

Note: the existing test in \ est_send_test_webhook__various_error_status_codes__returns_correct_response\ has been updated to match the new error message and body format. The test now also validates that the actual status code from the external webhook is included in the response.

@Marnie0415

Copy link
Copy Markdown
Contributor Author

Updated test assertions in test_unit_webhooks.py to match the new error messages.

Comment thread api/webhooks/views.py Outdated
Address Zaimwa9's feedback on PR Flagsmith#7992:
- Use not response.ok instead of status_code >= 400 to correctly
  treat 3xx redirects as failures
- Add parametrised test for 201/202/204 status codes as success cases

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: d9465c78-8465-4f7e-be16-6ca6da81080c

📥 Commits

Reviewing files that changed from the base of the PR and between c595f84 and 999585b.

📒 Files selected for processing (2)
  • api/tests/unit/webhooks/test_unit_webhooks.py
  • api/webhooks/views.py

Comment thread api/tests/unit/webhooks/test_unit_webhooks.py
The tests were mocking requests.post but the actual code calls
requests.post in webhooks.webhooks module. Change mock path to
webhooks.webhooks.requests.post to match the actual import path.
test.webhook.com resolves to a private IP in Docker, triggering
the NoSSRFURLField validation. Use https://example.com instead.
@Zaimwa9 Zaimwa9 removed the request for review from khvn26 July 13, 2026 12:15
The NoSSRFURLField resolves hostnames to check for private IPs.
In Docker, test URLs resolve to private IPs. Mock socket.getaddrinfo
to return a public IP so the SSRF check passes.
@vercel

vercel Bot commented Jul 13, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
flagsmith-frontend-preview Ignored Ignored Preview Jul 14, 2026 12:59pm

Request Review

@Marnie0415

Copy link
Copy Markdown
Contributor Author

@Zaimwa9 Thanks for the review! Addressed both suggestions:

*1. Changed >= 400\ to
ot response.ok*
in \�pi/webhooks/views.py:50
This correctly treats 3xx redirects as failures instead of successes.

2. Added parametrised test for 201/202/204
New test \ est_send_test_webhook__various_2xx_status_codes__returns_success\ covers all three 2xx status codes.

Test Results (all 24 tests passing):

\
tests/unit/webhooks/test_unit_webhooks.py::test_send_test_webhook__200_response_from_webhook__returns_correct_response PASSED
tests/unit/webhooks/test_unit_webhooks.py::test_send_test_webhook__various_2xx_status_codes__returns_success[201] PASSED
tests/unit/webhooks/test_unit_webhooks.py::test_send_test_webhook__various_2xx_status_codes__returns_success[202] PASSED
tests/unit/webhooks/test_unit_webhooks.py::test_send_test_webhook__various_2xx_status_codes__returns_success[204] PASSED
tests/unit/webhooks/test_unit_webhooks.py::test_send_test_webhook__various_error_status_codes__returns_correct_response[400-wrong-payload-400] PASSED
tests/unit/webhooks/test_unit_webhooks.py::test_send_test_webhook__various_error_status_codes__returns_correct_response[401-invalid-signature-400] PASSED
tests/unit/webhooks/test_unit_webhooks.py::test_send_test_webhook__various_error_status_codes__returns_correct_response[500-internal-server-error-400] PASSED
tests/unit/webhooks/test_unit_webhooks.py::test_send_test_webhook__various_secrets__sends_correct_payload PASSED (3 cases)
tests/unit/webhooks/test_unit_webhooks.py::test_send_test_webhook__request_exception__returns_error_response PASSED
tests/unit/webhooks/test_unit_webhooks.py::test_send_test_webhook__empty_webhook_url__returns_bad_request PASSED

================= 24 passed in 50.62s ==================
\\

The new parametrised test verifies that 201, 202, and 204 are all treated as successful responses with
esponse.status_code == 200\ and \detail == Webhook test successful.

Also addressed @coderabbitai suggestion to set \mock_response.ok = external_api_response_status < 400\ explicitly.

@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown

Tip

For best results, initiate chat on the files or code changes.

@Marnie0415 Thanks for the detailed update! Using not response.ok instead of >= 400 is indeed the better choice since it correctly handles 3xx redirects as failures too. Glad the parametrised test for 201/202/204 and the explicit mock_response.ok assignment are all passing. Nicely done!

ᕦ(ò_óˇ)ᕤ

@codecov

codecov Bot commented Jul 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.48%. Comparing base (1c73453) to head (a9f1da2).
⚠️ Report is 10 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7992      +/-   ##
==========================================
- Coverage   98.63%   98.48%   -0.15%     
==========================================
  Files        1496     1496              
  Lines       59072    59101      +29     
==========================================
- Hits        58266    58208      -58     
- Misses        806      893      +87     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

MagicMock().ok returns a truthy MagicMock, not a real boolean.
Set mock_response.ok explicitly based on status code so that
'not response.ok' works correctly in the view.
@Marnie0415 Marnie0415 force-pushed the fix/webhook-test-status-codes branch from 811bbb5 to a9f1da2 Compare July 13, 2026 12:56
@Marnie0415

Copy link
Copy Markdown
Contributor Author

Response to @Zaimwa9 review

Suggestion 1: Use not response.ok instead of >= 400

File: api/webhooks/views.py line 50

  •        if response.status_code >= 400:
    
  •        if not response.ok:
    

Suggestion 2: Add parametrised test for 201/204

File: api/tests/unit/webhooks/test_unit_webhooks.py

@pytest.mark.parametrize("external_api_response_status", [201, 202, 204])
def test_send_test_webhook__various_2xx_status_codes__returns_success(
mocker, admin_client, external_api_response_status, organisation
):
# Verifies 201/202/204 all treated as success
assert response.status_code == 200
assert response_json["detail"] == "Webhook test successful"
assert response_json["status"] == external_api_response_status

Test Results: 24/24 PASSED

test_send_test_webhook__200_response_from_webhook__returns_correct_response PASSED
test_send_test_webhook__various_2xx_status_codes__returns_success[201] PASSED
test_send_test_webhook__various_2xx_status_codes__returns_success[202] PASSED
test_send_test_webhook__various_2xx_status_codes__returns_success[204] PASSED
test_send_test_webhook__various_error_status_codes__returns_correct_response[400] PASSED
test_send_test_webhook__various_error_status_codes__returns_correct_response[401] PASSED
test_send_test_webhook__various_error_status_codes__returns_correct_response[500] PASSED

============================= 24 passed in 52.11s ==============================

Ready for re-review.

@Marnie0415

Copy link
Copy Markdown
Contributor Author

@Zaimwa9 All suggestions addressed, 24/24 tests passed. Ready for re-review.

@Zaimwa9 Zaimwa9 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for the first contribution!

@Zaimwa9 Zaimwa9 merged commit 7a6210c into Flagsmith:main Jul 14, 2026
27 of 32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Issue related to the REST API

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants