route.test.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* @vitest-environment node */
  2. import { describe, it, expect, vi, beforeEach } from "vitest";
  3. vi.mock("@/lib/auth/session", () => ({
  4. getSession: vi.fn(),
  5. }));
  6. import { getSession } from "@/lib/auth/session";
  7. import { GET, dynamic } from "./route.js";
  8. describe("GET /api/auth/me", () => {
  9. beforeEach(() => {
  10. vi.clearAllMocks();
  11. });
  12. it('exports dynamic="force-dynamic" (RHL-006)', () => {
  13. expect(dynamic).toBe("force-dynamic");
  14. });
  15. it("returns { user: null } when unauthenticated", async () => {
  16. getSession.mockResolvedValue(null);
  17. const res = await GET();
  18. expect(res.status).toBe(200);
  19. expect(await res.json()).toEqual({ user: null });
  20. });
  21. it("returns user payload when authenticated (includes email)", async () => {
  22. getSession.mockResolvedValue({
  23. userId: "u1",
  24. role: "branch",
  25. branchId: "NL01",
  26. email: "nl01@example.com",
  27. mustChangePassword: true,
  28. });
  29. const res = await GET();
  30. expect(res.status).toBe(200);
  31. expect(await res.json()).toEqual({
  32. user: {
  33. userId: "u1",
  34. role: "branch",
  35. branchId: "NL01",
  36. email: "nl01@example.com",
  37. mustChangePassword: true,
  38. },
  39. });
  40. });
  41. it("returns email=null and mustChangePassword=false when missing", async () => {
  42. getSession.mockResolvedValue({
  43. userId: "u2",
  44. role: "admin",
  45. branchId: null,
  46. email: null,
  47. mustChangePassword: undefined,
  48. });
  49. const res = await GET();
  50. expect(res.status).toBe(200);
  51. expect(await res.json()).toEqual({
  52. user: {
  53. userId: "u2",
  54. role: "admin",
  55. branchId: null,
  56. email: null,
  57. mustChangePassword: false,
  58. },
  59. });
  60. });
  61. });