This repository was archived by the owner on Feb 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEmailAuthControllerImpl.test.ts
More file actions
361 lines (286 loc) · 12.2 KB
/
Copy pathEmailAuthControllerImpl.test.ts
File metadata and controls
361 lines (286 loc) · 12.2 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright (c) 2023. Heusala Group Oy <info@hg.fi>. All rights reserved.
import { EmailAuthMessageService } from "../core/auth/EmailAuthMessageService";
import { EmailTokenService } from "../core/auth/EmailTokenService";
import { EmailVerificationService } from "../core/auth/EmailVerificationService";
import { JwtDecodeService } from "../core/jwt/JwtDecodeService";
import { ResponseEntity } from "../core/request/types/ResponseEntity";
import { Language } from "../core/types/Language";
import { LogLevel } from "../core/types/LogLevel";
import { EmailAuthControllerImpl } from "./EmailAuthControllerImpl";
describe('EmailAuthControllerImpl', () => {
beforeAll(() => {
EmailAuthControllerImpl.setLogLevel(LogLevel.NONE);
});
let emailTokenService: EmailTokenService;
let emailVerificationService: EmailVerificationService;
let emailAuthMessageService: EmailAuthMessageService;
let jwtDecodeService: JwtDecodeService;
let controller: EmailAuthControllerImpl;
beforeEach(() => {
// Reset the mocks before each test
jest.resetAllMocks();
emailTokenService = {
verifyToken: jest.fn(),
verifyValidTokenForSubject: jest.fn(),
isTokenValid: jest.fn(),
verifyTokenOnly: jest.fn(),
createUnverifiedEmailToken: jest.fn(),
createVerifiedEmailToken: jest.fn(),
};
emailVerificationService = {
destroy: jest.fn(),
verifyCode: jest.fn(),
removeVerificationCode: jest.fn(),
createVerificationCode: jest.fn(),
};
emailAuthMessageService = {
sendAuthenticationCode: jest.fn()
};
jwtDecodeService = {
setLogLevel: jest.fn(),
decodePayload: jest.fn(),
decodePayloadAudience: jest.fn(),
decodePayloadSubject: jest.fn(),
decodePayloadVerified: jest.fn(),
};
controller = EmailAuthControllerImpl.create(
Language.ENGLISH,
emailTokenService,
emailVerificationService,
emailAuthMessageService,
jwtDecodeService,
);
});
describe('authenticateEmail', () => {
it('should return bad request if the body is not an AuthenticateEmailDTO', async () => {
// Given
const body = {};
const langString = '';
// When
const result = await controller.authenticateEmail(body, langString);
// Then
expect(result).toBeInstanceOf(ResponseEntity);
expect(result.getStatusCode()).toBe(400);
});
it('should return bad request if the email is missing in the body', async () => {
// Given
const body = {
notEmail: 'Not an email'
};
const langString = 'ENGLISH';
// When
const result = await controller.authenticateEmail(body, langString);
// Then
expect(result).toBeInstanceOf(ResponseEntity);
expect(result.getStatusCode()).toBe(400);
});
it('should return internal server error if the email sending fails', async () => {
// Given
const body = {
email: 'test@email.com'
};
const langString = 'ENGLISH';
(emailVerificationService.createVerificationCode as jest.Mock).mockReturnValue('123456');
(emailTokenService.createUnverifiedEmailToken as jest.Mock).mockReturnValue({
token: 'random_token',
email: 'test@email.com',
verified: false
});
(emailAuthMessageService.sendAuthenticationCode as jest.Mock).mockImplementation(() => {
throw new Error('Email sending failed');
});
// When
const result = await controller.authenticateEmail(body, langString);
// Then
expect(result).toBeInstanceOf(ResponseEntity);
expect(result.getStatusCode()).toBe(500);
});
it('should return an email token if everything is okay', async () => {
// Given
const body = {
email: 'test@email.com'
};
const langString = 'ENGLISH';
(emailVerificationService.createVerificationCode as jest.Mock).mockReturnValue('123456');
(emailTokenService.createUnverifiedEmailToken as jest.Mock).mockReturnValue({
token: 'random_token',
email: 'test@email.com',
verified: false
});
(emailAuthMessageService.sendAuthenticationCode as jest.Mock).mockResolvedValue(undefined);
// When
const result = await controller.authenticateEmail(body, langString);
// Then
expect(result).toBeInstanceOf(ResponseEntity);
expect(result.getStatusCode()).toBe(200);
expect(result.getBody()).toEqual({
token: 'random_token',
email: 'test@email.com',
verified: false
});
});
});
describe('verifyEmailCode', () => {
it('should return bad request if the body is not an VerifyEmailCodeDTO', async () => {
// Given
const body = {};
// When
const result = await controller.verifyEmailCode(body);
// Then
expect(result).toBeInstanceOf(ResponseEntity);
expect(result.getStatusCode()).toBe(400);
});
it('should return bad request if the code or email is missing in the body', async () => {
// Given
const body = {
email: 'test@email.com'
// Missing code
};
// When
const result = await controller.verifyEmailCode(body);
// Then
expect(result).toBeInstanceOf(ResponseEntity);
expect(result.getStatusCode()).toBe(400);
});
it('should return unauthorized if the code is not correct', async () => {
// Given
const body = {
token: {
email: 'test@email.com',
token: 'token'
},
code: '123456'
};
(emailVerificationService.verifyCode as jest.Mock).mockReturnValue(false);
// When
const result = await controller.verifyEmailCode(body);
// Then
expect(result).toBeInstanceOf(ResponseEntity);
expect(result.getStatusCode()).toBe(403);
});
it('should return an verified email token if the code is correct', async () => {
// Given
const body = {
token: {
email: 'test@email.com',
token: 'token'
},
code: '123456'
};
(emailVerificationService.verifyCode as jest.Mock).mockReturnValue(true);
(emailTokenService.verifyToken as jest.Mock).mockReturnValue(true);
(emailTokenService.createVerifiedEmailToken as jest.Mock).mockReturnValue({
token: 'verified_token',
email: 'test@email.com',
verified: true
});
// When
const result = await controller.verifyEmailCode(body);
// Then
expect(emailVerificationService.verifyCode).toHaveBeenCalledTimes(1);
expect(emailVerificationService.verifyCode).toHaveBeenCalledWith(
'test@email.com',
'123456',
);
expect(emailTokenService.verifyToken).toHaveBeenCalledTimes(1);
expect(emailTokenService.verifyToken).toHaveBeenCalledWith(
'test@email.com',
'token',
false
);
expect(emailTokenService.createVerifiedEmailToken).toHaveBeenCalledTimes(1);
expect(emailTokenService.createVerifiedEmailToken).toHaveBeenCalledWith(
'test@email.com',
);
expect(result).toBeInstanceOf(ResponseEntity);
expect(result.getStatusCode()).toBe(200);
expect(result.getBody()).toEqual({
token: 'verified_token',
email: 'test@email.com',
verified: true
});
});
});
describe('verifyEmailToken', () => {
it('should return bad request if the token is not provided', async () => {
// Given
const token = '';
// When
const result = await controller.verifyEmailToken(token);
// Then
expect(result).toBeInstanceOf(ResponseEntity);
expect(result.getStatusCode()).toBe(400);
});
it('should return unauthorized if the token is not valid', async () => {
// Given
const token = {
token: {
email: 'test@email.com',
token: 'invalid_token'
}
};
(emailTokenService.verifyToken as jest.Mock).mockReturnValue(false);
// When
const result = await controller.verifyEmailToken(token);
// Then
expect(result).toBeInstanceOf(ResponseEntity);
expect(result.getStatusCode()).toBe(403);
});
it('should return token information if the token is valid', async () => {
// Given
const token = {
token: {
email: 'test@email.com',
token: 'valid_token'
}
};
const tokenInfo = {
email: 'test@email.com',
verified: true,
};
(emailTokenService.verifyToken as jest.Mock).mockReturnValue(true);
(jwtDecodeService.decodePayload as jest.Mock).mockReturnValue(tokenInfo);
(emailTokenService.createVerifiedEmailToken as jest.Mock).mockReturnValue(tokenInfo);
// When
const result = await controller.verifyEmailToken(token);
expect(emailTokenService.verifyToken).toHaveBeenCalledTimes(1);
expect(emailTokenService.verifyToken).toHaveBeenCalledWith('test@email.com', 'valid_token', true);
expect(emailTokenService.createVerifiedEmailToken).toHaveBeenCalledTimes(1);
expect(emailTokenService.createVerifiedEmailToken).toHaveBeenCalledWith('test@email.com');
// Then
expect(result).toBeInstanceOf(ResponseEntity);
expect(result.getStatusCode()).toBe(200);
expect(result.getBody()).toEqual(tokenInfo);
});
});
describe('verifyTokenAndReturnSubject', () => {
it('should have error if the token is not provided', async () => {
// Given
const token = '';
// Then
await expect(controller.verifyTokenAndReturnSubject(token)).rejects.toThrow('Token was invalid: ');
});
it('should have error if the token is not valid', async () => {
// Given
const token = 'invalid_token';
(emailTokenService.verifyToken as jest.Mock).mockReturnValue(false);
// When
await expect(controller.verifyTokenAndReturnSubject(token)).rejects.toThrow('Token was invalid: ');
});
it('should return subject information if the token is valid', async () => {
// Given
const token = 'valid_token';
const subjectInfo = 'test@email.com';
(emailTokenService.isTokenValid as jest.Mock).mockReturnValue(true);
(jwtDecodeService.decodePayloadSubject as jest.Mock).mockReturnValue(subjectInfo);
// When
const result = await controller.verifyTokenAndReturnSubject(token);
expect(emailTokenService.isTokenValid).toHaveBeenCalledTimes(1);
expect(emailTokenService.isTokenValid).toHaveBeenCalledWith(token);
expect(jwtDecodeService.decodePayloadSubject).toHaveBeenCalledTimes(1);
expect(jwtDecodeService.decodePayloadSubject).toHaveBeenCalledWith(token);
// Then
expect(result).toBe(subjectInfo);
});
});
});