File size: 2,921 Bytes
689359a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { GET } from "./route";
import { fetchBackend } from "@/server/backend-api";

vi.mock("@/server/backend-api", async () => {
  const actual = await vi.importActual<typeof import("@/server/backend-api")>(
    "@/server/backend-api",
  );
  return {
    ...actual,
    fetchBackend: vi.fn(),
  };
});

describe("/api/share/[...path]", () => {
  beforeEach(() => {
    vi.resetAllMocks();
  });

  it("proxies shared results without requiring a session", async () => {
    vi.mocked(fetchBackend).mockResolvedValue(
      new Response(JSON.stringify({ source_title: "Shared result" }), {
        status: 200,
        headers: { "Content-Type": "application/json" },
      }),
    );

    const response = await GET(
      new Request("http://localhost/api/share/token-1"),
      { params: Promise.resolve({ path: ["token-1"] }) },
    );

    expect(fetchBackend).toHaveBeenCalledWith(
      "/tasks/shared/token-1",
      expect.objectContaining({ method: "GET", cache: "no-store" }),
    );
    await expect(response.json()).resolves.toEqual({ source_title: "Shared result" });
  });

  it("forwards range headers for shared video playback", async () => {
    vi.mocked(fetchBackend).mockResolvedValue(
      new Response("video", {
        status: 206,
        headers: { "Content-Type": "video/mp4", "Content-Range": "bytes 0-4/5" },
      }),
    );

    await GET(
      new Request("http://localhost/api/share/token-1/clips/clip-1/file", {
        headers: { Range: "bytes=0-4" },
      }),
      { params: Promise.resolve({ path: ["token-1", "clips", "clip-1", "file"] }) },
    );

    expect(fetchBackend).toHaveBeenCalledWith(
      "/tasks/shared/token-1/clips/clip-1/file",
      expect.objectContaining({
        extraHeaders: expect.objectContaining({ Range: "bytes=0-4" }),
      }),
    );
  });

  it("rejects traversal segments without contacting the backend", async () => {
    const response = await GET(
      new Request("http://localhost/api/share/%2e%2e/%2e%2e/api-keys"),
      { params: Promise.resolve({ path: ["..", "..", "api-keys"] }) },
    );

    expect(response.status).toBe(404);
    expect(fetchBackend).not.toHaveBeenCalled();
  });

  it("preserves attachment downloads through a signed storage redirect", async () => {
    vi.mocked(fetchBackend).mockResolvedValue(new Response(null, {
      status: 307, headers: { Location: "https://storage.test/signed-download", "Cache-Control": "private, no-store" },
    }));
    const response = await GET(new Request("http://localhost/api/share/token/clips/clip/file?download=true&v=123"), {
      params: Promise.resolve({ path: ["token", "clips", "clip", "file"] }),
    });
    expect(fetchBackend).toHaveBeenCalledWith("/tasks/shared/token/clips/clip/file?download=true", expect.anything());
    expect(response.status).toBe(307);
    expect(response.headers.get("location")).toBe("https://storage.test/signed-download");
  });

});