1
1
import { TypeormMock } from '@lomray/microservice-helpers/mocks' ;
2
2
import { waitResult } from '@lomray/microservice-helpers/test-helpers' ;
3
3
import { expect } from 'chai' ;
4
+ import sinon from 'sinon' ;
5
+ import * as remoteConfig from '@config/remote' ;
4
6
import User from '@entities/user' ;
7
+ import TClearUserTokens from '@interfaces/clear-user-tokens' ;
5
8
import UserRepository from '@repositories/user' ;
6
9
import ChangePassword from '@services/change-password' ;
7
10
8
11
describe ( 'services/change-password' , ( ) => {
12
+ const sandbox = sinon . createSandbox ( ) ;
9
13
const repository = TypeormMock . entityManager . getCustomRepository ( UserRepository ) ;
14
+ let clearUserTokensStub : sinon . SinonStub ;
10
15
const userId = 'user-id' ;
11
16
const newPassword = 'new-password' ;
12
17
const oldPassword = 'old-password' ;
@@ -21,68 +26,177 @@ describe('services/change-password', () => {
21
26
} ) ;
22
27
23
28
beforeEach ( ( ) => {
29
+ clearUserTokensStub = sandbox . stub ( repository , 'clearUserTokens' ) ;
24
30
TypeormMock . sandbox . reset ( ) ;
25
31
} ) ;
26
32
27
- it ( 'should throw error: user not found' , async ( ) => {
28
- const service = ChangePassword . init ( {
29
- userId,
30
- repository,
33
+ afterEach ( ( ) => {
34
+ sandbox . restore ( ) ;
35
+ } ) ;
36
+
37
+ describe ( 'init' , ( ) => {
38
+ it ( 'should correctly build service' , ( ) => {
39
+ expect (
40
+ ChangePassword . init ( {
41
+ userId,
42
+ repository,
43
+ } ) ,
44
+ ) . to . instanceof ( ChangePassword ) ;
31
45
} ) ;
46
+ } ) ;
32
47
33
- TypeormMock . entityManager . findOne . resolves ( undefined ) ;
48
+ describe ( 'change' , ( ) => {
49
+ it ( 'should throw error: user not found' , async ( ) => {
50
+ const service = ChangePassword . init ( {
51
+ userId,
52
+ repository,
53
+ } ) ;
34
54
35
- expect ( await waitResult ( service . change ( newPassword , oldPassword ) ) ) . to . throw ( 'User not found' ) ;
36
- } ) ;
55
+ TypeormMock . entityManager . findOne . resolves ( undefined ) ;
37
56
38
- it ( 'should throw error: oldPassword or confirmation not provided' , async ( ) => {
39
- const service = ChangePassword . init ( {
40
- userId,
41
- repository,
57
+ expect ( await waitResult ( service . change ( newPassword , oldPassword ) ) ) . to . throw ( 'User not found' ) ;
42
58
} ) ;
43
59
44
- expect ( await waitResult ( service . change ( newPassword ) ) ) . to . throw (
45
- 'Either of confirm methods should be provided' ,
46
- ) ;
47
- } ) ;
60
+ it ( 'should throw error: oldPassword or confirmation not provided' , async ( ) => {
61
+ const service = ChangePassword . init ( {
62
+ userId,
63
+ repository,
64
+ } ) ;
48
65
49
- it ( 'should throw error: invalid old password' , async ( ) => {
50
- const service = ChangePassword . init ( {
51
- userId,
52
- repository,
66
+ expect ( await waitResult ( service . change ( newPassword ) ) ) . to . throw (
67
+ 'Either of confirm methods should be provided' ,
68
+ ) ;
53
69
} ) ;
54
70
55
- TypeormMock . entityManager . findOne . resolves ( mockUser ) ;
71
+ it ( 'should throw error: invalid old password' , async ( ) => {
72
+ const service = ChangePassword . init ( {
73
+ userId,
74
+ repository,
75
+ } ) ;
56
76
57
- expect ( await waitResult ( service . change ( newPassword , 'invalid-password' ) ) ) . to . throw (
58
- 'Invalid old password' ,
59
- ) ;
60
- } ) ;
77
+ TypeormMock . entityManager . findOne . resolves ( mockUser ) ;
78
+
79
+ expect ( await waitResult ( service . change ( newPassword , 'invalid-password' ) ) ) . to . throw (
80
+ 'Invalid old password' ,
81
+ ) ;
82
+ } ) ;
83
+
84
+ it ( 'should throw error: invalid confirmation' , async ( ) => {
85
+ const service = ChangePassword . init ( {
86
+ userId,
87
+ repository,
88
+ isConfirmed : ( ) => false ,
89
+ } ) ;
61
90
62
- it ( 'should throw error: invalid confirmation' , async ( ) => {
63
- const service = ChangePassword . init ( {
64
- userId,
65
- repository,
66
- isConfirmed : ( ) => false ,
91
+ TypeormMock . entityManager . findOne . resolves ( mockUser ) ;
92
+
93
+ expect ( await waitResult ( service . change ( newPassword ) ) ) . to . throw ( 'Invalid confirmation code' ) ;
67
94
} ) ;
68
95
69
- TypeormMock . entityManager . findOne . resolves ( mockUser ) ;
96
+ it ( 'should successful change password' , async ( ) => {
97
+ const service = ChangePassword . init ( {
98
+ userId,
99
+ repository,
100
+ } ) ;
101
+
102
+ // Private method
103
+ // @ts -ignore
104
+ const handleClearUserTokensStub = sandbox . stub ( service , 'handleClearUserTokens' ) ;
105
+
106
+ TypeormMock . entityManager . findOne . resolves ( mockUser ) ;
107
+
108
+ await service . change ( newPassword , oldPassword ) ;
109
+
110
+ const [ , user ] = TypeormMock . entityManager . save . firstCall . args ;
111
+ const [ argUserId ] = handleClearUserTokensStub . firstCall . args ;
70
112
71
- expect ( await waitResult ( service . change ( newPassword ) ) ) . to . throw ( 'Invalid confirmation code' ) ;
113
+ expect ( repository . isValidPassword ( user as User , newPassword ) ) . to . true ;
114
+ expect ( handleClearUserTokensStub ) . to . calledOnce ;
115
+ expect ( argUserId ) . to . equal ( userId ) ;
116
+ } ) ;
72
117
} ) ;
73
118
74
- it ( 'should successful change password' , async ( ) => {
75
- const service = ChangePassword . init ( {
76
- userId,
77
- repository,
119
+ describe ( 'handleClearUserTokens' , ( ) => {
120
+ it ( 'should stop validation: type is undefined or none' , async ( ) => {
121
+ for ( const type of [ undefined , 'none' ] ) {
122
+ const service = ChangePassword . init ( {
123
+ userId,
124
+ repository,
125
+ clearTokensType : type as TClearUserTokens ,
126
+ } ) ;
127
+
128
+ await service [ 'handleClearUserTokens' ] ( userId ) ;
129
+
130
+ expect ( clearUserTokensStub ) . to . not . called ;
131
+ }
78
132
} ) ;
79
133
80
- TypeormMock . entityManager . findOne . resolves ( mockUser ) ;
134
+ it ( 'should stop validation: type is undefined or none' , async ( ) => {
135
+ for ( const type of [ undefined , 'none' ] ) {
136
+ const service = ChangePassword . init ( {
137
+ userId,
138
+ repository,
139
+ } ) ;
140
+
141
+ const remoteConfigStub = sinon . stub ( ) . resolves ( {
142
+ changePasswordClearTokensType : type ,
143
+ } ) ;
81
144
82
- await service . change ( newPassword , oldPassword ) ;
145
+ // @ts -ignore
146
+ sinon . replace ( remoteConfig , 'default' , remoteConfigStub ) ;
83
147
84
- const [ , user ] = TypeormMock . entityManager . save . firstCall . args ;
148
+ await service [ 'handleClearUserTokens' ] ( userId ) ;
85
149
86
- expect ( repository . isValidPassword ( user as User , newPassword ) ) . to . true ;
150
+ expect ( clearUserTokensStub ) . to . not . called ;
151
+
152
+ sinon . restore ( ) ;
153
+ }
154
+ } ) ;
155
+
156
+ it ( 'should call clear all user tokens: with user id' , async ( ) => {
157
+ const service = ChangePassword . init ( {
158
+ userId,
159
+ repository,
160
+ clearTokensType : 'all' ,
161
+ } ) ;
162
+
163
+ await service [ 'handleClearUserTokens' ] ( userId ) ;
164
+
165
+ const [ argUserId ] = clearUserTokensStub . firstCall . args ;
166
+
167
+ expect ( clearUserTokensStub ) . to . calledOnce ;
168
+ expect ( argUserId ) . to . equal ( userId ) ;
169
+ } ) ;
170
+
171
+ it ( 'should call clear rest user tokens: with user id' , async ( ) => {
172
+ const token = 'token-id' ;
173
+
174
+ const service = ChangePassword . init ( {
175
+ userId,
176
+ repository,
177
+ clearTokensType : 'rest' ,
178
+ currentToken : token ,
179
+ } ) ;
180
+
181
+ await service [ 'handleClearUserTokens' ] ( userId ) ;
182
+
183
+ const [ argUserId , argToken ] = clearUserTokensStub . firstCall . args ;
184
+
185
+ expect ( clearUserTokensStub ) . to . calledOnce ;
186
+ expect ( argUserId ) . to . equal ( userId ) ;
187
+ expect ( argToken ) . to . equal ( token ) ;
188
+ } ) ;
189
+
190
+ it ( 'should skip rest tokens clean up: current token not passed' , async ( ) => {
191
+ const service = ChangePassword . init ( {
192
+ userId,
193
+ repository,
194
+ clearTokensType : 'rest' ,
195
+ } ) ;
196
+
197
+ await service [ 'handleClearUserTokens' ] ( userId ) ;
198
+
199
+ expect ( clearUserTokensStub ) . to . not . called ;
200
+ } ) ;
87
201
} ) ;
88
202
} ) ;
0 commit comments