NAC_Blockchain/services/nac-admin/server/auth.logout.test.ts

69 lines
1.9 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { appRouter } from "./routers";
import type { TrpcContext } from "./_core/context";
// Mock all external dependencies
vi.mock("./nacAuth", () => ({
loginWithNacCredentials: vi.fn(),
verifyNacToken: vi.fn(),
listNacUsers: vi.fn(),
getNacUserCount: vi.fn(),
}));
vi.mock("./mongodb", () => ({
getMongoDb: vi.fn().mockResolvedValue(null),
}));
vi.mock("./secrets", () => ({
getSecrets: () => ({
nacMysqlUrl: "mysql://test:test@localhost:3306/nac_id",
nacMongoUrl: "mongodb://test:test@localhost:27017",
nacJwtSecret: "test-jwt-secret-at-least-32-chars-long",
nacAdminToken: "test-admin-token",
}),
getNacJwtSecret: () => "test-jwt-secret-at-least-32-chars-long",
getNacMysqlUrl: () => "mysql://test:test@localhost:3306/nac_id",
getNacMongoUrl: () => "mongodb://test:test@localhost:27017",
getNacAdminToken: () => "test-admin-token",
validateSecrets: vi.fn(),
}));
type CookieCall = {
name: string;
options: Record<string, unknown>;
};
function createNacAuthContext() {
const clearedCookies: CookieCall[] = [];
const ctx: TrpcContext = {
user: null,
req: {
protocol: "https",
headers: {},
ip: "127.0.0.1",
} as any,
res: {
clearCookie: (name: string, options: Record<string, unknown>) => {
clearedCookies.push({ name, options });
},
cookie: vi.fn(),
} as any,
};
return { ctx, clearedCookies };
}
describe("nacAuth.logout", () => {
it("clears the nac_admin_token cookie and reports success", async () => {
const { ctx, clearedCookies } = createNacAuthContext();
const caller = appRouter.createCaller(ctx);
const result = await caller.nacAuth.logout();
expect(result).toEqual({ success: true });
expect(clearedCookies).toHaveLength(1);
expect(clearedCookies[0]?.name).toBe("nac_admin_token");
});
});