135 lines
5.1 KiB
TypeScript
135 lines
5.1 KiB
TypeScript
// @vitest-environment node
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import type { LoaderFunctionArgs } from "react-router";
|
|
|
|
const mocks = vi.hoisted(() => {
|
|
class MockProductAcquisitionError extends Error {
|
|
constructor(readonly code: string) {
|
|
super(code);
|
|
}
|
|
}
|
|
return {
|
|
MockProductAcquisitionError,
|
|
getTranslations: vi.fn(),
|
|
getTenantIdFromUrl: vi.fn(),
|
|
getUserInfo: vi.fn(),
|
|
verifyUserHasPermission: vi.fn(),
|
|
getTenant: vi.fn(),
|
|
completeProductAcquisition: vi.fn(),
|
|
runWithRlsContext: vi.fn((_context: unknown, operation: () => unknown) => operation()),
|
|
};
|
|
});
|
|
|
|
vi.mock("@hivegrid/db/rls-context.server", () => ({
|
|
runWithRlsContext: (...args: unknown[]) => mocks.runWithRlsContext(...args),
|
|
}));
|
|
vi.mock("~/locale/i18next.server", () => ({ getTranslations: (...args: unknown[]) => mocks.getTranslations(...args) }));
|
|
vi.mock("~/utils/services/.server/urlService", () => ({ getTenantIdFromUrl: (...args: unknown[]) => mocks.getTenantIdFromUrl(...args) }));
|
|
vi.mock("~/utils/session.server", () => ({ getUserInfo: (...args: unknown[]) => mocks.getUserInfo(...args) }));
|
|
vi.mock("~/utils/helpers/.server/PermissionsService", () => ({
|
|
verifyUserHasPermission: (...args: unknown[]) => mocks.verifyUserHasPermission(...args),
|
|
}));
|
|
vi.mock("~/utils/db/tenants.db.server", () => ({ getTenant: (...args: unknown[]) => mocks.getTenant(...args) }));
|
|
vi.mock("~/services/billing/product-acquisition.server", () => ({
|
|
completeProductAcquisition: (...args: unknown[]) => mocks.completeProductAcquisition(...args),
|
|
ProductAcquisitionError: mocks.MockProductAcquisitionError,
|
|
}));
|
|
|
|
import { loader } from "./subscribe.$tenant.product.$acquisition.success";
|
|
|
|
const tenant = { id: "tenant_1", name: "Acme GmbH", slug: "acme" };
|
|
const params = { tenant: "acme", acquisition: "acquisition_1" };
|
|
const request = new Request("https://app.example.test/subscribe/acme/product/acquisition_1/success");
|
|
|
|
function callLoader(routeParams = params, routeRequest = request) {
|
|
return loader({
|
|
request: routeRequest,
|
|
params: routeParams,
|
|
context: {},
|
|
unstable_pattern: "subscribe.$tenant.product.$acquisition.success",
|
|
} as LoaderFunctionArgs) as Promise<Response>;
|
|
}
|
|
|
|
describe("product acquisition return loader", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
process.env.APP_NAME = "HiveGrid";
|
|
mocks.getTranslations.mockResolvedValue({ t: (key: string) => `t:${key}` });
|
|
mocks.getTenantIdFromUrl.mockResolvedValue("tenant_1");
|
|
mocks.getUserInfo.mockResolvedValue({ userId: "user_1" });
|
|
mocks.verifyUserHasPermission.mockResolvedValue(undefined);
|
|
mocks.getTenant.mockResolvedValue(tenant);
|
|
mocks.completeProductAcquisition.mockResolvedValue({
|
|
id: "acquisition_1",
|
|
status: "active",
|
|
productKind: "recurring_add_on",
|
|
scopeKey: "tenant",
|
|
checkoutUrl: null,
|
|
providerPaymentId: "tr_test",
|
|
replayed: false,
|
|
});
|
|
});
|
|
|
|
it("binds permission, tenant and acquisition before returning an active status", async () => {
|
|
const response = await callLoader();
|
|
const data = await response.json();
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(data).toMatchObject({
|
|
tenant,
|
|
returnTarget: "billing",
|
|
acquisition: { id: "acquisition_1", status: "active", processing: false },
|
|
errorKey: null,
|
|
});
|
|
expect(mocks.verifyUserHasPermission).toHaveBeenCalledWith(request, "app.settings.subscription.update", "tenant_1");
|
|
expect(mocks.runWithRlsContext).toHaveBeenCalledWith({ tenantId: "tenant_1", userId: "user_1" }, expect.any(Function));
|
|
expect(mocks.completeProductAcquisition).toHaveBeenCalledWith({
|
|
tenantId: "tenant_1",
|
|
acquisitionId: "acquisition_1",
|
|
});
|
|
});
|
|
|
|
it("keeps a customer-portal purchase on its originating settings flow", async () => {
|
|
const response = await callLoader(
|
|
params,
|
|
new Request(
|
|
"https://app.example.test/subscribe/acme/product/acquisition_1/success?return=customer-portal-domain"
|
|
)
|
|
);
|
|
|
|
await expect(response.json()).resolves.toMatchObject({
|
|
returnTarget: "customer_portal_domain",
|
|
acquisition: { status: "active" },
|
|
});
|
|
});
|
|
|
|
it("keeps an open provider payment observable and pollable without activating it", async () => {
|
|
mocks.completeProductAcquisition.mockResolvedValueOnce({
|
|
id: "acquisition_1",
|
|
status: "payment_pending",
|
|
productKind: "recurring_add_on",
|
|
scopeKey: "tenant",
|
|
checkoutUrl: null,
|
|
providerPaymentId: "tr_test",
|
|
replayed: true,
|
|
});
|
|
|
|
const response = await callLoader();
|
|
await expect(response.json()).resolves.toMatchObject({
|
|
acquisition: { status: "payment_pending", processing: true },
|
|
});
|
|
});
|
|
|
|
it("returns a tenant-safe 404 when the acquisition cannot be resolved", async () => {
|
|
mocks.completeProductAcquisition.mockRejectedValueOnce(new mocks.MockProductAcquisitionError("ACQUISITION_NOT_FOUND"));
|
|
|
|
const response = await callLoader();
|
|
|
|
expect(response.status).toBe(404);
|
|
await expect(response.json()).resolves.toMatchObject({
|
|
acquisition: null,
|
|
errorKey: "billing:productAcquisitionReturn.errors.notFound",
|
|
});
|
|
});
|
|
});
|