1
|
|
|
import {mock, instance, when, verify, anything, anyString} from 'ts-mockito'; |
2
|
|
|
import {UserRepository} from 'src/Infrastructure/HumanResource/User/Repository/UserRepository'; |
3
|
|
|
import {PasswordEncoderAdapter} from 'src/Infrastructure/Adapter/PasswordEncoderAdapter'; |
4
|
|
|
import {IsEmailAlreadyExist} from 'src/Domain/HumanResource/User/Specification/IsEmailAlreadyExist'; |
5
|
|
|
import {EmailAlreadyExistException} from 'src/Domain/HumanResource/User/Exception/EmailAlreadyExistException'; |
6
|
|
|
import {User} from 'src/Domain/HumanResource/User/User.entity'; |
7
|
|
|
import {UpdateProfileCommandHandler} from './UpdateProfileCommandHandler'; |
8
|
|
|
import {UpdateProfileCommand} from './UpdateProfileCommand'; |
9
|
|
|
|
10
|
|
|
describe('UpdateProfileCommandHandler', () => { |
11
|
|
|
const email = '[email protected]'; |
12
|
|
|
|
13
|
|
|
let userRepository: UserRepository; |
14
|
|
|
let passwordEncoder: PasswordEncoderAdapter; |
15
|
|
|
let isEmailAlreadyExist: IsEmailAlreadyExist; |
16
|
|
|
let commandHandler: UpdateProfileCommandHandler; |
17
|
|
|
|
18
|
|
|
beforeEach(() => { |
19
|
|
|
userRepository = mock(UserRepository); |
20
|
|
|
passwordEncoder = mock(PasswordEncoderAdapter); |
21
|
|
|
isEmailAlreadyExist = mock(IsEmailAlreadyExist); |
22
|
|
|
|
23
|
|
|
commandHandler = new UpdateProfileCommandHandler( |
24
|
|
|
instance(userRepository), |
25
|
|
|
instance(passwordEncoder), |
26
|
|
|
instance(isEmailAlreadyExist) |
27
|
|
|
); |
28
|
|
|
}); |
29
|
|
|
|
30
|
|
|
it('testEmailAlreadyExist', async () => { |
31
|
|
|
const user = mock(User); |
32
|
|
|
const command = new UpdateProfileCommand( |
33
|
|
|
instance(user), |
34
|
|
|
'Mathieu', |
35
|
|
|
'Marchois', |
36
|
|
|
'[email protected]' |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
when(user.getEmail()).thenReturn('[email protected]'); |
40
|
|
|
when(isEmailAlreadyExist.isSatisfiedBy(email)).thenResolve(true); |
41
|
|
|
|
42
|
|
|
try { |
43
|
|
|
await commandHandler.execute(command); |
44
|
|
|
} catch (e) { |
45
|
|
|
expect(e).toBeInstanceOf(EmailAlreadyExistException); |
46
|
|
|
expect(e.message).toBe('user.errors.email_already_exist'); |
47
|
|
|
verify(isEmailAlreadyExist.isSatisfiedBy(email)).once(); |
48
|
|
|
verify(passwordEncoder.hash(anything())).never(); |
49
|
|
|
verify(userRepository.save(anything())).never(); |
50
|
|
|
} |
51
|
|
|
}); |
52
|
|
|
|
53
|
|
|
it('testUpdateWithoutPassword', async () => { |
54
|
|
|
const user = mock(User); |
55
|
|
|
const command = new UpdateProfileCommand( |
56
|
|
|
instance(user), |
57
|
|
|
'Mathieu', |
58
|
|
|
'Marchois', |
59
|
|
|
'[email protected]' |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
when(isEmailAlreadyExist.isSatisfiedBy(email)).thenResolve(false); |
63
|
|
|
|
64
|
|
|
// Command return nothing |
65
|
|
|
expect(await commandHandler.execute(command)).toBeUndefined(); |
66
|
|
|
|
67
|
|
|
verify(user.update('Mathieu', 'Marchois', '[email protected]')).once(); |
68
|
|
|
verify( |
69
|
|
|
user.update('Mathieu', 'Marchois', '[email protected]') |
70
|
|
|
).calledBefore(userRepository.save(instance(user))); |
71
|
|
|
verify(user.updatePassword(anyString())).never(); |
72
|
|
|
verify(isEmailAlreadyExist.isSatisfiedBy(email)).once(); |
73
|
|
|
verify(userRepository.save(instance(user))).once(); |
74
|
|
|
}); |
75
|
|
|
|
76
|
|
|
it('testUpdateWithPassword', async () => { |
77
|
|
|
const user = mock(User); |
78
|
|
|
const command = new UpdateProfileCommand( |
79
|
|
|
instance(user), |
80
|
|
|
'Mathieu', |
81
|
|
|
'Marchois', |
82
|
|
|
'[email protected]', |
83
|
|
|
'azerty' |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
when(isEmailAlreadyExist.isSatisfiedBy(email)).thenResolve(false); |
87
|
|
|
when(passwordEncoder.hash('azerty')).thenResolve('azertyCrypted'); |
88
|
|
|
|
89
|
|
|
// Command return nothing |
90
|
|
|
expect(await commandHandler.execute(command)).toBeUndefined(); |
91
|
|
|
|
92
|
|
|
verify(user.update('Mathieu', 'Marchois', '[email protected]')).once(); |
93
|
|
|
verify(user.updatePassword('azertyCrypted')).once(); |
94
|
|
|
verify(user.updatePassword('azertyCrypted')).calledBefore( |
95
|
|
|
userRepository.save(instance(user)) |
96
|
|
|
); |
97
|
|
|
verify(passwordEncoder.hash('azerty')).once(); |
98
|
|
|
verify( |
99
|
|
|
user.update('Mathieu', 'Marchois', '[email protected]') |
100
|
|
|
).calledBefore(userRepository.save(instance(user))); |
101
|
|
|
verify(isEmailAlreadyExist.isSatisfiedBy(email)).once(); |
102
|
|
|
verify(userRepository.save(instance(user))).once(); |
103
|
|
|
}); |
104
|
|
|
}); |
105
|
|
|
|