From 167bb4e8a0197c1de1f776043e637243e06dd076 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Tue, 12 Nov 2024 13:42:54 +0000 Subject: [PATCH 1/2] test(user): write test cases for isResetPasswordTokenValid --- packages/backend/src/models/user.test.js | 36 ++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/packages/backend/src/models/user.test.js b/packages/backend/src/models/user.test.js index 71434537..f92f6801 100644 --- a/packages/backend/src/models/user.test.js +++ b/packages/backend/src/models/user.test.js @@ -699,4 +699,40 @@ describe('User model', () => { vi.useRealTimers(); }); + + describe('isResetPasswordTokenValid', () => { + it('should return true when resetPasswordTokenSentAt is within the next four hours', async () => { + vi.useFakeTimers(); + + const date = new Date(2024, 10, 12, 16, 30, 0, 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 = new Date(2024, 10, 12, 16, 30, 0, 0); + vi.setSystemTime(date); + + const user = new User(); + user.resetPasswordTokenSentAt = '2024-11-12T12:29:00.000Z'; + + expect(user.isResetPasswordTokenValid()).toBe(false); + + vi.useRealTimers(); + }); + }); }); From 864c762fe21bca6d99883ee18b4fa4ba8d183563 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Thu, 21 Nov 2024 12:48:54 +0000 Subject: [PATCH 2/2] test(user): use luxon DateTime with zone over Date --- packages/backend/src/models/user.test.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/backend/src/models/user.test.js b/packages/backend/src/models/user.test.js index f92f6801..b32aae70 100644 --- a/packages/backend/src/models/user.test.js +++ b/packages/backend/src/models/user.test.js @@ -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,12 +699,16 @@ describe('User model', () => { vi.useRealTimers(); }); - + describe('isResetPasswordTokenValid', () => { it('should return true when resetPasswordTokenSentAt is within the next four hours', async () => { vi.useFakeTimers(); - const date = new Date(2024, 10, 12, 16, 30, 0, 0); + const date = DateTime.fromObject( + { year: 2024, month: 11, day: 12, hour: 16, minute: 30 }, + { zone: 'UTC+0' } + ); + vi.setSystemTime(date); const user = new User(); @@ -724,7 +728,11 @@ describe('User model', () => { it('should return false when resetPasswordTokenSentAt is older than four hours', async () => { vi.useFakeTimers(); - const date = new Date(2024, 10, 12, 16, 30, 0, 0); + const date = DateTime.fromObject( + { year: 2024, month: 11, day: 12, hour: 16, minute: 30 }, + { zone: 'UTC+0' } + ); + vi.setSystemTime(date); const user = new User();