|
| 1 | +import { postgresTest } from "@internal/testcontainers"; |
| 2 | +import rbac from "@trigger.dev/rbac"; |
| 3 | +import { expect } from "vitest"; |
| 4 | +import { dashboardEnvironmentAccess } from "./dashboardEnvironmentAccess.server"; |
| 5 | + |
| 6 | +postgresTest( |
| 7 | + "the writer resolves the actual tenant and tier; self-hosted members retain permissive abilities", |
| 8 | + async ({ prisma }) => { |
| 9 | + const member = await prisma.user.create({ |
| 10 | + data: { |
| 11 | + email: "runtime-member@example.test", |
| 12 | + authenticationMethod: "MAGIC_LINK", |
| 13 | + admin: false, |
| 14 | + }, |
| 15 | + }); |
| 16 | + const outsider = await prisma.user.create({ |
| 17 | + data: { email: "runtime-outsider@example.test", authenticationMethod: "MAGIC_LINK" }, |
| 18 | + }); |
| 19 | + const organization = await prisma.organization.create({ |
| 20 | + data: { |
| 21 | + slug: "runtime-auth", |
| 22 | + title: "Runtime auth", |
| 23 | + members: { create: { userId: member.id, role: "MEMBER" } }, |
| 24 | + }, |
| 25 | + }); |
| 26 | + const project = await prisma.project.create({ |
| 27 | + data: { |
| 28 | + slug: "runtime-auth", |
| 29 | + name: "Runtime auth", |
| 30 | + externalRef: "runtime-auth", |
| 31 | + organizationId: organization.id, |
| 32 | + }, |
| 33 | + }); |
| 34 | + const controller = rbac.create(prisma, { forceFallback: true }); |
| 35 | + const request = new Request("https://example.test/orgs/wrong/projects/wrong/env/dev"); |
| 36 | + |
| 37 | + for (const type of ["DEVELOPMENT", "STAGING", "PREVIEW", "PRODUCTION"] as const) { |
| 38 | + const environment = await prisma.runtimeEnvironment.create({ |
| 39 | + data: { |
| 40 | + slug: type, |
| 41 | + type, |
| 42 | + shortcode: type, |
| 43 | + apiKey: `key_${type}`, |
| 44 | + pkApiKey: `pk_${type}`, |
| 45 | + projectId: project.id, |
| 46 | + organizationId: organization.id, |
| 47 | + }, |
| 48 | + }); |
| 49 | + const access = await dashboardEnvironmentAccess( |
| 50 | + prisma, |
| 51 | + controller, |
| 52 | + request, |
| 53 | + member.id, |
| 54 | + environment.id |
| 55 | + ); |
| 56 | + expect(access.environment).toEqual({ |
| 57 | + id: environment.id, |
| 58 | + type, |
| 59 | + organizationId: organization.id, |
| 60 | + projectId: project.id, |
| 61 | + }); |
| 62 | + expect(access.ability.can("write", { type: "tasks", envType: type })).toBe(true); |
| 63 | + await expect( |
| 64 | + dashboardEnvironmentAccess(prisma, controller, request, outsider.id, environment.id) |
| 65 | + ).rejects.toMatchObject({ status: 404 }); |
| 66 | + await expect( |
| 67 | + dashboardEnvironmentAccess(prisma, controller, request, "", environment.id) |
| 68 | + ).rejects.toMatchObject({ status: 404 }); |
| 69 | + // Each inactive ancestor must independently reject an otherwise valid member. |
| 70 | + for (const inactiveTarget of ["environment", "project", "organization"] as const) { |
| 71 | + if (inactiveTarget === "environment") { |
| 72 | + await prisma.runtimeEnvironment.update({ |
| 73 | + where: { id: environment.id }, |
| 74 | + data: { archivedAt: new Date() }, |
| 75 | + }); |
| 76 | + } else if (inactiveTarget === "project") { |
| 77 | + await prisma.project.update({ |
| 78 | + where: { id: project.id }, |
| 79 | + data: { deletedAt: new Date() }, |
| 80 | + }); |
| 81 | + } else { |
| 82 | + await prisma.organization.update({ |
| 83 | + where: { id: organization.id }, |
| 84 | + data: { deletedAt: new Date() }, |
| 85 | + }); |
| 86 | + } |
| 87 | + await expect( |
| 88 | + dashboardEnvironmentAccess(prisma, controller, request, member.id, environment.id) |
| 89 | + ).rejects.toMatchObject({ status: 404 }); |
| 90 | + await prisma.runtimeEnvironment.update({ |
| 91 | + where: { id: environment.id }, |
| 92 | + data: { archivedAt: null }, |
| 93 | + }); |
| 94 | + await prisma.project.update({ where: { id: project.id }, data: { deletedAt: null } }); |
| 95 | + await prisma.organization.update({ |
| 96 | + where: { id: organization.id }, |
| 97 | + data: { deletedAt: null }, |
| 98 | + }); |
| 99 | + } |
| 100 | + } |
| 101 | + await expect( |
| 102 | + dashboardEnvironmentAccess(prisma, controller, request, member.id, "missing") |
| 103 | + ).rejects.toMatchObject({ status: 404 }); |
| 104 | + } |
| 105 | +); |
0 commit comments