File size: 3,952 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { headers } from "next/headers";

import { POST } from "./route";
import { auth } from "@/lib/auth";

vi.mock("next/headers", () => ({
  headers: vi.fn(),
}));

vi.mock("@/lib/auth", () => ({
  auth: {
    api: {
      getSession: vi.fn(),
    },
  },
}));

describe("/api/upload/authorization", () => {
  beforeEach(() => {
    vi.resetAllMocks();
    vi.unstubAllEnvs();
    vi.mocked(headers).mockResolvedValue(new Headers());
  });

  afterEach(() => vi.unstubAllGlobals());

  it("requests user-scoped multipart authorization from the backend", async () => {
    vi.stubEnv("BACKEND_AUTH_SECRET", "secret");
    vi.stubEnv("BACKEND_INTERNAL_URL", "http://backend:8000");
    vi.mocked(auth.api.getSession).mockResolvedValue({ user: { id: "owner" } } as never);
    const payload = { storage: "s3", video_path: "upload://s3/owner/id/video.mp4", parts: [] };
    const fetchMock = vi.fn().mockResolvedValue(Response.json(payload));
    vi.stubGlobal("fetch", fetchMock);
    const response = await POST(new Request("http://app/api/upload/authorization", {
      method: "POST", body: JSON.stringify({ filename: "video.mp4", size: 6, contentType: "video/mp4" }),
    }));
    await expect(response.json()).resolves.toEqual(payload);
    expect(fetchMock).toHaveBeenCalledWith("http://backend:8000/upload/authorize", expect.objectContaining({
      headers: expect.objectContaining({ "x-supoclip-user-id": "owner" }),
    }));
  });

  it("returns 401 when unauthenticated", async () => {
    vi.mocked(auth.api.getSession).mockResolvedValue(null as never);

    const response = await POST(new Request("http://app/api/upload/authorization", { method: "POST" }));

    expect(response.status).toBe(401);
    await expect(response.json()).resolves.toEqual({ error: "Unauthorized" });
  });

  it("falls back to the server-side proxy when signed backend auth is unavailable", async () => {
    vi.stubEnv("BACKEND_AUTH_SECRET", "");
    vi.mocked(auth.api.getSession).mockResolvedValue({
      user: { id: "user-1" },
    } as never);

    const response = await POST(new Request("http://app/api/upload/authorization", { method: "POST" }));

    expect(response.status).toBe(200);
    await expect(response.json()).resolves.toEqual({
      directUpload: false,
      reason: "signed_backend_auth_required",
    });
  });

  it("returns signed direct-upload headers when backend auth is configured", async () => {
    vi.stubEnv("BACKEND_AUTH_SECRET", "secret");
    vi.stubEnv("NEXT_PUBLIC_API_URL", "https://api.supoclip.com/");
    vi.spyOn(Date, "now").mockReturnValue(1_700_000_000_000);
    vi.mocked(auth.api.getSession).mockResolvedValue({
      user: { id: "user-1" },
    } as never);

    const response = await POST(new Request("http://app/api/upload/authorization", { method: "POST" }));

    expect(response.status).toBe(200);
    await expect(response.json()).resolves.toEqual({
      directUpload: true,
      uploadUrl: "https://api.supoclip.com/upload",
      headers: {
        "x-supoclip-user-id": "user-1",
        "x-supoclip-ts": "1700000000",
        "x-supoclip-signature":
          "cceb65b01012f5596122712d023d1def6663579f457362796a52133b3875c545",
      },
    });
  });

  it("keeps uploads available during a frontend-first rollout", async () => {
    vi.stubEnv("BACKEND_AUTH_SECRET", "secret");
    vi.stubEnv("NEXT_PUBLIC_API_URL", "https://api.supoclip.com");
    vi.mocked(auth.api.getSession).mockResolvedValue({ user: { id: "owner" } } as never);
    vi.stubGlobal("fetch", vi.fn().mockResolvedValue(new Response(null, { status: 404 })));
    const response = await POST(new Request("http://app/api/upload/authorization", {
      method: "POST", body: JSON.stringify({ filename: "video.mp4", size: 6, contentType: "video/mp4" }),
    }));
    expect(response.status).toBe(200);
    expect(await response.json()).toMatchObject({ directUpload: true, uploadUrl: "https://api.supoclip.com/upload" });
  });

});