|
1
|
|
|
import { mock, instance, when, verify, anything } from 'ts-mockito'; |
|
2
|
|
|
import { UserRepository } from 'src/Infrastructure/User/Repository/UserRepository'; |
|
3
|
|
|
import { UserLoginQueryHandler } from 'src/Application/User/Query/UserLoginQueryHandler'; |
|
4
|
|
|
import { PasswordEncoderAdapter } from 'src/Infrastructure/Adapter/PasswordEncoderAdapter'; |
|
5
|
|
|
import { UserLoginQuery } from 'src/Application/User/Query/UserLoginQuery'; |
|
6
|
|
|
import { PasswordNotMatchException } from 'src/Domain/User/Exception/PasswordNotMatchException'; |
|
7
|
|
|
import { UserView } from 'src/Application/User/View/UserView'; |
|
8
|
|
|
import { UserNotFoundException } from 'src/Domain/User/Exception/UserNotFoundException'; |
|
9
|
|
|
import { User, UserRole } from 'src/Domain/User/User.entity'; |
|
10
|
|
|
|
|
11
|
|
|
describe('UserLoginQueryHandler', () => { |
|
12
|
|
|
const email = '[email protected]'; |
|
13
|
|
|
const query = new UserLoginQuery('[email protected]', 'plainPassword'); |
|
14
|
|
|
|
|
15
|
|
|
let userRepository: UserRepository; |
|
16
|
|
|
let passwordEncoder: PasswordEncoderAdapter; |
|
17
|
|
|
let queryHandler: UserLoginQueryHandler; |
|
18
|
|
|
|
|
19
|
|
|
beforeEach(() => { |
|
20
|
|
|
userRepository = mock(UserRepository); |
|
21
|
|
|
passwordEncoder = mock(PasswordEncoderAdapter); |
|
22
|
|
|
queryHandler = new UserLoginQueryHandler( |
|
23
|
|
|
instance(userRepository), |
|
24
|
|
|
instance(passwordEncoder) |
|
25
|
|
|
); |
|
26
|
|
|
}); |
|
27
|
|
|
|
|
28
|
|
|
it('testUserNotFound', async () => { |
|
29
|
|
|
when(userRepository.findOneByEmail(email)).thenResolve(null); |
|
30
|
|
|
|
|
31
|
|
|
try { |
|
32
|
|
|
expect(await queryHandler.execute(query)).toBeUndefined(); |
|
33
|
|
|
} catch (e) { |
|
34
|
|
|
expect(e.message).toBe('users.errors.not_found'); |
|
35
|
|
|
expect(e).toBeInstanceOf(UserNotFoundException); |
|
36
|
|
|
verify(userRepository.findOneByEmail(email)).once(); |
|
37
|
|
|
verify(passwordEncoder.compare(anything(), anything())).never(); |
|
38
|
|
|
} |
|
39
|
|
|
}); |
|
40
|
|
|
|
|
41
|
|
|
it('testPasswordNotMatch', async () => { |
|
42
|
|
|
const user = mock(User); |
|
43
|
|
|
when(passwordEncoder.compare('hash', 'plainPassword')).thenResolve(false); |
|
44
|
|
|
when(userRepository.findOneByEmail(email)).thenResolve(instance(user)); |
|
45
|
|
|
when(user.getPassword()).thenReturn('hash'); |
|
46
|
|
|
|
|
47
|
|
|
try { |
|
48
|
|
|
expect(await queryHandler.execute(query)).toBeUndefined(); |
|
49
|
|
|
} catch (e) { |
|
50
|
|
|
expect(e.message).toBe('users.errors.password_not_match'); |
|
51
|
|
|
expect(e).toBeInstanceOf(PasswordNotMatchException); |
|
52
|
|
|
verify(userRepository.findOneByEmail(email)).once(); |
|
53
|
|
|
verify(passwordEncoder.compare('hash', 'plainPassword')).once(); |
|
54
|
|
|
verify(user.getPassword()).once(); |
|
55
|
|
|
} |
|
56
|
|
|
}); |
|
57
|
|
|
|
|
58
|
|
|
it('testLoginSuccess', async () => { |
|
59
|
|
|
const user = mock(User); |
|
60
|
|
|
when(userRepository.findOneByEmail(email)).thenResolve(instance(user)); |
|
61
|
|
|
when(passwordEncoder.compare('hash', 'plainPassword')).thenResolve(true); |
|
62
|
|
|
when(user.getId()).thenReturn('14984335-f5aa-402a-a170-5393bb954538'); |
|
63
|
|
|
when(user.getFirstName()).thenReturn('Mathieu'); |
|
64
|
|
|
when(user.getLastName()).thenReturn('MARCHOIS'); |
|
65
|
|
|
when(user.getEmail()).thenReturn(email); |
|
66
|
|
|
when(user.getPassword()).thenReturn('hash'); |
|
67
|
|
|
when(user.getApiToken()).thenReturn('apiToken'); |
|
68
|
|
|
when(user.getRole()).thenReturn(UserRole.PHOTOGRAPHER); |
|
69
|
|
|
|
|
70
|
|
|
expect(await queryHandler.execute(query)).toMatchObject( |
|
71
|
|
|
new UserView( |
|
72
|
|
|
'14984335-f5aa-402a-a170-5393bb954538', |
|
73
|
|
|
'Mathieu', |
|
74
|
|
|
'MARCHOIS', |
|
75
|
|
|
email, |
|
76
|
|
|
UserRole.PHOTOGRAPHER, |
|
77
|
|
|
'apiToken' |
|
78
|
|
|
) |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
verify(userRepository.findOneByEmail(email)).once(); |
|
82
|
|
|
verify(passwordEncoder.compare('hash', 'plainPassword')).once(); |
|
83
|
|
|
verify(user.getId()).once(); |
|
84
|
|
|
verify(user.getFirstName()).once(); |
|
85
|
|
|
verify(user.getLastName()).once(); |
|
86
|
|
|
verify(user.getEmail()).once(); |
|
87
|
|
|
verify(user.getRole()).once(); |
|
88
|
|
|
verify(user.getPassword()).once(); |
|
89
|
|
|
verify(user.getApiToken()).once(); |
|
90
|
|
|
}); |
|
91
|
|
|
}); |
|
92
|
|
|
|