Search in sources :

Example 6 with User

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

the class UserUtility method registerNewUserThenLogin.

/**
 * register new user then login.
 *
 * @return login token
 */
public static String registerNewUserThenLogin() {
    User randomUser = createUser();
    register(randomUser);
    ResponseEntity<DefaultOAuth2AccessToken> loginTokenResponse = login(randomUser.getEmail(), randomUser.getPassword());
    return Objects.requireNonNull(loginTokenResponse.getBody()).getValue();
}
Also used : User(com.hw.helper.User) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)

Example 7 with User

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

the class UserUtility method randomRegisterAnUser.

public static User randomRegisterAnUser() {
    User random = createUser();
    register(random);
    return random;
}
Also used : User(com.hw.helper.User)

Example 8 with User

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

the class UserTest method lock_then_unlock_user.

@Test
public void lock_then_unlock_user() {
    User user = UserUtility.createUser();
    ResponseEntity<DefaultOAuth2AccessToken> createResp = UserUtility.register(user);
    String s = createResp.getHeaders().getLocation().toString();
    ResponseEntity<DefaultOAuth2AccessToken> tokenResponse = UserUtility.login(AppConstant.ACCOUNT_USERNAME_ADMIN, AppConstant.ACCOUNT_PASSWORD_ADMIN);
    String bearer = tokenResponse.getBody().getValue();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setBearerAuth(bearer);
    user.setGrantedAuthorities(List.of(AccessConstant.USER_USER_ID));
    user.setLocked(true);
    user.setVersion(0);
    HttpEntity<User> request = new HttpEntity<>(user, headers);
    String url = UrlUtility.getAccessUrl(USER_MNGMT + "/" + s);
    ResponseEntity<DefaultOAuth2AccessToken> exchange = TestContext.getRestTemplate().exchange(url, HttpMethod.PUT, request, DefaultOAuth2AccessToken.class);
    Assert.assertEquals(HttpStatus.OK, exchange.getStatusCode());
    // login to verify account has been locked
    ResponseEntity<DefaultOAuth2AccessToken> tokenResponse1 = UserUtility.login(user.getEmail(), user.getPassword());
    Assert.assertEquals(HttpStatus.BAD_REQUEST, tokenResponse1.getStatusCode());
    user.setLocked(false);
    user.setVersion(1);
    HttpEntity<User> request22 = new HttpEntity<>(user, headers);
    ResponseEntity<DefaultOAuth2AccessToken> exchange22 = TestContext.getRestTemplate().exchange(url, HttpMethod.PUT, request22, DefaultOAuth2AccessToken.class);
    Assert.assertEquals(HttpStatus.OK, exchange22.getStatusCode());
    // login to verify account has been unlocked
    ResponseEntity<DefaultOAuth2AccessToken> tokenResponse12 = UserUtility.login(user.getEmail(), user.getPassword());
    Assert.assertEquals(HttpStatus.OK, tokenResponse12.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)

Example 9 with User

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

the class UserTest method should_not_able_to_create_user_with_user_name_not_email.

@Test
public void should_not_able_to_create_user_with_user_name_not_email() {
    User user = UserUtility.userCreateDraft(UUID.randomUUID().toString(), UUID.randomUUID().toString());
    ResponseEntity<DefaultOAuth2AccessToken> user1 = UserUtility.register(user);
    Assert.assertEquals(HttpStatus.BAD_REQUEST, user1.getStatusCode());
}
Also used : User(com.hw.helper.User) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Test(org.junit.Test)

Example 10 with User

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

the class UserTest method delete_user.

@Test
public void delete_user() {
    User user = UserUtility.createUser();
    ResponseEntity<DefaultOAuth2AccessToken> user1 = UserUtility.register(user);
    String s = user1.getHeaders().getLocation().toString();
    String url = UrlUtility.getAccessUrl(USER_MNGMT + "/" + s);
    ResponseEntity<DefaultOAuth2AccessToken> tokenResponse12 = UserUtility.login(user.getEmail(), user.getPassword());
    Assert.assertEquals(HttpStatus.OK, tokenResponse12.getStatusCode());
    ResponseEntity<DefaultOAuth2AccessToken> tokenResponse = UserUtility.login(AppConstant.ACCOUNT_USERNAME_ADMIN, AppConstant.ACCOUNT_PASSWORD_ADMIN);
    HttpHeaders headers = new HttpHeaders();
    headers.setBearerAuth(tokenResponse.getBody().getValue());
    HttpEntity<Object> request = new HttpEntity<>(null, headers);
    ResponseEntity<Object> exchange = TestContext.getRestTemplate().exchange(url, HttpMethod.DELETE, request, Object.class);
    Assert.assertEquals(HttpStatus.OK, exchange.getStatusCode());
    ResponseEntity<DefaultOAuth2AccessToken> tokenResponse123 = UserUtility.login(user.getEmail(), user.getPassword());
    Assert.assertEquals(HttpStatus.UNAUTHORIZED, tokenResponse123.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