|
| 1 | +import { containerTest } from "@internal/testcontainers"; |
| 2 | +import type { PrismaClient } from "@trigger.dev/database"; |
| 3 | +import { describe, expect, vi } from "vitest"; |
| 4 | +import { webhookEndpointLookup } from "~/v3/webhookEndpointLookup"; |
| 5 | + |
| 6 | +vi.setConfig({ testTimeout: 60_000 }); |
| 7 | + |
| 8 | +async function seedEnvironment(prisma: PrismaClient) { |
| 9 | + const slug = `wel_${Math.random().toString(36).slice(2, 10)}`; |
| 10 | + const organization = await prisma.organization.create({ data: { title: slug, slug } }); |
| 11 | + const project = await prisma.project.create({ |
| 12 | + data: { name: slug, slug, organizationId: organization.id, externalRef: slug }, |
| 13 | + }); |
| 14 | + const environment = await prisma.runtimeEnvironment.create({ |
| 15 | + data: { |
| 16 | + slug: "prod", |
| 17 | + type: "PRODUCTION", |
| 18 | + projectId: project.id, |
| 19 | + organizationId: organization.id, |
| 20 | + apiKey: `tr_prod_${slug}`, |
| 21 | + pkApiKey: `pk_prod_${slug}`, |
| 22 | + shortcode: `p${slug.slice(0, 5)}`, |
| 23 | + }, |
| 24 | + }); |
| 25 | + return { organizationId: organization.id, projectId: project.id, environmentId: environment.id }; |
| 26 | +} |
| 27 | + |
| 28 | +async function seedEndpoint( |
| 29 | + prisma: PrismaClient, |
| 30 | + env: { organizationId: string; projectId: string; environmentId: string }, |
| 31 | + declaredId: string, |
| 32 | + tenantId = "" |
| 33 | +) { |
| 34 | + const suffix = Math.random().toString(36).slice(2, 10); |
| 35 | + return prisma.webhookEndpoint.create({ |
| 36 | + data: { |
| 37 | + friendlyId: `wh_${suffix}`, |
| 38 | + opaqueId: `op_${suffix}${Math.random().toString(36).slice(2, 10)}`, |
| 39 | + organizationId: env.organizationId, |
| 40 | + projectId: env.projectId, |
| 41 | + runtimeEnvironmentId: env.environmentId, |
| 42 | + environmentType: "PRODUCTION", |
| 43 | + source: "stripe", |
| 44 | + declaredId, |
| 45 | + endpointTenantId: tenantId, |
| 46 | + routingTargets: [], |
| 47 | + verifierArtifact: { kind: "bundle", bundleUrl: "https://example.test/v.js", hash: "h" }, |
| 48 | + status: "ACTIVE", |
| 49 | + }, |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +describe("webhookEndpointLookup", () => { |
| 54 | + containerTest( |
| 55 | + "finds an endpoint by its declared id or its wh_ id, only in its own environment", |
| 56 | + async ({ prisma }) => { |
| 57 | + const env = await seedEnvironment(prisma); |
| 58 | + const otherEnv = await seedEnvironment(prisma); |
| 59 | + const declared = await seedEndpoint(prisma, env, "payments"); |
| 60 | + const tenantInstance = await seedEndpoint(prisma, env, "payments", "tenant_1"); |
| 61 | + const elsewhere = await seedEndpoint(prisma, otherEnv, "payments"); |
| 62 | + |
| 63 | + const find = (environmentId: string, endpointId: string) => |
| 64 | + prisma.webhookEndpoint.findFirst({ |
| 65 | + where: webhookEndpointLookup(environmentId, endpointId), |
| 66 | + }); |
| 67 | + |
| 68 | + expect((await find(env.environmentId, "payments"))?.id).toBe(declared.id); |
| 69 | + expect((await find(env.environmentId, declared.friendlyId))?.id).toBe(declared.id); |
| 70 | + expect((await find(env.environmentId, tenantInstance.friendlyId))?.id).toBe( |
| 71 | + tenantInstance.id |
| 72 | + ); |
| 73 | + expect(await find(env.environmentId, elsewhere.friendlyId)).toBeNull(); |
| 74 | + expect((await find(otherEnv.environmentId, "payments"))?.id).toBe(elsewhere.id); |
| 75 | + expect(await find(env.environmentId, "refunds")).toBeNull(); |
| 76 | + |
| 77 | + await seedEndpoint(prisma, env, "wh_prefixed"); |
| 78 | + expect(await find(env.environmentId, "wh_prefixed")).toBeNull(); |
| 79 | + } |
| 80 | + ); |
| 81 | +}); |
0 commit comments