test(auth): protected route integration coverage#162
Open
Jayking40 wants to merge 1 commit intoCalloraOrg:mainfrom
Open
test(auth): protected route integration coverage#162Jayking40 wants to merge 1 commit intoCalloraOrg:mainfrom
Jayking40 wants to merge 1 commit intoCalloraOrg:mainfrom
Conversation
|
@Jayking40 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
test(auth): requireAuth integration coverage for protected routes
Summary
Adds comprehensive integration tests for the requireAuth middleware across all six protected endpoints in the application. The existing protected.test.ts file tested a standalone JWT guard on a mock Express app — the new tests exercise the actual requireAuth middleware against routes wired through createApp, ensuring unauthenticated requests are consistently rejected with correct status codes and error bodies.
This directly addresses the need to verify that protected endpoints reject unauthenticated requests and return consistent
{ error, code }JSON responses via the global errorHandler.Changes
Failure-mode matrix across all protected routes: Added a
describe.eachblock that runs four auth failure scenarios (no header, empty Bearer, whitespace-only Bearer, non-Bearer scheme) against every protected endpoint:GET /api/developers/apisGET /api/developers/analyticsPOST /api/vault/deposit/prepareGET /api/vault/balanceDELETE /api/keys/:idPOST /api/developers/apisHappy-path credential acceptance: Added tests verifying that both
Authorization: Bearer <id>andx-user-idheaders pass authentication on each protected route (asserting the response is not 401).Error body consistency assertions: Added tests confirming:
application/jsoncontent type{ error: "Unauthorized", code: "UNAUTHORIZED" }shapeTest infrastructure: Added required
jest.mockcalls foruuid,better-sqlite3, anduserRepositoryto allow createApp to be imported in the integration test environment without native bindings or the Prisma generated client.Testing
PASS tests/integration/protected.test.ts GET /api/usage - JWT protected (5 existing tests) requireAuth – rejects unauthenticated requests on all protected routes get /api/developers/apis (4 tests) get /api/developers/analytics (4 tests) post /api/vault/deposit/prepare (4 tests) get /api/vault/balance (4 tests) delete /api/keys/nonexistent-id (4 tests) post /api/developers/apis (4 tests) requireAuth – accepts valid credentials on protected routes (8 tests) requireAuth – error body consistency (3 tests)
Test Suites: 1 passed, 1 total Tests: 40 passed, 40 total
Full test suite: 208 tests pass across 20 suites. Two pre-existing suite failures (app.test.ts, health.test.ts) are caused by a missing Prisma generated client and are unrelated to this change.
Security note: The requireAuth middleware correctly rejects empty and whitespace-only Bearer tokens via
.trim()— no bypass vector was found.Closes #110