Search in sources :

Example 11 with MockRequestCallback

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

the class SessionClientImplTest method authorizedRequestFailure.

@Test
public void authorizedRequestFailure() throws InterruptedException, JSONException {
    mEndPoint.enqueueReturnInvalidClient();
    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();
    assertNull(cb.getResult());
    assertNotNull(cb.getException());
}
Also used : 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 12 with MockRequestCallback

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

the class SessionClientImplTest method getUserProfile.

@Test
public void getUserProfile() throws InterruptedException, JSONException {
    mEndPoint.enqueueUserInfoSuccess();
    final CountDownLatch latch = new CountDownLatch(1);
    MockRequestCallback<UserInfo, AuthorizationException> cb = new MockRequestCallback<>(latch);
    mSessionClient.getUserProfile(cb);
    RecordedRequest recordedRequest = mEndPoint.takeRequest();
    latch.await();
    UserInfo result = cb.getResult();
    assertThat(recordedRequest.getHeader("Authorization"), is("Bearer " + ACCESS_TOKEN));
    assertThat(recordedRequest.getHeader("Accept"), is(ConnectionParameters.JSON_CONTENT_TYPE));
    assertThat(recordedRequest.getPath(), equalTo("/userinfo"));
    assertNotNull(result);
    assertEquals("John Doe", result.get("name"));
    assertEquals("Jimmy", result.get("nickname"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockRequestCallback(com.okta.oidc.util.MockRequestCallback) AuthorizationException(com.okta.oidc.util.AuthorizationException) UserInfo(com.okta.oidc.net.response.UserInfo) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 13 with MockRequestCallback

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

the class SessionClientImplTest method revokeToken.

@Test
public void revokeToken() throws InterruptedException {
    mEndPoint.enqueueReturnSuccessEmptyBody();
    final CountDownLatch latch = new CountDownLatch(1);
    MockRequestCallback<Boolean, AuthorizationException> cb = new MockRequestCallback<>(latch);
    mSessionClient.revokeToken("access_token", cb);
    RecordedRequest recordedRequest = mEndPoint.takeRequest();
    latch.await();
    assertNotNull(cb.getResult());
    assertTrue(cb.getResult());
    assertThat(recordedRequest.getPath(), equalTo("/revoke?client_id=CLIENT_ID&token=access_token"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockRequestCallback(com.okta.oidc.util.MockRequestCallback) AuthorizationException(com.okta.oidc.util.AuthorizationException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 14 with MockRequestCallback

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

the class SessionClientImplTest method revokeTokenFailure.

@Test
public void revokeTokenFailure() throws InterruptedException {
    mEndPoint.enqueueReturnInvalidClient();
    final CountDownLatch latch = new CountDownLatch(1);
    MockRequestCallback<Boolean, AuthorizationException> cb = new MockRequestCallback<>(latch);
    mSessionClient.revokeToken("access_token", cb);
    RecordedRequest recordedRequest = mEndPoint.takeRequest();
    latch.await();
    assertNull(cb.getResult());
    assertNotNull(cb.getException());
    assertEquals(TYPE_OAUTH_TOKEN_ERROR, cb.getException().type);
    assertThat(recordedRequest.getPath(), equalTo("/revoke?client_id=CLIENT_ID&token=access_token"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockRequestCallback(com.okta.oidc.util.MockRequestCallback) AuthorizationException(com.okta.oidc.util.AuthorizationException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 15 with MockRequestCallback

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

the class SessionClientImplTest method refreshTokenFailureInParallelCallbackCycle.

@Test
public void refreshTokenFailureInParallelCallbackCycle() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    String nonce = CodeVerifierUtil.generateRandomState();
    String jws = TestValues.getJwt(mEndPoint.getUrl(), nonce, mConfig.getClientId());
    mEndPoint.enqueueTokenSuccess(jws);
    MockRequestCallback<Tokens, AuthorizationException> errorCb = new MockRequestCallback<>(latch);
    RequestCallback<Tokens, AuthorizationException> cb = new RequestCallback<Tokens, AuthorizationException>() {

        @Override
        public void onSuccess(@NonNull Tokens result) {
            try {
                mSessionClient.refreshToken(errorCb);
            } catch (RuntimeException runtimeException) {
                assertEquals(runtimeException.getMessage(), "refreshToken can't be called from callback.");
                latch.countDown();
            }
        }

        @Override
        public void onError(String error, AuthorizationException exception) {
            fail();
        }
    };
    mSessionClient.refreshToken(cb);
    latch.await();
}
Also used : MockRequestCallback(com.okta.oidc.util.MockRequestCallback) MockRequestCallback(com.okta.oidc.util.MockRequestCallback) RequestCallback(com.okta.oidc.RequestCallback) AuthorizationException(com.okta.oidc.util.AuthorizationException) NonNull(androidx.annotation.NonNull) 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