|
| 1 | +/** |
| 2 | + * E2E auth baseline tests. |
| 3 | + * |
| 4 | + * These tests capture current auth behavior before the apiBuilder migration to RBAC. |
| 5 | + * Run them before and after the migration to verify behavior is identical. |
| 6 | + * |
| 7 | + * Requires a pre-built webapp: pnpm run build --filter webapp |
| 8 | + */ |
| 9 | +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; |
| 10 | +import type { TestServer } from "@internal/testcontainers/webapp"; |
| 11 | +import { startTestServer } from "@internal/testcontainers/webapp"; |
| 12 | +import { generateJWT } from "@trigger.dev/core/v3/jwt"; |
| 13 | +import { seedTestEnvironment } from "./helpers/seedTestEnvironment"; |
| 14 | + |
| 15 | +vi.setConfig({ testTimeout: 180_000 }); |
| 16 | + |
| 17 | +// Shared across all tests in this file — one postgres container + one webapp instance. |
| 18 | +let server: TestServer; |
| 19 | + |
| 20 | +beforeAll(async () => { |
| 21 | + server = await startTestServer(); |
| 22 | +}, 180_000); |
| 23 | + |
| 24 | +afterAll(async () => { |
| 25 | + await server?.stop(); |
| 26 | +}, 120_000); |
| 27 | + |
| 28 | +async function generateTestJWT( |
| 29 | + environment: { id: string; apiKey: string }, |
| 30 | + options: { scopes?: string[] } = {} |
| 31 | +): Promise<string> { |
| 32 | + const scopes = options.scopes ?? ["read:runs"]; |
| 33 | + return generateJWT({ |
| 34 | + secretKey: environment.apiKey, |
| 35 | + payload: { pub: true, sub: environment.id, scopes }, |
| 36 | + expirationTime: "15m", |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +describe("API bearer auth — baseline behavior", () => { |
| 41 | + it("valid API key: auth passes (404 not 401)", async () => { |
| 42 | + const { apiKey } = await seedTestEnvironment(server.prisma); |
| 43 | + const res = await server.webapp.fetch("/api/v1/runs/run_doesnotexist/result", { |
| 44 | + headers: { Authorization: `Bearer ${apiKey}` }, |
| 45 | + }); |
| 46 | + // Auth passed — resource just doesn't exist |
| 47 | + expect(res.status).not.toBe(401); |
| 48 | + expect(res.status).not.toBe(403); |
| 49 | + }); |
| 50 | + |
| 51 | + it("missing Authorization header: 401", async () => { |
| 52 | + const res = await server.webapp.fetch("/api/v1/runs/run_doesnotexist/result"); |
| 53 | + expect(res.status).toBe(401); |
| 54 | + }); |
| 55 | + |
| 56 | + it("invalid API key: 401", async () => { |
| 57 | + const res = await server.webapp.fetch("/api/v1/runs/run_doesnotexist/result", { |
| 58 | + headers: { Authorization: "Bearer tr_dev_completely_invalid_key_xyz_not_real" }, |
| 59 | + }); |
| 60 | + expect(res.status).toBe(401); |
| 61 | + }); |
| 62 | + |
| 63 | + it("401 response has error field", async () => { |
| 64 | + const res = await server.webapp.fetch("/api/v1/runs/run_doesnotexist/result"); |
| 65 | + const body = await res.json(); |
| 66 | + expect(body).toHaveProperty("error"); |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +describe("JWT bearer auth — baseline behavior", () => { |
| 71 | + it("valid JWT on JWT-enabled route: auth passes", async () => { |
| 72 | + const { environment } = await seedTestEnvironment(server.prisma); |
| 73 | + const jwt = await generateTestJWT(environment, { scopes: ["read:runs"] }); |
| 74 | + |
| 75 | + // /api/v1/runs has allowJWT: true with superScopes: ["read:runs", ...] |
| 76 | + const res = await server.webapp.fetch("/api/v1/runs", { |
| 77 | + headers: { Authorization: `Bearer ${jwt}` }, |
| 78 | + }); |
| 79 | + |
| 80 | + // Auth passed — 200 (empty list) or 400 (bad search params), not 401 |
| 81 | + expect(res.status).not.toBe(401); |
| 82 | + }); |
| 83 | + |
| 84 | + it("valid JWT on non-JWT route: 401", async () => { |
| 85 | + const { environment } = await seedTestEnvironment(server.prisma); |
| 86 | + const jwt = await generateTestJWT(environment, { scopes: ["read:runs"] }); |
| 87 | + |
| 88 | + // /api/v1/runs/$runParam/result does NOT have allowJWT: true |
| 89 | + const res = await server.webapp.fetch("/api/v1/runs/run_doesnotexist/result", { |
| 90 | + headers: { Authorization: `Bearer ${jwt}` }, |
| 91 | + }); |
| 92 | + |
| 93 | + expect(res.status).toBe(401); |
| 94 | + }); |
| 95 | + |
| 96 | + it("JWT with empty scopes on JWT-enabled route: 403", async () => { |
| 97 | + const { environment } = await seedTestEnvironment(server.prisma); |
| 98 | + const jwt = await generateTestJWT(environment, { scopes: [] }); |
| 99 | + |
| 100 | + const res = await server.webapp.fetch("/api/v1/runs", { |
| 101 | + headers: { Authorization: `Bearer ${jwt}` }, |
| 102 | + }); |
| 103 | + |
| 104 | + // Empty scopes → no read:runs permission → 403 |
| 105 | + expect(res.status).toBe(403); |
| 106 | + }); |
| 107 | + |
| 108 | + it("JWT signed with wrong key: 401", async () => { |
| 109 | + const { environment } = await seedTestEnvironment(server.prisma); |
| 110 | + const jwt = await generateJWT({ |
| 111 | + secretKey: "wrong-signing-key-that-does-not-match-environment-key", |
| 112 | + payload: { pub: true, sub: environment.id, scopes: ["read:runs"] }, |
| 113 | + }); |
| 114 | + |
| 115 | + const res = await server.webapp.fetch("/api/v1/runs", { |
| 116 | + headers: { Authorization: `Bearer ${jwt}` }, |
| 117 | + }); |
| 118 | + |
| 119 | + expect(res.status).toBe(401); |
| 120 | + }); |
| 121 | +}); |
0 commit comments