220 lines
6.9 KiB
TypeScript
220 lines
6.9 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { createUniqueTenantIpAddressRecord } from "./auth-boundary.server.js";
|
|
import {
|
|
adminGetAllLegacyTenants,
|
|
adminGetAllLegacyTenantSummaries,
|
|
createUniqueLegacyTenantIpAddress,
|
|
findLegacyTenantById,
|
|
findLegacyTenantByIdOrSlug,
|
|
findPlatformOperatorByUserId,
|
|
getLegacyTenantMembershipsForUser,
|
|
legacyTenantDetailsInclude,
|
|
type LegacyTenantAccountReaderClient,
|
|
type LegacyTenantIpAddressWriterClient,
|
|
type PlatformOperatorReaderClient,
|
|
} from "./accounts.server.js";
|
|
|
|
function createTenantClient(items: unknown[] = []): LegacyTenantAccountReaderClient {
|
|
return {
|
|
tenant: {
|
|
findMany: vi.fn().mockResolvedValue(items),
|
|
findUnique: vi.fn().mockResolvedValue(items[0] ?? null),
|
|
findFirst: vi.fn().mockResolvedValue(items[0] ?? null),
|
|
},
|
|
} as unknown as LegacyTenantAccountReaderClient;
|
|
}
|
|
|
|
describe("legacy account tenant reads", () => {
|
|
it("revalidates a normalized operator identity through AdminUser", async () => {
|
|
const client: PlatformOperatorReaderClient = {
|
|
adminUser: {
|
|
findUnique: vi.fn().mockResolvedValue({ id: "operator_1" }),
|
|
},
|
|
};
|
|
|
|
await expect(findPlatformOperatorByUserId(" operator_1 ", client)).resolves.toEqual({ userId: "operator_1" });
|
|
expect(client.adminUser.findUnique).toHaveBeenCalledWith({
|
|
where: { id: "operator_1" },
|
|
select: { id: true },
|
|
});
|
|
});
|
|
|
|
it("rejects malformed operator identifiers before querying AdminUser", async () => {
|
|
const client: PlatformOperatorReaderClient = {
|
|
adminUser: {
|
|
findUnique: vi.fn(),
|
|
},
|
|
};
|
|
|
|
await expect(findPlatformOperatorByUserId(" ", client)).resolves.toBeNull();
|
|
await expect(findPlatformOperatorByUserId(`operator_${"x".repeat(192)}`, client)).resolves.toBeNull();
|
|
expect(client.adminUser.findUnique).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("fetches all tenants with the account API details shape", async () => {
|
|
const client = createTenantClient([]);
|
|
|
|
await expect(adminGetAllLegacyTenants(client)).resolves.toEqual([]);
|
|
expect(client.tenant.findMany).toHaveBeenCalledWith({
|
|
include: legacyTenantDetailsInclude,
|
|
orderBy: { createdAt: "desc" },
|
|
});
|
|
});
|
|
|
|
it("fetches tenant summaries for admin filters", async () => {
|
|
const client = createTenantClient([{ id: "tenant_1", name: "Acme", slug: "acme" }]);
|
|
|
|
await expect(adminGetAllLegacyTenantSummaries(client)).resolves.toEqual([{ id: "tenant_1", name: "Acme", slug: "acme" }]);
|
|
expect(client.tenant.findMany).toHaveBeenCalledWith({
|
|
select: { id: true, name: true, slug: true },
|
|
});
|
|
});
|
|
|
|
it("fetches simple tenant memberships for API authorization", async () => {
|
|
const client = createTenantClient([{ id: "tenant_1", name: "Acme", slug: "acme" }]);
|
|
|
|
await expect(getLegacyTenantMembershipsForUser("user_1", client)).resolves.toEqual([{ id: "tenant_1", name: "Acme", slug: "acme" }]);
|
|
expect(client.tenant.findMany).toHaveBeenCalledWith({
|
|
where: { active: true, users: { some: { userId: "user_1", status: 2 } } },
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
slug: true,
|
|
icon: true,
|
|
logo: true,
|
|
deactivatedReason: true,
|
|
active: true,
|
|
theme: true,
|
|
themeConfig: true,
|
|
},
|
|
orderBy: { name: "asc" },
|
|
});
|
|
});
|
|
|
|
it("fetches one tenant by id with the account API details shape", async () => {
|
|
const tenant = { id: "tenant_1" };
|
|
const client = createTenantClient([tenant]);
|
|
|
|
await expect(findLegacyTenantById("tenant_1", client)).resolves.toBe(tenant);
|
|
expect(client.tenant.findUnique).toHaveBeenCalledWith({
|
|
where: { id: "tenant_1" },
|
|
include: legacyTenantDetailsInclude,
|
|
});
|
|
});
|
|
|
|
it("fetches the tenant identity by id or slug before loading details", async () => {
|
|
const tenant = { id: "tenant_1", slug: "acme", subscription: null };
|
|
const client = createTenantClient([tenant]);
|
|
|
|
await expect(findLegacyTenantByIdOrSlug("acme", client)).resolves.toBe(tenant);
|
|
expect(client.tenant.findFirst).toHaveBeenCalledWith({
|
|
where: {
|
|
OR: [{ id: "acme" }, { slug: "acme" }],
|
|
},
|
|
include: {
|
|
subscription: true,
|
|
},
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("legacy tenant IP address writes", () => {
|
|
it("creates a tenant IP address only when the tenant/user/key tuple is new", async () => {
|
|
const client: LegacyTenantIpAddressWriterClient = {
|
|
tenantIpAddress: {
|
|
findFirst: vi.fn().mockResolvedValue(null),
|
|
create: vi.fn().mockResolvedValue({ id: "ip_1" }),
|
|
},
|
|
} as unknown as LegacyTenantIpAddressWriterClient;
|
|
|
|
await expect(
|
|
createUniqueLegacyTenantIpAddress(
|
|
{
|
|
ip: "127.0.0.1",
|
|
fromUrl: "/api/widgets",
|
|
tenantId: "tenant_1",
|
|
apiKeyId: "api_key_1",
|
|
},
|
|
client,
|
|
),
|
|
).resolves.toEqual({ id: "ip_1" });
|
|
|
|
expect(client.tenantIpAddress.findFirst).toHaveBeenCalledWith({
|
|
where: {
|
|
ip: "127.0.0.1",
|
|
tenantId: "tenant_1",
|
|
userId: undefined,
|
|
apiKeyId: "api_key_1",
|
|
},
|
|
});
|
|
expect(client.tenantIpAddress.create).toHaveBeenCalledWith({
|
|
data: {
|
|
ip: "127.0.0.1",
|
|
fromUrl: "/api/widgets",
|
|
tenantId: "tenant_1",
|
|
apiKeyId: "api_key_1",
|
|
},
|
|
});
|
|
});
|
|
|
|
it("does not recreate an existing tenant IP address", async () => {
|
|
const client: LegacyTenantIpAddressWriterClient = {
|
|
tenantIpAddress: {
|
|
findFirst: vi.fn().mockResolvedValue({ id: "ip_1" }),
|
|
create: vi.fn(),
|
|
},
|
|
} as unknown as LegacyTenantIpAddressWriterClient;
|
|
|
|
await expect(
|
|
createUniqueLegacyTenantIpAddress(
|
|
{
|
|
ip: "127.0.0.1",
|
|
fromUrl: "/api/widgets",
|
|
tenantId: "tenant_1",
|
|
},
|
|
client,
|
|
),
|
|
).resolves.toBeUndefined();
|
|
expect(client.tenantIpAddress.create).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("exposes the tenant IP address write through the auth boundary alias", async () => {
|
|
const client: LegacyTenantIpAddressWriterClient = {
|
|
tenantIpAddress: {
|
|
findFirst: vi.fn().mockResolvedValue(null),
|
|
create: vi.fn().mockResolvedValue({ id: "ip_1" }),
|
|
},
|
|
} as unknown as LegacyTenantIpAddressWriterClient;
|
|
|
|
await expect(
|
|
createUniqueTenantIpAddressRecord(
|
|
{
|
|
ip: "127.0.0.1",
|
|
fromUrl: "/app/acme",
|
|
tenantId: "tenant_1",
|
|
userId: "user_1",
|
|
},
|
|
client,
|
|
),
|
|
).resolves.toEqual({ id: "ip_1" });
|
|
|
|
expect(client.tenantIpAddress.findFirst).toHaveBeenCalledWith({
|
|
where: {
|
|
ip: "127.0.0.1",
|
|
tenantId: "tenant_1",
|
|
userId: "user_1",
|
|
apiKeyId: undefined,
|
|
},
|
|
});
|
|
expect(client.tenantIpAddress.create).toHaveBeenCalledWith({
|
|
data: {
|
|
ip: "127.0.0.1",
|
|
fromUrl: "/app/acme",
|
|
tenantId: "tenant_1",
|
|
userId: "user_1",
|
|
},
|
|
});
|
|
});
|
|
});
|