Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/db/src/accounts.server.test.ts
T

194 lines
5.9 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { createUniqueTenantIpAddressRecord } from "./auth-boundary.server.js";
import {
adminGetAllLegacyTenants,
adminGetAllLegacyTenantSummaries,
createUniqueLegacyTenantIpAddress,
findLegacyTenantById,
findLegacyTenantByIdOrSlug,
getLegacyTenantMembershipsForUser,
legacyTenantDetailsInclude,
type LegacyTenantAccountReaderClient,
type LegacyTenantIpAddressWriterClient,
} 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("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,
types: 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,
types: 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",
},
});
});
});