Files
co-lite/solvway-admin-portal/app/lib/server/signedInElsewhere.test.ts
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

49 lines
2.3 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import { NextResponse } from "next/server";
const h = vi.hoisted(() => ({ set: vi.fn() }));
vi.mock("next/headers", () => ({ cookies: async () => ({ set: h.set, get: () => undefined }) }));
import { isSessionReplaced, markReplacedOn, clearMarkerOn, endReplacedSession } from "./signedInElsewhere";
import { SIGNED_IN_ELSEWHERE_COOKIE } from "@/app/lib/signedInElsewhere";
import { sessionCookieName } from "@/app/lib/sessionCookieName";
beforeEach(() => h.set.mockReset());
describe("isSessionReplaced", () => {
it("is true only for mobile-api's 401 SESSION_REPLACED", () => {
expect(isSessionReplaced({ ok: false, error: "x", status: 401, code: "SESSION_REPLACED" })).toBe(true);
expect(isSessionReplaced({ ok: false, error: "x", status: 401 })).toBe(false);
expect(isSessionReplaced({ ok: false, error: "x", status: 403, code: "SESSION_REPLACED" })).toBe(false);
expect(isSessionReplaced({ ok: true, data: null })).toBe(false);
});
});
describe("markReplacedOn", () => {
it("clears the session cookie and sets the marker for five minutes", () => {
const res = markReplacedOn(NextResponse.json({}, { status: 401 }));
const session = res.cookies.get(sessionCookieName());
const marker = res.cookies.get(SIGNED_IN_ELSEWHERE_COOKIE);
expect(session?.value).toBe("");
expect(marker?.value).toBe("1");
const setCookie = res.headers.getSetCookie().join("\n");
expect(setCookie).toMatch(new RegExp(`${SIGNED_IN_ELSEWHERE_COOKIE}=1;[^\\n]*Max-Age=300`));
expect(setCookie).toMatch(new RegExp(`${SIGNED_IN_ELSEWHERE_COOKIE}=1;[^\\n]*Secure`));
});
});
describe("clearMarkerOn", () => {
it("expires the marker", () => {
const res = clearMarkerOn(NextResponse.json({}));
expect(res.headers.getSetCookie().join("\n")).toMatch(new RegExp(`${SIGNED_IN_ELSEWHERE_COOKIE}=;[^\\n]*Max-Age=0`));
});
});
describe("endReplacedSession", () => {
it("clears the session cookie and sets the marker", async () => {
await endReplacedSession();
expect(h.set).toHaveBeenCalledWith(sessionCookieName(), "", expect.objectContaining({ maxAge: 0 }));
expect(h.set).toHaveBeenCalledWith(SIGNED_IN_ELSEWHERE_COOKIE, "1", expect.objectContaining({ maxAge: 300, secure: true }));
});
});