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 {CreateUserCommand} from 'src/Application/HumanResource/User/Command/CreateUserCommand'; |
5
|
|
|
import {CreateUserCommandHandler} from 'src/Application/HumanResource/User/Command/CreateUserCommandHandler'; |
6
|
|
|
import {IsEmailAlreadyExist} from 'src/Domain/HumanResource/User/Specification/IsEmailAlreadyExist'; |
7
|
|
|
import {EmailAlreadyExistException} from 'src/Domain/HumanResource/User/Exception/EmailAlreadyExistException'; |
8
|
|
|
import {User, UserRole} from 'src/Domain/HumanResource/User/User.entity'; |
9
|
|
|
import {DateUtilsAdapter} from 'src/Infrastructure/Adapter/DateUtilsAdapter'; |
10
|
|
|
import {EntryDateMissingException} from 'src/Domain/HumanResource/User/Exception/EntryDateMissingException'; |
11
|
|
|
|
12
|
|
|
describe('CreatUserCommandHandler', () => { |
13
|
|
|
const email = '[email protected]'; |
14
|
|
|
const command = new CreateUserCommand( |
15
|
|
|
'Mathieu', |
16
|
|
|
'MARCHOIS', |
17
|
|
|
'[email protected]', |
18
|
|
|
'plainPassword', |
19
|
|
|
UserRole.COOPERATOR, |
20
|
|
|
new Date('2019-09-12') |
21
|
|
|
); |
22
|
|
|
|
23
|
|
|
let userRepository: UserRepository; |
24
|
|
|
let passwordEncoder: PasswordEncoderAdapter; |
25
|
|
|
let dateUtilsAdapter: DateUtilsAdapter; |
26
|
|
|
let isEmailAlreadyExist: IsEmailAlreadyExist; |
27
|
|
|
let commandHandler: CreateUserCommandHandler; |
28
|
|
|
|
29
|
|
|
beforeEach(() => { |
30
|
|
|
userRepository = mock(UserRepository); |
31
|
|
|
passwordEncoder = mock(PasswordEncoderAdapter); |
32
|
|
|
dateUtilsAdapter = mock(DateUtilsAdapter); |
33
|
|
|
isEmailAlreadyExist = mock(IsEmailAlreadyExist); |
34
|
|
|
|
35
|
|
|
commandHandler = new CreateUserCommandHandler( |
36
|
|
|
instance(userRepository), |
37
|
|
|
instance(passwordEncoder), |
38
|
|
|
instance(dateUtilsAdapter), |
39
|
|
|
instance(isEmailAlreadyExist) |
40
|
|
|
); |
41
|
|
|
}); |
42
|
|
|
|
43
|
|
|
it('testEmailAlreadyExist', async () => { |
44
|
|
|
when(isEmailAlreadyExist.isSatisfiedBy(email)).thenResolve(true); |
45
|
|
|
|
46
|
|
|
try { |
47
|
|
|
await commandHandler.execute(command); |
48
|
|
|
} catch (e) { |
49
|
|
|
expect(e).toBeInstanceOf(EmailAlreadyExistException); |
50
|
|
|
expect(e.message).toBe('user.errors.email_already_exist'); |
51
|
|
|
verify(isEmailAlreadyExist.isSatisfiedBy(email)).once(); |
52
|
|
|
verify(passwordEncoder.hash('plainPassword')).never(); |
53
|
|
|
verify(dateUtilsAdapter.format(anything(), anything())).never(); |
54
|
|
|
verify( |
55
|
|
|
passwordEncoder.hash('[email protected]') |
56
|
|
|
).never(); |
57
|
|
|
verify( |
58
|
|
|
userRepository.save( |
59
|
|
|
deepEqual( |
60
|
|
|
new User( |
61
|
|
|
'Mathieu', |
62
|
|
|
'MARCHOIS', |
63
|
|
|
'[email protected]', |
64
|
|
|
'hashToken', |
65
|
|
|
'hashPassword', |
66
|
|
|
UserRole.COOPERATOR, |
67
|
|
|
'2019-09-12' |
68
|
|
|
) |
69
|
|
|
) |
70
|
|
|
) |
71
|
|
|
).never(); |
72
|
|
|
} |
73
|
|
|
}); |
74
|
|
|
|
75
|
|
|
it('testCooperatorEntryDateMissing', async () => { |
76
|
|
|
const command3 = new CreateUserCommand( |
77
|
|
|
'Mathieu', |
78
|
|
|
'MARCHOIS', |
79
|
|
|
'[email protected]', |
80
|
|
|
'plainPassword', |
81
|
|
|
UserRole.COOPERATOR |
82
|
|
|
); |
83
|
|
|
when(isEmailAlreadyExist.isSatisfiedBy(email)).thenResolve(false); |
84
|
|
|
|
85
|
|
|
try { |
86
|
|
|
await commandHandler.execute(command3); |
87
|
|
|
} catch (e) { |
88
|
|
|
expect(e).toBeInstanceOf(EntryDateMissingException); |
89
|
|
|
expect(e.message).toBe('user.errors.entry_date_missing'); |
90
|
|
|
verify(isEmailAlreadyExist.isSatisfiedBy(email)).once(); |
91
|
|
|
verify(passwordEncoder.hash('plainPassword')).never(); |
92
|
|
|
verify(dateUtilsAdapter.format(anything(), anything())).never(); |
93
|
|
|
verify( |
94
|
|
|
passwordEncoder.hash('[email protected]') |
95
|
|
|
).never(); |
96
|
|
|
verify( |
97
|
|
|
userRepository.save( |
98
|
|
|
deepEqual( |
99
|
|
|
new User( |
100
|
|
|
'Mathieu', |
101
|
|
|
'MARCHOIS', |
102
|
|
|
'[email protected]', |
103
|
|
|
'hashToken', |
104
|
|
|
'hashPassword', |
105
|
|
|
UserRole.COOPERATOR |
106
|
|
|
) |
107
|
|
|
) |
108
|
|
|
) |
109
|
|
|
).never(); |
110
|
|
|
} |
111
|
|
|
}); |
112
|
|
|
|
113
|
|
|
it('testRegisterSuccess', async () => { |
114
|
|
|
const createdUser: User = mock(User); |
115
|
|
|
|
116
|
|
|
when(createdUser.getId()).thenReturn( |
117
|
|
|
'fcf9a99f-0c7b-45ca-b68a-bfd79d73a49f' |
118
|
|
|
); |
119
|
|
|
when(isEmailAlreadyExist.isSatisfiedBy(email)).thenResolve(false); |
120
|
|
|
when(passwordEncoder.hash(command.password)).thenResolve('hashPassword'); |
121
|
|
|
when( |
122
|
|
|
dateUtilsAdapter.format(deepEqual(new Date('2019-09-12')), 'y-MM-dd') |
123
|
|
|
).thenReturn('2019-09-12'); |
124
|
|
|
when( |
125
|
|
|
userRepository.save( |
126
|
|
|
deepEqual( |
127
|
|
|
new User( |
128
|
|
|
'Mathieu', |
129
|
|
|
'MARCHOIS', |
130
|
|
|
'[email protected]', |
131
|
|
|
'hashToken', |
132
|
|
|
'hashPassword', |
133
|
|
|
UserRole.COOPERATOR, |
134
|
|
|
'2019-09-12' |
135
|
|
|
) |
136
|
|
|
) |
137
|
|
|
) |
138
|
|
|
).thenResolve(instance(createdUser)); |
139
|
|
|
when(passwordEncoder.hash(email + command.password)).thenResolve( |
140
|
|
|
'hashToken' |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
expect(await commandHandler.execute(command)).toBe( |
144
|
|
|
'fcf9a99f-0c7b-45ca-b68a-bfd79d73a49f' |
145
|
|
|
); |
146
|
|
|
|
147
|
|
|
verify(isEmailAlreadyExist.isSatisfiedBy(email)).once(); |
148
|
|
|
verify(passwordEncoder.hash('plainPassword')).once(); |
149
|
|
|
verify( |
150
|
|
|
dateUtilsAdapter.format(deepEqual(new Date('2019-09-12')), 'y-MM-dd') |
151
|
|
|
).once(); |
152
|
|
|
verify(passwordEncoder.hash('[email protected]')).once(); |
153
|
|
|
verify( |
154
|
|
|
userRepository.save( |
155
|
|
|
deepEqual( |
156
|
|
|
new User( |
157
|
|
|
'Mathieu', |
158
|
|
|
'MARCHOIS', |
159
|
|
|
'[email protected]', |
160
|
|
|
'hashToken', |
161
|
|
|
'hashPassword', |
162
|
|
|
UserRole.COOPERATOR, |
163
|
|
|
'2019-09-12' |
164
|
|
|
) |
165
|
|
|
) |
166
|
|
|
) |
167
|
|
|
).once(); |
168
|
|
|
verify(createdUser.getId()).once(); |
169
|
|
|
}); |
170
|
|
|
|
171
|
|
|
it('testRegisterWithoutEntryDateSuccess', async () => { |
172
|
|
|
const command2 = new CreateUserCommand( |
173
|
|
|
'Mathieu', |
174
|
|
|
'MARCHOIS', |
175
|
|
|
'[email protected]', |
176
|
|
|
'plainPassword', |
177
|
|
|
UserRole.ACCOUNTANT |
178
|
|
|
); |
179
|
|
|
const createdUser: User = mock(User); |
180
|
|
|
|
181
|
|
|
when(createdUser.getId()).thenReturn( |
182
|
|
|
'fcf9a99f-0c7b-45ca-b68a-bfd79d73a49f' |
183
|
|
|
); |
184
|
|
|
when(isEmailAlreadyExist.isSatisfiedBy(email)).thenResolve(false); |
185
|
|
|
when(passwordEncoder.hash(command.password)).thenResolve('hashPassword'); |
186
|
|
|
when( |
187
|
|
|
userRepository.save( |
188
|
|
|
deepEqual( |
189
|
|
|
new User( |
190
|
|
|
'Mathieu', |
191
|
|
|
'MARCHOIS', |
192
|
|
|
'[email protected]', |
193
|
|
|
'hashToken', |
194
|
|
|
'hashPassword', |
195
|
|
|
UserRole.ACCOUNTANT, |
196
|
|
|
null |
197
|
|
|
) |
198
|
|
|
) |
199
|
|
|
) |
200
|
|
|
).thenResolve(instance(createdUser)); |
201
|
|
|
when(passwordEncoder.hash(email + command.password)).thenResolve( |
202
|
|
|
'hashToken' |
203
|
|
|
); |
204
|
|
|
|
205
|
|
|
expect(await commandHandler.execute(command2)).toBe( |
206
|
|
|
'fcf9a99f-0c7b-45ca-b68a-bfd79d73a49f' |
207
|
|
|
); |
208
|
|
|
|
209
|
|
|
verify(isEmailAlreadyExist.isSatisfiedBy(email)).once(); |
210
|
|
|
verify(passwordEncoder.hash('plainPassword')).once(); |
211
|
|
|
verify(dateUtilsAdapter.format(anything(), anything())).never(); |
212
|
|
|
verify(passwordEncoder.hash('[email protected]')).once(); |
213
|
|
|
verify( |
214
|
|
|
userRepository.save( |
215
|
|
|
deepEqual( |
216
|
|
|
new User( |
217
|
|
|
'Mathieu', |
218
|
|
|
'MARCHOIS', |
219
|
|
|
'[email protected]', |
220
|
|
|
'hashToken', |
221
|
|
|
'hashPassword', |
222
|
|
|
UserRole.ACCOUNTANT, |
223
|
|
|
null |
224
|
|
|
) |
225
|
|
|
) |
226
|
|
|
) |
227
|
|
|
).once(); |
228
|
|
|
verify(createdUser.getId()).once(); |
229
|
|
|
}); |
230
|
|
|
}); |
231
|
|
|
|