Search in sources :

Example 11 with User

use of com.hw.helper.User 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());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) User(com.hw.helper.User) HttpEntity(org.springframework.http.HttpEntity) UserUpdatePwd(com.hw.helper.UserUpdatePwd) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Test(org.junit.Test)

Example 12 with User

use of com.hw.helper.User in project mt-auth by publicdevop2019.

the class UserTest method update_user_password_without_current_pwd.

@Test
public void update_user_password_without_current_pwd() throws JsonProcessingException {
    User user = UserUtility.createUser();
    UserUtility.register(user);
    // Location is not used in this case, root/admin/user can only update their password
    String url = UrlUtility.getAccessUrl("/users" + "/pwd");
    String newPassword = UUID.randomUUID().toString().replace("-", "");
    // Login
    ResponseEntity<DefaultOAuth2AccessToken> login = UserUtility.login(user.getEmail(), user.getPassword());
    String bearer = login.getBody().getValue();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setBearerAuth(bearer);
    user.setPassword(newPassword);
    String s1 = mapper.writeValueAsString(user);
    HttpEntity<String> request = new HttpEntity<>(s1, headers);
    ResponseEntity<Object> exchange = TestContext.getRestTemplate().exchange(url, HttpMethod.PUT, request, Object.class);
    Assert.assertEquals(HttpStatus.BAD_REQUEST, exchange.getStatusCode());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) User(com.hw.helper.User) HttpEntity(org.springframework.http.HttpEntity) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Test(org.junit.Test)

Aggregations

User (com.hw.helper.User)12 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)10 Test (org.junit.Test)9 HttpEntity (org.springframework.http.HttpEntity)6 HttpHeaders (org.springframework.http.HttpHeaders)6 ForgetPasswordRequest (com.hw.helper.ForgetPasswordRequest)1 PendingResourceOwner (com.hw.helper.PendingResourceOwner)1 UserUpdatePwd (com.hw.helper.UserUpdatePwd)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1