1
|
|
|
import { mock, instance, when, verify, deepEqual, anything } from 'ts-mockito'; |
2
|
|
|
import { UserRepository } from 'src/Infrastructure/HumanResource/User/Repository/UserRepository'; |
3
|
|
|
import { PasswordEncoderAdapter } from 'src/Infrastructure/Adapter/PasswordEncoderAdapter'; |
4
|
|
|
import { |
5
|
|
|
CreateUserCommand, |
6
|
|
|
IUserAdministrativeCommand |
7
|
|
|
} from 'src/Application/HumanResource/User/Command/CreateUserCommand'; |
8
|
|
|
import { CreateUserCommandHandler } from 'src/Application/HumanResource/User/Command/CreateUserCommandHandler'; |
9
|
|
|
import { IsEmailAlreadyExist } from 'src/Domain/HumanResource/User/Specification/IsEmailAlreadyExist'; |
10
|
|
|
import { EmailAlreadyExistException } from 'src/Domain/HumanResource/User/Exception/EmailAlreadyExistException'; |
11
|
|
|
import { User, UserRole } from 'src/Domain/HumanResource/User/User.entity'; |
12
|
|
|
import { UserAdministrativeRepository } from 'src/Infrastructure/HumanResource/User/Repository/UserAdministrativeRepository'; |
13
|
|
|
import { |
14
|
|
|
UserAdministrative, |
15
|
|
|
ContractType, |
16
|
|
|
WorkingTimeType |
17
|
|
|
} from 'src/Domain/HumanResource/User/UserAdministrative.entity'; |
18
|
|
|
import { UserAdministrativeMissingException } from 'src/Domain/HumanResource/User/Exception/UserAdministrativeMissingException'; |
19
|
|
|
|
20
|
|
|
describe('CreatUserCommandHandler', () => { |
21
|
|
|
const email = '[email protected]'; |
22
|
|
|
const command = new CreateUserCommand( |
23
|
|
|
'Mathieu', |
24
|
|
|
'MARCHOIS', |
25
|
|
|
'[email protected]', |
26
|
|
|
'plainPassword', |
27
|
|
|
UserRole.COOPERATOR |
28
|
|
|
); |
29
|
|
|
const admin: IUserAdministrativeCommand = { |
30
|
|
|
annualEarnings: 50000, |
31
|
|
|
healthInsurance: true, |
32
|
|
|
executivePosition: true, |
33
|
|
|
contract: ContractType.CDI, |
34
|
|
|
workingTime: WorkingTimeType.FULL_TIME, |
35
|
|
|
joiningDate: '2018-04-09', |
36
|
|
|
leavingDate: null, |
37
|
|
|
transportFee: 75.2, |
38
|
|
|
sustainableMobilityFee: 70 |
39
|
|
|
}; |
40
|
|
|
const userAdministrative = new UserAdministrative( |
41
|
|
|
5000000, |
42
|
|
|
true, |
43
|
|
|
true, |
44
|
|
|
ContractType.CDI, |
45
|
|
|
WorkingTimeType.FULL_TIME, |
46
|
|
|
'2018-04-09', |
47
|
|
|
null, |
48
|
|
|
7520, |
49
|
|
|
7000 |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
let userRepository: UserRepository; |
53
|
|
|
let userAdministrativeRepository: UserAdministrativeRepository; |
54
|
|
|
let passwordEncoder: PasswordEncoderAdapter; |
55
|
|
|
let isEmailAlreadyExist: IsEmailAlreadyExist; |
56
|
|
|
let commandHandler: CreateUserCommandHandler; |
57
|
|
|
|
58
|
|
|
beforeEach(() => { |
59
|
|
|
userRepository = mock(UserRepository); |
60
|
|
|
userAdministrativeRepository = mock(UserAdministrativeRepository); |
61
|
|
|
passwordEncoder = mock(PasswordEncoderAdapter); |
62
|
|
|
isEmailAlreadyExist = mock(IsEmailAlreadyExist); |
63
|
|
|
|
64
|
|
|
commandHandler = new CreateUserCommandHandler( |
65
|
|
|
instance(userAdministrativeRepository), |
66
|
|
|
instance(userRepository), |
67
|
|
|
instance(passwordEncoder), |
68
|
|
|
instance(isEmailAlreadyExist) |
69
|
|
|
); |
70
|
|
|
}); |
71
|
|
|
|
72
|
|
|
it('testEmailAlreadyExist', async () => { |
73
|
|
|
when(isEmailAlreadyExist.isSatisfiedBy(email)).thenResolve(true); |
74
|
|
|
|
75
|
|
|
try { |
76
|
|
|
await commandHandler.execute(command); |
77
|
|
|
} catch (e) { |
78
|
|
|
expect(e).toBeInstanceOf(EmailAlreadyExistException); |
79
|
|
|
expect(e.message).toBe( |
80
|
|
|
'human_resources.users.errors.email_already_exist' |
81
|
|
|
); |
82
|
|
|
verify(isEmailAlreadyExist.isSatisfiedBy(email)).once(); |
83
|
|
|
verify(passwordEncoder.hash('plainPassword')).never(); |
84
|
|
|
verify( |
85
|
|
|
passwordEncoder.hash('[email protected]') |
86
|
|
|
).never(); |
87
|
|
|
verify(userRepository.save(anything())).never(); |
88
|
|
|
verify(userAdministrativeRepository.save(anything())).never(); |
89
|
|
|
} |
90
|
|
|
}); |
91
|
|
|
|
92
|
|
|
it('testRegisterSuccess', async () => { |
93
|
|
|
const command1 = new CreateUserCommand( |
94
|
|
|
'Mathieu', |
95
|
|
|
'MARCHOIS', |
96
|
|
|
'[email protected]', |
97
|
|
|
'plainPassword', |
98
|
|
|
UserRole.COOPERATOR, |
99
|
|
|
admin |
100
|
|
|
); |
101
|
|
|
const createdUserAdministrative: UserAdministrative = mock( |
102
|
|
|
UserAdministrative |
103
|
|
|
); |
104
|
|
|
const createdUser: User = mock(User); |
105
|
|
|
|
106
|
|
|
when(createdUser.getId()).thenReturn( |
107
|
|
|
'fcf9a99f-0c7b-45ca-b68a-bfd79d73a49f' |
108
|
|
|
); |
109
|
|
|
when(isEmailAlreadyExist.isSatisfiedBy(email)).thenResolve(false); |
110
|
|
|
when(passwordEncoder.hash(command1.password)).thenResolve('hashPassword'); |
111
|
|
|
|
112
|
|
|
when( |
113
|
|
|
userAdministrativeRepository.save(deepEqual(userAdministrative)) |
114
|
|
|
).thenResolve(instance(createdUserAdministrative)); |
115
|
|
|
when( |
116
|
|
|
userRepository.save( |
117
|
|
|
deepEqual( |
118
|
|
|
new User( |
119
|
|
|
'Mathieu', |
120
|
|
|
'MARCHOIS', |
121
|
|
|
'[email protected]', |
122
|
|
|
'hashToken', |
123
|
|
|
'hashPassword', |
124
|
|
|
UserRole.COOPERATOR, |
125
|
|
|
instance(createdUserAdministrative) |
126
|
|
|
) |
127
|
|
|
) |
128
|
|
|
) |
129
|
|
|
).thenResolve(instance(createdUser)); |
130
|
|
|
when(passwordEncoder.hash(email + command1.password)).thenResolve( |
131
|
|
|
'hashToken' |
132
|
|
|
); |
133
|
|
|
|
134
|
|
|
expect(await commandHandler.execute(command1)).toBe( |
135
|
|
|
'fcf9a99f-0c7b-45ca-b68a-bfd79d73a49f' |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
verify(isEmailAlreadyExist.isSatisfiedBy(email)).once(); |
139
|
|
|
verify(passwordEncoder.hash('plainPassword')).once(); |
140
|
|
|
verify(passwordEncoder.hash('[email protected]')).once(); |
141
|
|
|
verify( |
142
|
|
|
userRepository.save( |
143
|
|
|
deepEqual( |
144
|
|
|
new User( |
145
|
|
|
'Mathieu', |
146
|
|
|
'MARCHOIS', |
147
|
|
|
'[email protected]', |
148
|
|
|
'hashToken', |
149
|
|
|
'hashPassword', |
150
|
|
|
UserRole.COOPERATOR, |
151
|
|
|
instance(createdUserAdministrative) |
152
|
|
|
) |
153
|
|
|
) |
154
|
|
|
) |
155
|
|
|
).once(); |
156
|
|
|
verify( |
157
|
|
|
userAdministrativeRepository.save(deepEqual(userAdministrative)) |
158
|
|
|
).once(); |
159
|
|
|
verify(createdUser.getId()).once(); |
160
|
|
|
}); |
161
|
|
|
|
162
|
|
|
it('testAccountantRegisterSuccess', async () => { |
163
|
|
|
const command1 = new CreateUserCommand( |
164
|
|
|
'Mathieu', |
165
|
|
|
'MARCHOIS', |
166
|
|
|
'[email protected]', |
167
|
|
|
'plainPassword', |
168
|
|
|
UserRole.ACCOUNTANT |
169
|
|
|
); |
170
|
|
|
|
171
|
|
|
const createdUser: User = mock(User); |
172
|
|
|
|
173
|
|
|
when(createdUser.getId()).thenReturn( |
174
|
|
|
'fcf9a99f-0c7b-45ca-b68a-bfd79d73a49f' |
175
|
|
|
); |
176
|
|
|
when(isEmailAlreadyExist.isSatisfiedBy(email)).thenResolve(false); |
177
|
|
|
when(passwordEncoder.hash(command1.password)).thenResolve('hashPassword'); |
178
|
|
|
when( |
179
|
|
|
userRepository.save( |
180
|
|
|
deepEqual( |
181
|
|
|
new User( |
182
|
|
|
'Mathieu', |
183
|
|
|
'MARCHOIS', |
184
|
|
|
'[email protected]', |
185
|
|
|
'hashToken', |
186
|
|
|
'hashPassword', |
187
|
|
|
UserRole.ACCOUNTANT, |
188
|
|
|
null |
189
|
|
|
) |
190
|
|
|
) |
191
|
|
|
) |
192
|
|
|
).thenResolve(instance(createdUser)); |
193
|
|
|
when(passwordEncoder.hash(email + command1.password)).thenResolve( |
194
|
|
|
'hashToken' |
195
|
|
|
); |
196
|
|
|
|
197
|
|
|
expect(await commandHandler.execute(command1)).toBe( |
198
|
|
|
'fcf9a99f-0c7b-45ca-b68a-bfd79d73a49f' |
199
|
|
|
); |
200
|
|
|
|
201
|
|
|
verify(isEmailAlreadyExist.isSatisfiedBy(email)).once(); |
202
|
|
|
verify(passwordEncoder.hash('plainPassword')).once(); |
203
|
|
|
verify(passwordEncoder.hash('[email protected]')).once(); |
204
|
|
|
verify( |
205
|
|
|
userRepository.save( |
206
|
|
|
deepEqual( |
207
|
|
|
new User( |
208
|
|
|
'Mathieu', |
209
|
|
|
'MARCHOIS', |
210
|
|
|
'[email protected]', |
211
|
|
|
'hashToken', |
212
|
|
|
'hashPassword', |
213
|
|
|
UserRole.ACCOUNTANT, |
214
|
|
|
null |
215
|
|
|
) |
216
|
|
|
) |
217
|
|
|
) |
218
|
|
|
).once(); |
219
|
|
|
verify(userAdministrativeRepository.save(anything())).never(); |
220
|
|
|
verify(createdUser.getId()).once(); |
221
|
|
|
}); |
222
|
|
|
|
223
|
|
|
it('testRegisterAccountantWithUserAdministrativeSuccess', async () => { |
224
|
|
|
const command1 = new CreateUserCommand( |
225
|
|
|
'Mathieu', |
226
|
|
|
'MARCHOIS', |
227
|
|
|
'[email protected]', |
228
|
|
|
'plainPassword', |
229
|
|
|
UserRole.ACCOUNTANT, |
230
|
|
|
admin |
231
|
|
|
); |
232
|
|
|
const createdUser: User = mock(User); |
233
|
|
|
|
234
|
|
|
when(createdUser.getId()).thenReturn( |
235
|
|
|
'fcf9a99f-0c7b-45ca-b68a-bfd79d73a49f' |
236
|
|
|
); |
237
|
|
|
when(isEmailAlreadyExist.isSatisfiedBy(email)).thenResolve(false); |
238
|
|
|
when(passwordEncoder.hash(command1.password)).thenResolve('hashPassword'); |
239
|
|
|
when( |
240
|
|
|
userRepository.save( |
241
|
|
|
deepEqual( |
242
|
|
|
new User( |
243
|
|
|
'Mathieu', |
244
|
|
|
'MARCHOIS', |
245
|
|
|
'[email protected]', |
246
|
|
|
'hashToken', |
247
|
|
|
'hashPassword', |
248
|
|
|
UserRole.ACCOUNTANT, |
249
|
|
|
null |
250
|
|
|
) |
251
|
|
|
) |
252
|
|
|
) |
253
|
|
|
).thenResolve(instance(createdUser)); |
254
|
|
|
when(passwordEncoder.hash(email + command1.password)).thenResolve( |
255
|
|
|
'hashToken' |
256
|
|
|
); |
257
|
|
|
|
258
|
|
|
expect(await commandHandler.execute(command1)).toBe( |
259
|
|
|
'fcf9a99f-0c7b-45ca-b68a-bfd79d73a49f' |
260
|
|
|
); |
261
|
|
|
|
262
|
|
|
verify(isEmailAlreadyExist.isSatisfiedBy(email)).once(); |
263
|
|
|
verify(passwordEncoder.hash('plainPassword')).once(); |
264
|
|
|
verify(passwordEncoder.hash('[email protected]')).once(); |
265
|
|
|
verify( |
266
|
|
|
userRepository.save( |
267
|
|
|
deepEqual( |
268
|
|
|
new User( |
269
|
|
|
'Mathieu', |
270
|
|
|
'MARCHOIS', |
271
|
|
|
'[email protected]', |
272
|
|
|
'hashToken', |
273
|
|
|
'hashPassword', |
274
|
|
|
UserRole.ACCOUNTANT, |
275
|
|
|
null |
276
|
|
|
) |
277
|
|
|
) |
278
|
|
|
) |
279
|
|
|
).once(); |
280
|
|
|
verify(userAdministrativeRepository.save(anything())).never(); |
281
|
|
|
verify(createdUser.getId()).once(); |
282
|
|
|
}); |
283
|
|
|
|
284
|
|
|
it('testMissingUserAdministrative', async () => { |
285
|
|
|
const command1 = new CreateUserCommand( |
286
|
|
|
'Mathieu', |
287
|
|
|
'MARCHOIS', |
288
|
|
|
'[email protected]', |
289
|
|
|
'plainPassword', |
290
|
|
|
UserRole.COOPERATOR |
291
|
|
|
); |
292
|
|
|
|
293
|
|
|
when(isEmailAlreadyExist.isSatisfiedBy(email)).thenResolve(false); |
294
|
|
|
|
295
|
|
|
try { |
296
|
|
|
await commandHandler.execute(command1); |
297
|
|
|
} catch (e) { |
298
|
|
|
expect(e).toBeInstanceOf(UserAdministrativeMissingException); |
299
|
|
|
expect(e.message).toBe( |
300
|
|
|
'human_resources.users.errors.user_administrative_missing' |
301
|
|
|
); |
302
|
|
|
verify(isEmailAlreadyExist.isSatisfiedBy(email)).once(); |
303
|
|
|
verify(passwordEncoder.hash(anything())).never(); |
304
|
|
|
verify(passwordEncoder.hash(anything())).never(); |
305
|
|
|
verify(userRepository.save(anything())).never(); |
306
|
|
|
verify(userAdministrativeRepository.save(anything())).never(); |
307
|
|
|
} |
308
|
|
|
}); |
309
|
|
|
}); |
310
|
|
|
|