-
Notifications
You must be signed in to change notification settings - Fork 120
feat(auth): add request validation for utility endpoints #576
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
Open
Chaitanya-970
wants to merge
2
commits into
Dev-Card:main
Choose a base branch
from
Chaitanya-970:fix/assigned-issue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+140
−2
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| import cookiePlugin from '@fastify/cookie'; | ||
| import jwtPlugin from '@fastify/jwt'; | ||
| import Fastify, { type FastifyInstance } from 'fastify'; | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||
|
|
||
| import { authRoutes } from '../routes/auth.js'; | ||
|
|
||
| async function buildTestApp() { | ||
| const app = Fastify({ logger: false }); | ||
| await app.register(cookiePlugin as any); | ||
| await app.register(jwtPlugin as any, { secret: 'test-secret-for-unit-tests-only' }); | ||
|
|
||
| app.decorate('prisma', { | ||
| refreshToken: { | ||
| findUnique: vi.fn(), | ||
| update: vi.fn(), | ||
| create: vi.fn(), | ||
| updateMany: vi.fn(), | ||
| }, | ||
| user: { | ||
| findUnique: vi.fn() | ||
| } | ||
| } as any); | ||
|
|
||
| app.decorate('redis', { | ||
| getdel: vi.fn(), | ||
| set: vi.fn(), | ||
| exists: vi.fn(), | ||
| } as any); | ||
|
|
||
| app.decorate('authenticate', async () => {}); | ||
|
|
||
| await app.register(authRoutes, { prefix: '/auth' }); | ||
| await app.ready(); | ||
| return app; | ||
| } | ||
|
|
||
| describe('auth validation', () => { | ||
| let app: FastifyInstance; | ||
|
|
||
| beforeEach(async () => { | ||
| app = await buildTestApp(); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await app.close(); | ||
| }); | ||
|
|
||
| describe('POST /auth/mobile/exchange', () => { | ||
| it('rejects missing code', async () => { | ||
| const res = await app.inject({ | ||
| method: 'POST', | ||
| url: '/auth/mobile/exchange', | ||
| payload: {} | ||
| }); | ||
| expect(res.statusCode).toBe(400); | ||
| expect(res.json().error).toBe('Invalid request body'); | ||
| }); | ||
|
|
||
| it('rejects non-string code', async () => { | ||
| const res = await app.inject({ | ||
| method: 'POST', | ||
| url: '/auth/mobile/exchange', | ||
| payload: { code: 123 } | ||
| }); | ||
| expect(res.statusCode).toBe(400); | ||
| expect(res.json().error).toBe('Invalid request body'); | ||
| }); | ||
|
|
||
| it('rejects empty code', async () => { | ||
| const res = await app.inject({ | ||
| method: 'POST', | ||
| url: '/auth/mobile/exchange', | ||
| payload: { code: '' } | ||
| }); | ||
| expect(res.statusCode).toBe(400); | ||
| expect(res.json().error).toBe('Invalid request body'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('POST /auth/refresh', () => { | ||
| it('rejects empty refresh_token in body', async () => { | ||
| const res = await app.inject({ | ||
| method: 'POST', | ||
| url: '/auth/refresh', | ||
| payload: { refresh_token: '' } | ||
| }); | ||
| expect(res.statusCode).toBe(400); | ||
| expect(res.json().error).toBe('Invalid request body'); | ||
| }); | ||
|
|
||
| it('rejects non-string refresh_token in body', async () => { | ||
| const res = await app.inject({ | ||
| method: 'POST', | ||
| url: '/auth/refresh', | ||
| payload: { refresh_token: 123 } | ||
| }); | ||
| expect(res.statusCode).toBe(400); | ||
| expect(res.json().error).toBe('Invalid request body'); | ||
| }); | ||
|
|
||
| it('fails when no token is provided', async () => { | ||
| const res = await app.inject({ | ||
| method: 'POST', | ||
| url: '/auth/refresh' | ||
| }); | ||
| expect(res.statusCode).toBe(401); | ||
| expect(res.json().error).toBe('Refresh token missing'); | ||
| }); | ||
| }); | ||
| }); | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { z } from 'zod'; | ||
|
|
||
| export const mobileExchangeSchema = z.object({ | ||
| code: z.string({ | ||
| required_error: 'missing code', | ||
| invalid_type_error: 'code must be a string' | ||
| }).trim().min(1, 'code is empty') | ||
| }); | ||
|
Chaitanya-970 marked this conversation as resolved.
|
||
|
|
||
| export const refreshTokenSchema = z.object({ | ||
| refresh_token: z.string({ | ||
| required_error: 'missing refresh_token', | ||
| invalid_type_error: 'refresh_token must be a string' | ||
| }).trim().min(1, 'refresh_token is empty') | ||
| }); | ||
|
Chaitanya-970 marked this conversation as resolved.
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.