|
@@ -4,6 +4,7 @@ import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
|
|
|
|
vi.mock("@/lib/auth/session", () => ({
|
|
vi.mock("@/lib/auth/session", () => ({
|
|
|
getSession: vi.fn(),
|
|
getSession: vi.fn(),
|
|
|
|
|
+ createSession: vi.fn(),
|
|
|
}));
|
|
}));
|
|
|
|
|
|
|
|
vi.mock("@/lib/db", () => ({
|
|
vi.mock("@/lib/db", () => ({
|
|
@@ -26,7 +27,7 @@ vi.mock("bcryptjs", () => {
|
|
|
};
|
|
};
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
-import { getSession } from "@/lib/auth/session";
|
|
|
|
|
|
|
+import { getSession, createSession } from "@/lib/auth/session";
|
|
|
import { getDb } from "@/lib/db";
|
|
import { getDb } from "@/lib/db";
|
|
|
import User from "@/models/user";
|
|
import User from "@/models/user";
|
|
|
import { compare as bcryptCompare, hash as bcryptHash } from "bcryptjs";
|
|
import { compare as bcryptCompare, hash as bcryptHash } from "bcryptjs";
|
|
@@ -186,6 +187,7 @@ describe("POST /api/auth/change-password", () => {
|
|
|
|
|
|
|
|
expect(bcryptHash).not.toHaveBeenCalled();
|
|
expect(bcryptHash).not.toHaveBeenCalled();
|
|
|
expect(user.save).not.toHaveBeenCalled();
|
|
expect(user.save).not.toHaveBeenCalled();
|
|
|
|
|
+ expect(createSession).not.toHaveBeenCalled();
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
it("returns 400 when new password is weak", async () => {
|
|
it("returns 400 when new password is weak", async () => {
|
|
@@ -230,6 +232,7 @@ describe("POST /api/auth/change-password", () => {
|
|
|
|
|
|
|
|
expect(bcryptHash).not.toHaveBeenCalled();
|
|
expect(bcryptHash).not.toHaveBeenCalled();
|
|
|
expect(user.save).not.toHaveBeenCalled();
|
|
expect(user.save).not.toHaveBeenCalled();
|
|
|
|
|
+ expect(createSession).not.toHaveBeenCalled();
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
it("returns 200 and updates passwordHash + clears flags on success", async () => {
|
|
it("returns 200 and updates passwordHash + clears flags on success", async () => {
|
|
@@ -237,11 +240,15 @@ describe("POST /api/auth/change-password", () => {
|
|
|
userId: "u1",
|
|
userId: "u1",
|
|
|
role: "branch",
|
|
role: "branch",
|
|
|
branchId: "NL01",
|
|
branchId: "NL01",
|
|
|
|
|
+ email: "branch@example.com",
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
const user = {
|
|
const user = {
|
|
|
_id: "507f1f77bcf86cd799439011",
|
|
_id: "507f1f77bcf86cd799439011",
|
|
|
passwordHash: "old-hash",
|
|
passwordHash: "old-hash",
|
|
|
|
|
+ role: "branch",
|
|
|
|
|
+ branchId: "NL01",
|
|
|
|
|
+ email: "branch@example.com",
|
|
|
mustChangePassword: true,
|
|
mustChangePassword: true,
|
|
|
passwordResetToken: "tok",
|
|
passwordResetToken: "tok",
|
|
|
passwordResetExpiresAt: new Date("2030-01-01"),
|
|
passwordResetExpiresAt: new Date("2030-01-01"),
|
|
@@ -273,5 +280,12 @@ describe("POST /api/auth/change-password", () => {
|
|
|
expect(user.passwordResetExpiresAt).toBe(null);
|
|
expect(user.passwordResetExpiresAt).toBe(null);
|
|
|
|
|
|
|
|
expect(user.save).toHaveBeenCalledTimes(1);
|
|
expect(user.save).toHaveBeenCalledTimes(1);
|
|
|
|
|
+ expect(createSession).toHaveBeenCalledWith({
|
|
|
|
|
+ userId: "507f1f77bcf86cd799439011",
|
|
|
|
|
+ role: "branch",
|
|
|
|
|
+ branchId: "NL01",
|
|
|
|
|
+ email: "branch@example.com",
|
|
|
|
|
+ mustChangePassword: false,
|
|
|
|
|
+ });
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|