route.test.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 } 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("returns 401 when unauthenticated", async () => {
  27. getSession.mockResolvedValue(null);
  28. const req = new Request(
  29. "http://localhost/api/files?branch=NL01&year=2024&month=10&day=23"
  30. );
  31. const res = await GET(req);
  32. expect(res.status).toBe(401);
  33. expect(await res.json()).toEqual({ error: "Unauthorized" });
  34. });
  35. it("returns 403 when branch user accesses a different branch", async () => {
  36. getSession.mockResolvedValue({
  37. role: "branch",
  38. branchId: "NL01",
  39. userId: "u1",
  40. });
  41. const req = new Request(
  42. "http://localhost/api/files?branch=NL02&year=2024&month=10&day=23"
  43. );
  44. const res = await GET(req);
  45. expect(res.status).toBe(403);
  46. expect(await res.json()).toEqual({ error: "Forbidden" });
  47. });
  48. it("returns files for a valid query when allowed", async () => {
  49. getSession.mockResolvedValue({
  50. role: "branch",
  51. branchId: "NL01",
  52. userId: "u1",
  53. });
  54. const req = new Request(
  55. "http://localhost/api/files?branch=NL01&year=2024&month=10&day=23"
  56. );
  57. const res = await GET(req);
  58. expect(res.status).toBe(200);
  59. const body = await res.json();
  60. expect(body.branch).toBe("NL01");
  61. expect(body.files).toHaveLength(1);
  62. expect(body.files[0]).toMatchObject({
  63. name: "test.pdf",
  64. relativePath: "NL01/2024/10/23/test.pdf",
  65. });
  66. });
  67. it("returns 400 when query params are missing (authenticated)", async () => {
  68. getSession.mockResolvedValue({
  69. role: "admin",
  70. branchId: null,
  71. userId: "u2",
  72. });
  73. const req = new Request("http://localhost/api/files");
  74. const res = await GET(req);
  75. expect(res.status).toBe(400);
  76. const body = await res.json();
  77. expect(body).toEqual({
  78. error: "branch, year, month, day sind erforderlich",
  79. });
  80. });
  81. });