use of com.hw.helper.UserUpdatePwd in project mt-auth by publicdevop2019.
the class UserTest method update_user_password_with_current_pwd.
@Test
public void update_user_password_with_current_pwd() throws JsonProcessingException {
User user = UserUtility.createUser();
UserUpdatePwd resourceOwnerUpdatePwd = new UserUpdatePwd();
resourceOwnerUpdatePwd.setCurrentPwd(user.getPassword());
resourceOwnerUpdatePwd.setEmail(user.getEmail());
resourceOwnerUpdatePwd.setPassword("P1!" + UUID.randomUUID().toString().replaceAll("-", "").substring(0, 10));
ResponseEntity<DefaultOAuth2AccessToken> createResponse = UserUtility.register(user);
// Location is not used in this case, root/admin/user can only update their password
String url = UrlUtility.getAccessUrl("/users" + "/pwd");
// Login
String oldPassword = user.getPassword();
ResponseEntity<DefaultOAuth2AccessToken> tokenResponse = UserUtility.login(user.getEmail(), user.getPassword());
String bearer = tokenResponse.getBody().getValue();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setBearerAuth(bearer);
String s1 = mapper.writeValueAsString(resourceOwnerUpdatePwd);
HttpEntity<String> request = new HttpEntity<>(s1, headers);
ResponseEntity<Object> exchange = TestContext.getRestTemplate().exchange(url, HttpMethod.PUT, request, Object.class);
Assert.assertEquals(HttpStatus.OK, exchange.getStatusCode());
ResponseEntity<DefaultOAuth2AccessToken> resp3 = UserUtility.login(user.getEmail(), oldPassword);
Assert.assertEquals(HttpStatus.BAD_REQUEST, resp3.getStatusCode());
ResponseEntity<DefaultOAuth2AccessToken> resp4 = UserUtility.login(user.getEmail(), resourceOwnerUpdatePwd.getPassword());
Assert.assertEquals(HttpStatus.OK, resp4.getStatusCode());
}
Aggregations