|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +/** |
| 4 | + * The run-commit route answers with a deployment's git metadata — commit message, author, branch, |
| 5 | + * PR title. The deployments list serves the same blob behind `read` on `deployments`, so a |
| 6 | + * credential without that scope must not get it from here either. |
| 7 | + */ |
| 8 | + |
| 9 | +const { SESSION_SECRET } = vi.hoisted(() => ({ |
| 10 | + SESSION_SECRET: "test-session-secret-for-run-commit-authorization", |
| 11 | +})); |
| 12 | + |
| 13 | +const mocks = vi.hoisted(() => ({ |
| 14 | + can: vi.fn<(...args: any[]) => boolean>(), |
| 15 | + resolveRunCommit: vi.fn<(...args: any[]) => Promise<any>>(), |
| 16 | +})); |
| 17 | + |
| 18 | +vi.mock("@internal/tracing", () => ({ |
| 19 | + getMeter: () => ({ |
| 20 | + createCounter: () => ({ add: vi.fn() }), |
| 21 | + createHistogram: () => ({ record: vi.fn() }), |
| 22 | + createObservableGauge: () => ({ addCallback: vi.fn() }), |
| 23 | + }), |
| 24 | +})); |
| 25 | +vi.mock("~/env.server", () => ({ |
| 26 | + env: { SESSION_SECRET, APP_ORIGIN: "https://example.com" }, |
| 27 | +})); |
| 28 | +vi.mock("~/services/logger.server", () => ({ |
| 29 | + logger: { debug: vi.fn(), error: vi.fn(), warn: vi.fn(), info: vi.fn() }, |
| 30 | +})); |
| 31 | +vi.mock("~/services/rbac.server", () => ({ |
| 32 | + rbac: { |
| 33 | + authenticateBearer: vi.fn(), |
| 34 | + authenticateUserActor: async () => ({ ok: true, ability: { can: mocks.can } }), |
| 35 | + authenticatePat: async () => ({ ok: true, ability: { can: mocks.can } }), |
| 36 | + }, |
| 37 | +})); |
| 38 | +vi.mock("~/services/personalAccessToken.server", () => ({ |
| 39 | + authenticateApiRequestWithPersonalAccessToken: vi.fn(), |
| 40 | + isPersonalAccessToken: () => false, |
| 41 | +})); |
| 42 | +vi.mock("~/services/organizationAccessToken.server", () => ({ |
| 43 | + authenticateApiRequestWithOrganizationAccessToken: vi.fn(), |
| 44 | + isOrganizationAccessToken: () => false, |
| 45 | +})); |
| 46 | +vi.mock("~/services/realtime/jwtAuth.server", () => ({ |
| 47 | + isPublicJWT: () => false, |
| 48 | + validatePublicJwtKey: vi.fn(), |
| 49 | +})); |
| 50 | +vi.mock("~/models/project.server", () => ({ |
| 51 | + findProjectByRef: async (externalRef: string, userId: string) => |
| 52 | + externalRef === PROJECT.externalRef && userId === USER_ID ? PROJECT : null, |
| 53 | +})); |
| 54 | +vi.mock("~/models/runtimeEnvironment.server", () => ({ |
| 55 | + authIncludeBase: {}, |
| 56 | + authIncludeWithParent: {}, |
| 57 | + findEnvironmentByApiKey: vi.fn(), |
| 58 | + findEnvironmentByApiKeyWithResolution: vi.fn(), |
| 59 | + findEnvironmentByPublicApiKey: vi.fn(), |
| 60 | + toAuthenticated: (environment: any) => environment, |
| 61 | +})); |
| 62 | +vi.mock("~/db.server", () => ({ |
| 63 | + prisma: {}, |
| 64 | + $replica: { |
| 65 | + user: { |
| 66 | + findUnique: async ({ where }: any) => (where.id === USER_ID ? { id: where.id } : null), |
| 67 | + }, |
| 68 | + runtimeEnvironment: { |
| 69 | + findFirst: async ({ where }: any) => (where.slug === ENVIRONMENT.slug ? ENVIRONMENT : null), |
| 70 | + }, |
| 71 | + workerDeployment: { |
| 72 | + findFirst: async () => ({ |
| 73 | + git: { commitMessage: "fix billing for Acme Corp", commitAuthorName: "Alice" }, |
| 74 | + shortCode: "abcd", |
| 75 | + deployedAt: new Date(), |
| 76 | + }), |
| 77 | + }, |
| 78 | + }, |
| 79 | +})); |
| 80 | +vi.mock("~/services/dashboardAgent.server", () => ({ |
| 81 | + resolveRunCommit: mocks.resolveRunCommit, |
| 82 | +})); |
| 83 | + |
| 84 | +import { signUserActorToken } from "@trigger.dev/rbac"; |
| 85 | +import { loader as commitLoader } from "~/routes/api.v1.projects.$projectRef.$env.runs.$runId.commit"; |
| 86 | + |
| 87 | +const ORGANIZATION = { id: "org_1234", slug: "test-org" }; |
| 88 | +const PROJECT = { id: "proj_1234", externalRef: "proj_ref_1234", slug: "test-project" }; |
| 89 | +const USER_ID = "usr_member"; |
| 90 | +const ENVIRONMENT = { |
| 91 | + id: "env_prod", |
| 92 | + slug: "prod", |
| 93 | + type: "PRODUCTION" as const, |
| 94 | + apiKey: "tr_prod_abcdefghijklmnop", |
| 95 | + organizationId: ORGANIZATION.id, |
| 96 | + organization: ORGANIZATION, |
| 97 | + projectId: PROJECT.id, |
| 98 | + project: PROJECT, |
| 99 | +}; |
| 100 | + |
| 101 | +async function getCommit(): Promise<{ status: number; body: any }> { |
| 102 | + const token = await signUserActorToken(SESSION_SECRET, { |
| 103 | + userId: USER_ID, |
| 104 | + client: "dashboard-agent", |
| 105 | + environmentId: ENVIRONMENT.id, |
| 106 | + cap: ["read:runs"], |
| 107 | + }); |
| 108 | + |
| 109 | + const response = await commitLoader({ |
| 110 | + request: new Request( |
| 111 | + `https://example.com/api/v1/projects/${PROJECT.externalRef}/prod/runs/run_1/commit`, |
| 112 | + { headers: { Authorization: `Bearer ${token}` } } |
| 113 | + ), |
| 114 | + params: { projectRef: PROJECT.externalRef, env: "prod", runId: "run_1" }, |
| 115 | + context: {} as any, |
| 116 | + } as any); |
| 117 | + return { status: response.status, body: await response.json() }; |
| 118 | +} |
| 119 | + |
| 120 | +describe("reading a run's commit metadata", () => { |
| 121 | + beforeEach(() => { |
| 122 | + mocks.can.mockReset(); |
| 123 | + mocks.resolveRunCommit.mockReset(); |
| 124 | + // The commit resolves fine, so a refusal can only come from the gate. |
| 125 | + mocks.resolveRunCommit.mockResolvedValue({ |
| 126 | + version: "20240101.1", |
| 127 | + sha: "abc123", |
| 128 | + dirty: false, |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + it("refuses a caller who may not read deployments", async () => { |
| 133 | + mocks.can.mockImplementation((_action, resource) => resource?.type !== "deployments"); |
| 134 | + |
| 135 | + const result = await getCommit(); |
| 136 | + |
| 137 | + expect(result.status).toBe(403); |
| 138 | + expect(JSON.stringify(result.body)).not.toContain("Acme Corp"); |
| 139 | + expect(mocks.resolveRunCommit).not.toHaveBeenCalled(); |
| 140 | + }); |
| 141 | + |
| 142 | + it("answers a caller who may", async () => { |
| 143 | + mocks.can.mockReturnValue(true); |
| 144 | + |
| 145 | + const result = await getCommit(); |
| 146 | + |
| 147 | + expect(result.status).toBe(200); |
| 148 | + expect(result.body.sha).toBe("abc123"); |
| 149 | + expect(result.body.git.commitMessage).toBe("fix billing for Acme Corp"); |
| 150 | + }); |
| 151 | +}); |
0 commit comments