108 lines
3.9 KiB
TypeScript
108 lines
3.9 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
buildRemindCookie,
|
|
evaluatePasswordPolicyBanner,
|
|
PASSWORD_POLICY_REMIND_COOKIE,
|
|
PASSWORD_POLICY_REMIND_DURATION_MS,
|
|
readRemindAfterCookie,
|
|
} from "./password-policy-banner.server.js";
|
|
|
|
describe("evaluatePasswordPolicyBanner", () => {
|
|
it("hides the banner when the session flag is not set", () => {
|
|
expect(evaluatePasswordPolicyBanner({ policyOutdated: false })).toEqual({ show: false, reason: "not_outdated" });
|
|
});
|
|
|
|
it("shows the banner when the flag is set and no snooze cookie is present", () => {
|
|
const result = evaluatePasswordPolicyBanner({ policyOutdated: true, cookieHeader: null });
|
|
expect(result.show).toBe(true);
|
|
expect(result.reason).toBe("show");
|
|
});
|
|
|
|
it("hides the banner when the snooze cookie is still in the future", () => {
|
|
const now = 1_700_000_000_000;
|
|
const future = now + 60_000;
|
|
const result = evaluatePasswordPolicyBanner({
|
|
policyOutdated: true,
|
|
cookieHeader: `${PASSWORD_POLICY_REMIND_COOKIE}=${future}`,
|
|
now,
|
|
});
|
|
expect(result.show).toBe(false);
|
|
expect(result.reason).toBe("remind_active");
|
|
});
|
|
|
|
it("shows the banner once the snooze cookie has expired", () => {
|
|
const now = 1_700_000_000_000;
|
|
const past = now - 1;
|
|
const result = evaluatePasswordPolicyBanner({
|
|
policyOutdated: true,
|
|
cookieHeader: `${PASSWORD_POLICY_REMIND_COOKIE}=${past}`,
|
|
now,
|
|
});
|
|
expect(result.show).toBe(true);
|
|
});
|
|
|
|
it("ignores unrelated cookies", () => {
|
|
const result = evaluatePasswordPolicyBanner({
|
|
policyOutdated: true,
|
|
cookieHeader: "session=abc; theme=dark",
|
|
});
|
|
expect(result.show).toBe(true);
|
|
});
|
|
|
|
it("treats a malformed cookie value as 'no snooze'", () => {
|
|
const result = evaluatePasswordPolicyBanner({
|
|
policyOutdated: true,
|
|
cookieHeader: `${PASSWORD_POLICY_REMIND_COOKIE}=not-a-number`,
|
|
});
|
|
expect(result.show).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe("readRemindAfterCookie", () => {
|
|
it("returns undefined when the header is missing", () => {
|
|
expect(readRemindAfterCookie(undefined)).toBeUndefined();
|
|
expect(readRemindAfterCookie("")).toBeUndefined();
|
|
});
|
|
|
|
it("parses an unsigned integer", () => {
|
|
expect(readRemindAfterCookie(`${PASSWORD_POLICY_REMIND_COOKIE}=12345`)).toBe(12345);
|
|
});
|
|
|
|
it("returns undefined for a negative or zero value", () => {
|
|
expect(readRemindAfterCookie(`${PASSWORD_POLICY_REMIND_COOKIE}=-1`)).toBeUndefined();
|
|
expect(readRemindAfterCookie(`${PASSWORD_POLICY_REMIND_COOKIE}=0`)).toBeUndefined();
|
|
});
|
|
|
|
it("decodes URL-encoded values", () => {
|
|
expect(readRemindAfterCookie(`${PASSWORD_POLICY_REMIND_COOKIE}=${encodeURIComponent("123")}`)).toBe(123);
|
|
});
|
|
|
|
it("treats malformed percent-encoding as a malformed snooze value", () => {
|
|
expect(readRemindAfterCookie(`${PASSWORD_POLICY_REMIND_COOKIE}=%E0%A4%A`)).toBeUndefined();
|
|
});
|
|
|
|
it("ignores other cookies on the line", () => {
|
|
expect(readRemindAfterCookie(`a=1; ${PASSWORD_POLICY_REMIND_COOKIE}=42; b=2`)).toBe(42);
|
|
});
|
|
});
|
|
|
|
describe("buildRemindCookie", () => {
|
|
it("emits the expected Set-Cookie shape", () => {
|
|
const now = 1_700_000_000_000;
|
|
const cookie = buildRemindCookie(now);
|
|
const expectedExpiry = now + PASSWORD_POLICY_REMIND_DURATION_MS;
|
|
expect(cookie).toContain(`${PASSWORD_POLICY_REMIND_COOKIE}=${expectedExpiry}`);
|
|
expect(cookie).toContain("Path=/");
|
|
expect(cookie).toContain(`Max-Age=${Math.floor(PASSWORD_POLICY_REMIND_DURATION_MS / 1000)}`);
|
|
expect(cookie).toContain("SameSite=Lax");
|
|
});
|
|
|
|
it("produces a value the parser can read back", () => {
|
|
const now = 1_700_000_000_000;
|
|
const cookie = buildRemindCookie(now);
|
|
// Strip attributes; the parser only cares about the name=value segment.
|
|
const firstSegment = cookie.split(";")[0];
|
|
expect(readRemindAfterCookie(firstSegment)).toBe(now + PASSWORD_POLICY_REMIND_DURATION_MS);
|
|
});
|
|
});
|