test: add malformed/non-object body tests for assets, employees, requests

Closes the coverage gap noted in CLAUDE.md's "Việc còn dở" — mirrors the
existing pattern in tests/licenses.test.ts and tests/assignments.test.ts for
POST /api/assets, POST /api/employees, POST /api/requests (the only body-
accepting methods on these three routes; none have a PATCH route yet).
This commit is contained in:
toanhoang
2026-08-04 06:40:13 -04:00
parent 2a0acd11a9
commit be47eb69d3
3 changed files with 72 additions and 0 deletions
+24
View File
@@ -297,6 +297,30 @@ describe("POST /api/assets", () => {
const remaining = await prisma.assetItem.findMany(); const remaining = await prisma.assetItem.findMany();
expect(remaining).toHaveLength(0); expect(remaining).toHaveLength(0);
}); });
it("rejects an invalid JSON body", async () => {
const req = new Request("http://localhost/api/assets", {
method: "POST",
headers: { "content-type": "application/json" },
body: "not json",
});
const res = await POST(req);
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toMatch(/JSON/i);
});
it("rejects a non-object JSON body", async () => {
// Note: arrays are `typeof "object"` in JS, so they pass the route's
// object check — use a JSON string primitive here to actually exercise
// the "not an object" branch (mirrors tests/licenses.test.ts).
const res = await POST(postRequest("not an object"));
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toMatch(/JSON object/i);
});
}); });
describe("GET /api/assets", () => { describe("GET /api/assets", () => {
+24
View File
@@ -125,6 +125,30 @@ describe("POST /api/employees", () => {
}); });
expect(updated.managerId).toBe(created.id); expect(updated.managerId).toBe(created.id);
}); });
it("rejects an invalid JSON body", async () => {
const req = new Request("http://localhost/api/employees", {
method: "POST",
headers: { "content-type": "application/json" },
body: "not json",
});
const res = await POST(req);
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toMatch(/JSON/i);
});
it("rejects a non-object JSON body", async () => {
// Note: arrays are `typeof "object"` in JS, so they pass the route's
// object check — use a JSON string primitive here to actually exercise
// the "not an object" branch (mirrors tests/licenses.test.ts).
const res = await POST(postRequest("not an object"));
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toMatch(/JSON object/i);
});
}); });
describe("GET /api/employees", () => { describe("GET /api/employees", () => {
+24
View File
@@ -239,6 +239,30 @@ describe("POST /api/requests", () => {
const body = await res.json(); const body = await res.json();
expect(body.error).toMatch(/purchase_order_id/i); expect(body.error).toMatch(/purchase_order_id/i);
}); });
it("rejects an invalid JSON body", async () => {
const req = new Request("http://localhost/api/requests", {
method: "POST",
headers: { "content-type": "application/json" },
body: "not json",
});
const res = await POST(req);
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toMatch(/JSON/i);
});
it("rejects a non-object JSON body", async () => {
// Note: arrays are `typeof "object"` in JS, so they pass the route's
// object check — use a JSON string primitive here to actually exercise
// the "not an object" branch (mirrors tests/licenses.test.ts).
const res = await POST(postRequest("not an object"));
expect(res.status).toBe(400);
const body = await res.json();
expect(body.error).toMatch(/JSON object/i);
});
}); });
describe("GET /api/requests", () => { describe("GET /api/requests", () => {