|
| 1 | +import type http from "node:http"; |
| 2 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 3 | +import { handleAuthRoutes } from "./auth-routes"; |
| 4 | + |
| 5 | +type CapturedResponse = { |
| 6 | + status: number; |
| 7 | + body: unknown; |
| 8 | +}; |
| 9 | + |
| 10 | +function createAuthRouteHarness(options: { |
| 11 | + headers?: Record<string, string>; |
| 12 | + pathname?: string; |
| 13 | +}): { |
| 14 | + captured: CapturedResponse; |
| 15 | + ctx: Parameters<typeof handleAuthRoutes>[0]; |
| 16 | +} { |
| 17 | + const captured: CapturedResponse = { |
| 18 | + status: 200, |
| 19 | + body: null, |
| 20 | + }; |
| 21 | + const req = { |
| 22 | + headers: { |
| 23 | + host: "127.0.0.1:31337", |
| 24 | + ...options.headers, |
| 25 | + }, |
| 26 | + socket: { |
| 27 | + remoteAddress: "127.0.0.1", |
| 28 | + }, |
| 29 | + } as http.IncomingMessage; |
| 30 | + const res = {} as http.ServerResponse; |
| 31 | + |
| 32 | + return { |
| 33 | + captured, |
| 34 | + ctx: { |
| 35 | + req, |
| 36 | + res, |
| 37 | + method: "GET", |
| 38 | + pathname: options.pathname ?? "/api/auth/me", |
| 39 | + readJsonBody: async () => null, |
| 40 | + json: (_res, data, status = 200) => { |
| 41 | + captured.status = status; |
| 42 | + captured.body = data; |
| 43 | + }, |
| 44 | + error: (_res, message, status = 500) => { |
| 45 | + captured.status = status; |
| 46 | + captured.body = { error: message }; |
| 47 | + }, |
| 48 | + pairingEnabled: () => false, |
| 49 | + ensurePairingCode: () => null, |
| 50 | + normalizePairingCode: (code) => code, |
| 51 | + rateLimitPairing: () => true, |
| 52 | + getPairingExpiresAt: () => Date.now() + 60_000, |
| 53 | + clearPairing: () => {}, |
| 54 | + }, |
| 55 | + }; |
| 56 | +} |
| 57 | + |
| 58 | +describe("handleAuthRoutes", () => { |
| 59 | + const originalEnv = { ...process.env }; |
| 60 | + |
| 61 | + beforeEach(() => { |
| 62 | + process.env = { ...originalEnv }; |
| 63 | + process.env.ELIZA_REQUIRE_LOCAL_AUTH = "1"; |
| 64 | + process.env.ELIZA_API_TOKEN = "native-token"; |
| 65 | + }); |
| 66 | + |
| 67 | + afterEach(() => { |
| 68 | + process.env = { ...originalEnv }; |
| 69 | + }); |
| 70 | + |
| 71 | + it("returns a local session for the authorized on-device agent token", async () => { |
| 72 | + const { ctx, captured } = createAuthRouteHarness({ |
| 73 | + headers: { |
| 74 | + authorization: "Bearer native-token", |
| 75 | + }, |
| 76 | + }); |
| 77 | + |
| 78 | + await expect(handleAuthRoutes(ctx)).resolves.toBe(true); |
| 79 | + |
| 80 | + expect(captured.status).toBe(200); |
| 81 | + expect(captured.body).toMatchObject({ |
| 82 | + identity: { |
| 83 | + id: "local-agent", |
| 84 | + displayName: "Local Agent", |
| 85 | + kind: "machine", |
| 86 | + }, |
| 87 | + session: { |
| 88 | + id: "local", |
| 89 | + kind: "local", |
| 90 | + expiresAt: null, |
| 91 | + }, |
| 92 | + access: { |
| 93 | + mode: "local", |
| 94 | + passwordConfigured: false, |
| 95 | + ownerConfigured: false, |
| 96 | + }, |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + it("requires the bearer token when Android local auth is enforced", async () => { |
| 101 | + const { ctx, captured } = createAuthRouteHarness({}); |
| 102 | + |
| 103 | + await expect(handleAuthRoutes(ctx)).resolves.toBe(true); |
| 104 | + |
| 105 | + expect(captured.status).toBe(401); |
| 106 | + expect(captured.body).toMatchObject({ |
| 107 | + reason: "remote_auth_required", |
| 108 | + access: { |
| 109 | + mode: "local", |
| 110 | + passwordConfigured: true, |
| 111 | + ownerConfigured: false, |
| 112 | + }, |
| 113 | + }); |
| 114 | + }); |
| 115 | +}); |
0 commit comments