forked from heusalagroup/fi.hg.backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmailVerificationServiceImpl.test.ts
100 lines (79 loc) · 3.84 KB
/
EmailVerificationServiceImpl.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) 2023. Heusala Group Oy <[email protected]>. All rights reserved.
import { LogLevel } from "../core/types/LogLevel";
import { EmailVerificationServiceImpl } from "./EmailVerificationServiceImpl";
jest.useFakeTimers();
beforeAll(() => {
EmailVerificationServiceImpl.setLogLevel(LogLevel.NONE);
});
describe('EmailVerificationServiceImpl', () => {
describe('create', () => {
it('creates a new instance with default verification timeout', () => {
const service = EmailVerificationServiceImpl.create();
expect(service).toBeInstanceOf(EmailVerificationServiceImpl);
// Test if default timeout is set, using any method to access it.
});
it('creates a new instance with provided verification timeout', () => {
const customTimeout = 10 * 60 * 1000;
const service = EmailVerificationServiceImpl.create(customTimeout);
expect(service).toBeInstanceOf(EmailVerificationServiceImpl);
// Test if custom timeout is set, using any method to access it.
});
});
describe('destroy', () => {
it('clears all timers and codes', () => {
const service = EmailVerificationServiceImpl.create();
// const code = service.createVerificationCode('[email protected]');
service.destroy();
// Check the timer is cleared and codes array is empty
});
});
describe('verifyCode', () => {
it('verifies a valid code and removes it', () => {
const service = EmailVerificationServiceImpl.create();
const email = '[email protected]';
const code = service.createVerificationCode(email);
expect(service.verifyCode(email, code)).toBeTruthy();
expect(service.verifyCode(email, code)).toBeFalsy();
});
it('does not verify an invalid code', () => {
const service = EmailVerificationServiceImpl.create();
const email = '[email protected]';
service.createVerificationCode(email);
expect(service.verifyCode(email, '0000')).toBeFalsy();
});
});
describe('removeVerificationCode', () => {
it('removes a valid code', () => {
const service = EmailVerificationServiceImpl.create();
const email = '[email protected]';
const code = service.createVerificationCode(email);
service.removeVerificationCode(email, code);
expect(service.verifyCode(email, code)).toBeFalsy();
});
it('throws an error when email or code are not provided', () => {
const service = EmailVerificationServiceImpl.create();
const email = '[email protected]';
const code = service.createVerificationCode(email);
expect(() => service.removeVerificationCode('', code)).toThrow(TypeError);
expect(() => service.removeVerificationCode(email, '')).toThrow(TypeError);
});
});
describe('createVerificationCode', () => {
it('creates a new code for a given email and removes old ones', () => {
const service = EmailVerificationServiceImpl.create();
const email = '[email protected]';
const code1 = service.createVerificationCode(email);
const code2 = service.createVerificationCode(email);
expect(service.verifyCode(email, code1)).toBeFalsy();
expect(service.verifyCode(email, code2)).toBeTruthy();
});
it('automatically removes the code after timeout', () => {
const timeout = 1000;
const service = EmailVerificationServiceImpl.create(timeout);
const email = '[email protected]';
const code = service.createVerificationCode(email);
jest.runAllTimers();
expect(service.verifyCode(email, code)).toBeFalsy();
});
});
});