Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/integrations/src/admin-calendar-oauth.server.test.ts
T

121 lines
3.5 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { createAdminCalendarOAuthGateway } from "./admin-calendar-oauth.server.js";
const originalFetch = globalThis.fetch;
afterEach(() => {
globalThis.fetch = originalFetch;
vi.restoreAllMocks();
});
describe("admin calendar OAuth gateway", () => {
it("exchanges a Google grant and selects the primary calendar", async () => {
globalThis.fetch = vi
.fn()
.mockResolvedValueOnce(
Response.json({
access_token: "google-access",
refresh_token: "google-refresh",
expires_in: 3600,
scope:
"https://www.googleapis.com/auth/calendar.events https://www.googleapis.com/auth/calendar.freebusy openid email",
})
)
.mockResolvedValueOnce(
Response.json({ sub: "google-1", email: "sales@example.test" })
)
.mockResolvedValueOnce(
Response.json({
items: [
{ id: "secondary", summary: "Other" },
{ id: "primary", summary: "Sales", primary: true },
],
})
);
const gateway = createAdminCalendarOAuthGateway({
GOOGLE_CALENDAR_CLIENT_ID: "client",
GOOGLE_CALENDAR_CLIENT_SECRET: "secret",
GOOGLE_CALENDAR_REDIRECT_URI: "https://admin.example.test/callback",
});
const grant = await gateway.exchange("google_calendar", "code");
expect(grant).toMatchObject({
account: {
externalAccountId: "google-1",
emailAddress: "sales@example.test",
},
calendar: {
externalCalendarId: "primary",
label: "Sales",
},
});
});
it("exchanges a Microsoft grant and selects an editable default calendar", async () => {
globalThis.fetch = vi
.fn()
.mockResolvedValueOnce(
Response.json({
access_token: "microsoft-access",
refresh_token: "microsoft-refresh",
expires_in: 3600,
scope: "offline_access Calendars.ReadWrite User.Read",
})
)
.mockResolvedValueOnce(
Response.json({
id: "microsoft-1",
mail: "sales@example.test",
displayName: "Sales",
})
)
.mockResolvedValueOnce(
Response.json({
value: [
{
id: "calendar-1",
name: "Sales",
isDefaultCalendar: true,
canEdit: true,
},
],
})
);
const gateway = createAdminCalendarOAuthGateway({
MICROSOFT_CALENDAR_CLIENT_ID: "client",
MICROSOFT_CALENDAR_CLIENT_SECRET: "secret",
MICROSOFT_CALENDAR_REDIRECT_URI:
"https://admin.example.test/callback",
});
const grant = await gateway.exchange("microsoft_calendar", "code");
expect(grant).toMatchObject({
account: {
externalAccountId: "microsoft-1",
emailAddress: "sales@example.test",
},
calendar: {
externalCalendarId: "calendar-1",
label: "Sales",
},
});
});
it("exposes provider-specific authorization URLs without credentials", () => {
const gateway = createAdminCalendarOAuthGateway({
GOOGLE_CALENDAR_CLIENT_ID: "client",
GOOGLE_CALENDAR_CLIENT_SECRET: "secret",
GOOGLE_CALENDAR_REDIRECT_URI: "https://admin.example.test/callback",
});
const url = new URL(
gateway.authorizationUrl("google_calendar", "state-1")
);
expect(url.hostname).toBe("accounts.google.com");
expect(url.searchParams.get("state")).toBe("state-1");
expect(url.searchParams.get("scope")).toContain("calendar.freebusy");
});
});