Search in sources :

Example 1 with AuthSignInResult

use of com.amplifyframework.auth.result.AuthSignInResult 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 AuthSignInResult

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

the class RxAuthBindingTest method testConfirmSignInSucceeds.

/**
 * Validates that a successful call to confirm sign-in will propagate the result
 * back through the binding.
 * @throws InterruptedException If test observer is interrupted while awaiting terminal event
 */
@Test
public void testConfirmSignInSucceeds() throws InterruptedException {
    String confirmationCode = RandomString.string();
    // Arrange a successful result.
    AuthSignInStep step = AuthSignInStep.CONFIRM_SIGN_IN_WITH_SMS_MFA_CODE;
    AuthCodeDeliveryDetails details = new AuthCodeDeliveryDetails(RandomString.string(), DeliveryMedium.UNKNOWN);
    AuthNextSignInStep nextStep = new AuthNextSignInStep(step, Collections.emptyMap(), details);
    AuthSignInResult expected = new AuthSignInResult(true, nextStep);
    doAnswer(invocation -> {
        // 0 = confirm code, 1 = onResult, 2 = onFailure
        int positionOfResultConsumer = 1;
        Consumer<AuthSignInResult> onResult = invocation.getArgument(positionOfResultConsumer);
        onResult.accept(expected);
        return null;
    }).when(delegate).confirmSignIn(eq(confirmationCode), anyConsumer(), anyConsumer());
    // Act: call the binding
    TestObserver<AuthSignInResult> observer = auth.confirmSignIn(confirmationCode).test();
    // Assert: result is furnished
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertNoErrors().assertValue(expected);
}
Also used : AuthCodeDeliveryDetails(com.amplifyframework.auth.AuthCodeDeliveryDetails) AuthNextSignInStep(com.amplifyframework.auth.result.step.AuthNextSignInStep) RandomString(com.amplifyframework.testutils.random.RandomString) AuthSignInResult(com.amplifyframework.auth.result.AuthSignInResult) AuthSignInStep(com.amplifyframework.auth.result.step.AuthSignInStep) Test(org.junit.Test)

Example 3 with AuthSignInResult

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

the class RxAuthBindingTest method testSignInWithSocialWebUISucceeds.

/**
 * Validates that a successful call to sign-in with social web UI will propagate the result
 * back through the binding.
 * @throws InterruptedException If test observer is interrupted while awaiting terminal event
 */
@Test
public void testSignInWithSocialWebUISucceeds() throws InterruptedException {
    AuthProvider provider = AuthProvider.amazon();
    Activity activity = new Activity();
    // Arrange a successful result
    AuthSignInStep step = AuthSignInStep.CONFIRM_SIGN_IN_WITH_SMS_MFA_CODE;
    AuthCodeDeliveryDetails details = new AuthCodeDeliveryDetails(RandomString.string(), DeliveryMedium.PHONE);
    AuthNextSignInStep nextStep = new AuthNextSignInStep(step, Collections.emptyMap(), details);
    AuthSignInResult result = new AuthSignInResult(false, nextStep);
    doAnswer(invocation -> {
        // 0 = provider, 1 = activity, 2 = result consumer, 3 = failure consumer
        int positionOfResultConsumer = 2;
        Consumer<AuthSignInResult> onResult = invocation.getArgument(positionOfResultConsumer);
        onResult.accept(result);
        return null;
    }).when(delegate).signInWithSocialWebUI(eq(provider), eq(activity), anyConsumer(), anyConsumer());
    // Act: call the binding
    TestObserver<AuthSignInResult> observer = auth.signInWithSocialWebUI(provider, activity).test();
    // Assert: result is furnished the via the Rx Single
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertNoErrors().assertValue(result);
}
Also used : AuthCodeDeliveryDetails(com.amplifyframework.auth.AuthCodeDeliveryDetails) Activity(android.app.Activity) AuthProvider(com.amplifyframework.auth.AuthProvider) AuthNextSignInStep(com.amplifyframework.auth.result.step.AuthNextSignInStep) AuthSignInResult(com.amplifyframework.auth.result.AuthSignInResult) AuthSignInStep(com.amplifyframework.auth.result.step.AuthSignInStep) Test(org.junit.Test)

Example 4 with AuthSignInResult

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

the class AuthComponentTest method signInWithWebUI.

/**
 * Tests that the signInWithWebUI method of the Auth wrapper of AWSMobileClient (AMC) calls AMC.showSignIn
 * with the proper parameters and converts the returned result to the proper AuthSignInResult.
 * @throws AuthException test fails if this gets thrown since method should succeed
 */
@Test
public void signInWithWebUI() throws AuthException {
    Map<String, String> additionalInfoMap = Collections.singletonMap("testKey", "testVal");
    UserStateDetails userStateResult = new UserStateDetails(UserState.SIGNED_IN, additionalInfoMap);
    doAnswer(invocation -> {
        Callback<UserStateDetails> callback = invocation.getArgument(2);
        callback.onResult(userStateResult);
        return null;
    }).when(mobileClient).showSignIn(any(), any(), any());
    Tokens tokensResult = new Tokens(ACCESS_TOKEN, ID_TOKEN, REFRESH_TOKEN);
    doAnswer(invocation -> {
        Callback<Tokens> callback = invocation.getArgument(0);
        callback.onResult(tokensResult);
        return null;
    }).when(mobileClient).getTokens(any());
    Activity activity = new Activity();
    String federationProviderName = "testFedProvider";
    String idpIdentifier = "testIdpID";
    String browserPackage = "org.mozilla.firefox";
    List<String> scopes = Collections.singletonList("scope");
    Map<String, String> signInMap = Collections.singletonMap("signInKey", "signInVal");
    Map<String, String> signOutMap = Collections.singletonMap("signOutKey", "signOutVal");
    Map<String, String> tokensMap = Collections.singletonMap("tokensKey", "tokensVal");
    AuthSignInResult result = synchronousAuth.signInWithWebUI(activity, AWSCognitoAuthWebUISignInOptions.builder().federationProviderName(federationProviderName).idpIdentifier(idpIdentifier).scopes(scopes).signInQueryParameters(signInMap).signOutQueryParameters(signOutMap).tokenQueryParameters(tokensMap).browserPackage(browserPackage).build());
    assertTrue(result.isSignInComplete());
    assertEquals(AuthSignInStep.DONE, result.getNextStep().getSignInStep());
    assertEquals(additionalInfoMap, result.getNextStep().getAdditionalInfo());
    ArgumentCaptor<SignInUIOptions> optionsCaptor = ArgumentCaptor.forClass(SignInUIOptions.class);
    verify(mobileClient).showSignIn(eq(activity), optionsCaptor.capture(), any());
    SignInUIOptions signInUIOptions = optionsCaptor.getValue();
    HostedUIOptions hostedUIOptions = signInUIOptions.getHostedUIOptions();
    assertNotNull(hostedUIOptions);
    assertNull(hostedUIOptions.getIdentityProvider());
    assertEquals(federationProviderName, hostedUIOptions.getFederationProviderName());
    assertEquals(idpIdentifier, hostedUIOptions.getIdpIdentifier());
    assertEquals(browserPackage, signInUIOptions.getBrowserPackage());
    assertArrayEquals(scopes.toArray(), hostedUIOptions.getScopes());
    assertEquals(signInMap, hostedUIOptions.getSignInQueryParameters());
    assertEquals(signOutMap, hostedUIOptions.getSignOutQueryParameters());
    assertEquals(tokensMap, hostedUIOptions.getTokenQueryParameters());
}
Also used : HostedUIOptions(com.amazonaws.mobile.client.HostedUIOptions) SignInUIOptions(com.amazonaws.mobile.client.SignInUIOptions) Activity(android.app.Activity) RandomString(com.amplifyframework.testutils.random.RandomString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) UserStateDetails(com.amazonaws.mobile.client.UserStateDetails) AuthSignInResult(com.amplifyframework.auth.result.AuthSignInResult) Tokens(com.amazonaws.mobile.client.results.Tokens) Test(org.junit.Test)

Example 5 with AuthSignInResult

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

the class AuthComponentTest method signInWithAuthFlow.

/**
 * Tests that the signIn method of the Auth wrapper of AWSMobileClient (AMC) calls AMC.signIn with
 * the username, password it received, authFLowType it received, and, if included,
 * the metadata sent in the options object.
 * Also ensures that in the onResult case, the success callback receives a valid AuthSignInResult.
 * @throws AuthException test fails if this gets thrown since method should succeed
 */
@Test
// Casts final parameter to Callback to differentiate methods
@SuppressWarnings("unchecked")
public void signInWithAuthFlow() throws AuthException {
    SignInResult amcResult = new SignInResult(SignInState.SMS_MFA, new UserCodeDeliveryDetails(DESTINATION, DELIVERY_MEDIUM, ATTRIBUTE_NAME));
    Tokens tokensResult = new Tokens(ACCESS_TOKEN, ID_TOKEN, REFRESH_TOKEN);
    doAnswer(invocation -> {
        Callback<Tokens> callback = invocation.getArgument(0);
        callback.onResult(tokensResult);
        return null;
    }).when(mobileClient).getTokens(any());
    doAnswer(invocation -> {
        Callback<SignInResult> callback = invocation.getArgument(5);
        callback.onResult(amcResult);
        return null;
    }).when(mobileClient).signIn(any(), any(), any(), any(), any(), (Callback<SignInResult>) any());
    AuthSignInResult result = synchronousAuth.signIn(USERNAME, PASSWORD, AWSCognitoAuthSignInOptions.builder().metadata(CLIENTMETADATA).authFlowType(AuthFlowType.CUSTOM_AUTH).build());
    validateSignInResult(result, false, AuthSignInStep.CONFIRM_SIGN_IN_WITH_SMS_MFA_CODE);
    verify(mobileClient).signIn(eq(USERNAME), eq(PASSWORD), eq(CLIENTMETADATA), eq(Collections.emptyMap()), eq(com.amazonaws.services.cognitoidentityprovider.model.AuthFlowType.CUSTOM_AUTH), (Callback<SignInResult>) any());
}
Also used : UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) AuthSignInResult(com.amplifyframework.auth.result.AuthSignInResult) SignInResult(com.amazonaws.mobile.client.results.SignInResult) AuthSignInResult(com.amplifyframework.auth.result.AuthSignInResult) Tokens(com.amazonaws.mobile.client.results.Tokens) Test(org.junit.Test)

Aggregations

AuthSignInResult (com.amplifyframework.auth.result.AuthSignInResult)18 Test (org.junit.Test)15 RandomString (com.amplifyframework.testutils.random.RandomString)9 AuthException (com.amplifyframework.auth.AuthException)7 Activity (android.app.Activity)6 SignInResult (com.amazonaws.mobile.client.results.SignInResult)6 Tokens (com.amazonaws.mobile.client.results.Tokens)6 AuthNextSignInStep (com.amplifyframework.auth.result.step.AuthNextSignInStep)5 UserCodeDeliveryDetails (com.amazonaws.mobile.client.results.UserCodeDeliveryDetails)4 AmplifyException (com.amplifyframework.AmplifyException)4 AuthCodeDeliveryDetails (com.amplifyframework.auth.AuthCodeDeliveryDetails)4 AuthSignInStep (com.amplifyframework.auth.result.step.AuthSignInStep)4 HashMap (java.util.HashMap)4 JSONException (org.json.JSONException)4 HostedUIOptions (com.amazonaws.mobile.client.HostedUIOptions)3 SignInUIOptions (com.amazonaws.mobile.client.SignInUIOptions)3 UserStateDetails (com.amazonaws.mobile.client.UserStateDetails)3 AuthNavigationException (com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthNavigationException)3 NotAuthorizedException (com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3