|
|
@@ -1,4 +1,3 @@
|
|
|
-// app/api/auth/login/route.test.js
|
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
|
|
// 1) Mocks
|
|
|
@@ -86,6 +85,26 @@ describe("POST /api/auth/login", () => {
|
|
|
});
|
|
|
});
|
|
|
|
|
|
+ it("returns 400 when JSON parsing fails", async () => {
|
|
|
+ // Simulate request.json() throwing (invalid JSON body).
|
|
|
+ const request = {
|
|
|
+ json: vi.fn().mockRejectedValue(new Error("invalid json")),
|
|
|
+ };
|
|
|
+
|
|
|
+ const response = await POST(request);
|
|
|
+ const body = await response.json();
|
|
|
+
|
|
|
+ expect(response.status).toBe(400);
|
|
|
+ expect(body).toEqual({
|
|
|
+ error: {
|
|
|
+ message: "Invalid request body",
|
|
|
+ code: "VALIDATION_INVALID_JSON",
|
|
|
+ },
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(createSession).not.toHaveBeenCalled();
|
|
|
+ });
|
|
|
+
|
|
|
it("returns 401 when user does not exist", async () => {
|
|
|
User.findOne.mockReturnValue({
|
|
|
exec: vi.fn().mockResolvedValue(null),
|
|
|
@@ -97,10 +116,15 @@ describe("POST /api/auth/login", () => {
|
|
|
});
|
|
|
|
|
|
const response = await POST(request);
|
|
|
- const json = await response.json();
|
|
|
+ const body = await response.json();
|
|
|
|
|
|
expect(response.status).toBe(401);
|
|
|
- expect(json).toEqual({ error: "Invalid credentials" });
|
|
|
+ expect(body).toEqual({
|
|
|
+ error: {
|
|
|
+ message: "Invalid credentials",
|
|
|
+ code: "AUTH_INVALID_CREDENTIALS",
|
|
|
+ },
|
|
|
+ });
|
|
|
|
|
|
expect(createSession).not.toHaveBeenCalled();
|
|
|
});
|
|
|
@@ -122,10 +146,15 @@ describe("POST /api/auth/login", () => {
|
|
|
});
|
|
|
|
|
|
const response = await POST(request);
|
|
|
- const json = await response.json();
|
|
|
+ const body = await response.json();
|
|
|
|
|
|
expect(response.status).toBe(401);
|
|
|
- expect(json).toEqual({ error: "Invalid credentials" });
|
|
|
+ expect(body).toEqual({
|
|
|
+ error: {
|
|
|
+ message: "Invalid credentials",
|
|
|
+ code: "AUTH_INVALID_CREDENTIALS",
|
|
|
+ },
|
|
|
+ });
|
|
|
|
|
|
expect(bcryptCompare).not.toHaveBeenCalled();
|
|
|
expect(createSession).not.toHaveBeenCalled();
|
|
|
@@ -152,10 +181,15 @@ describe("POST /api/auth/login", () => {
|
|
|
});
|
|
|
|
|
|
const response = await POST(request);
|
|
|
- const json = await response.json();
|
|
|
+ const body = await response.json();
|
|
|
|
|
|
expect(response.status).toBe(401);
|
|
|
- expect(json).toEqual({ error: "Invalid credentials" });
|
|
|
+ expect(body).toEqual({
|
|
|
+ error: {
|
|
|
+ message: "Invalid credentials",
|
|
|
+ code: "AUTH_INVALID_CREDENTIALS",
|
|
|
+ },
|
|
|
+ });
|
|
|
|
|
|
expect(createSession).not.toHaveBeenCalled();
|
|
|
});
|
|
|
@@ -166,10 +200,16 @@ describe("POST /api/auth/login", () => {
|
|
|
});
|
|
|
|
|
|
const response = await POST(request);
|
|
|
- const json = await response.json();
|
|
|
+ const body = await response.json();
|
|
|
|
|
|
expect(response.status).toBe(400);
|
|
|
- expect(json).toEqual({ error: "Missing username or password" });
|
|
|
+ expect(body).toEqual({
|
|
|
+ error: {
|
|
|
+ message: "Missing username or password",
|
|
|
+ code: "VALIDATION_MISSING_FIELD",
|
|
|
+ details: { fields: ["username", "password"] },
|
|
|
+ },
|
|
|
+ });
|
|
|
|
|
|
expect(User.findOne).not.toHaveBeenCalled();
|
|
|
expect(createSession).not.toHaveBeenCalled();
|
|
|
@@ -186,10 +226,15 @@ describe("POST /api/auth/login", () => {
|
|
|
});
|
|
|
|
|
|
const response = await POST(request);
|
|
|
- const json = await response.json();
|
|
|
+ const body = await response.json();
|
|
|
|
|
|
expect(response.status).toBe(500);
|
|
|
- expect(json).toEqual({ error: "Internal server error" });
|
|
|
+ expect(body).toEqual({
|
|
|
+ error: {
|
|
|
+ message: "Internal server error",
|
|
|
+ code: "INTERNAL_SERVER_ERROR",
|
|
|
+ },
|
|
|
+ });
|
|
|
|
|
|
expect(createSession).not.toHaveBeenCalled();
|
|
|
});
|