File size: 3,795 Bytes
d9e3e09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test";
import { Hono } from "hono";

import * as shared from "@mentra/cloud-shared";
import * as camera from "../services/camera/camera.service";
import { cameraApi, publicOrigin } from "./camera.api";

async function originFor(url: string, headers: Record<string, string> = {}): Promise<string> {
  const app = new Hono();
  app.get("/probe", (c) => c.text(publicOrigin(c)));
  const response = await app.request(url, { headers });
  return response.text();
}

describe("camera publicOrigin", () => {
  test("derives HTTPS from the TLS-terminating ingress", async () => {
    expect(
      await originFor("http://runtime.dev.us-west-2.mentraglass.com/probe", {
        "x-forwarded-proto": "https",
      }),
    ).toBe("https://runtime.dev.us-west-2.mentraglass.com");
  });

  test("uses the first protocol when multiple trusted proxies append values", async () => {
    expect(
      await originFor("http://runtime.internal/probe", {
        "x-forwarded-proto": "https, http",
      }),
    ).toBe("https://runtime.internal");
  });

  test("falls back to the request protocol for direct local requests", async () => {
    expect(await originFor("http://localhost:3001/probe")).toBe("http://localhost:3001");
    expect(await originFor("https://runtime.example.com/probe")).toBe(
      "https://runtime.example.com",
    );
  });

  test("ignores unsupported forwarded protocols", async () => {
    expect(
      await originFor("http://runtime.example.com/probe", {
        "x-forwarded-proto": "javascript",
      }),
    ).toBe("http://runtime.example.com");
  });
});

describe("photo URL allocation", () => {
  const result = { requestId: "photo-1", uploadUrl: "https://upload", readUrl: "https://read" };
  let verify: ReturnType<typeof spyOn<typeof shared, "verifyRuntimeToken">>;
  let allocate: ReturnType<typeof spyOn<typeof camera, "requestPhoto">>;

  beforeEach(() => {
    verify = spyOn(shared, "verifyRuntimeToken").mockResolvedValue({
      mentraUserId: "user-1",
      tenantId: "tenant-1",
      sessionId: "session-1",
      jti: "token-1",
      exp: 9999999999,
    });
    allocate = spyOn(camera, "requestPhoto").mockResolvedValue(result);
  });

  afterEach(() => {
    verify.mockRestore();
    allocate.mockRestore();
  });

  test.each([
    undefined,
    JSON.stringify({ size: "medium" }),
    JSON.stringify({ size: "medium", compress: "heavy", sound: false, saveToGallery: true }),
    JSON.stringify({ size: "unknown", compress: null }),
    "not JSON",
  ])("allocates URLs regardless of unused body %p", async (body) => {
    const response = await cameraApi.request("http://runtime.test/photo", {
      method: "POST",
      headers: { Authorization: "Bearer token", "x-forwarded-proto": "https" },
      body,
    });
    expect(response.status).toBe(200);
    expect(await response.json()).toEqual(result);
    expect(verify).toHaveBeenCalledWith("token");
    expect(allocate.mock.calls).toEqual([["user-1", "https://runtime.test"]]);
  });

  test("still requires authentication before allocating URLs", async () => {
    const response = await cameraApi.request("/photo", { method: "POST" });
    expect(response.status).toBe(401);
    expect(allocate).not.toHaveBeenCalled();
  });

  test("rejects an invalid token before allocating URLs", async () => {
    verify.mockRejectedValueOnce(new shared.AccessTokenError("invalid token"));
    const response = await cameraApi.request("/photo", {
      method: "POST",
      headers: { Authorization: "Bearer invalid" },
    });
    expect(response.status).toBe(401);
    expect(allocate).not.toHaveBeenCalled();
  });
});