File size: 2,802 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
import { createProxyResponse, fetchBackend } from "./backend-api";

const incomingHeaders = vi.hoisted(() => ({ current: new Headers() }));
vi.mock("next/headers", () => ({ headers: async () => incomingHeaders.current }));

describe("backend S3 redirects", () => {
  afterEach(() => vi.unstubAllGlobals());

  it("returns signed redirects to the browser without following them with backend credentials", async () => {
    const fetchMock = vi.fn().mockResolvedValue(new Response(null, { status: 307, headers: {
      Location: "https://s3.test/private-clip?signature=test", "Cache-Control": "private, no-store",
    } }));
    vi.stubGlobal("fetch", fetchMock);
    const response = createProxyResponse(await fetchBackend("/tasks/task/clips/clip/file", { userId: "owner" }));
    expect(fetchMock).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({ redirect: "manual" }));
    expect(response.status).toBe(307);
    expect(response.headers.get("location")).toBe("https://s3.test/private-clip?signature=test");
    expect(response.headers.get("cache-control")).toBe("private, no-store");
  });
});

describe("backend trace ids", () => {
  afterEach(() => {
    vi.unstubAllGlobals();
    incomingHeaders.current = new Headers();
  });

  it("forwards the browser's trace id to the backend", async () => {
    incomingHeaders.current = new Headers({ "x-trace-id": "3f1c9a52-7d1e-4b8a-9c11-0a2b3c4d5e6f" });
    const fetchMock = vi.fn().mockResolvedValue(new Response("{}"));
    vi.stubGlobal("fetch", fetchMock);
    await fetchBackend("/tasks/", { userId: "owner" });
    expect(fetchMock.mock.calls[0][1].headers["x-trace-id"]).toBe("3f1c9a52-7d1e-4b8a-9c11-0a2b3c4d5e6f");
  });

  it("replaces a missing or malformed trace id with a fresh one", async () => {
    incomingHeaders.current = new Headers({ "x-trace-id": "bad id <script>" });
    const fetchMock = vi.fn().mockResolvedValue(new Response("{}"));
    vi.stubGlobal("fetch", fetchMock);
    await fetchBackend("/tasks/");
    expect(fetchMock.mock.calls[0][1].headers["x-trace-id"]).toMatch(/^[0-9a-f-]{36}$/);
  });
});

describe("client platform", () => {
  afterEach(() => {
    vi.unstubAllGlobals();
    incomingHeaders.current = new Headers();
  });

  it.each([
    ["ios", "ios"],
    [" iOS ", "ios"],
    ["web", undefined],
    ["android", undefined],
    [null, undefined],
  ])("forwards x-supoclip-client %j as %j", async (sent, forwarded) => {
    incomingHeaders.current = new Headers(sent === null ? {} : { "x-supoclip-client": sent });
    const fetchMock = vi.fn().mockResolvedValue(new Response("{}"));
    vi.stubGlobal("fetch", fetchMock);
    await fetchBackend("/tasks/workflows", { userId: "owner" });
    expect(fetchMock.mock.calls[0][1].headers["x-supoclip-client"]).toBe(forwarded);
  });
});