Merge pull request #2187 from automatisch/aut-1350-isResetPasswordTokenValid

test(user): write test cases for isResetPasswordTokenValid
This commit is contained in:
Ömer Faruk Aydın
2024-11-21 16:29:50 +03:00
committed by GitHub

View File

@@ -1,5 +1,5 @@
import { describe, it, expect, vi } from 'vitest';
import { Duration } from 'luxon';
import { DateTime, Duration } from 'luxon';
import appConfig from '../config/app.js';
import Base from './base.js';
import AccessToken from './access-token.js';
@@ -699,4 +699,48 @@ describe('User model', () => {
vi.useRealTimers();
});
describe('isResetPasswordTokenValid', () => {
it('should return true when resetPasswordTokenSentAt is within the next four hours', async () => {
vi.useFakeTimers();
const date = DateTime.fromObject(
{ year: 2024, month: 11, day: 12, hour: 16, minute: 30 },
{ zone: 'UTC+0' }
);
vi.setSystemTime(date);
const user = new User();
user.resetPasswordTokenSentAt = '2024-11-12T13:31:00.000Z';
expect(user.isResetPasswordTokenValid()).toBe(true);
vi.useRealTimers();
});
it('should return false when there is no resetPasswordTokenSentAt', async () => {
const user = new User();
expect(user.isResetPasswordTokenValid()).toBe(false);
});
it('should return false when resetPasswordTokenSentAt is older than four hours', async () => {
vi.useFakeTimers();
const date = DateTime.fromObject(
{ year: 2024, month: 11, day: 12, hour: 16, minute: 30 },
{ zone: 'UTC+0' }
);
vi.setSystemTime(date);
const user = new User();
user.resetPasswordTokenSentAt = '2024-11-12T12:29:00.000Z';
expect(user.isResetPasswordTokenValid()).toBe(false);
vi.useRealTimers();
});
});
});