forked from heusalagroup/fi.hg.backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmsVerificationServiceImpl.test.ts
100 lines (79 loc) · 3.75 KB
/
SmsVerificationServiceImpl.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 { SmsVerificationServiceImpl } from "./SmsVerificationServiceImpl";
jest.useFakeTimers();
beforeAll(() => {
SmsVerificationServiceImpl.setLogLevel(LogLevel.NONE);
});
describe('SmsVerificationServiceImpl', () => {
describe('create', () => {
it('creates a new instance with default verification timeout', () => {
const service = SmsVerificationServiceImpl.create();
expect(service).toBeInstanceOf(SmsVerificationServiceImpl);
// 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 = SmsVerificationServiceImpl.create(customTimeout);
expect(service).toBeInstanceOf(SmsVerificationServiceImpl);
// Test if custom timeout is set, using any method to access it.
});
});
describe('destroy', () => {
it('clears all timers and codes', () => {
const service = SmsVerificationServiceImpl.create();
/*const code = */service.createVerificationCode('+358409970704');
service.destroy();
// Check the timer is cleared and codes array is empty
});
});
describe('verifyCode', () => {
it('verifies a valid code and removes it', () => {
const service = SmsVerificationServiceImpl.create();
const sms = '+358409970704';
const code = service.createVerificationCode(sms);
expect(service.verifyCode(sms, code)).toBeTruthy();
expect(service.verifyCode(sms, code)).toBeFalsy();
});
it('does not verify an invalid code', () => {
const service = SmsVerificationServiceImpl.create();
const sms = '+358409970704';
service.createVerificationCode(sms);
expect(service.verifyCode(sms, '0000')).toBeFalsy();
});
});
describe('removeVerificationCode', () => {
it('removes a valid code', () => {
const service = SmsVerificationServiceImpl.create();
const sms = '+358409970704';
const code = service.createVerificationCode(sms);
service.removeVerificationCode(sms, code);
expect(service.verifyCode(sms, code)).toBeFalsy();
});
it('throws an error when sms or code are not provided', () => {
const service = SmsVerificationServiceImpl.create();
const sms = '+358409970704';
const code = service.createVerificationCode(sms);
expect(() => service.removeVerificationCode('', code)).toThrow(TypeError);
expect(() => service.removeVerificationCode(sms, '')).toThrow(TypeError);
});
});
describe('createVerificationCode', () => {
it('creates a new code for a given sms and removes old ones', () => {
const service = SmsVerificationServiceImpl.create();
const sms = '+358409970704';
const code1 = service.createVerificationCode(sms);
const code2 = service.createVerificationCode(sms);
expect(service.verifyCode(sms, code1)).toBeFalsy();
expect(service.verifyCode(sms, code2)).toBeTruthy();
});
it('automatically removes the code after timeout', () => {
const timeout = 1000;
const service = SmsVerificationServiceImpl.create(timeout);
const sms = '+358409970704';
const code = service.createVerificationCode(sms);
jest.runAllTimers();
expect(service.verifyCode(sms, code)).toBeFalsy();
});
});
});