|
|
@@ -1,9 +1,10 @@
|
|
|
+// app/api/auth/login/route.test.js
|
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
|
|
// 1) Mocks
|
|
|
|
|
|
vi.mock("@/lib/db", () => ({
|
|
|
- default: vi.fn(),
|
|
|
+ getDb: vi.fn(),
|
|
|
}));
|
|
|
|
|
|
vi.mock("@/models/user", () => ({
|
|
|
@@ -24,9 +25,9 @@ vi.mock("bcryptjs", () => {
|
|
|
};
|
|
|
});
|
|
|
|
|
|
-// 2) Imports NACH den Mocks
|
|
|
+// 2) Imports AFTER the mocks
|
|
|
|
|
|
-import dbConnect from "@/lib/db";
|
|
|
+import { getDb } from "@/lib/db";
|
|
|
import User from "@/models/user";
|
|
|
import { createSession } from "@/lib/auth/session";
|
|
|
import { compare as bcryptCompare } from "bcryptjs";
|
|
|
@@ -43,7 +44,7 @@ function createRequestStub(body) {
|
|
|
describe("POST /api/auth/login", () => {
|
|
|
beforeEach(() => {
|
|
|
vi.clearAllMocks();
|
|
|
- dbConnect.mockResolvedValue(undefined);
|
|
|
+ getDb.mockResolvedValue({}); // we only need it to "connect"
|
|
|
});
|
|
|
|
|
|
it("logs in successfully with correct credentials", async () => {
|
|
|
@@ -72,7 +73,7 @@ describe("POST /api/auth/login", () => {
|
|
|
expect(response.status).toBe(200);
|
|
|
expect(json).toEqual({ ok: true });
|
|
|
|
|
|
- expect(dbConnect).toHaveBeenCalledTimes(1);
|
|
|
+ expect(getDb).toHaveBeenCalledTimes(1);
|
|
|
expect(User.findOne).toHaveBeenCalledWith({ username: "branchuser" });
|
|
|
|
|
|
expect(bcryptCompare).toHaveBeenCalledWith(
|