test(auth): mint session fixtures in the shape the lookup accepts

The session time-zone test stored sessions under readable secrets that
the cookie shape check now refuses before the query, so it tested the
refusal instead of the expiry. A shared helper mints hls_ plus 32 hex
bytes, and the two route tests that set a session cookie use it too.
This commit is contained in:
Marc-André Bombeck committed 2026-10-20 10:56:55 +02:00
1 parent 4f02004146
commit b08d527045
4 files changed
+23 -6

No files matched your search

+11
View File
@@ -0,0 +1,11 @@
/**
* A session cookie secret of the exact shape `createSession` mints (`hls_`
* and 32 random bytes in hex). The session lookup refuses any other shape
* before it asks the database, so a fixture that invents a readable secret
* tests the refusal, not the lookup.
*/
import { randomBytes } from "node:crypto";
export function fixtureSessionSecret(): string {
return `hls_${randomBytes(32).toString("hex")}`;
}
@@ -45,6 +45,7 @@ import { GET } from "../route";
import { checkAuthSurfaceRateLimit } from "@/lib/rate-limit";
import { mintNativeHandoff } from "@/lib/auth/native-handoff";
import { prisma } from "@/lib/db";
import { fixtureSessionSecret } from "@/__tests__/helpers/session-secret";
import {
NATIVE_HANDOFF_STATE_COOKIE,
encodeNativeHandoffState,
@@ -71,7 +72,7 @@ function makeRequest(opts: {
);
}
if (opts.withSession !== false) {
r.cookies.set("healthlog_session", "hls_sessionsecret");
r.cookies.set("healthlog_session", fixtureSessionSecret());
}
return r;
}
@@ -101,6 +101,7 @@ import {
verifyIdToken,
} from "@/lib/auth/oidc";
import { mintNativeHandoff } from "@/lib/auth/oidc-native-handoff";
import { fixtureSessionSecret } from "@/__tests__/helpers/session-secret";
const CONFIG = {
issuerUrl: "https://idp.example.com",
@@ -616,7 +617,8 @@ describe("GET /api/auth/oidc/callback — duplicate-callback idempotency", () =>
const req = new NextRequest(
"http://localhost/api/auth/oidc/callback?code=abc&state=STATE",
);
if (withSession) req.cookies.set("healthlog_session", "hls_valid_secret");
if (withSession)
req.cookies.set("healthlog_session", fixtureSessionSecret());
return req;
}