-
-
Notifications
You must be signed in to change notification settings - Fork 254
fix(api_views): remove throttles and add auth classes to webhook api #7453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
95d7e89
fix(api_views): remove throttles and add auth classes to webhook endp…
nadahlberg 2573e93
feat(tests): add session client helper for webhook tests
nadahlberg d86334e
Merge branch 'main' into remove-webhooks-api-from-rate-limits
albertisfu 35003ce
Merge branch 'main' into remove-webhooks-api-from-rate-limits
nadahlberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 Restricting these viewsets to
[ReplicaRoutingSessionAuthentication]replaces (not extends) DRF's default auth classes, so Token auth is no longer accepted. The existingWebhooksHTMXTestssuite incl/users/tests.py(line 3546) usesmake_client()fromcl/tests/utils.py:107-113, which authenticates exclusively via theAuthorization: Token <token>header and never establishes a Django session — so all ~9 tests in that class (test_make_an_webhook,test_make_an_http_webhook_fails,test_list_users_webhooks,test_delete_webhook,test_webhook_detail,test_webhook_update,test_send_webhook_test,test_send_webhook_test_all_types,test_list_webhook_events) will return 401/403 and CI will break. Fix by migrating those tests to a session-authenticated client (e.g.client.force_login/APIClient.force_authenticate) alongside this PR.Extended reasoning...
What the bug is
DRF's
authentication_classeson a view replacesDEFAULT_AUTHENTICATION_CLASSESrather than appending to them. Before this PR,WebhooksViewSetandWebhookEventViewSetinherited the project defaults fromcl/settings/third_party/rest_framework.py(Token, OAuth2, Basic, Session). After this PR they accept onlyReplicaRoutingSessionAuthentication, which extends DRFSessionAuthentication(cl/api/authentication.py:100) and only authenticates via the Django session cookie — it ignoresAuthorization: Token …headers entirely.The test path that breaks
WebhooksHTMXTests(cl/users/tests.py:3546) builds its clients insetUp:make_client(cl/tests/utils.py:107-113) is the only authentication step:No
login, noforce_login, no session cookie — just the Token header.Step-by-step proof that
test_make_an_webhookwill failsetUprunsself.client = make_client(self.user_1.pk)→AsyncAPIClientwith onlyHTTP_AUTHORIZATION: Token <token>set.test_make_an_webhookcallsself.make_a_webhook(self.client)→POST /…webhooks-list….WebhooksViewSet; with this PR,authentication_classes = [ReplicaRoutingSessionAuthentication]is the only class consulted.ReplicaRoutingSessionAuthentication(subclass ofSessionAuthentication) readsrequest._request.userfrom the session middleware. No session cookie is present, sorequest.userresolves toAnonymousUserandauthenticate()returnsNone. TheAuthorization: Token <token>header is never inspected becauseTokenAuthenticationis no longer in the list.IsAuthenticatedrejects → DRF returns 401 Unauthorized.response.status_code == HTTPStatus.CREATED(201) →AssertionError, CI fails.Why the existing code doesn't protect against this
The PR diff only touches
cl/users/api_views.py; no companion update tocl/users/tests.pyand no override ofsetUpto establish a session. All nine listed tests (test_make_an_webhook,test_make_an_http_webhook_fails,test_list_users_webhooks,test_delete_webhook,test_webhook_detail,test_webhook_update,test_send_webhook_test,test_send_webhook_test_all_types,test_list_webhook_events) follow the same pattern: they assertHTTPStatus.OK/CREATED/NO_CONTENT/NOT_FOUNDand will all instead receive 401/403.Impact and fix
This is a hard CI break on merge — every test in
WebhooksHTMXTestsflips red. The fix is to switch the test client to session auth. Either replacemake_client(user.pk)with anAsyncAPIClient()and callawait sync_to_async(client.force_login)(user)insetUp, or useclient.force_authenticate(user=user). (The product intent — that these UI viewsets are only callable via the logged-in browser session — is correct; only the tests need updating.)