Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/app/api/settings/disappearing-messages/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,14 @@ export async function PUT(request) {
return NextResponse.json({ error: 'Invalid token' }, { status: 401 });
}

const { default_message_retention_days } = await request.json();
let body;
try {
body = await request.json();
} catch {
return NextResponse.json({ error: 'Invalid JSON body' }, { status: 400 });
}

const { default_message_retention_days } = body;

// Validate input
if (typeof default_message_retention_days !== 'number' || default_message_retention_days < 0) {
Expand Down
13 changes: 13 additions & 0 deletions src/app/api/settings/disappearing-messages/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,17 @@ describe('settings disappearing messages authentication', () => {
expect(mocks.authGetUser).toHaveBeenCalledWith('valid-token');
expect(mocks.updateEq).toHaveBeenCalledWith('auth_user_id', 'auth-user-id');
});

it('returns 400 for malformed PUT JSON instead of a generic 500', async () => {
const { PUT } = await import('./route.js');
const response = await PUT({
headers: new Headers({ authorization: 'Bearer valid-token' }),
json: vi.fn().mockRejectedValue(new SyntaxError('Unexpected token'))
});
const body = await response.json();

expect(response.status).toBe(400);
expect(body.error).toBe('Invalid JSON body');
expect(mocks.from).not.toHaveBeenCalled();
});
});
Loading