From bb1f91a921a421d74aecdb43f4ccf9703e6ecbe4 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Wed, 4 Dec 2024 11:07:02 +0700 Subject: [PATCH] Create twoFactorAuth.test.js --- tests/twoFactorAuth.test.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 tests/twoFactorAuth.test.js diff --git a/tests/twoFactorAuth.test.js b/tests/twoFactorAuth.test.js new file mode 100644 index 000000000..03dbf3ccf --- /dev/null +++ b/tests/twoFactorAuth.test.js @@ -0,0 +1,35 @@ +// twoFactorAuth.test.js + +import TwoFactorAuth from './twoFactorAuth'; // Import the module to be tested + +describe('TwoFactorAuth', () => { + let twoFactorAuth; + + beforeEach(() => { + twoFactorAuth = new TwoFactorAuth(); + }); + + test('should generate a new 2FA code', () => { + const code = twoFactorAuth.generateCode('user@example.com'); + expect(code).toHaveLength(6); // Assuming the code is 6 digits + }); + + test('should validate a correct 2FA code', () => { + const code = twoFactorAuth.generateCode('user@example.com'); + const isValid = twoFactorAuth.validateCode('user@example.com', code); + expect(isValid).toBe(true); + }); + + test('should invalidate an incorrect 2FA code', () => { + twoFactorAuth.generateCode('user@example.com'); + const isValid = twoFactorAuth.validateCode('user@example.com', '123456'); + expect(isValid).toBe(false); + }); + + test('should expire codes after a certain time', () => { + const code = twoFactorAuth.generateCode('user@example.com'); + jest.advanceTimersByTime(300000); // Assuming the code expires after 5 minutes + const isValid = twoFactorAuth.validateCode('user@example.com', code); + expect(isValid).toBe(false); + }); +});