Search in sources :

Example 16 with UserCodeDeliveryDetails

use of com.amazonaws.mobile.client.results.UserCodeDeliveryDetails in project aws-sdk-android by aws-amplify.

the class AWSMobileClientTest method testSignUp.

@Test(expected = com.amazonaws.services.cognitoidentityprovider.model.UserNotConfirmedException.class)
public void testSignUp() throws Exception {
    final String username = "testUser" + System.currentTimeMillis() + new Random().nextInt();
    assertNotEquals("generated usernames are the same", this.username, username);
    final HashMap<String, String> userAttributes = new HashMap<String, String>();
    userAttributes.put("email", EMAIL);
    final SignUpResult signUpResult = auth.signUp(username, PASSWORD, userAttributes, null);
    // Check for non-null user sub in the SignUpResult
    String userSub = signUpResult.getUserSub();
    assertNotNull(userSub);
    // Validate that the userSub is a valid UUID
    assertEquals(36, userSub.length());
    assertEquals(5, userSub.split("-").length);
    assertEquals(8, userSub.split("-")[0].length());
    assertEquals(4, userSub.split("-")[1].length());
    assertEquals(4, userSub.split("-")[2].length());
    assertEquals(4, userSub.split("-")[3].length());
    assertEquals(12, userSub.split("-")[4].length());
    final UserCodeDeliveryDetails details = signUpResult.getUserCodeDeliveryDetails();
    if (details != null) {
        assertEquals("s***@s***.com", details.getDestination());
        assertEquals("email", details.getAttributeName());
        assertEquals("EMAIL", details.getDeliveryMedium());
    }
    assertNotNull(signUpResult.getUserSub());
    final SignInResult signInResult = auth.signIn(username, PASSWORD, null);
    assertEquals("Cannot support MFA in tests", SignInState.DONE, signInResult.getSignInState());
}
Also used : Random(java.util.Random) HashMap(java.util.HashMap) UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) SignUpResult(com.amazonaws.mobile.client.results.SignUpResult) SignInResult(com.amazonaws.mobile.client.results.SignInResult) Test(org.junit.Test)

Example 17 with UserCodeDeliveryDetails

use of com.amazonaws.mobile.client.results.UserCodeDeliveryDetails in project amplify-android by aws-amplify.

the class AuthComponentTest method signIn.

/**
 * Tests that the signIn method of the Auth wrapper of AWSMobileClient (AMC) calls AMC.signIn with
 * the username, password 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 signIn() 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).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(null), (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)

Example 18 with UserCodeDeliveryDetails

use of com.amazonaws.mobile.client.results.UserCodeDeliveryDetails in project amplify-android by aws-amplify.

the class AuthComponentTest method confirmSignUp.

/**
 * Tests that the confirmSignUp method of the Auth wrapper of AWSMobileClient (AMC) calls AMC.confirmSignUp with
 * the username and confirmation code it received.
 * Also ensures that in the onResult case, the success callback receives a valid AuthSignUpResult.
 * @throws AuthException test fails if this gets thrown since method should succeed
 */
@Test
public void confirmSignUp() throws AuthException {
    SignUpResult amcResult = new SignUpResult(true, new UserCodeDeliveryDetails(DESTINATION, DELIVERY_MEDIUM, ATTRIBUTE_NAME), USER_SUB);
    doAnswer(invocation -> {
        Callback<SignUpResult> callback = invocation.getArgument(3);
        callback.onResult(amcResult);
        return null;
    }).when(mobileClient).confirmSignUp(any(), any(), any(), Mockito.<Callback<SignUpResult>>any());
    AuthSignUpResult result = synchronousAuth.confirmSignUp(USERNAME, CONFIRMATION_CODE);
    validateSignUpResult(result, AuthSignUpStep.DONE);
    verify(mobileClient).confirmSignUp(eq(USERNAME), eq(CONFIRMATION_CODE), any(), Mockito.<Callback<SignUpResult>>any());
}
Also used : UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) AuthSignUpResult(com.amplifyframework.auth.result.AuthSignUpResult) AuthSignUpResult(com.amplifyframework.auth.result.AuthSignUpResult) SignUpResult(com.amazonaws.mobile.client.results.SignUpResult) Test(org.junit.Test)

Example 19 with UserCodeDeliveryDetails

use of com.amazonaws.mobile.client.results.UserCodeDeliveryDetails in project amplify-android by aws-amplify.

the class AuthComponentTest method confirmSignUpWithOptions.

/**
 * Tests that the confirmSignUp method of the Auth wrapper of AWSMobileClient (AMC) calls AMC.confirmSignUp with
 * the username and confirmation code and options it received.
 * Also ensures that in the onResult case, the success callback receives a valid AuthSignUpResult.
 * @throws AuthException test fails if this gets thrown since method should succeed
 */
@Test
public void confirmSignUpWithOptions() throws AuthException {
    SignUpResult amcResult = new SignUpResult(true, new UserCodeDeliveryDetails(DESTINATION, DELIVERY_MEDIUM, ATTRIBUTE_NAME), USER_SUB);
    AWSCognitoAuthConfirmSignUpOptions.CognitoBuilder options = AWSCognitoAuthConfirmSignUpOptions.builder();
    Map<String, String> clientMetadata = new HashMap<String, String>();
    clientMetadata.put("testKey", "testValue");
    options.clientMetadata(clientMetadata);
    AWSCognitoAuthConfirmSignUpOptions builtOptions = options.build();
    doAnswer(invocation -> {
        Callback<SignUpResult> callback = invocation.getArgument(3);
        callback.onResult(amcResult);
        return null;
    }).when(mobileClient).confirmSignUp(any(), any(), any(), Mockito.<Callback<SignUpResult>>any());
    AuthSignUpResult result = synchronousAuth.confirmSignUp(USERNAME, CONFIRMATION_CODE, builtOptions);
    validateSignUpResult(result, AuthSignUpStep.DONE);
    verify(mobileClient).confirmSignUp(eq(USERNAME), eq(CONFIRMATION_CODE), eq(clientMetadata), Mockito.<Callback<SignUpResult>>any());
}
Also used : AWSCognitoAuthConfirmSignUpOptions(com.amplifyframework.auth.cognito.options.AWSCognitoAuthConfirmSignUpOptions) HashMap(java.util.HashMap) UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) AuthSignUpResult(com.amplifyframework.auth.result.AuthSignUpResult) RandomString(com.amplifyframework.testutils.random.RandomString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AuthSignUpResult(com.amplifyframework.auth.result.AuthSignUpResult) SignUpResult(com.amazonaws.mobile.client.results.SignUpResult) Test(org.junit.Test)

Example 20 with UserCodeDeliveryDetails

use of com.amazonaws.mobile.client.results.UserCodeDeliveryDetails in project amplify-android by aws-amplify.

the class AuthComponentTest method confirmSignIn.

/**
 * Tests that the confirmSignIn method of the Auth wrapper of AWSMobileClient (AMC) calls AMC.confirmSignIn with
 * the confirmation code it received and 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 confirmSignIn() throws AuthException {
    SignInResult amcResult = new SignInResult(SignInState.DONE, 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(3);
        callback.onResult(amcResult);
        return null;
    }).when(mobileClient).confirmSignIn(any(String.class), any(), any(), (Callback<SignInResult>) any());
    AuthSignInResult result = synchronousAuth.confirmSignIn(CONFIRMATION_CODE);
    validateSignInResult(result, true, AuthSignInStep.DONE);
    verify(mobileClient).confirmSignIn(eq(CONFIRMATION_CODE), any(), any(), (Callback<SignInResult>) any());
}
Also used : UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) RandomString(com.amplifyframework.testutils.random.RandomString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) 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

UserCodeDeliveryDetails (com.amazonaws.mobile.client.results.UserCodeDeliveryDetails)28 Test (org.junit.Test)18 HashMap (java.util.HashMap)14 RandomString (com.amplifyframework.testutils.random.RandomString)11 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)11 JSONException (org.json.JSONException)8 SignInResult (com.amazonaws.mobile.client.results.SignInResult)7 SignUpResult (com.amazonaws.mobile.client.results.SignUpResult)7 ArrayList (java.util.ArrayList)7 List (java.util.List)7 CognitoUserCodeDeliveryDetails (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails)6 AuthUserAttribute (com.amplifyframework.auth.AuthUserAttribute)6 AuthSignUpResult (com.amplifyframework.auth.result.AuthSignUpResult)6 AuthUpdateAttributeResult (com.amplifyframework.auth.result.AuthUpdateAttributeResult)6 AmazonClientException (com.amazonaws.AmazonClientException)5 ReturningRunnable (com.amazonaws.mobile.client.internal.ReturningRunnable)5 NotAuthorizedException (com.amazonaws.services.cognitoidentity.model.NotAuthorizedException)5 InvalidUserPoolConfigurationException (com.amazonaws.services.cognitoidentityprovider.model.InvalidUserPoolConfigurationException)5 Tokens (com.amazonaws.mobile.client.results.Tokens)4 AuthSignInResult (com.amplifyframework.auth.result.AuthSignInResult)4