mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 15:17:30 +00:00
76df44d8de
Every refresh writes a new oauth_tokens row and nothing ever deleted the old ones, so an MCP client refreshing hourly grew the table, and every backup with it, without end. A nightly job now deletes tokens whose refresh token expired more than 30 days ago, leaves first, and keeps any row a live chain still points at for replay detection. Also types the backup's per-entry store flag.
40 lines
2.0 KiB
TypeScript
40 lines
2.0 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest';
|
|
|
|
const logMock = vi.hoisted(() => ({ logInfo: vi.fn(), logError: vi.fn(), logWarn: vi.fn(), logDebug: vi.fn() }));
|
|
vi.mock('../../../src/nest/audit/audit-log.logger', () => logMock);
|
|
|
|
import { OauthTokenRetentionJob, OAUTH_TOKEN_RETENTION_DAYS } from '../../../src/nest/oauth/oauth-token-retention.job';
|
|
import type { OauthTokensRepository } from '../../../src/db/repositories/OauthTokens.repository';
|
|
import type { CronRegistrarService } from '../../../src/nest/scheduling/cron-registrar.service';
|
|
|
|
function make(deleteExpiredBefore: (cutoff: string) => Promise<number>, enabled = true) {
|
|
const registrar = { isEnabled: vi.fn(() => enabled), register: vi.fn() };
|
|
const job = new OauthTokenRetentionJob({ deleteExpiredBefore } as unknown as OauthTokensRepository, registrar as unknown as CronRegistrarService);
|
|
return { job, registrar };
|
|
}
|
|
|
|
describe('OauthTokenRetentionJob', () => {
|
|
it('OAUTHRET-001: registers a nightly job, and nothing under the test gate', () => {
|
|
const { job, registrar } = make(vi.fn());
|
|
job.onApplicationBootstrap();
|
|
expect(registrar.register).toHaveBeenCalledWith('oauth-token-retention', '30 3 * * *', expect.any(Function));
|
|
|
|
const gated = make(vi.fn(), false);
|
|
gated.job.onApplicationBootstrap();
|
|
expect(gated.registrar.register).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('OAUTHRET-002: deletes what expired more than the retention period ago and logs the count', async () => {
|
|
const del = vi.fn().mockResolvedValue(4);
|
|
await make(del).job.tick(new Date('2026-10-07T00:00:00.000Z'));
|
|
expect(OAUTH_TOKEN_RETENTION_DAYS).toBe(30);
|
|
expect(del).toHaveBeenCalledWith('2026-09-07T00:00:00.000Z');
|
|
expect(logMock.logInfo).toHaveBeenCalledWith('OAuth token retention: removed 4 expired token(s)');
|
|
});
|
|
|
|
it('OAUTHRET-003: a failing purge is logged, not thrown', async () => {
|
|
await make(vi.fn().mockRejectedValue(new Error('database is locked'))).job.tick();
|
|
expect(logMock.logError).toHaveBeenCalledWith('OAuth token retention: database is locked');
|
|
});
|
|
});
|