Search in sources :

Example 6 with MockRequestCallback

use of com.okta.oidc.util.MockRequestCallback in project okta-oidc-android by okta.

the class SessionClientImplTest method refreshTokenInParallel.

@Test
public void refreshTokenInParallel() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(2);
    String nonce = CodeVerifierUtil.generateRandomState();
    String jws = TestValues.getJwt(mEndPoint.getUrl(), nonce, mConfig.getClientId());
    mEndPoint.enqueueTokenSuccess(jws);
    MockRequestCallback<Tokens, AuthorizationException> cb = new MockRequestCallback<>(latch);
    MockRequestCallback<Tokens, AuthorizationException> cb2 = new MockRequestCallback<>(latch);
    new Thread(() -> mSessionClient.refreshToken(cb)).start();
    new Thread(() -> mSessionClient.refreshToken(cb2)).start();
    latch.await();
    Tokens result = cb.getResult();
    Tokens result2 = cb2.getResult();
    TokenResponse original = mGson.fromJson(String.format(TOKEN_SUCCESS, jws), TokenResponse.class);
    assertEquals(original.getIdToken(), result.getIdToken());
    assertEquals(original.getRefreshToken(), result.getRefreshToken());
    assertEquals(original.getIdToken(), result.getIdToken());
    assertEquals(result, result2);
}
Also used : MockRequestCallback(com.okta.oidc.util.MockRequestCallback) TokenResponse(com.okta.oidc.net.response.TokenResponse) AuthorizationException(com.okta.oidc.util.AuthorizationException) CountDownLatch(java.util.concurrent.CountDownLatch) Tokens(com.okta.oidc.Tokens) Test(org.junit.Test)

Example 7 with MockRequestCallback

use of com.okta.oidc.util.MockRequestCallback in project okta-oidc-android by okta.

the class SessionClientImplTest method refreshTokenFailure.

@Test
public void refreshTokenFailure() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    mEndPoint.enqueueReturnInvalidClient();
    MockRequestCallback<Tokens, AuthorizationException> cb = new MockRequestCallback<>(latch);
    mSessionClient.refreshToken(cb);
    latch.await();
    assertNull(cb.getResult());
    assertNotNull(cb.getException());
    assertEquals(cb.getException().getMessage(), "No client credentials found.");
    assertEquals(cb.getException().type, TYPE_OAUTH_TOKEN_ERROR);
}
Also used : MockRequestCallback(com.okta.oidc.util.MockRequestCallback) AuthorizationException(com.okta.oidc.util.AuthorizationException) CountDownLatch(java.util.concurrent.CountDownLatch) Tokens(com.okta.oidc.Tokens) Test(org.junit.Test)

Example 8 with MockRequestCallback

use of com.okta.oidc.util.MockRequestCallback in project okta-oidc-android by okta.

the class SessionClientImplTest method authorizedRequest.

@Test
public void authorizedRequest() throws InterruptedException, JSONException {
    mEndPoint.enqueueUserInfoSuccess();
    Uri uri = Uri.parse(mProviderConfig.userinfo_endpoint);
    HashMap<String, String> properties = new HashMap<>();
    properties.put("state", CUSTOM_STATE);
    final CountDownLatch latch = new CountDownLatch(1);
    MockRequestCallback<JSONObject, AuthorizationException> cb = new MockRequestCallback<>(latch);
    mSessionClient.authorizedRequest(uri, properties, null, ConnectionParameters.RequestMethod.GET, cb);
    latch.await();
    RecordedRequest recordedRequest = mEndPoint.takeRequest();
    assertNotNull(cb.getResult());
    JSONObject result = cb.getResult();
    assertThat(recordedRequest.getHeader("state"), is(CUSTOM_STATE));
    assertNull(cb.getException());
    assertEquals("John Doe", result.getString("name"));
    assertEquals("Jimmy", result.getString("nickname"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockRequestCallback(com.okta.oidc.util.MockRequestCallback) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) AuthorizationException(com.okta.oidc.util.AuthorizationException) CountDownLatch(java.util.concurrent.CountDownLatch) Uri(android.net.Uri) Test(org.junit.Test)

Example 9 with MockRequestCallback

use of com.okta.oidc.util.MockRequestCallback in project okta-oidc-android by okta.

the class SessionClientImplTest method introspectTokenFailure.

@Test
public void introspectTokenFailure() throws InterruptedException {
    mEndPoint.enqueueReturnInvalidClient();
    final CountDownLatch latch = new CountDownLatch(1);
    MockRequestCallback<IntrospectInfo, AuthorizationException> cb = new MockRequestCallback<>(latch);
    mSessionClient.introspectToken(ACCESS_TOKEN, TokenTypeHint.ACCESS_TOKEN, cb);
    latch.await();
    assertNull(cb.getResult());
    assertNotNull(cb.getException());
}
Also used : MockRequestCallback(com.okta.oidc.util.MockRequestCallback) AuthorizationException(com.okta.oidc.util.AuthorizationException) CountDownLatch(java.util.concurrent.CountDownLatch) IntrospectInfo(com.okta.oidc.net.response.IntrospectInfo) Test(org.junit.Test)

Example 10 with MockRequestCallback

use of com.okta.oidc.util.MockRequestCallback in project okta-oidc-android by okta.

the class SessionClientImplTest method requestsAreRunInSerial.

@Test
public void requestsAreRunInSerial() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(2);
    MockRequestCallback<UserInfo, AuthorizationException> userProfileCallback = new MockRequestCallback<>(latch);
    mEndPoint.enqueueUserInfoSuccess();
    mSessionClient.getUserProfile(userProfileCallback);
    String nonce = CodeVerifierUtil.generateRandomState();
    String jws = TestValues.getJwt(mEndPoint.getUrl(), nonce, mConfig.getClientId());
    mEndPoint.enqueueTokenSuccess(jws);
    MockRequestCallback<Tokens, AuthorizationException> refreshCallback = new MockRequestCallback<>(latch);
    mSessionClient.refreshToken(refreshCallback);
    latch.await();
    UserInfo userInfoResult = userProfileCallback.getResult();
    assertNotNull(userInfoResult);
    assertEquals("John Doe", userInfoResult.get("name"));
    assertEquals("Jimmy", userInfoResult.get("nickname"));
    Tokens result = refreshCallback.getResult();
    TokenResponse original = mGson.fromJson(String.format(TOKEN_SUCCESS, jws), TokenResponse.class);
    assertEquals(original.getIdToken(), result.getIdToken());
    assertEquals(original.getRefreshToken(), result.getRefreshToken());
    assertEquals(original.getIdToken(), result.getIdToken());
}
Also used : MockRequestCallback(com.okta.oidc.util.MockRequestCallback) TokenResponse(com.okta.oidc.net.response.TokenResponse) AuthorizationException(com.okta.oidc.util.AuthorizationException) UserInfo(com.okta.oidc.net.response.UserInfo) CountDownLatch(java.util.concurrent.CountDownLatch) Tokens(com.okta.oidc.Tokens) Test(org.junit.Test)

Aggregations

AuthorizationException (com.okta.oidc.util.AuthorizationException)15 MockRequestCallback (com.okta.oidc.util.MockRequestCallback)15 CountDownLatch (java.util.concurrent.CountDownLatch)15 Test (org.junit.Test)15 Tokens (com.okta.oidc.Tokens)5 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)5 Uri (android.net.Uri)3 TokenResponse (com.okta.oidc.net.response.TokenResponse)3 UserInfo (com.okta.oidc.net.response.UserInfo)3 HashMap (java.util.HashMap)3 JSONObject (org.json.JSONObject)3 IntrospectInfo (com.okta.oidc.net.response.IntrospectInfo)2 NonNull (androidx.annotation.NonNull)1 AuthenticationPayload (com.okta.oidc.AuthenticationPayload)1 RequestCallback (com.okta.oidc.RequestCallback)1 Result (com.okta.oidc.results.Result)1