Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
@@ -0,0 +1,193 @@
|
||||
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",
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user