Repository: strapi/strapi
File: packages/core/admin/server/src/services/__tests__/token.test.ts (TypeScript)

Code before:
  createJwtToken,
  getTokenOptions,
  decodeJwtToken,
  createToken,
  expiresInToSeconds,
} from '../token';

const delay = (time: any) =>
  new Promise((resolve) => {
    setTimeout(resolve, time);
  });

describe('expiresInToSeconds', () => {
  const secret = 'test-123';

  test('returns undefined for null/undefined', () => {
    expect(expiresInToSeconds(undefined as any, secret)).toBeUndefined();
    expect(expiresInToSeconds(null as any, secret)).toBeUndefined();
  });

  test('accepts numeric seconds', () => {
    expect(expiresInToSeconds(3600, secret)).toBe(3600);
  });

  test('accepts numeric string seconds', () => {
    expect(expiresInToSeconds('180', secret)).toBe(180);
  });

  test("parses '30d' correctly (approx in seconds)", () => {
    const seconds = expiresInToSeconds('30d', secret);
    expect(typeof seconds).toBe('number');
    expect(seconds).toBe(30 * 24 * 60 * 60);
  });

  test("parses shorthand like '12h', '15m', '45s'", () => {
    expect(expiresInToSeconds('12h', secret)).toBe(12 * 60 * 60);
    expect(expiresInToSeconds('15m', secret)).toBe(15 * 60);
    expect(expiresInToSeconds('45s', secret)).toBe(45);
  });

  test("parses '1w' as 7 days", () => {
    expect(expiresInToSeconds('1w', secret)).toBe(7 * 24 * 60 * 60);
  });

  test('returns undefined for invalid strings', () => {
    expect(expiresInToSeconds('bad', secret)).toBeUndefined();
    expect(expiresInToSeconds({} as any, secret)).toBeUndefined();
  });
});

describe('Token', () => {

Review comment (bug):
This is failing but I think maybe we should just remove this case? It doesn't seem to be covered in the logic

Code after:
  createJwtToken,
  getTokenOptions,
  decodeJwtToken,
  createToken,
  expiresInToSeconds,
} from '../token';

const delay = (time: any) =>
  new Promise((resolve) => {
    setTimeout(resolve, time);
  });

describe('expiresInToSeconds', () => {
  test('returns undefined for null/undefined', () => {
    expect(expiresInToSeconds(undefined as any)).toBeUndefined();
    expect(expiresInToSeconds(null as any)).toBeUndefined();
  });

  test('accepts numeric seconds', () => {
    expect(expiresInToSeconds(3600)).toBe(3600);
  });

  test('accepts numeric string seconds', () => {
    expect(expiresInToSeconds('180')).toBe(180);
  });

  test("parses '30d' correctly (approx in seconds)", () => {
    const seconds = expiresInToSeconds('30d');
    expect(typeof seconds).toBe('number');
    expect(seconds).toBe(30 * 24 * 60 * 60);
  });

  test("parses shorthand like '12h', '15m', '45s'", () => {
    expect(expiresInToSeconds('12h')).toBe(12 * 60 * 60);
    expect(expiresInToSeconds('15m')).toBe(15 * 60);
    expect(expiresInToSeconds('45s')).toBe(45);
  });

  test("parses '1w' as 7 days", () => {
    expect(expiresInToSeconds('1w')).toBe(7 * 24 * 60 * 60);
  });

  test('returns undefined for invalid strings', () => {
    expect(expiresInToSeconds('bad')).toBeUndefined();
    expect(expiresInToSeconds({} as any)).toBeUndefined();
  });
});

describe('Token', () => {
  describe('token options', () => {
    test('Has defaults', () => {