Search in sources :

Example 1 with ForgetPasswordRequest

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

the class UserTest method forget_password.

@Test
public void forget_password() throws JsonProcessingException {
    ResponseEntity<DefaultOAuth2AccessToken> registerTokenResponse = OAuth2Utility.getOAuth2ClientCredentialToken(AppConstant.CLIENT_ID_REGISTER_ID, AppConstant.EMPTY_CLIENT_SECRET);
    String value = registerTokenResponse.getBody().getValue();
    User user = UserUtility.createUser();
    UserUtility.register(user);
    ForgetPasswordRequest forgetPasswordRequest = new ForgetPasswordRequest();
    forgetPasswordRequest.setEmail(user.getEmail());
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setBearerAuth(value);
    String s1 = mapper.writeValueAsString(forgetPasswordRequest);
    HttpEntity<String> request = new HttpEntity<>(s1, headers);
    String url = UrlUtility.getAccessUrl("/users" + "/forgetPwd");
    ResponseEntity<Object> exchange = TestContext.getRestTemplate().exchange(url, HttpMethod.POST, request, Object.class);
    Assert.assertEquals(HttpStatus.OK, exchange.getStatusCode());
    forgetPasswordRequest.setToken("123456789");
    forgetPasswordRequest.setNewPassword("P1!" + UUID.randomUUID().toString().replaceAll("-", "").substring(0, 10));
    String s2 = mapper.writeValueAsString(forgetPasswordRequest);
    HttpHeaders header2 = new HttpHeaders();
    header2.setContentType(MediaType.APPLICATION_JSON);
    header2.setBearerAuth(value);
    HttpEntity<String> request2 = new HttpEntity<>(s2, header2);
    String url2 = UrlUtility.getAccessUrl("/users" + "/resetPwd");
    TestContext.getRestTemplate().exchange(url2, HttpMethod.POST, request2, Object.class);
    Assert.assertEquals(HttpStatus.OK, exchange.getStatusCode());
    // login
    ResponseEntity<DefaultOAuth2AccessToken> tokenResponse = UserUtility.login(forgetPasswordRequest.getEmail(), forgetPasswordRequest.getNewPassword());
    Assert.assertEquals(HttpStatus.OK, tokenResponse.getStatusCode());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) User(com.hw.helper.User) HttpEntity(org.springframework.http.HttpEntity) ForgetPasswordRequest(com.hw.helper.ForgetPasswordRequest) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Test(org.junit.Test)

Example 2 with ForgetPasswordRequest

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

the class MessengerServiceTest method concurrent_reset_pwd_for_same_user.

@Test
public void concurrent_reset_pwd_for_same_user() {
    AtomicInteger emailSuccessCount = new AtomicInteger(0);
    AtomicInteger emailFailedCount = new AtomicInteger(0);
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            ArrayList<Integer> integers = new ArrayList<>();
            integers.add(200);
            integers.add(400);
            ResponseEntity<DefaultOAuth2AccessToken> registerTokenResponse = action.getJwtClientCredential(valid_register_clientId, valid_empty_secret);
            action.testUser.get(0).getEmail();
            String value = registerTokenResponse.getBody().getValue();
            String url = UserAction.proxyUrl + UserAction.SVC_NAME_AUTH + "/resourceOwners/forgetPwd";
            ForgetPasswordRequest forgetPasswordRequest = new ForgetPasswordRequest();
            forgetPasswordRequest.setEmail(action.testUser.get(0).getEmail());
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            headers.setBearerAuth(value);
            String s1 = null;
            try {
                s1 = mapper.writeValueAsString(forgetPasswordRequest);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            HttpEntity<String> request = new HttpEntity<>(s1, headers);
            ResponseEntity<Object> exchange = action.restTemplate.exchange(url, HttpMethod.POST, request, Object.class);
            log.info("response " + exchange.toString());
            Assert.assertTrue("expected status code but is " + exchange.getStatusCodeValue(), integers.contains(exchange.getStatusCodeValue()));
            if (exchange.getStatusCodeValue() == 200) {
                emailSuccessCount.incrementAndGet();
            }
            if (exchange.getStatusCodeValue() == 400) {
                emailFailedCount.incrementAndGet();
            }
        }
    };
    ArrayList<Runnable> runnables = new ArrayList<>();
    IntStream.range(0, numOfConcurrent).forEach(e -> {
        runnables.add(runnable);
    });
    try {
        assertConcurrent("", runnables, 30000);
        assertTrue("success email should be 1 but is " + emailSuccessCount.get(), emailSuccessCount.get() == 1);
        assertTrue("failed email should be 9 but is " + emailFailedCount.get(), emailFailedCount.get() == numOfConcurrent - 1);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : ArrayList(java.util.ArrayList) ForgetPasswordRequest(com.hw.helper.ForgetPasswordRequest) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

ForgetPasswordRequest (com.hw.helper.ForgetPasswordRequest)2 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 User (com.hw.helper.User)1 ArrayList (java.util.ArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1