fix(tests): stop unconditionally mocking require_api_key in saved-views tests - #2193
fix(tests): stop unconditionally mocking require_api_key in saved-views tests#2193Subramaniyajothi6 wants to merge 4 commits into
Conversation
9755d3f to
663cc7a
Compare
|
Rebased onto current Why the extra commit is needed
@pytest.mark.skip(reason="pre-existing upstream issue: app_client overrides auth so 401 cannot be tested here")That reason is accurate about the cause — the Merging as-is would have kept them. Simulating the merge against current So the PR would have gone green, closed #2192, and left both auth tests skipped on Verification (rebased branch,
On the issue title: worth noting there was never a live auth bypass. Diff is still one file: |
utksh1
left a comment
There was a problem hiding this comment.
Reviewed the updated test fixture and auth coverage. Removing the unconditional dependency override allows the unauthenticated and invalid-key tests to exercise the real router dependency, and the added commit removes the skips introduced on main. The branch still needs a fresh rebase and required checks before merge.
663cc7a to
fa68e3f
Compare
|
Fresh required CI is blocked at the shared frontend npm audit gate by high-severity undici advisory GHSA-4cwx-7wf7-3272. This PR is backend-test-only and does not change frontend dependencies; approval remains in place while the repository-level audit issue is unresolved. |
…ws tests The app_client fixture overrode require_api_key with a mock that always succeeds, regardless of the X-Api-Key header. That made the fixture's own negative-path tests (test_unauthenticated_request_rejected, test_wrong_api_key_rejected) always observe a 200, since the real auth check never ran. The fixture already issues a real API key and sends it via X-Api-Key, so the mock was redundant for the happy-path tests and actively broke the auth-negative ones. Production auth in saved_views.py (router-level Depends(require_api_key)) was never affected — this was a test-only gap, not a live auth bypass.
Commit 1c33d64 marked test_unauthenticated_request_rejected and test_wrong_api_key_rejected as skipped, noting that app_client overrode require_api_key so 401 could not be observed. This branch already removes that override, so the tests can run for real. Dropping the skip markers restores actual coverage of auth enforcement on the saved-views router, which does declare dependencies=[Depends(require_api_key)] and rejects both an empty and an incorrect API key.
fa68e3f to
d4d3aa8
Compare
Summary
Fixes the
backend-unitfailure tracked in #2192 (test_saved_views.py::test_unauthenticated_request_rejectedandtest_wrong_api_key_rejected, both asserting200 == 401).Correction to #2192: after digging in, this is a test-fixture bug, not a live security regression. Production auth is fine —
saved_views_routerinbackend/secuscan/saved_views.pystill declaresdependencies=[Depends(require_api_key)]at the router level, andrequire_api_keyinauth.pyis untouched and correctly rejects missing/invalid keys.The actual cause: the
app_clientfixture intesting/backend/unit/test_saved_views.pydid this unconditionally:_mock_require_api_keyalways returns success, regardless of what's in the request. That override predates PR #2025/issue #1743. When #2025 added the two auth-negative tests (test_unauthenticated_request_rejected,test_wrong_api_key_rejected) alongside a real API key already being issued and sent viaX-Api-Key, the override made those two tests structurally unable to pass — they always hit the mock, never the real check, so they always got200no matter what header was sent.Fix
Remove the
require_api_keydependency override entirely. The fixture already provisions a real API key (_auth_module.init_api_key(...)) and sends it viaX-Api-Keyon every client, so the real dependency is exercised for all existing happy-path tests too — nothing else needed mocking here.Verification
pytest testing/backend/unit/test_saved_views.py -q→ 47 passed (was 2 failed, 45 passed before the fix).pytest testing/backend/unit -q -m "not benchmark"→ same pass count as before plus these 2, no new failures.ruff check backend testing/backend→ clean.Test plan
test_unauthenticated_request_rejectedandtest_wrong_api_key_rejectedpasstesting/backend/unitsuite has no new failuresruff check backend testing/backendpasses