126 lines
6.4 KiB
TypeScript
126 lines
6.4 KiB
TypeScript
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";
|
|
import { eventsOn, parseIcs, suggestionFor } from "../src/calendar/ics.js";
|
|
import { isPublicAddress, normalizeCalendarUrl } from "../src/calendar/service.js";
|
|
import { bearer, createTestApp, login, resetDb, seedUser, type TestContext } from "./helpers.js";
|
|
|
|
const KEY = Buffer.alloc(32, 7).toString("base64");
|
|
|
|
// Google writes UTC times, Outlook a TZID with Windows names, Apple a TZID with folded lines.
|
|
const ICS = [
|
|
"BEGIN:VCALENDAR",
|
|
"VERSION:2.0",
|
|
"BEGIN:VEVENT",
|
|
"UID:press@google",
|
|
"DTSTART:20261014T100000Z",
|
|
"DTEND:20261014T110000Z",
|
|
"SUMMARY:Conferência de imprensa do FC Porto",
|
|
"LOCATION:Estádio do Dragão\\, Via Futebol Clube do Porto\\, 4350-415 Porto\\, Portugal",
|
|
"DESCRIPTION:Antevisão do clássico.\\nLevar 70-200.",
|
|
"BEGIN:VALARM",
|
|
"TRIGGER:-PT30M",
|
|
"DESCRIPTION:Lembrete",
|
|
"END:VALARM",
|
|
"END:VEVENT",
|
|
"BEGIN:VEVENT",
|
|
"UID:gala@outlook",
|
|
'DTSTART;TZID="GMT Standard Time":20261014T213000',
|
|
"SUMMARY:Gala do Desporto de Bra",
|
|
" ga",
|
|
"LOCATION:Theatro Circo, Braga",
|
|
"END:VEVENT",
|
|
"BEGIN:VEVENT",
|
|
"UID:cancelled",
|
|
"DTSTART:20261014T150000Z",
|
|
"SUMMARY:Treino aberto",
|
|
"STATUS:CANCELLED",
|
|
"END:VEVENT",
|
|
"BEGIN:VEVENT",
|
|
"UID:allday",
|
|
"DTSTART;VALUE=DATE:20261015",
|
|
"DTEND;VALUE=DATE:20261016",
|
|
"SUMMARY:Folga",
|
|
"END:VEVENT",
|
|
"END:VCALENDAR",
|
|
].join("\r\n");
|
|
|
|
describe("agenda (iCal)", () => {
|
|
it("reads Google, Outlook and Apple entries in Lisbon time and skips cancelled ones", () => {
|
|
const events = parseIcs(ICS);
|
|
expect(events.map((e) => e.summary)).toEqual(["Conferência de imprensa do FC Porto", "Gala do Desporto de Braga", "Folga"]);
|
|
expect(events[1]!.start.toISOString()).toBe("2026-10-14T20:30:00.000Z");
|
|
expect(events[0]!.description).toBe("Antevisão do clássico.\nLevar 70-200.");
|
|
expect(eventsOn(events, "2026-10-14").map((e) => e.uid)).toEqual(["press@google", "gala@outlook"]);
|
|
expect(eventsOn(events, "2026-10-15").map((e) => e.uid)).toEqual(["allday"]);
|
|
expect(eventsOn(events, "2026-10-16")).toEqual([]);
|
|
expect(() => parseIcs("<html>login</html>")).toThrow();
|
|
});
|
|
|
|
it("suggests the name, venue, city and a caption to start from", () => {
|
|
const [press, gala] = parseIcs(ICS);
|
|
expect(suggestionFor(press!)).toEqual({ name: "Conferência de imprensa do FC Porto", venue: "Estádio do Dragão", city: "Porto", caption: "Conferência de imprensa do FC Porto, Estádio do Dragão, Porto." });
|
|
expect(suggestionFor(gala!)).toMatchObject({ venue: "Theatro Circo", city: "Braga" });
|
|
});
|
|
|
|
it("only fetches public https addresses", () => {
|
|
expect(normalizeCalendarUrl("webcal://p01-caldav.icloud.com/published/2/abc").toString()).toBe("https://p01-caldav.icloud.com/published/2/abc");
|
|
expect(() => normalizeCalendarUrl("http://calendar.google.com/x.ics")).toThrow();
|
|
expect(() => normalizeCalendarUrl("file:///etc/passwd")).toThrow();
|
|
for (const ip of ["127.0.0.1", "10.1.2.3", "192.168.1.10", "172.20.0.1", "169.254.169.254", "100.64.0.1", "::1", "fd00::1", "fe80::1", "::ffff:10.0.0.1"]) expect(isPublicAddress(ip)).toBe(false);
|
|
for (const ip of ["142.250.184.14", "2a00:1450:4003::200e"]) expect(isPublicAddress(ip)).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe("agenda routes", () => {
|
|
let ctx: TestContext;
|
|
const fetched: string[] = [];
|
|
const fakeFetch: typeof fetch = async (input) => {
|
|
const url = String(input);
|
|
fetched.push(url);
|
|
if (url === "https://203.0.113.10/moved.ics") return new Response(null, { status: 302, headers: { location: "https://203.0.113.10/cal.ics" } });
|
|
if (url === "https://203.0.113.10/inside.ics") return new Response(null, { status: 302, headers: { location: "https://127.0.0.1/admin" } });
|
|
if (url === "https://203.0.113.10/cal.ics") return new Response(ICS, { headers: { "content-type": "text/calendar" } });
|
|
if (url === "https://203.0.113.10/page") return new Response("<html></html>");
|
|
return new Response("nope", { status: 404 });
|
|
};
|
|
|
|
beforeAll(async () => {
|
|
ctx = await createTestApp({ DELIVERY_SECRET_KEY: KEY }, undefined, fakeFetch);
|
|
});
|
|
afterAll(async () => {
|
|
await ctx.app.close();
|
|
await ctx.db.$disconnect();
|
|
});
|
|
beforeEach(async () => {
|
|
await resetDb(ctx.db);
|
|
fetched.length = 0;
|
|
});
|
|
|
|
it("connects a calendar once, keeps it sealed, and lists a day's entries with suggestions", async () => {
|
|
await seedUser(ctx.db, "[email protected]", { org: "photoreport", role: "OWNER" });
|
|
const token = (await login(ctx.app, "[email protected]")).body.accessToken;
|
|
const call = (method: "GET" | "PUT", url: string, payload?: object) => ctx.app.inject({ method, url, headers: bearer(token), ...(payload && { payload }) });
|
|
|
|
expect((await call("GET", "/v1/me/calendar")).json()).toEqual({ connected: false });
|
|
expect((await call("GET", "/v1/me/calendar/events?date=2026-10-14")).json()).toEqual({ error: "calendar_not_connected" });
|
|
expect((await call("PUT", "/v1/me/calendar", { url: "http://203.0.113.10/cal.ics" })).json()).toEqual({ error: "calendar_url" });
|
|
expect((await call("PUT", "/v1/me/calendar", { url: "https://203.0.113.10/inside.ics" })).json()).toEqual({ error: "calendar_url" });
|
|
expect((await call("PUT", "/v1/me/calendar", { url: "https://203.0.113.10/page" })).json()).toEqual({ error: "calendar_invalid" });
|
|
expect(fetched).not.toContain("https://127.0.0.1/admin");
|
|
|
|
expect((await call("PUT", "/v1/me/calendar", { url: "webcal://203.0.113.10/moved.ics" })).json()).toEqual({ connected: true, events: 3 });
|
|
const stored = await ctx.db.userPreferences.findFirstOrThrow({ select: { calendarUrl: true } });
|
|
expect(stored.calendarUrl).toMatch(/^v1\./);
|
|
expect(stored.calendarUrl).not.toContain("203.0.113.10");
|
|
|
|
const day = (await call("GET", "/v1/me/calendar/events?date=2026-10-14")).json() as { date: string; events: { title: string; start: string; suggestion: { venue: string | null } }[] };
|
|
expect(day.date).toBe("2026-10-14");
|
|
expect(day.events.map((e) => [e.title, e.start, e.suggestion.venue])).toEqual([
|
|
["Conferência de imprensa do FC Porto", "2026-10-14T10:00:00.000Z", "Estádio do Dragão"],
|
|
["Gala do Desporto de Braga", "2026-10-14T20:30:00.000Z", "Theatro Circo"],
|
|
]);
|
|
|
|
expect((await call("PUT", "/v1/me/calendar", { url: null })).json()).toEqual({ connected: false, events: 0 });
|
|
expect((await call("GET", "/v1/me/calendar")).json()).toEqual({ connected: false });
|
|
});
|
|
});
|