diff --git a/services/identity/src/main/java/com/crapi/config/InitialDataConfig.java b/services/identity/src/main/java/com/crapi/config/InitialDataConfig.java index e18d9c63..b2a4e2c7 100644 --- a/services/identity/src/main/java/com/crapi/config/InitialDataConfig.java +++ b/services/identity/src/main/java/com/crapi/config/InitialDataConfig.java @@ -14,15 +14,18 @@ package com.crapi.config; +import com.crapi.constant.TestUsers; import com.crapi.entity.User; import com.crapi.entity.UserDetails; import com.crapi.entity.VehicleDetails; import com.crapi.enums.ERole; +import com.crapi.model.SeedUser; import com.crapi.repository.*; import com.crapi.service.VehicleService; import com.crapi.utils.UserData; import com.crapi.utils.VehicleLocationData; import com.crapi.utils.VehicleModelData; +import java.util.ArrayList; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -77,22 +80,18 @@ public void setup(ApplicationReadyEvent event) { public void addUser() { if (CollectionUtils.isEmpty(userDetailsRepository.findAll())) { - boolean user1 = - predefineUserData( - "Adam", "adam007@example.com", "adam007!123", "9876895423", ERole.ROLE_PREDEFINE); - boolean user2 = - predefineUserData( - "Pogba", "pogba006@example.com", "pogba006!123", "9876570006", ERole.ROLE_PREDEFINE); - boolean user3 = - predefineUserData( - "Robot", "robot001@example.com", "robot001!123", "9876570001", ERole.ROLE_PREDEFINE); - boolean userTest = - predefineUserData("Test", "test@example.com", "Test!123", "9876540001", ERole.ROLE_USER); - boolean userAdmin = - predefineUserData( - "Admin", "admin@example.com", "Admin!123", "9010203040", ERole.ROLE_ADMIN); - if (!user1 || !user2 || !user3 || !userTest || !userAdmin) { - logger.error("Fail to create predefined users"); + ArrayList userDetailList = new TestUsers().getUsers(); + for (SeedUser userDetails : userDetailList) { + boolean user = + predefineUserData( + userDetails.getName(), + userDetails.getEmail(), + userDetails.getPassword(), + userDetails.getNumber(), + userDetails.getRole()); + if (!user) { + logger.error("Fail to create predefined users"); + } } } } diff --git a/services/identity/src/main/java/com/crapi/constant/TestUsers.java b/services/identity/src/main/java/com/crapi/constant/TestUsers.java new file mode 100644 index 00000000..165184aa --- /dev/null +++ b/services/identity/src/main/java/com/crapi/constant/TestUsers.java @@ -0,0 +1,39 @@ +/* + * 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.constant; + +import com.crapi.enums.ERole; +import com.crapi.model.SeedUser; +import java.util.ArrayList; +import lombok.Getter; + +public class TestUsers { + @Getter public ArrayList users = new ArrayList(); + + public TestUsers() { + users.add( + new SeedUser( + "Adam", "adam007@example.com", "9876895423", "adam007!123", ERole.ROLE_PREDEFINE)); + users.add( + new SeedUser( + "Pogba", "pogba006@example.com", "9876570006", "pogba006!123", ERole.ROLE_PREDEFINE)); + users.add( + new SeedUser( + "Robot", "robot001@example.com", "9876570001", "robot001!123", ERole.ROLE_PREDEFINE)); + users.add(new SeedUser("Test", "test@example.com", "9876540001", "Test!123", ERole.ROLE_USER)); + users.add( + new SeedUser("Admin", "admin@example.com", "9010203040", "Admin!123", ERole.ROLE_ADMIN)); + } +} diff --git a/services/identity/src/main/java/com/crapi/controller/AuthController.java b/services/identity/src/main/java/com/crapi/controller/AuthController.java index fb6899c7..356972a1 100644 --- a/services/identity/src/main/java/com/crapi/controller/AuthController.java +++ b/services/identity/src/main/java/com/crapi/controller/AuthController.java @@ -15,12 +15,16 @@ package com.crapi.controller; import com.crapi.config.JwtProvider; +import com.crapi.constant.TestUsers; import com.crapi.constant.UserMessage; +import com.crapi.entity.User; import com.crapi.model.*; +import com.crapi.model.SeedUser; import com.crapi.service.OtpService; import com.crapi.service.UserRegistrationService; import com.crapi.service.UserService; import java.io.UnsupportedEncodingException; +import java.util.ArrayList; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -32,7 +36,6 @@ @RestController @RequestMapping("/identity/api/auth") public class AuthController { - @Autowired UserService userService; @Autowired UserRegistrationService userRegistrationService; @@ -177,4 +180,19 @@ public ResponseEntity loginWithTokenV2( } return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(jwt); } + + /** @return success or failure of password updation. */ + @GetMapping("/reset-test-users") + public ResponseEntity resetPassword() { + ArrayList userDetailList = new TestUsers().getUsers(); + for (SeedUser userDetails : userDetailList) { + User resetUser = + userService.updateUserPassword(userDetails.getPassword(), userDetails.getEmail()); + if (resetUser == null) + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body(new CRAPIResponse("Internal Server Error", 500)); + } + return ResponseEntity.status(HttpStatus.OK) + .body(new CRAPIResponse("Test Users Password Resetted", 200)); + } } diff --git a/services/identity/src/main/java/com/crapi/model/SeedUser.java b/services/identity/src/main/java/com/crapi/model/SeedUser.java new file mode 100644 index 00000000..fe92b721 --- /dev/null +++ b/services/identity/src/main/java/com/crapi/model/SeedUser.java @@ -0,0 +1,53 @@ +/* + * 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.model; + +import com.crapi.enums.ERole; +import javax.validation.constraints.Email; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.Size; +import lombok.Data; + +@Data +public class SeedUser { + @NotBlank + @Size(min = 3, max = 100) + private String name; + + @NotBlank + @Size(min = 6, max = 100) + private String password; + + @NotBlank + @Size(max = 100) + @Email + private String email; + + @NotBlank + @Size(max = 15) + private String number; + + @NotBlank + @Size(min = 3, max = 100) + private ERole role; + + public SeedUser(String name, String email, String number, String password, ERole role) { + this.name = name; + this.email = email; + this.number = number; + this.password = password; + this.role = role; + } +} diff --git a/services/identity/src/main/java/com/crapi/service/Impl/UserServiceImpl.java b/services/identity/src/main/java/com/crapi/service/Impl/UserServiceImpl.java index b3e27def..88b5ae5e 100644 --- a/services/identity/src/main/java/com/crapi/service/Impl/UserServiceImpl.java +++ b/services/identity/src/main/java/com/crapi/service/Impl/UserServiceImpl.java @@ -147,6 +147,21 @@ public void updateUserToken(String jwt, String email) { } } + /** + * @param password update password in database + * @param email by email user details and update password + */ + @Transactional + @Override + public User updateUserPassword(String password, String email) { + User user = userRepository.findByEmail(email); + if (user != null) { + user.setPassword(encoder.encode(password)); + userRepository.saveAndFlush(user); + } + return user; + } + /** * @param loginForm Contains user email, password and number * @param request getting jwt token for user from request header diff --git a/services/identity/src/main/java/com/crapi/service/UserService.java b/services/identity/src/main/java/com/crapi/service/UserService.java index e2633c47..86de22ad 100644 --- a/services/identity/src/main/java/com/crapi/service/UserService.java +++ b/services/identity/src/main/java/com/crapi/service/UserService.java @@ -23,6 +23,8 @@ public interface UserService { void updateUserToken(String jwt, String email); + User updateUserPassword(String password, String email); + CRAPIResponse resetPassword(LoginForm loginForm, HttpServletRequest request) throws UnsupportedEncodingException; diff --git a/services/identity/src/test/java/com/crapi/service/Impl/UserServiceImplTest.java b/services/identity/src/test/java/com/crapi/service/Impl/UserServiceImplTest.java index 24f274d1..d8429ffb 100644 --- a/services/identity/src/test/java/com/crapi/service/Impl/UserServiceImplTest.java +++ b/services/identity/src/test/java/com/crapi/service/Impl/UserServiceImplTest.java @@ -217,6 +217,18 @@ public void testUpdateUserToken() { Mockito.verify(userRepository, Mockito.times(1)).findByEmail(user.getEmail()); } + @Test + public void testUpdateUserPassword() { + User user = getDummyUser(); + String samplePassword = "samplePassword"; + Mockito.when(userRepository.findByEmail(user.getEmail())).thenReturn(user); + Mockito.when(userRepository.saveAndFlush(Mockito.any())).thenReturn(user); + userService.updateUserPassword(samplePassword, getDummyUser().getEmail()); + Assertions.assertEquals(user.getPassword(), encoder.encode(samplePassword)); + Mockito.verify(userRepository, Mockito.times(1)).saveAndFlush(Mockito.any()); + Mockito.verify(userRepository, Mockito.times(1)).findByEmail(user.getEmail()); + } + @Test public void getUserByRequestTokenRequestSuccessFull() { User user = getDummyUser();