route.test.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/files", () => {
  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-files-"));
  17. process.env.NAS_ROOT_PATH = tmpRoot;
  18. const dir = path.join(tmpRoot, "NL01", "2024", "10", "23");
  19. await fs.mkdir(dir, { recursive: true });
  20. await fs.writeFile(path.join(dir, "test.pdf"), "dummy-pdf-content");
  21. });
  22. afterEach(async () => {
  23. process.env.NAS_ROOT_PATH = originalNasRoot;
  24. if (tmpRoot) await fs.rm(tmpRoot, { recursive: true, force: true });
  25. });
  26. it('exports dynamic="force-dynamic" (RHL-006)', () => {
  27. expect(dynamic).toBe("force-dynamic");
  28. });
  29. it("returns 401 when unauthenticated", async () => {
  30. getSession.mockResolvedValue(null);
  31. const req = new Request(
  32. "http://localhost/api/files?branch=NL01&year=2024&month=10&day=23"
  33. );
  34. const res = await GET(req);
  35. expect(res.status).toBe(401);
  36. expect(await res.json()).toEqual({
  37. error: { message: "Unauthorized", code: "AUTH_UNAUTHENTICATED" },
  38. });
  39. });
  40. it("returns 403 when branch user accesses a different branch", async () => {
  41. getSession.mockResolvedValue({
  42. role: "branch",
  43. branchId: "NL01",
  44. userId: "u1",
  45. });
  46. const req = new Request(
  47. "http://localhost/api/files?branch=NL02&year=2024&month=10&day=23"
  48. );
  49. const res = await GET(req);
  50. expect(res.status).toBe(403);
  51. expect(await res.json()).toEqual({
  52. error: { message: "Forbidden", code: "AUTH_FORBIDDEN_BRANCH" },
  53. });
  54. });
  55. it("returns files for a valid query when allowed", async () => {
  56. getSession.mockResolvedValue({
  57. role: "branch",
  58. branchId: "NL01",
  59. userId: "u1",
  60. });
  61. const req = new Request(
  62. "http://localhost/api/files?branch=NL01&year=2024&month=10&day=23"
  63. );
  64. const res = await GET(req);
  65. expect(res.status).toBe(200);
  66. const body = await res.json();
  67. expect(body.branch).toBe("NL01");
  68. expect(body.files).toHaveLength(1);
  69. expect(body.files[0]).toMatchObject({
  70. name: "test.pdf",
  71. relativePath: "NL01/2024/10/23/test.pdf",
  72. });
  73. });
  74. it("returns 400 when query params are missing (authenticated)", async () => {
  75. getSession.mockResolvedValue({
  76. role: "admin",
  77. branchId: null,
  78. userId: "u2",
  79. });
  80. const req = new Request("http://localhost/api/files");
  81. const res = await GET(req);
  82. expect(res.status).toBe(400);
  83. const body = await res.json();
  84. expect(body).toEqual({
  85. error: {
  86. message: "Missing required query parameter(s)",
  87. code: "VALIDATION_MISSING_QUERY",
  88. details: { params: ["branch", "year", "month", "day"] },
  89. },
  90. });
  91. });
  92. it("returns 404 when the day folder does not exist (authorized)", async () => {
  93. getSession.mockResolvedValue({
  94. role: "admin",
  95. branchId: null,
  96. userId: "u2",
  97. });
  98. const req = new Request(
  99. "http://localhost/api/files?branch=NL01&year=2024&month=10&day=99"
  100. );
  101. const res = await GET(req);
  102. expect(res.status).toBe(404);
  103. expect(await res.json()).toEqual({
  104. error: {
  105. message: "Not found",
  106. code: "FS_NOT_FOUND",
  107. details: { branch: "NL01", year: "2024", month: "10", day: "99" },
  108. },
  109. });
  110. });
  111. });