route.test.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* @vitest-environment node */
  2. import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
  3. import fs from "node:fs/promises";
  4. import os from "node:os";
  5. import path from "node:path";
  6. vi.mock("@/lib/auth/session", () => ({
  7. getSession: vi.fn(),
  8. }));
  9. import { getSession } from "@/lib/auth/session";
  10. import { GET, dynamic } from "./route.js";
  11. describe("GET /api/branches", () => {
  12. let tmpRoot;
  13. const originalNasRoot = process.env.NAS_ROOT_PATH;
  14. beforeEach(async () => {
  15. vi.clearAllMocks();
  16. tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "api-branches-"));
  17. process.env.NAS_ROOT_PATH = tmpRoot;
  18. });
  19. afterEach(async () => {
  20. process.env.NAS_ROOT_PATH = originalNasRoot;
  21. if (tmpRoot) {
  22. await fs.rm(tmpRoot, { recursive: true, force: true });
  23. }
  24. });
  25. it('exports dynamic="force-dynamic" (RHL-006)', () => {
  26. expect(dynamic).toBe("force-dynamic");
  27. });
  28. it("returns 401 when unauthenticated", async () => {
  29. getSession.mockResolvedValue(null);
  30. const res = await GET();
  31. expect(res.status).toBe(401);
  32. const body = await res.json();
  33. expect(body).toEqual({
  34. error: { message: "Unauthorized", code: "AUTH_UNAUTHENTICATED" },
  35. });
  36. });
  37. it("returns only the own branch for branch users", async () => {
  38. getSession.mockResolvedValue({
  39. role: "branch",
  40. branchId: "NL01",
  41. userId: "u1",
  42. });
  43. await fs.mkdir(path.join(tmpRoot, "NL01"), { recursive: true });
  44. await fs.mkdir(path.join(tmpRoot, "NL02"), { recursive: true });
  45. const res = await GET();
  46. expect(res.status).toBe(200);
  47. const body = await res.json();
  48. expect(body.branches).toEqual(["NL01"]);
  49. });
  50. it("returns all branches for admin/dev users", async () => {
  51. getSession.mockResolvedValue({
  52. role: "admin",
  53. branchId: null,
  54. userId: "u2",
  55. });
  56. await fs.mkdir(path.join(tmpRoot, "NL01"), { recursive: true });
  57. await fs.mkdir(path.join(tmpRoot, "NL02"), { recursive: true });
  58. const res = await GET();
  59. expect(res.status).toBe(200);
  60. const body = await res.json();
  61. expect([...body.branches].sort()).toEqual(["NL01", "NL02"]);
  62. });
  63. it("returns 500 when NAS_ROOT_PATH is invalid (authenticated)", async () => {
  64. getSession.mockResolvedValue({
  65. role: "admin",
  66. branchId: null,
  67. userId: "u2",
  68. });
  69. process.env.NAS_ROOT_PATH = path.join(tmpRoot, "does-not-exist");
  70. const res = await GET();
  71. expect(res.status).toBe(500);
  72. const body = await res.json();
  73. expect(body).toEqual({
  74. error: { message: "Internal server error", code: "FS_STORAGE_ERROR" },
  75. });
  76. });
  77. });