Skip to content

Commit

Permalink
Added A Reset Password for Test Users Endpoint to Identity Service (#185
Browse files Browse the repository at this point in the history
)

* added reset endpoint to identity service
  • Loading branch information
mathew-jose authored Apr 4, 2023
1 parent 4a6988b commit df1be10
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<SeedUser> 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");
}
}
}
}
Expand Down
39 changes: 39 additions & 0 deletions services/identity/src/main/java/com/crapi/constant/TestUsers.java
Original file line number Diff line number Diff line change
@@ -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<SeedUser> users = new ArrayList<SeedUser>();

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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,7 +36,6 @@
@RestController
@RequestMapping("/identity/api/auth")
public class AuthController {

@Autowired UserService userService;

@Autowired UserRegistrationService userRegistrationService;
Expand Down Expand Up @@ -177,4 +180,19 @@ public ResponseEntity<JwtResponse> 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<SeedUser> 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));
}
}
53 changes: 53 additions & 0 deletions services/identity/src/main/java/com/crapi/model/SeedUser.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit df1be10

Please sign in to comment.