Skip to content

Commit e572add

Browse files
authored
test(api): enforce maxLength validation boundaries for user parameter (JhaSourav07#2059)
Fixes JhaSourav07#1428 ## Description Added comprehensive unit tests in `lib/validations.test.ts` and endpoint integration tests in `app/api/streak/route.test.ts` to verify that the `?user=` query handle parameter strictly enforces the 39-character maxLength boundary condition. ## Pillar - [x] 🛠️ Other (Bug fix, refactoring, docs, testing) ## Checklist - [x] I have read the CONTRIBUTING.md file. - [x] My commits follow the Conventional Commits format. - [x] I have made sure that I have only one commit to merge in this PR.
2 parents 0ca1dad + a50f97e commit e572add

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

app/api/streak/route.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { describe, it, expect, vi, beforeEach } from 'vitest';
2+
import { NextRequest } from 'next/server';
23
import { GET } from './route';
34

45
// We only mock the two things that reach outside this process:
@@ -205,6 +206,18 @@ describe('GET /api/streak', () => {
205206
expect(JSON.stringify(body)).toContain('cannot exceed 39 characters');
206207
});
207208

209+
it('returns 400 Bad Request and details indicating the username cannot exceed 39 characters when using NextRequest', async () => {
210+
const url = `http://localhost/api/streak?user=${'a'.repeat(40)}`;
211+
const request = new NextRequest(url);
212+
213+
const response = await GET(request);
214+
215+
expect(response.status).toBe(400);
216+
const body = await response.json();
217+
expect(body.error).toBe('Invalid parameters');
218+
expect(body.details.fieldErrors.user[0]).toMatch(/cannot exceed 39 characters/);
219+
});
220+
208221
it('returns 400 for invalid monthly badge dimensions', async () => {
209222
const invalidDimensionParams: Array<Record<string, string>> = [
210223
{ width: 'abc' },

lib/validations.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ describe('streakParamsSchema user validation', () => {
9999

100100
expect(result.success).toBe(true);
101101
});
102+
103+
it('should enforce a maximum length constraint of 39 characters for the user parameter', () => {
104+
const invalidUser = 'a'.repeat(40);
105+
const result = streakParamsSchema.safeParse({
106+
user: invalidUser,
107+
});
108+
109+
expect(result.success).toBe(false);
110+
if (!result.success) {
111+
expect(result.error.issues[0]?.message).toMatch(/cannot exceed 39 characters/);
112+
}
113+
});
102114
});
103115

104116
describe('streakParamsSchema', () => {

0 commit comments

Comments
 (0)