Refresh HiveGrid Cloud reference snapshot

This commit is contained in:
david committed 2026-08-03 15:31:14 +02:00
1 parent 5e99e14772
commit 7f88026f79
5955 files changed
+1177325 -188362

No files matched your search

@@ -0,0 +1,249 @@
// @vitest-environment node
import { describe, expect, it, vi } from "vitest";
import { EnvKeyWrapper } from "@hivegrid/foundation/crypto";
import type { AdminDataConnection } from "../generated/prisma/client/client.js";
import {
createAdminConnectionsService,
type AdminConnectionCredentialSnapshot,
type AdminConnectionsRepository,
} from "./admin-connections.server.js";
function fixture() {
process.env.ADMIN_CONNECTION_MASTER_KEY =
"admin-connection-master-key-test-aaaaaaaa";
let credential: AdminConnectionCredentialSnapshot["credential"] = {
wrappedDek: new Uint8Array(),
ciphertext: "",
keyVersion: 1,
revokedAt: null,
};
const connection: AdminDataConnection = {
id: "connection_1",
adminUserId: "admin_1",
provider: "google_mail",
capability: "email",
label: "Sales",
status: "active",
createdAt: new Date(),
updatedAt: new Date(),
externalAccountId: "provider_1",
externalAccountEmail: "sales@example.test",
scopes: ["scope"],
tokenExpiresAt: null,
lastHealthCheckAt: null,
healthMessageKey: null,
webhookRegistrationId: null,
revokedAt: null,
archivedAt: null,
};
const repository: AdminConnectionsRepository = {
connect: vi.fn(async (input) => {
credential = {
...credential,
wrappedDek: input.credentialEnvelope.wrappedDek,
ciphertext: input.credentialEnvelope.ciphertext,
keyVersion: input.credentialEnvelope.keyVersion,
};
return connection;
}),
loadCredential: vi.fn(async (_connectionId, adminUserId) =>
adminUserId === connection.adminUserId
? { connection, credential }
: null
),
disconnect: vi.fn(async () => ({
...connection,
status: "revoked" as const,
})),
refresh: vi.fn(async (input) => {
credential = {
...credential,
wrappedDek: input.credentialEnvelope.wrappedDek,
ciphertext: input.credentialEnvelope.ciphertext,
keyVersion: input.credentialEnvelope.keyVersion,
};
return {
...connection,
scopes: [...input.scopes],
tokenExpiresAt: input.tokenExpiresAt,
};
}),
};
const authorize = vi.fn();
const service = createAdminConnectionsService({
repository,
authorize,
wrapper: new EnvKeyWrapper("ADMIN_CONNECTION_MASTER_KEY"),
});
return { service, repository, authorize, connection };
}
const actor = {
adminUserId: "admin_1",
email: "admin@example.test",
reason: "Persönliches Vertriebspostfach verbinden",
};
describe("admin connections service", () => {
it("supports the dedicated integrations permission for non-mail capabilities", async () => {
const { repository, authorize } = fixture();
const service = createAdminConnectionsService({
repository,
authorize,
wrapper: new EnvKeyWrapper("ADMIN_CONNECTION_MASTER_KEY"),
permission: "admin_sales.integrations.manage",
});
await service.connect({
adminUserId: "admin_1",
provider: "aircall",
capability: "telephony",
label: "Aircall",
externalAccountId: "42",
scopes: ["public_api"],
credentials: { accessToken: "permanent-token" },
actor: { ...actor, reason: "Aircall verbinden" },
});
expect(authorize).toHaveBeenCalledWith(
"admin_sales.integrations.manage"
);
});
it("supports the dedicated booking permission for personal calendars", async () => {
const { repository, authorize } = fixture();
const service = createAdminConnectionsService({
repository,
authorize,
wrapper: new EnvKeyWrapper("ADMIN_CONNECTION_MASTER_KEY"),
permission: "admin_sales.booking.manage",
});
await service.connect({
adminUserId: "admin_1",
provider: "google_calendar",
capability: "calendar",
label: "Persönlicher Kalender",
externalAccountId: "calendar-account-1",
scopes: ["calendar.events"],
credentials: { accessToken: "access", refreshToken: "refresh" },
actor: { ...actor, reason: "Persönlichen Kalender verbinden" },
});
expect(authorize).toHaveBeenCalledWith(
"admin_sales.booking.manage"
);
});
it("seals credentials before they cross the repository boundary", async () => {
const { service, repository, authorize } = fixture();
await service.connect({
adminUserId: "admin_1",
provider: "google_mail",
capability: "email",
label: " Sales ",
externalAccountId: "provider_1",
externalAccountEmail: "SALES@EXAMPLE.TEST",
scopes: ["scope"],
credentials: { accessToken: "access", refreshToken: "refresh" },
actor,
});
expect(authorize).toHaveBeenCalledWith("admin_sales.email.connect");
expect(repository.connect).toHaveBeenCalledWith(
expect.objectContaining({
label: "Sales",
externalAccountEmail: "sales@example.test",
credentialEnvelope: expect.objectContaining({
keyVersion: 1,
}),
})
);
expect(
JSON.stringify(vi.mocked(repository.connect).mock.calls[0])
).not.toContain("refresh");
});
it("opens a frozen credential bundle only inside the callback", async () => {
const { service, connection } = fixture();
await service.connect({
adminUserId: "admin_1",
provider: "google_mail",
capability: "email",
label: "Sales",
externalAccountId: "provider_1",
scopes: ["scope"],
credentials: { accessToken: "access", refreshToken: "refresh" },
actor,
});
await expect(
service.withCredentials(connection.id, "admin_1", async (credentials) => {
expect(Object.isFrozen(credentials)).toBe(true);
return credentials.refreshToken;
})
).resolves.toBe("refresh");
});
it("does not expose a foreign admin's credential envelope", async () => {
const { service } = fixture();
await expect(
service.withCredentials("connection_1", "admin_2", async () => null)
).rejects.toMatchObject({
code: "connection_unavailable",
});
});
it("revokes at the provider before persisting disconnect", async () => {
const { service, repository } = fixture();
await service.connect({
adminUserId: "admin_1",
provider: "google_mail",
capability: "email",
label: "Sales",
externalAccountId: "provider_1",
scopes: ["scope"],
credentials: { accessToken: "access", refreshToken: "refresh" },
actor,
});
const revoke = vi.fn(async () => undefined);
await service.disconnect({
connectionId: "connection_1",
adminUserId: "admin_1",
actor: { ...actor, reason: "Verbindung trennen" },
revokeProviderCredentials: revoke,
});
expect(revoke).toHaveBeenCalledWith(
expect.objectContaining({ refreshToken: "refresh" }),
expect.anything()
);
expect(repository.disconnect).toHaveBeenCalledOnce();
expect(revoke.mock.invocationCallOrder[0]).toBeLessThan(
vi.mocked(repository.disconnect).mock.invocationCallOrder[0]
);
});
it("rotates refreshed credentials without exposing plaintext", async () => {
const { service, repository } = fixture();
const expiresAt = new Date("2026-07-24T12:00:00.000Z");
await service.refresh({
connectionId: "connection_1",
adminUserId: "admin_1",
credentials: {
accessToken: "access-next",
refreshToken: "refresh-next",
},
scopes: ["scope"],
tokenExpiresAt: expiresAt,
actor: { ...actor, reason: "OAuth-Token erneuern" },
});
expect(repository.refresh).toHaveBeenCalledWith(
expect.objectContaining({
tokenExpiresAt: expiresAt,
credentialEnvelope: expect.objectContaining({ keyVersion: 1 }),
})
);
expect(
JSON.stringify(vi.mocked(repository.refresh).mock.calls[0])
).not.toContain("refresh-next");
});
});