Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+150
@@ -0,0 +1,150 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
type TokenRow = {
|
||||
clientId: string;
|
||||
tenantId: string;
|
||||
userId: string;
|
||||
revokedAt: Date | null;
|
||||
scopes: string[];
|
||||
createdAt: Date;
|
||||
expiresAt: Date;
|
||||
client: { clientName: string };
|
||||
};
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
accessRows: [] as TokenRow[],
|
||||
refreshRows: [] as TokenRow[],
|
||||
audit: vi.fn(),
|
||||
boundTenantIds: [] as string[],
|
||||
}));
|
||||
|
||||
/**
|
||||
* The fake transaction mirrors the policy instead of answering every query:
|
||||
* a mock that returns rows regardless of the bound tenant/user would hide
|
||||
* exactly the cross-tenant bug this suite is here to catch (AGENTS.md, RLS).
|
||||
*/
|
||||
function matches(row: TokenRow, where: Record<string, unknown>, boundTenantId: string): boolean {
|
||||
if (row.tenantId !== boundTenantId) return false;
|
||||
for (const [key, value] of Object.entries(where)) {
|
||||
if (key === "revokedAt") {
|
||||
if (value === null && row.revokedAt !== null) return false;
|
||||
continue;
|
||||
}
|
||||
if ((row as unknown as Record<string, unknown>)[key] !== value) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
vi.mock("~/utils/db/.server/tenantRls.server", () => ({
|
||||
withTenantRLS: (tenantId: string, callback: (tx: unknown) => unknown) => {
|
||||
mocks.boundTenantIds.push(tenantId);
|
||||
const model = (rows: TokenRow[]) => ({
|
||||
findMany: async ({ where }: { where: Record<string, unknown> }) => rows.filter((row) => matches(row, where, tenantId)),
|
||||
count: async ({ where }: { where: Record<string, unknown> }) => rows.filter((row) => matches(row, where, tenantId)).length,
|
||||
updateMany: async ({ where, data }: { where: Record<string, unknown>; data: { revokedAt: Date } }) => {
|
||||
const hits = rows.filter((row) => matches(row, where, tenantId));
|
||||
for (const row of hits) row.revokedAt = data.revokedAt;
|
||||
return { count: hits.length };
|
||||
},
|
||||
});
|
||||
return callback({
|
||||
mcpOAuthAccessToken: model(mocks.accessRows),
|
||||
mcpOAuthRefreshToken: model(mocks.refreshRows),
|
||||
tenant: { findUnique: async () => ({ name: "Acme", slug: "acme" }) },
|
||||
});
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("@hivegrid/db/audit-events.server", () => ({
|
||||
createAuditEventInExistingTransaction: (input: unknown) => {
|
||||
mocks.audit(input);
|
||||
return Promise.resolve({});
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("~/services/mcp/oauth.server", () => ({
|
||||
getMcpOAuthPublicUrls: () => ({ resource: "https://staging-mcp.hivegrid.de" }),
|
||||
}));
|
||||
|
||||
vi.mock("~/utils/connector/tenantConnector.server", () => ({ tenantHasModule: vi.fn() }));
|
||||
vi.mock("~/utils/loaders.middleware", () => ({ requireAuth: vi.fn() }));
|
||||
vi.mock("~/utils/services/.server/urlService", () => ({ getTenantIdFromUrl: vi.fn() }));
|
||||
vi.mock("~/utils/session.server", () => ({ getUserInfo: vi.fn() }));
|
||||
|
||||
import { loadMcpConnectVM, revokeMcpConnection } from "./connect.server";
|
||||
|
||||
const CTX = { tenantId: "tenant-1", tenantSlug: "acme", tenantName: "Acme", userId: "user-1" };
|
||||
|
||||
function row(overrides: Partial<TokenRow> = {}): TokenRow {
|
||||
return {
|
||||
clientId: "client-1",
|
||||
tenantId: "tenant-1",
|
||||
userId: "user-1",
|
||||
revokedAt: null,
|
||||
scopes: ["mcp:read"],
|
||||
createdAt: new Date("2026-07-20T08:00:00.000Z"),
|
||||
expiresAt: new Date("2026-07-28T18:00:00.000Z"),
|
||||
client: { clientName: "Claude" },
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
mocks.accessRows = [];
|
||||
mocks.refreshRows = [];
|
||||
mocks.boundTenantIds = [];
|
||||
mocks.audit.mockClear();
|
||||
});
|
||||
|
||||
describe("loadMcpConnectVM", () => {
|
||||
it("lists the current user's own connections only", async () => {
|
||||
mocks.accessRows = [row(), row({ clientId: "client-2", userId: "user-2", client: { clientName: "Fremd" } })];
|
||||
|
||||
const vm = await loadMcpConnectVM(CTX, new Date("2026-07-28T12:00:00.000Z"));
|
||||
|
||||
expect(vm.status).toBe("ready");
|
||||
expect(vm.data?.connections.map((connection) => connection.id)).toEqual(["client-1"]);
|
||||
});
|
||||
|
||||
it("reads inside the tenant's RLS context", async () => {
|
||||
await loadMcpConnectVM(CTX, new Date("2026-07-28T12:00:00.000Z"));
|
||||
expect(mocks.boundTenantIds).toEqual(["tenant-1"]);
|
||||
});
|
||||
|
||||
it("hands the environment connector URL to the view-model", async () => {
|
||||
const vm = await loadMcpConnectVM(CTX, new Date("2026-07-28T12:00:00.000Z"));
|
||||
expect(vm.data?.connectorUrl).toBe("https://staging-mcp.hivegrid.de");
|
||||
});
|
||||
});
|
||||
|
||||
describe("revokeMcpConnection", () => {
|
||||
it("cuts access and refresh tokens together", async () => {
|
||||
mocks.accessRows = [row()];
|
||||
mocks.refreshRows = [row()];
|
||||
|
||||
const result = await revokeMcpConnection(CTX, "client-1", new Date("2026-07-28T12:00:00.000Z"));
|
||||
|
||||
expect(result.revokedTokens).toBe(1);
|
||||
// A live refresh token would mint a new access token minutes later.
|
||||
expect(mocks.refreshRows[0]?.revokedAt).not.toBeNull();
|
||||
});
|
||||
|
||||
it("refuses to revoke another user's connection", async () => {
|
||||
mocks.accessRows = [row({ userId: "user-2" })];
|
||||
|
||||
await expect(revokeMcpConnection(CTX, "client-1")).rejects.toMatchObject({ name: "CatalogError" });
|
||||
expect(mocks.accessRows[0]?.revokedAt).toBeNull();
|
||||
});
|
||||
|
||||
it("refuses an unknown client id", async () => {
|
||||
await expect(revokeMcpConnection(CTX, "does-not-exist")).rejects.toMatchObject({ name: "CatalogError" });
|
||||
});
|
||||
|
||||
it("writes the audit record in the same transaction", async () => {
|
||||
mocks.accessRows = [row()];
|
||||
|
||||
await revokeMcpConnection(CTX, "client-1");
|
||||
|
||||
expect(mocks.audit).toHaveBeenCalledWith(expect.objectContaining({ action: "mcp.connection.revoke", actorId: "user-1", targetId: "client-1" }));
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user