160 lines
4.8 KiB
TypeScript
160 lines
4.8 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
import {
|
|
GraphCalendarError,
|
|
MICROSOFT_CALENDAR_SCOPES,
|
|
buildInitialCalendarDeltaUrl,
|
|
buildMicrosoftCalendarAuthorizationUrl,
|
|
deleteGraphCalendarEvent,
|
|
exchangeMicrosoftCalendarCode,
|
|
fetchCalendarDeltaPage,
|
|
listGraphCalendars,
|
|
refreshMicrosoftCalendarToken,
|
|
resolveMicrosoftCalendarOAuthConfig,
|
|
} from "./graphCalendarApi.js";
|
|
|
|
const CONFIG = {
|
|
clientId: "calendar-client",
|
|
clientSecret: "calendar-secret",
|
|
redirectUri: "https://admin.hivegrid.de/sales/settings/calendars/microsoft/callback",
|
|
tenant: "common",
|
|
};
|
|
|
|
function jsonResponse(status: number, body: unknown): Response {
|
|
return new Response(JSON.stringify(body), {
|
|
status,
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
}
|
|
|
|
let fetchMock: ReturnType<typeof vi.fn>;
|
|
|
|
beforeEach(() => {
|
|
fetchMock = vi.fn();
|
|
vi.stubGlobal("fetch", fetchMock);
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
describe("Microsoft admin calendar OAuth", () => {
|
|
it("fails closed when the dedicated admin calendar app is absent", () => {
|
|
expect(resolveMicrosoftCalendarOAuthConfig({})).toBeNull();
|
|
});
|
|
|
|
it("requests delegated calendar access and offline refresh", () => {
|
|
const url = new URL(
|
|
buildMicrosoftCalendarAuthorizationUrl(CONFIG, "signed-state")
|
|
);
|
|
expect(url.searchParams.get("scope")).toBe(
|
|
MICROSOFT_CALENDAR_SCOPES.join(" ")
|
|
);
|
|
expect(url.searchParams.get("state")).toBe("signed-state");
|
|
});
|
|
|
|
it("exchanges a code and persists the rotated refresh token", async () => {
|
|
fetchMock
|
|
.mockResolvedValueOnce(
|
|
jsonResponse(200, {
|
|
access_token: "access-1",
|
|
refresh_token: "refresh-1",
|
|
expires_in: 3600,
|
|
scope: MICROSOFT_CALENDAR_SCOPES.join(" "),
|
|
})
|
|
)
|
|
.mockResolvedValueOnce(
|
|
jsonResponse(200, {
|
|
access_token: "access-2",
|
|
refresh_token: "refresh-2",
|
|
expires_in: 3600,
|
|
scope: MICROSOFT_CALENDAR_SCOPES.join(" "),
|
|
})
|
|
);
|
|
|
|
await expect(
|
|
exchangeMicrosoftCalendarCode(CONFIG, "code")
|
|
).resolves.toMatchObject({ refreshToken: "refresh-1" });
|
|
await expect(
|
|
refreshMicrosoftCalendarToken(CONFIG, "refresh-1")
|
|
).resolves.toMatchObject({ refreshToken: "refresh-2" });
|
|
});
|
|
});
|
|
|
|
describe("Microsoft admin calendar REST", () => {
|
|
it("requests only the calendar metadata needed by the selector", async () => {
|
|
fetchMock.mockResolvedValueOnce(
|
|
jsonResponse(200, {
|
|
value: [
|
|
{
|
|
id: "primary",
|
|
name: "Kalender",
|
|
isDefaultCalendar: true,
|
|
canEdit: true,
|
|
},
|
|
],
|
|
})
|
|
);
|
|
await expect(listGraphCalendars("access")).resolves.toEqual([
|
|
{
|
|
id: "primary",
|
|
name: "Kalender",
|
|
isDefault: true,
|
|
canEdit: true,
|
|
},
|
|
]);
|
|
expect(String(fetchMock.mock.calls[0]?.[0])).toContain(
|
|
"$select=id,name,isDefaultCalendar,canEdit"
|
|
);
|
|
});
|
|
|
|
it("uses a bounded calendar-view delta and follows provider links", async () => {
|
|
const initial = buildInitialCalendarDeltaUrl(
|
|
"primary",
|
|
new Date("2026-07-01T00:00:00Z"),
|
|
new Date("2026-10-01T00:00:00Z")
|
|
);
|
|
expect(initial).toContain("/calendarView/delta?");
|
|
expect(initial).toContain("startDateTime=2026-07-01T00%3A00%3A00.000Z");
|
|
|
|
fetchMock.mockResolvedValueOnce(
|
|
jsonResponse(200, {
|
|
value: [{ id: "event-1", showAs: "busy" }],
|
|
"@odata.deltaLink":
|
|
"https://graph.microsoft.com/v1.0/me/calendarView/delta?$deltatoken=x",
|
|
})
|
|
);
|
|
await expect(fetchCalendarDeltaPage("access", initial)).resolves.toMatchObject(
|
|
{
|
|
events: [{ id: "event-1", showAs: "busy" }],
|
|
deltaLink:
|
|
"https://graph.microsoft.com/v1.0/me/calendarView/delta?$deltatoken=x",
|
|
}
|
|
);
|
|
});
|
|
|
|
it("rejects an off-origin cursor before forwarding the bearer token", async () => {
|
|
await expect(
|
|
fetchCalendarDeltaPage("secret", "https://attacker.example/steal")
|
|
).rejects.toBeInstanceOf(GraphCalendarError);
|
|
expect(fetchMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("maps 410 to deterministic full-sync recovery", async () => {
|
|
fetchMock.mockResolvedValueOnce(jsonResponse(410, {}));
|
|
const error = await fetchCalendarDeltaPage(
|
|
"access",
|
|
"https://graph.microsoft.com/v1.0/me/calendarView/delta?$deltatoken=old"
|
|
).catch((caught: unknown) => caught);
|
|
expect(error).toBeInstanceOf(GraphCalendarError);
|
|
expect((error as GraphCalendarError).fullResyncRequired).toBe(true);
|
|
});
|
|
|
|
it("treats an already deleted provider event as success", async () => {
|
|
fetchMock.mockResolvedValueOnce(new Response(null, { status: 404 }));
|
|
await expect(
|
|
deleteGraphCalendarEvent("access", "primary", "gone")
|
|
).resolves.toBeUndefined();
|
|
});
|
|
});
|