-
Notifications
You must be signed in to change notification settings - Fork 549
Expand file tree
/
Copy pathOtpServiceImplTest.java
More file actions
259 lines (235 loc) · 10.8 KB
/
OtpServiceImplTest.java
File metadata and controls
259 lines (235 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* Licensed under the Apache License, Version 2.0 (the “License”);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.crapi.service.Impl;
import com.crapi.constant.UserMessage;
import com.crapi.entity.Otp;
import com.crapi.entity.User;
import com.crapi.enums.ERole;
import com.crapi.enums.EStatus;
import com.crapi.exception.EntityNotFoundException;
import com.crapi.model.CRAPIResponse;
import com.crapi.model.ForgetPassword;
import com.crapi.model.OtpForm;
import com.crapi.repository.OtpRepository;
import com.crapi.repository.UserRepository;
import com.crapi.utils.SMTPMailServer;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.http.HttpStatus;
import org.springframework.security.crypto.password.PasswordEncoder;
@RunWith(MockitoJUnitRunner.class)
public class OtpServiceImplTest {
@InjectMocks private OtpServiceImpl otpService;
@Mock private OtpRepository otpRepository;
@Mock private UserRepository userRepository;
@Mock private PasswordEncoder encoder;
@Mock private SMTPMailServer smtpMailServer;
@Test
public void invalidateOtpSuccess() {
Otp otp = getDummyOtp();
Mockito.when(otpRepository.save(Mockito.any())).thenReturn(otp);
Assertions.assertTrue(otpService.invalidateOtp(otp));
}
@Test
public void validateOTPAndEmailSuccess() {
Otp otp = getDummyOtp();
OtpForm otpForm = getDummyOtpForm();
otpForm.setOtp(otp.getOtp());
otp.setStatus(EStatus.ACTIVE.toString());
otp.getUser().setEmail(otpForm.getEmail());
Assertions.assertTrue(otpService.validateOTPAndEmail(otp, otpForm));
}
@Test(expected = EntityNotFoundException.class)
public void validateOTPAndEmailThrowsExceptionWhenOtpNull() {
Otp otp = null;
OtpForm otpForm = getDummyOtpForm();
otpService.validateOTPAndEmail(otp, otpForm);
}
@Test
public void validateOTPAndEmailFailureWhenFormAndOtpNotMatch() {
Otp otp = getDummyOtp();
OtpForm otpForm = getDummyOtpForm();
Assertions.assertFalse(otpService.validateOTPAndEmail(otp, otpForm));
}
@Test(expected = EntityNotFoundException.class)
public void validateOtpThrowsExceptionWhenUserNotFound() {
Mockito.when(userRepository.findByEmail(Mockito.anyString())).thenReturn(null);
otpService.validateOtp(getDummyOtpForm());
}
@Test
public void validateOtpSuccess() {
Otp otp = getDummyOtp();
User user = getDummyUser();
OtpForm otpForm = getDummyOtpForm();
otpForm.setOtp(otp.getOtp());
otp.setStatus(EStatus.ACTIVE.toString());
otp.getUser().setEmail(otpForm.getEmail());
Mockito.when(userRepository.findByEmail(Mockito.anyString())).thenReturn(user);
Mockito.when(encoder.encode(Mockito.anyString())).thenReturn("DUMMY ENCODE");
Mockito.when(otpRepository.findByUser(Mockito.any())).thenReturn(otp);
CRAPIResponse crapiAPIResponse = otpService.validateOtp(otpForm);
Mockito.verify(userRepository, Mockito.times(1)).save(Mockito.any());
Mockito.verify(otpRepository, Mockito.times(1)).save(Mockito.any());
Assertions.assertEquals(UserMessage.OTP_VERIFIED_SUCCESS, crapiAPIResponse.getMessage());
Assertions.assertEquals(HttpStatus.OK.value(), crapiAPIResponse.getStatus());
}
@Test
public void validateOtpFailure() {
Otp otp = getDummyOtp();
User user = getDummyUser();
OtpForm otpForm = getDummyOtpForm();
Mockito.when(userRepository.findByEmail(Mockito.anyString())).thenReturn(user);
Mockito.when(otpRepository.findByUser(Mockito.any())).thenReturn(otp);
CRAPIResponse crapiAPIResponse = otpService.validateOtp(otpForm);
Mockito.verify(userRepository, Mockito.times(0)).save(Mockito.any());
Mockito.verify(otpRepository, Mockito.times(1)).save(Mockito.any());
Assertions.assertEquals(UserMessage.INVALID_OTP, crapiAPIResponse.getMessage());
Assertions.assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), crapiAPIResponse.getStatus());
}
@Test
public void secureValidateOtpSuccess() {
Otp otp = getDummyOtp();
User user = getDummyUser();
OtpForm otpForm = getDummyOtpForm();
otpForm.setOtp(otp.getOtp());
otp.setStatus(EStatus.ACTIVE.toString());
otp.getUser().setEmail(otpForm.getEmail());
Mockito.when(userRepository.findByEmail(Mockito.anyString())).thenReturn(user);
Mockito.when(otpRepository.findByUser(Mockito.any())).thenReturn(otp);
CRAPIResponse crapiAPIResponse = otpService.secureValidateOtp(otpForm);
Mockito.verify(userRepository, Mockito.times(1)).save(Mockito.any());
Mockito.verify(otpRepository, Mockito.times(1)).save(Mockito.any());
Assertions.assertEquals(UserMessage.OTP_VERIFIED_SUCCESS, crapiAPIResponse.getMessage());
Assertions.assertEquals(HttpStatus.OK.value(), crapiAPIResponse.getStatus());
}
@Test
public void secureValidateOtpFailsWithInvalidOtpWhenOtpCountIs9() {
Otp otp = getDummyOtp();
User user = getDummyUser();
OtpForm otpForm = getDummyOtpForm();
otp.setCount(9);
Mockito.when(userRepository.findByEmail(Mockito.anyString())).thenReturn(user);
Mockito.when(otpRepository.findByUser(Mockito.any())).thenReturn(otp);
CRAPIResponse crapiAPIResponse = otpService.secureValidateOtp(otpForm);
Mockito.verify(userRepository, Mockito.times(0)).save(Mockito.any());
Mockito.verify(otpRepository, Mockito.times(2)).save(Mockito.any());
Assertions.assertEquals(UserMessage.EXCEED_NUMBER_OF_ATTEMPS, crapiAPIResponse.getMessage());
Assertions.assertEquals(503, crapiAPIResponse.getStatus());
}
@Test
public void secureValidateOtpFailsWithInvalidOtpWhenOtpCountIsGreaterThan9() {
Otp otp = getDummyOtp();
User user = getDummyUser();
OtpForm otpForm = getDummyOtpForm();
otp.setCount(10);
Mockito.when(userRepository.findByEmail(Mockito.anyString())).thenReturn(user);
Mockito.when(otpRepository.findByUser(Mockito.any())).thenReturn(otp);
CRAPIResponse crapiAPIResponse = otpService.secureValidateOtp(otpForm);
Mockito.verify(userRepository, Mockito.times(0)).save(Mockito.any());
Mockito.verify(otpRepository, Mockito.times(2)).save(Mockito.any());
Assertions.assertEquals(UserMessage.ERROR, crapiAPIResponse.getMessage());
Assertions.assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), crapiAPIResponse.getStatus());
}
@Test
public void secureValidateOtpFailsWithDefaultCondition() {
Otp otp = getDummyOtp();
User user = getDummyUser();
OtpForm otpForm = getDummyOtpForm();
Mockito.when(userRepository.findByEmail(Mockito.anyString())).thenReturn(user);
Mockito.when(otpRepository.findByUser(Mockito.any())).thenReturn(otp);
CRAPIResponse crapiAPIResponse = otpService.secureValidateOtp(otpForm);
Mockito.verify(userRepository, Mockito.times(0)).save(Mockito.any());
Mockito.verify(otpRepository, Mockito.times(1)).save(Mockito.any());
Assertions.assertEquals(UserMessage.INVALID_OTP, crapiAPIResponse.getMessage());
Assertions.assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), crapiAPIResponse.getStatus());
}
@Test(expected = EntityNotFoundException.class)
public void secureValidateOtpThrowsExceptionWhenUserNotFound() {
Otp otp = getDummyOtp();
OtpForm otpForm = getDummyOtpForm();
Mockito.when(userRepository.findByEmail(Mockito.anyString())).thenReturn(null);
otpService.secureValidateOtp(otpForm);
}
@Test
public void generateOtpFailWhenUserNotFound() {
ForgetPassword forgetPassword = getDummyForgetPassword();
String expectedMessage = UserMessage.EMAIL_NOT_REGISTERED + forgetPassword.getEmail();
Mockito.when(userRepository.findByEmail(Mockito.anyString())).thenReturn(null);
CRAPIResponse crapiAPIResponse = otpService.generateOtp(getDummyForgetPassword());
Assertions.assertEquals(expectedMessage, crapiAPIResponse.getMessage());
Assertions.assertEquals(HttpStatus.NOT_FOUND.value(), crapiAPIResponse.getStatus());
}
@Test
public void generateOtpSuccessWhenOtpEntryAlreadyPresent() {
ForgetPassword forgetPassword = getDummyForgetPassword();
User user = getDummyUser();
Otp otp = getDummyOtp();
String expectedMessage = UserMessage.OTP_SEND_SUCCESS_ON_EMAIL + user.getEmail();
Mockito.when(userRepository.findByEmail(Mockito.anyString())).thenReturn(user);
Mockito.when(otpRepository.findByUser(user)).thenReturn(otp);
Mockito.doNothing()
.when(smtpMailServer)
.sendMail(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
CRAPIResponse crapiAPIResponse = otpService.generateOtp(getDummyForgetPassword());
Mockito.verify(otpRepository, Mockito.times(1)).save(Mockito.any());
Assertions.assertEquals(expectedMessage, crapiAPIResponse.getMessage());
Assertions.assertEquals(HttpStatus.OK.value(), crapiAPIResponse.getStatus());
}
@Test
public void generateOtpSuccessWhenOtpNotPresentAlreadyPresent() {
ForgetPassword forgetPassword = getDummyForgetPassword();
User user = getDummyUser();
String expectedMessage = UserMessage.OTP_SEND_SUCCESS_ON_EMAIL + user.getEmail();
Mockito.when(userRepository.findByEmail(Mockito.anyString())).thenReturn(user);
Mockito.when(otpRepository.findByUser(user)).thenReturn(null);
Mockito.doNothing()
.when(smtpMailServer)
.sendMail(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
CRAPIResponse crapiAPIResponse = otpService.generateOtp(getDummyForgetPassword());
Mockito.verify(otpRepository, Mockito.times(1)).save(Mockito.any());
Assertions.assertEquals(expectedMessage, crapiAPIResponse.getMessage());
Assertions.assertEquals(HttpStatus.OK.value(), crapiAPIResponse.getStatus());
}
private ForgetPassword getDummyForgetPassword() {
ForgetPassword forgetPassword = new ForgetPassword();
forgetPassword.setEmail("mail@example.com");
return forgetPassword;
}
private OtpForm getDummyOtpForm() {
OtpForm otpForm = new OtpForm();
otpForm.setEmail("sample@example.com");
otpForm.setOtp("123456");
otpForm.setPassword("password");
return otpForm;
}
private Otp getDummyOtp() {
Otp otp = new Otp();
otp.setCount(1);
otp.setId(1l);
otp.setOtp("123123");
otp.setStatus("DUMMY STATUS");
otp.setUser(getDummyUser());
return otp;
}
private User getDummyUser() {
User user = new User("email@example.com", "9798789212", "Pass", ERole.ROLE_USER);
user.setId(1l);
return user;
}
}