Files
pitchfast/tests/mock-auth.test.ts

72 lines
1.8 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import {
MOCK_SESSION_COOKIE_NAME,
MOCK_SESSION_COOKIE_VALUE,
createClearedMockSessionCookie,
createMockSessionCookie,
getMockSession,
hasMockSession,
} from "../lib/mock-auth";
type CookieLookupName = string;
test("hasMockSession returns true only for the expected mock session cookie", () => {
assert.equal(
hasMockSession({
get: (name: CookieLookupName) =>
name === MOCK_SESSION_COOKIE_NAME
? { name, value: MOCK_SESSION_COOKIE_VALUE }
: undefined,
}),
true,
);
assert.equal(
hasMockSession({
get: () => ({ name: MOCK_SESSION_COOKIE_NAME, value: "wrong" }),
}),
false,
);
assert.equal(hasMockSession({ get: () => undefined }), false);
});
test("createMockSessionCookie creates an http-only lax root cookie", () => {
assert.deepEqual(createMockSessionCookie(), {
name: MOCK_SESSION_COOKIE_NAME,
value: MOCK_SESSION_COOKIE_VALUE,
httpOnly: true,
sameSite: "lax",
secure: true,
path: "/",
maxAge: 60 * 60 * 24 * 7,
});
});
test("getMockSession returns the simulated admin user only when the cookie is valid", () => {
const validStore = {
get: (name: CookieLookupName) =>
name === MOCK_SESSION_COOKIE_NAME
? { name, value: MOCK_SESSION_COOKIE_VALUE }
: undefined,
};
assert.deepEqual(getMockSession(validStore), {
name: "Matthias Meister",
email: "matthias@webdev-pipeline.local",
});
assert.equal(getMockSession({ get: () => undefined }), null);
});
test("createClearedMockSessionCookie expires the mock session at the root path", () => {
assert.deepEqual(createClearedMockSessionCookie(), {
name: MOCK_SESSION_COOKIE_NAME,
value: "",
path: "/",
maxAge: 0,
});
});