Files
outline/server/utils/LogoutTokenStore.test.ts
T
Tom MoorandClaude Opus 5.5 00b124d03c chore: Rename APIContext to Context and flatten its fields (#14684)
* chore: Rename APIContext to Context and flatten its fields

Auth and transaction now live directly on the context instead of in
ctx.state and a nested ctx.context, and model helpers read them directly.
APIContext remains as a deprecated alias.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Remove deprecated APIContext alias

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* fix: Read auth from context in OAuth provider callbacks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Add withUser and withTransaction context helpers

Replaces manual createContext rebuilds that copy fields from an existing context.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Remove arbitrary property access from Koa context types

AppContext and Context no longer inherit the any index signature from
Koa's DefaultContext, custom fields are declared explicitly, and importing
Koa's loose Context type is disallowed by lint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-23 23:17:06 -05:00

73 lines
2.2 KiB
TypeScript

import Redis from "@server/storage/redis";
import { LogoutTokenStore } from "./LogoutTokenStore";
import { RedisPrefixHelper } from "./RedisPrefixHelper";
import type { AppContext } from "@server/types";
/**
* Minimal Koa context stub exposing only the cookie jar and hostname the store
* relies on.
*/
function buildContext(cookies: Record<string, string> = {}) {
const jar = new Map(Object.entries(cookies));
return {
request: { hostname: "localhost" },
cookies: {
get: (name: string) => jar.get(name),
set: (name: string, value: string) => {
if (value) {
jar.set(name, value);
} else {
jar.delete(name);
}
},
},
} as unknown as AppContext;
}
describe("LogoutTokenStore", () => {
const store = new LogoutTokenStore("oidc");
it("persists a token behind a session cookie and consumes it once", async () => {
const signInCtx = buildContext();
await store.persist(signInCtx, "the-token");
const sessionId = signInCtx.cookies.get("oidcSession");
expect(sessionId).toBeTruthy();
expect(
await Redis.defaultClient.get(
RedisPrefixHelper.getLogoutTokenKey("oidc", sessionId!)
)
).toEqual("the-token");
const logoutCtx = buildContext({ oidcSession: sessionId! });
expect(await store.consume(logoutCtx)).toEqual("the-token");
// Consuming clears both the cookie and the server-side token.
expect(logoutCtx.cookies.get("oidcSession")).toBeUndefined();
expect(
await Redis.defaultClient.get(
RedisPrefixHelper.getLogoutTokenKey("oidc", sessionId!)
)
).toBeNull();
});
it("returns null when there is no session cookie", async () => {
expect(await store.consume(buildContext())).toBeNull();
});
it("namespaces the cookie and key by provider", async () => {
const samlStore = new LogoutTokenStore("saml");
const ctx = buildContext();
await samlStore.persist(ctx, "saml-token");
const sessionId = ctx.cookies.get("samlSession");
expect(sessionId).toBeTruthy();
expect(ctx.cookies.get("oidcSession")).toBeUndefined();
expect(
await Redis.defaultClient.get(
RedisPrefixHelper.getLogoutTokenKey("saml", sessionId!)
)
).toEqual("saml-token");
});
});