Files
HealthLog/src/__tests__/proxy-cycle-feed-public.test.ts
T
Marc-André Bombeck d9a31524a2 feat(cycle): serve the cycle forecast as a subscribable calendar
GET /api/cycle/feed/<hlf_token>[.ics] answers any calendar app with the
record's forecast as RFC 5545: all-day events for the next few periods
over their widening windows, optionally past periods, and for a
conception goal that asks for it the next fertile window. Dates and a
title only; discreet titles and the calendar name "HealthLog" by default.
The body is built live on every fetch in the record's own zone and
language, never the request's. ETag is the content hash and a matching
If-None-Match answers 304; DTSTAMP moves only when the content does.

The feed is anonymous, so every gate is read per fetch: the auth-surface
limit before any lookup, the token's shape, the HMAC lookup of a live
feed, a per-token bucket, then the record and carrier not pending
deletion, the operator's external-access switch, cycle tracking for the
record, and for a feed a delegate minted, a live MANAGE grant covering
cycle. Every miss is the same 404.

/api/cycle/feed-subscription (status, turn on, options, turn off) and
its rotate are cookie-only through requireRecordAuth("manage", "cycle"),
with a fresh proof on turning on and rotating and a per-actor mint
bucket. The URL is returned once; the status carries the prefix, the
options, the fetch statistics as a calendar-app family and the
projection state.

The hlf_ family is registered with the path and token redactors, the
secret-shape refusal, the proxy's public paths and edge headers, the
URL-credential, sharing, recent-proof, step-up and module-gate guards,
the error-code catalogue, the activity feed and the destructive-control
registry. The cycle settings tab gains the subscription card and the
predictions panel lists the later cycles.
2026-11-06 18:03:37 +01:00

79 lines
2.6 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { NextRequest } from "next/server";
/**
* #1350 — the cycle calendar feed at `/api/cycle/feed/<hlf_token>.ics`.
*
* A calendar provider polls it from its own servers with no cookie, so the
* proxy must pass the request through untouched, keep it out of indexes and
* referrers, and leave its caching to the route (`private, no-cache` plus an
* ETag) rather than stamping `no-store` over the validator path. The
* management routes beside it stay cookie-only and are not on the list.
*/
vi.mock("@/lib/process-type", () => ({
shouldRunWeb: () => true,
}));
import { PUBLIC_PATHS, proxy } from "../proxy";
// Built at run time so no token-shaped literal sits in the source.
const TOKEN = `hlf_${"7d".repeat(24)}`;
afterEach(() => {
vi.unstubAllEnvs();
});
describe("proxy on /api/cycle/feed/<token>", () => {
it("lists the feed prefix, trailing slash included, and not the management namespace", () => {
expect(PUBLIC_PATHS).toContain("/api/cycle/feed/");
expect(
PUBLIC_PATHS.some((p) => "/api/cycle/feed-subscription".startsWith(p)),
).toBe(false);
});
it("passes a cookie-less GET through to the route", () => {
const res = proxy(
new NextRequest(`http://localhost/api/cycle/feed/${TOKEN}.ics`, {
headers: { "user-agent": "Google-Calendar-Importer" },
}),
);
expect(res.status).toBe(200);
expect(res.headers.get("x-middleware-next")).toBe("1");
expect(res.headers.get("location")).toBeNull();
});
it("keeps the token out of indexes and referrers", () => {
const res = proxy(
new NextRequest(`http://localhost/api/cycle/feed/${TOKEN}.ics`),
);
expect(res.headers.get("x-robots-tag")).toBe("noindex, nofollow");
expect(res.headers.get("referrer-policy")).toBe("no-referrer");
});
it("leaves the route's Cache-Control alone, so the ETag path survives", () => {
const res = proxy(
new NextRequest(`http://localhost/api/cycle/feed/${TOKEN}.ics`),
);
expect(res.headers.get("cache-control")).toBeNull();
});
it("serves a cookie-less GET in demo mode too: a read, not a mutation", () => {
vi.stubEnv("DEMO_MODE", "true");
const res = proxy(
new NextRequest(`http://localhost/api/cycle/feed/${TOKEN}.ics`),
);
expect(res.status).toBe(200);
});
it("refuses a demo-mode mint at the edge", () => {
vi.stubEnv("DEMO_MODE", "true");
const res = proxy(
new NextRequest("http://localhost/api/cycle/feed-subscription", {
method: "POST",
}),
);
expect(res.status).toBe(403);
});
});