Search in sources :

Example 1 with MockRequestCallback

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

the class SyncAuthClientTest method signInNativeCancel.

@Test
public void signInNativeCancel() throws AuthorizationException, InterruptedException {
    String nonce = CodeVerifierUtil.generateRandomState();
    String state = CodeVerifierUtil.generateRandomState();
    String jws = TestValues.getJwt(mEndPoint.getUrl(), nonce, mConfig.getClientId());
    AuthenticationPayload payload = new AuthenticationPayload.Builder().addParameter("nonce", nonce).setState(state).build();
    mEndPoint.enqueueNativeRequestSuccess(state, 5);
    mEndPoint.enqueueTokenSuccess(jws);
    CountDownLatch latch = new CountDownLatch(1);
    MockRequestCallback<Result, AuthorizationException> mockCallback = new MockRequestCallback<>(latch);
    mAuthClient.signIn(SESSION_TOKEN, payload, mockCallback);
    // wait for request to be created
    Thread.sleep(200);
    mAuthClient.cancel();
    latch.await();
    assertNull(mockCallback.getResult());
    assertNotNull(mockCallback.getException());
    String errorMessage = mockCallback.getException().getMessage();
    // Socket closed or canceled or stream is closed or network error.
    if (errorMessage == null) {
        assertTrue(mockCallback.getException().getCause() instanceof InterruptedException);
    } else {
        assertTrue("Socket closed".equals(errorMessage) || "Canceled".equals(errorMessage) || "stream is closed".equals(errorMessage) || "Network error".equals(errorMessage) || "interrupted".equals(errorMessage));
    }
}
Also used : MockRequestCallback(com.okta.oidc.util.MockRequestCallback) AuthorizationException(com.okta.oidc.util.AuthorizationException) CountDownLatch(java.util.concurrent.CountDownLatch) AuthenticationPayload(com.okta.oidc.AuthenticationPayload) Result(com.okta.oidc.results.Result) Test(org.junit.Test)

Example 2 with MockRequestCallback

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

the class SessionClientImplTest method authorizedRequestCancel.

@Test
public void authorizedRequestCancel() throws InterruptedException, JSONException {
    mEndPoint.enqueueUserInfoSuccess(5);
    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);
    // wait for request to be created
    Thread.sleep(200);
    mSessionClient.cancel();
    latch.await();
    assertNull(cb.getResult());
    assertNotNull(cb.getException());
    String errorMessage = cb.getException().getMessage();
    // Socket closed or canceled or stream is closed or network error.
    if (errorMessage == null) {
        assertTrue(cb.getException().getCause() instanceof InterruptedException);
    } else {
        assertTrue("Socket closed".equals(errorMessage) || "Canceled".equals(errorMessage) || "stream is closed".equals(errorMessage) || "Network error".equals(errorMessage) || "interrupted".equals(errorMessage));
    }
}
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 3 with MockRequestCallback

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

the class SessionClientImplTest method refreshToken.

@Test
public void refreshToken() 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> cb = new MockRequestCallback<>(latch);
    mSessionClient.refreshToken(cb);
    latch.await();
    Tokens result = cb.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) CountDownLatch(java.util.concurrent.CountDownLatch) Tokens(com.okta.oidc.Tokens) Test(org.junit.Test)

Example 4 with MockRequestCallback

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

the class SessionClientImplTest method getUserProfileFailure.

@Test
public void getUserProfileFailure() throws InterruptedException, JSONException {
    mEndPoint.enqueueReturnUnauthorizedRevoked();
    final CountDownLatch latch = new CountDownLatch(1);
    MockRequestCallback<UserInfo, AuthorizationException> cb = new MockRequestCallback<>(latch);
    mSessionClient.getUserProfile(cb);
    RecordedRequest recordedRequest = mEndPoint.takeRequest();
    latch.await();
    assertNull(cb.getResult());
    assertNotNull(cb.getException());
    assertThat(recordedRequest.getPath(), equalTo("/userinfo"));
    assertEquals(TYPE_GENERAL_ERROR, cb.getException().type);
}
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 5 with MockRequestCallback

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

the class SessionClientImplTest method introspectToken.

@Test
public void introspectToken() throws InterruptedException {
    mEndPoint.enqueueIntrospectSuccess();
    final CountDownLatch latch = new CountDownLatch(1);
    MockRequestCallback<IntrospectInfo, AuthorizationException> cb = new MockRequestCallback<>(latch);
    mSessionClient.introspectToken(ACCESS_TOKEN, TokenTypeHint.ACCESS_TOKEN, cb);
    latch.await();
    assertNotNull(cb.getResult());
    assertTrue(cb.getResult().isActive());
}
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)

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