Files
liqa/server/test/revoked-token-cleanup.spec.ts
agent 2976a08aef feat(auth): clean expired revoked tokens
- Remove expired token revocations at startup and on the configured cron.
- Verify expiry boundaries and disabled-auth behavior.
2026-11-05 02:48:53 -05:00

117 lines
3.9 KiB
TypeScript

import { ConfigService } from "@nestjs/config";
import { SchedulerRegistry } from "@nestjs/schedule";
import { CronJob } from "cron";
import { DataSource, Repository } from "typeorm";
import { AppConfig } from "../src/config/app.config";
import { dataSourceOptions } from "../src/database/data-source";
import { RevokedTokenEntity } from "../src/auth/revoked-token.entity";
import {
AUTH_REVOKED_TOKEN_CLEANUP_JOB,
RevokedTokenCleanupService,
} from "../src/auth/revoked-token-cleanup.service";
describe("RevokedTokenCleanupService", () => {
let dataSource: DataSource;
let repository: Repository<RevokedTokenEntity>;
let addCronJob: jest.Mock;
let registry: SchedulerRegistry;
async function createService(
enabled: boolean,
cron = "0 3 * * *",
): Promise<RevokedTokenCleanupService> {
const config = {
get: jest.fn((key: keyof AppConfig) => {
if (key === "AUTH_ENABLED") return enabled;
if (key === "AUTH_REVOKED_TOKEN_CLEANUP_CRON") return cron;
throw new Error(`Unexpected config key ${key}`);
}),
} as unknown as ConfigService<AppConfig, true>;
return new RevokedTokenCleanupService(repository, config, registry);
}
beforeEach(async () => {
dataSource = new DataSource(dataSourceOptions(":memory:"));
await dataSource.initialize();
await dataSource.runMigrations();
repository = dataSource.getRepository(RevokedTokenEntity);
addCronJob = jest.fn();
registry = { addCronJob } as unknown as SchedulerRegistry;
});
afterEach(async () => {
await dataSource.destroy();
jest.useRealTimers();
});
it("deletes only expired revocations, including expiry at the current instant", async () => {
const now = new Date("2026-11-05T12:00:00.000Z");
jest.useFakeTimers().setSystemTime(now);
await repository.insert([
{ tokenHash: "a".repeat(64), expiresAt: new Date(now.getTime() - 1) },
{ tokenHash: "b".repeat(64), expiresAt: now },
{ tokenHash: "c".repeat(64), expiresAt: new Date(now.getTime() + 1) },
]);
const service = await createService(true);
await service.cleanupExpiredTokens();
const retained = await repository.find();
expect(retained.map(({ tokenHash }) => tokenHash)).toEqual([
"c".repeat(64),
]);
});
it("tolerates an empty table and repeated cleanup runs", async () => {
const service = await createService(true);
await expect(service.cleanupExpiredTokens()).resolves.toBeUndefined();
await expect(service.cleanupExpiredTokens()).resolves.toBeUndefined();
await expect(repository.count()).resolves.toBe(0);
});
it("cleans at startup and registers the configured in-process cron job", async () => {
const now = new Date("2026-11-05T12:00:00.000Z");
jest.useFakeTimers().setSystemTime(now);
await repository.insert({
tokenHash: "d".repeat(64),
expiresAt: new Date(now.getTime() - 1),
});
const service = await createService(true, "*/5 * * * *");
await service.onModuleInit();
expect(addCronJob).toHaveBeenCalledTimes(1);
expect(addCronJob).toHaveBeenCalledWith(
AUTH_REVOKED_TOKEN_CLEANUP_JOB,
expect.any(CronJob),
);
expect(await repository.count()).toBe(0);
const job = addCronJob.mock.calls[0][1] as CronJob;
expect(job.cronTime.source).toBe("*/5 * * * *");
await repository.insert({
tokenHash: "e".repeat(64),
expiresAt: new Date(now.getTime() - 1),
});
await job.fireOnTick();
expect(await repository.count()).toBe(0);
job.stop();
});
it("does not schedule or write cleanup data when authentication is disabled", async () => {
const service = await createService(false);
await repository.insert({
tokenHash: "f".repeat(64),
expiresAt: new Date("2020-01-01T00:00:00.000Z"),
});
await service.onModuleInit();
await service.cleanupExpiredTokens();
expect(addCronJob).not.toHaveBeenCalled();
expect(await repository.count()).toBe(1);
});
});