File tree Expand file tree Collapse file tree 3 files changed +31
-1
lines changed
main/java/com/joaov1ct0r/restful_api_users_java/modules
test/java/com/joaov1ct0r/restful_api_users_java/modules/users/services/users Expand file tree Collapse file tree 3 files changed +31
-1
lines changed Original file line number Diff line number Diff line change
1
+ package com .joaov1ct0r .restful_api_users_java .modules .domain .services ;
2
+
3
+ import org .springframework .stereotype .Service ;
4
+ import java .util .Random ;
5
+
6
+ @ Service
7
+ public class Generator {
8
+ private final String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%&" ;
9
+ private final Random rnd = new Random ();
10
+ public String generateRandomPassword (int len ) {
11
+ StringBuilder sb = new StringBuilder (len );
12
+
13
+ for (int i = 0 ; i < len ; i ++) {
14
+ sb .append (this .chars .charAt (this .rnd .nextInt (this .chars .length ())));
15
+ }
16
+
17
+ return sb .toString ();
18
+ }
19
+ }
Original file line number Diff line number Diff line change 2
2
3
3
import com .joaov1ct0r .restful_api_users_java .modules .domain .services .BaseService ;
4
4
import com .joaov1ct0r .restful_api_users_java .modules .domain .services .EmailService ;
5
+ import com .joaov1ct0r .restful_api_users_java .modules .domain .services .Generator ;
5
6
import com .joaov1ct0r .restful_api_users_java .modules .users .dtos .ResetPasswordDTO ;
6
7
import com .joaov1ct0r .restful_api_users_java .modules .users .repositories .UserRepository ;
7
8
import org .springframework .beans .factory .annotation .Autowired ;
@@ -15,6 +16,9 @@ public class ResetPasswordService extends BaseService {
15
16
@ Autowired
16
17
private UserRepository userRepository ;
17
18
19
+ @ Autowired
20
+ private Generator generator ;
21
+
18
22
public void execute (ResetPasswordDTO resetPasswordDTO ) throws Exception {
19
23
var isUserRegistered = this .userRepository .findByEmail (resetPasswordDTO .getEmail ());
20
24
@@ -36,7 +40,7 @@ public void execute(ResetPasswordDTO resetPasswordDTO) throws Exception {
36
40
this .emailService .sendMail (
37
41
userToUpdate .getEmail (),
38
42
"Password Updated" ,
39
- "Your new password is: 123456"
43
+ "Your new password is: " + this . generator . generateRandomPassword ( 9 )
40
44
);
41
45
}
42
46
}
Original file line number Diff line number Diff line change 3
3
import com .joaov1ct0r .restful_api_users_java .modules .domain .entities .ErrorLogEntity ;
4
4
import com .joaov1ct0r .restful_api_users_java .modules .domain .repositories .ErrorLogsRepository ;
5
5
import com .joaov1ct0r .restful_api_users_java .modules .domain .services .EmailService ;
6
+ import com .joaov1ct0r .restful_api_users_java .modules .domain .services .Generator ;
6
7
import com .joaov1ct0r .restful_api_users_java .modules .users .dtos .ResetPasswordDTO ;
7
8
import com .joaov1ct0r .restful_api_users_java .modules .users .entities .UserEntity ;
8
9
import com .joaov1ct0r .restful_api_users_java .modules .users .repositories .UserRepository ;
@@ -40,11 +41,15 @@ public class ResetPasswordServiceTest {
40
41
@ Mock
41
42
private ErrorLogsRepository errorLogsRepository ;
42
43
44
+ @ Mock
45
+ private Generator generator ;
46
+
43
47
@ BeforeEach
44
48
public void beforeEachSetUp () {
45
49
Mockito .reset (this .userRepository );
46
50
Mockito .reset (this .emailService );
47
51
Mockito .reset (this .errorLogsRepository );
52
+ Mockito .reset (this .generator );
48
53
Mockito .when (this .errorLogsRepository .save (any ())).thenReturn (new ErrorLogEntity (
49
54
UUID .randomUUID (),
50
55
UUID .randomUUID (),
@@ -86,6 +91,8 @@ public void shouldBeAbleToResetPassword() {
86
91
)
87
92
));
88
93
94
+ Mockito .when (this .generator .generateRandomPassword (9 )).thenReturn ("any_generated_string" );
95
+
89
96
try {
90
97
this .sut .execute (resetPasswordDTO );
91
98
} catch (Exception e ) {
You can’t perform that action at this time.
0 commit comments