53 lines
1.7 KiB
TypeScript
53 lines
1.7 KiB
TypeScript
// @vitest-environment node
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { getRlsExecutionContext, runWithRlsContext } from "@hivegrid/db/rls-context.server";
|
|
import { SubscriptionBillingPeriod } from "~/application/enums/subscriptions/SubscriptionBillingPeriod";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
getTenantSubscription: vi.fn(),
|
|
getTenantSubscriptions: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("../../db/tenantSubscriptions.db.server", () => ({
|
|
getTenantSubscription: mocks.getTenantSubscription,
|
|
getTenantSubscriptions: mocks.getTenantSubscriptions,
|
|
}));
|
|
|
|
import { getMrr } from "./subscriptionService";
|
|
|
|
describe("getMrr tenant RLS aggregation", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it("loads every tenant subscription only inside its matching tenant context", async () => {
|
|
mocks.getTenantSubscription.mockImplementation(async (tenantId: string) => {
|
|
expect(getRlsExecutionContext()).toMatchObject({ userId: "operator_1", tenantId });
|
|
return {
|
|
id: `subscription_${tenantId}`,
|
|
tenantId,
|
|
products: [
|
|
{
|
|
endsAt: tenantId === "tenant_2" ? new Date("2020-01-01T00:00:00.000Z") : null,
|
|
prices: [
|
|
{
|
|
subscriptionPrice: {
|
|
billingPeriod: SubscriptionBillingPeriod.MONTHLY,
|
|
price: 199,
|
|
currency: "eur",
|
|
},
|
|
},
|
|
],
|
|
},
|
|
],
|
|
};
|
|
});
|
|
|
|
const result = await runWithRlsContext({ userId: "operator_1" }, () => getMrr("eur", ["tenant_1", "tenant_2"]));
|
|
|
|
expect(result).toEqual({ total: 199, count: 1 });
|
|
expect(mocks.getTenantSubscription).toHaveBeenCalledTimes(2);
|
|
expect(getRlsExecutionContext()).toBeNull();
|
|
});
|
|
});
|