Skip to content
Merged
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
39 changes: 39 additions & 0 deletions src/app/api/notification-settings/route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { NextRequest } from "next/server";
import { PUT } from "./route";

const mocks = vi.hoisted(() => ({
mockGetAuthContext: vi.fn(),
mockFrom: vi.fn(),
}));

vi.mock("@/lib/auth/get-user", () => ({
getAuthContext: mocks.mockGetAuthContext,
}));

function makePutRequest(body: BodyInit) {
return new NextRequest("http://localhost/api/notification-settings", {
method: "PUT",
body,
headers: { "Content-Type": "application/json" },
});
}

describe("PUT /api/notification-settings", () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.mockGetAuthContext.mockResolvedValue({
user: { id: "user-1" },
supabase: { from: mocks.mockFrom },
});
});

it("returns 400 for malformed JSON before touching settings", async () => {
const response = await PUT(makePutRequest("{not valid json"));
const body = await response.json();

expect(response.status).toBe(400);
expect(body.error).toBe("Invalid JSON body");
expect(mocks.mockFrom).not.toHaveBeenCalled();
});
Comment on lines +31 to +38

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Non-object JSON paths untested

readJsonObject has two distinct branches that both return null: a JSON parse failure and successfully-parsed JSON that is not an object (array, null, a string, a number). The single test only exercises the parse-failure path. Sending "null" or "[]" hits the !body || typeof body !== "object" || Array.isArray(body) guard and returns the same 400, but neither case is covered. If that guard were ever accidentally dropped, the upsert code would receive an unexpected value and these paths would silently regress.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

});
17 changes: 16 additions & 1 deletion src/app/api/notification-settings/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ const SETTING_KEYS = [
"email_upvote_milestone",
] as const;

async function readJsonObject(request: NextRequest) {
try {
const body = await request.json();
if (!body || typeof body !== "object" || Array.isArray(body)) {
return null;
}
return body as Record<string, unknown>;
} catch {
return null;
}
}

// GET /api/notification-settings
export async function GET(request: NextRequest) {
try {
Expand Down Expand Up @@ -60,7 +72,10 @@ export async function PUT(request: NextRequest) {
}
const { user, supabase } = auth;

const body = await request.json();
const body = await readJsonObject(request);
if (!body) {
return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 });
}

// Only allow known boolean keys
const updateData: Record<string, boolean> = {};
Expand Down
Loading