Search in sources :

Example 1 with AuthException

use of com.amplifyframework.auth.AuthException in project amplify-android by aws-amplify.

the class RxAuthBindingTest method testSignInWithWebUIFails.

/**
 * Validates that a failed call to sign-in with web UI will propagate the failure
 * back through the binding.
 * @throws InterruptedException If test observer is interrupted while awaiting terminal event
 */
@Test
public void testSignInWithWebUIFails() throws InterruptedException {
    Activity activity = new Activity();
    // Arrange a failure
    AuthException failure = new AuthException("Sign in with web UI", " has failed");
    doAnswer(invocation -> {
        // 0 = activity, 1 = result consumer, 2 = failure consumer
        int positionOfFailureConsumer = 2;
        Consumer<AuthException> onFailure = invocation.getArgument(positionOfFailureConsumer);
        onFailure.accept(failure);
        return null;
    }).when(delegate).signInWithWebUI(eq(activity), anyConsumer(), anyConsumer());
    // Act: call the binding
    TestObserver<AuthSignInResult> observer = auth.signInWithWebUI(activity).test();
    // Assert: failure is furnished the via the Rx Single
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertNoValues().assertError(failure);
}
Also used : Activity(android.app.Activity) AuthException(com.amplifyframework.auth.AuthException) AuthSignInResult(com.amplifyframework.auth.result.AuthSignInResult) Test(org.junit.Test)

Example 2 with AuthException

use of com.amplifyframework.auth.AuthException in project amplify-android by aws-amplify.

the class RxAuthBindingTest method testConfirmResetPasswordFails.

/**
 * Tests that a failed request to confirm password reset will propagate a failure
 * back through the binding.
 * @throws InterruptedException If test observer is interrupted while awaiting terminal event
 */
@Test
public void testConfirmResetPasswordFails() throws InterruptedException {
    String newPassword = RandomString.string();
    String confirmationCode = RandomString.string();
    // Arrange delegate to furnish a failure
    AuthException failure = new AuthException("Confirm password reset ", " has failed.");
    doAnswer(invocation -> {
        // 0 = new pass, 1 = confirmation code, 2 = onComplete, 3 = onFailure
        int positionOfFailureConsumer = 3;
        Consumer<AuthException> onFailure = invocation.getArgument(positionOfFailureConsumer);
        onFailure.accept(failure);
        return null;
    }).when(delegate).confirmResetPassword(eq(newPassword), eq(confirmationCode), anyAction(), anyConsumer());
    // Act: call the binding
    TestObserver<Void> observer = auth.confirmResetPassword(newPassword, confirmationCode).test();
    // Assert: Completable terminated with failure
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertNotComplete().assertError(failure);
}
Also used : AuthException(com.amplifyframework.auth.AuthException) RandomString(com.amplifyframework.testutils.random.RandomString) Test(org.junit.Test)

Example 3 with AuthException

use of com.amplifyframework.auth.AuthException in project amplify-android by aws-amplify.

the class RxAuthBindingTest method testResendSignUpCodeFails.

/**
 * Validates that a failed call to resend the sign-up code will propagate the failure
 * back through the binding.
 * @throws InterruptedException If test observer is interrupted while awaiting terminal event
 */
@Test
public void testResendSignUpCodeFails() throws InterruptedException {
    String username = RandomString.string();
    // Arrange a failure on the failure consumer
    AuthException failure = new AuthException("Reset sign up", " has failed.");
    doAnswer(invocation -> {
        // 0 = username, 1 = onResult, 2 = onFailure
        int positionOfFailureConsumer = 2;
        Consumer<AuthException> onFailure = invocation.getArgument(positionOfFailureConsumer);
        onFailure.accept(failure);
        return null;
    }).when(delegate).resendSignUpCode(eq(username), anyConsumer(), anyConsumer());
    // Act: call the binding
    TestObserver<AuthSignUpResult> observer = auth.resendSignUpCode(username).test();
    // Assert: the result was furnished to the Rx Single
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertNoValues().assertError(failure);
}
Also used : AuthSignUpResult(com.amplifyframework.auth.result.AuthSignUpResult) AuthException(com.amplifyframework.auth.AuthException) RandomString(com.amplifyframework.testutils.random.RandomString) Test(org.junit.Test)

Example 4 with AuthException

use of com.amplifyframework.auth.AuthException in project amplify-android by aws-amplify.

the class RxAuthBindingTest method testDeleteUserFails.

/**
 * Validate that a delete user failure is propagated up through the binding.
 * @throws InterruptedException If test observer is interrupted while awaiting terminal event
 */
@Test
public void testDeleteUserFails() throws InterruptedException {
    // Arrange a callback on the failure consumer
    AuthException failure = new AuthException("Delete user", "has failed");
    doAnswer(invocation -> {
        // 0 = onComplete, 1 = onFailure
        int positionOfFailureConsumer = 1;
        Consumer<AuthException> onFailure = invocation.getArgument(positionOfFailureConsumer);
        onFailure.accept(failure);
        return null;
    }).when(delegate).deleteUser(anyAction(), anyConsumer());
    // Act: call the binding
    TestObserver<Void> observer = auth.deleteUser().test();
    // Assert: failure is furnished via Rx Completable.
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertNotComplete().assertError(failure);
}
Also used : AuthException(com.amplifyframework.auth.AuthException) Test(org.junit.Test)

Example 5 with AuthException

use of com.amplifyframework.auth.AuthException in project amplify-android by aws-amplify.

the class AuthComponentConfigureTest method testConfigureExceptionHandling.

/**
 * If {@link AWSMobileClient} emits an error during initialization, the
 * {@link com.amplifyframework.auth.AuthPlugin#configure(JSONObject, Context)} method should wrap that exception
 * in an {@link AuthException} and throw it on its calling thread.
 * @throws AmplifyException the exception expected to be thrown when configuration fails.
 * @throws JSONException has to be declared as part of creating a test JSON object
 */
@Test(expected = AuthException.class)
public void testConfigureExceptionHandling() throws AmplifyException, JSONException {
    JSONObject pluginConfig = new JSONObject().put("TestKey", "TestVal");
    JSONObject json = new JSONObject().put("plugins", new JSONObject().put(PLUGIN_KEY, pluginConfig));
    AuthCategoryConfiguration authConfig = new AuthCategoryConfiguration();
    authConfig.populateFromJSON(json);
    doAnswer(invocation -> {
        Callback<UserStateDetails> callback = invocation.getArgument(2);
        callback.onError(new Exception());
        return null;
    }).when(mobileClient).initialize(any(), any(), any());
    authCategory.configure(authConfig, getApplicationContext());
}
Also used : JSONObject(org.json.JSONObject) AuthCategoryConfiguration(com.amplifyframework.auth.AuthCategoryConfiguration) UserStateDetails(com.amazonaws.mobile.client.UserStateDetails) AmplifyException(com.amplifyframework.AmplifyException) AuthException(com.amplifyframework.auth.AuthException) JSONException(org.json.JSONException) Test(org.junit.Test)

Aggregations

AuthException (com.amplifyframework.auth.AuthException)30 AmplifyException (com.amplifyframework.AmplifyException)18 JSONException (org.json.JSONException)18 Test (org.junit.Test)17 AuthNavigationException (com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthNavigationException)13 NotAuthorizedException (com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException)13 HashMap (java.util.HashMap)11 AuthSignInResult (com.amplifyframework.auth.result.AuthSignInResult)8 RandomString (com.amplifyframework.testutils.random.RandomString)7 AuthSignUpResult (com.amplifyframework.auth.result.AuthSignUpResult)6 UserStateDetails (com.amazonaws.mobile.client.UserStateDetails)5 AmazonClientException (com.amazonaws.AmazonClientException)4 SignUpResult (com.amazonaws.mobile.client.results.SignUpResult)4 UserCodeDeliveryDetails (com.amazonaws.mobile.client.results.UserCodeDeliveryDetails)4 AuthUserAttribute (com.amplifyframework.auth.AuthUserAttribute)4 Activity (android.app.Activity)3 SignInResult (com.amazonaws.mobile.client.results.SignInResult)3 Tokens (com.amazonaws.mobile.client.results.Tokens)3 AuthResetPasswordResult (com.amplifyframework.auth.result.AuthResetPasswordResult)3 AuthUpdateAttributeResult (com.amplifyframework.auth.result.AuthUpdateAttributeResult)3