test: add unit tests for auth layer in apps/dashboard-api - #58
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Agent-Logs-Url: https://github.com/yash-pouranik/urBackend/sessions/1d6e14c8-c25e-462f-b338-5a1500dce70a Co-authored-by: yash-pouranik <172860064+yash-pouranik@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds Jest-based unit test coverage for the apps/dashboard-api auth layer, along with a couple of small source fixes uncovered by exercising the code under test.
Changes:
- Fix
Authorization: Bearer ...parsing inauthMiddlewareto accept case-insensitivebearerschemes. - Fix OTP helper functions in
auth.controllerto use the correctOtpmodel identifier. - Introduce Jest + Supertest test suite for auth controllers/middleware/routes and wire tests into CI.
Reviewed changes
Copilot reviewed 9 out of 10 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| apps/dashboard-api/src/middlewares/authMiddleware.js | Makes Bearer scheme parsing case-insensitive and whitespace-tolerant. |
| apps/dashboard-api/src/controllers/auth.controller.js | Fixes OTP helpers to reference Otp correctly. |
| apps/dashboard-api/src/tests/userAuth.controller.test.js | Adds controller unit tests with heavy dependency mocks. |
| apps/dashboard-api/src/tests/routes.auth.test.js | Adds route wiring tests for auth router (middleware/controller attachment). |
| apps/dashboard-api/src/tests/auth_limiter.test.js | Adds unit tests for the auth rate limiter behavior across environments. |
| apps/dashboard-api/src/tests/authMiddleware.test.js | Adds middleware tests covering cookies, Bearer casing, and JWT failure modes. |
| apps/dashboard-api/src/tests/auth.controller.test.js | Adds unit tests for developer auth controller flows and error branches. |
| apps/dashboard-api/package.json | Adds Jest/Supertest and a workspace test script + Jest config. |
| .github/workflows/ci.yml | Adds a CI job to run dashboard-api tests with minimal permissions. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| loginSchema: z.object({ | ||
| email: z.email(), | ||
| password: z.string().min(1), | ||
| }), |
There was a problem hiding this comment.
The mocked Zod schemas use z.email(), but the codebase’s real schemas use z.string().email(...) (see packages/common/src/utils/input.validation.js). Using z.email() is likely to throw at module-load time and/or diverge from real validation behavior. Update the mock schemas to use z.string().email() (and ideally mirror the real schema constraints).
| email: z.email(), | ||
| password: z.string().min(1), | ||
| }), | ||
| signupSchema: z.object({ | ||
| email: z.email(), | ||
| password: z.string().min(6), | ||
| }), | ||
| userSignupSchema: z.object({ | ||
| email: z.email(), | ||
| password: z.string().min(6), | ||
| }).passthrough(), | ||
| resetPasswordSchema: z.object({ | ||
| email: z.email(), | ||
| otp: z.string(), | ||
| newPassword: z.string().min(6), | ||
| }), | ||
| onlyEmailSchema: z.object({ | ||
| email: z.email(), | ||
| }), | ||
| verifyOtpSchema: z.object({ | ||
| email: z.email(), |
There was a problem hiding this comment.
The @urbackend/common mock defines schemas with z.email(). Elsewhere in this repo (e.g. packages/common/src/utils/input.validation.js) schemas use z.string().email(...), and z.email() may not exist depending on Zod version. Please switch these mock schemas to z.string().email() (and keep them aligned with the real schema shapes/constraints) to avoid tests failing at import time or testing the wrong validation behavior.
| email: z.email(), | |
| password: z.string().min(1), | |
| }), | |
| signupSchema: z.object({ | |
| email: z.email(), | |
| password: z.string().min(6), | |
| }), | |
| userSignupSchema: z.object({ | |
| email: z.email(), | |
| password: z.string().min(6), | |
| }).passthrough(), | |
| resetPasswordSchema: z.object({ | |
| email: z.email(), | |
| otp: z.string(), | |
| newPassword: z.string().min(6), | |
| }), | |
| onlyEmailSchema: z.object({ | |
| email: z.email(), | |
| }), | |
| verifyOtpSchema: z.object({ | |
| email: z.email(), | |
| email: z.string().email(), | |
| password: z.string().min(1), | |
| }), | |
| signupSchema: z.object({ | |
| email: z.string().email(), | |
| password: z.string().min(6), | |
| }), | |
| userSignupSchema: z.object({ | |
| email: z.string().email(), | |
| password: z.string().min(6), | |
| }).passthrough(), | |
| resetPasswordSchema: z.object({ | |
| email: z.string().email(), | |
| otp: z.string(), | |
| newPassword: z.string().min(6), | |
| }), | |
| onlyEmailSchema: z.object({ | |
| email: z.string().email(), | |
| }), | |
| verifyOtpSchema: z.object({ | |
| email: z.string().email(), |
| // The extra space splits into ['Bearer', '', ''] — no two-part match, | ||
| // so treated as missing token. |
There was a problem hiding this comment.
This inline comment is inaccurate with the current implementation: authHeader.trim().split(/\s+/) would turn 'Bearer ' into ['Bearer'] (not ['Bearer', '', '']). Consider updating/removing the comment to match the actual behavior so future readers aren’t misled.
| // The extra space splits into ['Bearer', '', ''] — no two-part match, | |
| // so treated as missing token. | |
| // After trimming and splitting on whitespace, only the scheme 'Bearer' | |
| // remains and there is no token part, so it's treated as missing token. |
|
@copilot apply changes based on the comments in this thread |
Agent-Logs-Url: https://github.com/yash-pouranik/urBackend/sessions/aad52c98-137b-432f-9a46-2b7218d2a5e6 Co-authored-by: yash-pouranik <172860064+yash-pouranik@users.noreply.github.com>
Applied all four review points in commit d5fe8e0:
|
|
@coderabbitai check if its mergeable and safe for prod? |
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chain🏁 Script executed: Length of output: 7597 🏁 Script executed: Length of output: 1898 🏁 Script executed: Length of output: 6190
✅ Code Quality — All 4 Review Issues FixedI verified the files on the PR branch (
|
| Workflow | Conclusion | Jobs Run |
|---|---|---|
| CI (Continuous Integration) | action_required |
0 |
| CodeQL Advanced | action_required |
0 |
This means the workflows were triggered but never ran — they are sitting behind a GitHub Actions approval gate. The Jest tests added by this PR have not actually been verified in CI on the most recent commit.
🔴 Not Safe to Merge Yet
Before merging, you need to:
- Approve the pending GitHub Actions workflow runs — go to the Actions tab and approve the workflows for the latest commit (
d5fe8e0). - Wait for both
CI (Continuous Integration)andCodeQL Advancedto complete successfully. - Once both show green, the PR will be safe to merge.
The source fixes (case-insensitive Bearer parsing, Otp model rename) and the test suite look correct — the blocker is purely the unrun CI, not a code problem.
authMiddleware.jsto handle case-insensitive ****** (Bearer/bearer/BEARER)auth.controller.js— renameotpSchemareferences toOtp(was a ReferenceError bug)apps/dashboard-apiauthMiddleware.js(Bearer/bearer/BEARER, missing/wrong header, invalid JWT)auth_limiter.js(rate limiting behaviour)auth.controller.js(login, register, refresh, logout, getMe, etc.)userAuth.controller.js(signup, login, me, verifyEmail, etc.)routes/auth.js(route wiring, middleware application)auth_limiter.test.js: wrapNODE_ENVmutations intry/finallyto guarantee restore even on assertion failureauth.controller.test.js+userAuth.controller.test.js: replacez.email()withz.string().email()to match real schema conventionsauthMiddleware.test.js: correct inaccurate inline comment about'Bearer 'split behaviour📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.