test(unit): add lightweight vitest coverage for auth duration and seq pagination utils
This commit is contained in:
@@ -11,22 +11,7 @@ import { User } from '../entities/user.entity';
|
||||
import { RegisterDto } from './dto.register.dto';
|
||||
import { LoginDto } from './dto.login.dto';
|
||||
import { AuditService } from '../audit/audit.service';
|
||||
|
||||
function parseDurationToSeconds(input: string, fallbackSeconds: number): number {
|
||||
const raw = input.trim();
|
||||
if (/^\d+$/.test(raw)) return Number(raw);
|
||||
|
||||
const m = raw.match(/^(\d+)([smhd])$/i);
|
||||
if (!m) return fallbackSeconds;
|
||||
|
||||
const value = Number(m[1]);
|
||||
const unit = m[2].toLowerCase();
|
||||
if (unit === 's') return value;
|
||||
if (unit === 'm') return value * 60;
|
||||
if (unit === 'h') return value * 3600;
|
||||
if (unit === 'd') return value * 86400;
|
||||
return fallbackSeconds;
|
||||
}
|
||||
import { parseDurationToSeconds } from './token.util';
|
||||
|
||||
function signAccessToken(userId: string, email: string): string {
|
||||
const secret = process.env.JWT_ACCESS_SECRET as string;
|
||||
|
||||
14
Fabric.Backend.Center/src/auth/token.util.spec.ts
Normal file
14
Fabric.Backend.Center/src/auth/token.util.spec.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { parseDurationToSeconds } from './token.util';
|
||||
|
||||
describe('parseDurationToSeconds', () => {
|
||||
it('parses time units', () => {
|
||||
expect(parseDurationToSeconds('15m', 1)).toBe(900);
|
||||
expect(parseDurationToSeconds('2h', 1)).toBe(7200);
|
||||
expect(parseDurationToSeconds('10', 1)).toBe(10);
|
||||
});
|
||||
|
||||
it('falls back on invalid input', () => {
|
||||
expect(parseDurationToSeconds('abc', 42)).toBe(42);
|
||||
});
|
||||
});
|
||||
15
Fabric.Backend.Center/src/auth/token.util.ts
Normal file
15
Fabric.Backend.Center/src/auth/token.util.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
export function parseDurationToSeconds(input: string, fallbackSeconds: number): number {
|
||||
const raw = input.trim();
|
||||
if (/^\d+$/.test(raw)) return Number(raw);
|
||||
|
||||
const m = raw.match(/^(\d+)([smhd])$/i);
|
||||
if (!m) return fallbackSeconds;
|
||||
|
||||
const value = Number(m[1]);
|
||||
const unit = m[2].toLowerCase();
|
||||
if (unit === 's') return value;
|
||||
if (unit === 'm') return value * 60;
|
||||
if (unit === 'h') return value * 3600;
|
||||
if (unit === 'd') return value * 86400;
|
||||
return fallbackSeconds;
|
||||
}
|
||||
Reference in New Issue
Block a user