|
| 1 | +import { postgresTest } from "@internal/testcontainers"; |
| 2 | +import type { PrismaClient } from "@trigger.dev/database"; |
| 3 | +import { beforeEach, describe, expect, vi } from "vitest"; |
| 4 | +import { DeleteOrganizationService } from "~/services/deleteOrganization.server"; |
| 5 | +import { createTestUser, uniqueId } from "./fixtures/environmentVariablesFixtures"; |
| 6 | + |
| 7 | +const { getCurrentPlanOrThrowMock, cancelSubscriptionMock } = vi.hoisted(() => ({ |
| 8 | + getCurrentPlanOrThrowMock: vi.fn(async (_orgId: string): Promise<unknown> => undefined), |
| 9 | + cancelSubscriptionMock: vi.fn(async (_orgId: string): Promise<unknown> => undefined), |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock("~/services/platform.v3.server", async (importOriginal) => { |
| 13 | + const mod = (await importOriginal()) as Record<string, unknown>; |
| 14 | + return { |
| 15 | + ...mod, |
| 16 | + getCurrentPlanOrThrow: getCurrentPlanOrThrowMock, |
| 17 | + cancelSubscription: cancelSubscriptionMock, |
| 18 | + }; |
| 19 | +}); |
| 20 | + |
| 21 | +vi.mock("~/v3/commonWorker.server", () => ({ |
| 22 | + commonWorker: { enqueue: async () => undefined }, |
| 23 | +})); |
| 24 | + |
| 25 | +vi.setConfig({ testTimeout: 60_000 }); |
| 26 | + |
| 27 | +async function createOrg(prisma: PrismaClient) { |
| 28 | + const user = await createTestUser(prisma); |
| 29 | + const organization = await prisma.organization.create({ |
| 30 | + data: { |
| 31 | + title: "Test Org", |
| 32 | + slug: uniqueId("org"), |
| 33 | + members: { create: { userId: user.id, role: "ADMIN" } }, |
| 34 | + }, |
| 35 | + }); |
| 36 | + return { user, organization }; |
| 37 | +} |
| 38 | + |
| 39 | +async function deleteOrg(prisma: PrismaClient, slug: string, userId: string) { |
| 40 | + return new DeleteOrganizationService(prisma).call({ |
| 41 | + organizationSlug: slug, |
| 42 | + userId, |
| 43 | + request: new Request("https://cloud.trigger.dev/"), |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +function plan(v3Subscription: Record<string, unknown>) { |
| 48 | + return { success: true, v3Subscription: { freeTierStatus: "approved", ...v3Subscription } }; |
| 49 | +} |
| 50 | + |
| 51 | +describe("DeleteOrganizationService billing", () => { |
| 52 | + beforeEach(() => { |
| 53 | + getCurrentPlanOrThrowMock.mockReset(); |
| 54 | + cancelSubscriptionMock.mockReset(); |
| 55 | + }); |
| 56 | + |
| 57 | + postgresTest("aborts when the plan lookup fails", async ({ prisma }) => { |
| 58 | + const { user, organization } = await createOrg(prisma); |
| 59 | + getCurrentPlanOrThrowMock.mockRejectedValue(new Error("billing down")); |
| 60 | + |
| 61 | + await expect(deleteOrg(prisma, organization.slug, user.id)).rejects.toThrow( |
| 62 | + "Couldn't verify billing state, try again" |
| 63 | + ); |
| 64 | + |
| 65 | + expect(cancelSubscriptionMock).not.toHaveBeenCalled(); |
| 66 | + const org = await prisma.organization.findUniqueOrThrow({ where: { id: organization.id } }); |
| 67 | + expect(org.deletedAt).toBeNull(); |
| 68 | + }); |
| 69 | + |
| 70 | + postgresTest("cancels a paid subscription then deletes", async ({ prisma }) => { |
| 71 | + const { user, organization } = await createOrg(prisma); |
| 72 | + getCurrentPlanOrThrowMock.mockResolvedValue(plan({ isPaying: true })); |
| 73 | + cancelSubscriptionMock.mockResolvedValue({ |
| 74 | + success: true, |
| 75 | + outcome: "canceled", |
| 76 | + cancelAt: null, |
| 77 | + }); |
| 78 | + |
| 79 | + await deleteOrg(prisma, organization.slug, user.id); |
| 80 | + |
| 81 | + expect(cancelSubscriptionMock).toHaveBeenCalledTimes(1); |
| 82 | + expect(cancelSubscriptionMock).toHaveBeenCalledWith(organization.id); |
| 83 | + const org = await prisma.organization.findUniqueOrThrow({ where: { id: organization.id } }); |
| 84 | + expect(org.deletedAt).toBeInstanceOf(Date); |
| 85 | + }); |
| 86 | + |
| 87 | + postgresTest("aborts when the cancel call returns an error", async ({ prisma }) => { |
| 88 | + const { user, organization } = await createOrg(prisma); |
| 89 | + getCurrentPlanOrThrowMock.mockResolvedValue(plan({ isPaying: true })); |
| 90 | + cancelSubscriptionMock.mockResolvedValue({ success: false, error: "stripe_error" }); |
| 91 | + |
| 92 | + await expect(deleteOrg(prisma, organization.slug, user.id)).rejects.toThrow( |
| 93 | + "Couldn't cancel the subscription, try again" |
| 94 | + ); |
| 95 | + |
| 96 | + const org = await prisma.organization.findUniqueOrThrow({ where: { id: organization.id } }); |
| 97 | + expect(org.deletedAt).toBeNull(); |
| 98 | + }); |
| 99 | + |
| 100 | + postgresTest("aborts when the cancel call throws", async ({ prisma }) => { |
| 101 | + const { user, organization } = await createOrg(prisma); |
| 102 | + getCurrentPlanOrThrowMock.mockResolvedValue(plan({ isPaying: true })); |
| 103 | + cancelSubscriptionMock.mockRejectedValue(new Error("network down")); |
| 104 | + |
| 105 | + await expect(deleteOrg(prisma, organization.slug, user.id)).rejects.toThrow( |
| 106 | + "Couldn't cancel the subscription, try again" |
| 107 | + ); |
| 108 | + |
| 109 | + const org = await prisma.organization.findUniqueOrThrow({ where: { id: organization.id } }); |
| 110 | + expect(org.deletedAt).toBeNull(); |
| 111 | + }); |
| 112 | + |
| 113 | + postgresTest("deletes when the subscription is already canceled", async ({ prisma }) => { |
| 114 | + const { user, organization } = await createOrg(prisma); |
| 115 | + getCurrentPlanOrThrowMock.mockResolvedValue(plan({ isPaying: true })); |
| 116 | + cancelSubscriptionMock.mockResolvedValue({ |
| 117 | + success: true, |
| 118 | + outcome: "already_canceled", |
| 119 | + cancelAt: null, |
| 120 | + }); |
| 121 | + |
| 122 | + await deleteOrg(prisma, organization.slug, user.id); |
| 123 | + |
| 124 | + expect(cancelSubscriptionMock).toHaveBeenCalledTimes(1); |
| 125 | + const org = await prisma.organization.findUniqueOrThrow({ where: { id: organization.id } }); |
| 126 | + expect(org.deletedAt).toBeInstanceOf(Date); |
| 127 | + }); |
| 128 | + |
| 129 | + postgresTest("asks billing to cancel even when not paying", async ({ prisma }) => { |
| 130 | + const { user, organization } = await createOrg(prisma); |
| 131 | + getCurrentPlanOrThrowMock.mockResolvedValue(plan({ isPaying: false })); |
| 132 | + cancelSubscriptionMock.mockResolvedValue({ |
| 133 | + success: true, |
| 134 | + outcome: "no_subscription", |
| 135 | + cancelAt: null, |
| 136 | + }); |
| 137 | + |
| 138 | + await deleteOrg(prisma, organization.slug, user.id); |
| 139 | + |
| 140 | + expect(cancelSubscriptionMock).toHaveBeenCalledTimes(1); |
| 141 | + expect(cancelSubscriptionMock).toHaveBeenCalledWith(organization.id); |
| 142 | + const org = await prisma.organization.findUniqueOrThrow({ where: { id: organization.id } }); |
| 143 | + expect(org.deletedAt).toBeInstanceOf(Date); |
| 144 | + }); |
| 145 | +}); |
0 commit comments