Search in sources :

Example 1 with UserCodeDeliveryDetails

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

the class AuthComponentTest method updateUserAttribute.

/**
 * Tests that updateUserAttribute method of the Auth wrapper of AWSMobileClient (AMC) calls
 * AMC.updateUserAttributes with the user attribute it received.
 * Also ensures that in the onResult case, the success callback receives a valid AuthUpdateAttributeResult and in
 * the onError case, the error call back receives an AuthException with the root cause attached.
 * @throws AuthException test fails if this gets thrown since method should succeed
 */
@Test
public void updateUserAttribute() throws AuthException {
    AuthUserAttribute attribute = new AuthUserAttribute(AuthUserAttributeKey.custom(ATTRIBUTE_KEY), ATTRIBUTE_VAL);
    Map<String, String> attributeMap = Collections.singletonMap(attribute.getKey().getKeyString(), attribute.getValue());
    List<UserCodeDeliveryDetails> userCodeDeliveryDetailsList = Collections.singletonList(new UserCodeDeliveryDetails(DESTINATION, DELIVERY_MEDIUM, ATTRIBUTE_NAME));
    doAnswer(invocation -> {
        Callback<List<UserCodeDeliveryDetails>> callback = invocation.getArgument(2);
        callback.onResult(userCodeDeliveryDetailsList);
        return null;
    }).when(mobileClient).updateUserAttributes(any(), any(), Mockito.<Callback<List<UserCodeDeliveryDetails>>>any());
    AuthUpdateAttributeResult result = synchronousAuth.updateUserAttribute(attribute);
    assertTrue(result.isUpdated());
    assertEquals(AuthUpdateAttributeStep.CONFIRM_ATTRIBUTE_WITH_CODE, result.getNextStep().getUpdateAttributeStep());
    validateCodeDeliveryDetails(result.getNextStep().getCodeDeliveryDetails());
    verify(mobileClient).updateUserAttributes(eq(attributeMap), any(), Mockito.<Callback<List<UserCodeDeliveryDetails>>>any());
}
Also used : AuthUserAttribute(com.amplifyframework.auth.AuthUserAttribute) AuthUpdateAttributeResult(com.amplifyframework.auth.result.AuthUpdateAttributeResult) UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) List(java.util.List) ArrayList(java.util.ArrayList) RandomString(com.amplifyframework.testutils.random.RandomString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 2 with UserCodeDeliveryDetails

use of com.amazonaws.mobile.client.results.UserCodeDeliveryDetails 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)

Example 3 with UserCodeDeliveryDetails

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

the class AuthComponentTest method updateUserAttributesWithOptions.

/**
 * Tests that updateUserAttributes method of the Auth wrapper of AWSMobileClient (AMC) Calls
 * AMC.updateUserAttributes with the user attributes and options it received.
 * Also ensures that in the onResult case, the success callback receives a valid Map which maps
 * AuthUserAttributeKey into AuthUpdateAttributeResult, and in the onError case, the error call
 * back receives an AuthException with the root cause attached.
 * @throws AuthException test fails if this gets thrown since method should succeed
 */
@Test
public void updateUserAttributesWithOptions() throws AuthException {
    List<AuthUserAttribute> attributes = new ArrayList<>();
    AuthUserAttributeKey attributeKey = AuthUserAttributeKey.custom(ATTRIBUTE_KEY);
    AuthUserAttributeKey attributeKeyWithoutCode = AuthUserAttributeKey.custom(ATTRIBUTE_KEY_WITHOUT_CODE_DELIVERY);
    attributes.add(new AuthUserAttribute(attributeKey, ATTRIBUTE_VAL));
    attributes.add(new AuthUserAttribute(attributeKeyWithoutCode, ATTRIBUTE_VAL_WITHOUT_CODE_DELIVERY));
    Map<String, String> attributesMap = new HashMap<>();
    for (AuthUserAttribute attribute : attributes) {
        attributesMap.put(attribute.getKey().getKeyString(), attribute.getValue());
    }
    List<UserCodeDeliveryDetails> userCodeDeliveryDetailsList = Collections.singletonList(new UserCodeDeliveryDetails(DESTINATION, DELIVERY_MEDIUM, ATTRIBUTE_NAME));
    AWSCognitoAuthUpdateUserAttributesOptions.CognitoBuilder options = AWSCognitoAuthUpdateUserAttributesOptions.builder();
    Map<String, String> metadata = new HashMap<String, String>();
    metadata.put("key", "value");
    options.metadata(metadata);
    AWSCognitoAuthUpdateUserAttributesOptions builtOptions = options.build();
    doAnswer(invocation -> {
        Callback<List<UserCodeDeliveryDetails>> callback = invocation.getArgument(2);
        callback.onResult(userCodeDeliveryDetailsList);
        return null;
    }).when(mobileClient).updateUserAttributes(any(), any(), Mockito.<Callback<List<UserCodeDeliveryDetails>>>any());
    Map<AuthUserAttributeKey, AuthUpdateAttributeResult> result = synchronousAuth.updateUserAttributes(attributes, builtOptions);
    assertTrue(result.get(attributeKey).isUpdated());
    assertTrue(result.get(attributeKeyWithoutCode).isUpdated());
    assertEquals(AuthUpdateAttributeStep.CONFIRM_ATTRIBUTE_WITH_CODE, result.get(attributeKey).getNextStep().getUpdateAttributeStep());
    assertEquals(AuthUpdateAttributeStep.DONE, result.get(attributeKeyWithoutCode).getNextStep().getUpdateAttributeStep());
    validateCodeDeliveryDetails(result.get(attributeKey).getNextStep().getCodeDeliveryDetails());
    verify(mobileClient).updateUserAttributes(eq(attributesMap), eq(metadata), Mockito.<Callback<List<UserCodeDeliveryDetails>>>any());
}
Also used : AuthUserAttribute(com.amplifyframework.auth.AuthUserAttribute) AuthUpdateAttributeResult(com.amplifyframework.auth.result.AuthUpdateAttributeResult) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) AuthUserAttributeKey(com.amplifyframework.auth.AuthUserAttributeKey) RandomString(com.amplifyframework.testutils.random.RandomString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AWSCognitoAuthUpdateUserAttributesOptions(com.amplifyframework.auth.cognito.options.AWSCognitoAuthUpdateUserAttributesOptions) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 4 with UserCodeDeliveryDetails

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

the class AuthComponentTest method resendUserAttributeConfirmationCode.

/**
 * Tests the resendUserAttributeConfirmationCode method of the Auth wrapper of AWSMobileClient (AMC) Calls
 * AMC.verifyUserAttribute with the user attribute key to be verified.
 * @throws AuthException test fails if this gets thrown since method should succeed
 */
@Test
public void resendUserAttributeConfirmationCode() throws AuthException {
    AuthUserAttributeKey attributeKey = AuthUserAttributeKey.custom(ATTRIBUTE_KEY);
    UserCodeDeliveryDetails userCodeDeliveryDetails = new UserCodeDeliveryDetails(DESTINATION, DELIVERY_MEDIUM, ATTRIBUTE_NAME);
    doAnswer(invocation -> {
        Callback<UserCodeDeliveryDetails> callback = invocation.getArgument(2);
        callback.onResult(userCodeDeliveryDetails);
        return null;
    }).when(mobileClient).verifyUserAttribute(any(), any(), Mockito.<Callback<UserCodeDeliveryDetails>>any());
    AuthCodeDeliveryDetails result = synchronousAuth.resendUserAttributeConfirmationCode(attributeKey);
    validateCodeDeliveryDetails(result);
    verify(mobileClient).verifyUserAttribute(eq(attributeKey.getKeyString()), any(), Mockito.<Callback<UserCodeDeliveryDetails>>any());
}
Also used : AuthCodeDeliveryDetails(com.amplifyframework.auth.AuthCodeDeliveryDetails) UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) AuthUserAttributeKey(com.amplifyframework.auth.AuthUserAttributeKey) Test(org.junit.Test)

Example 5 with UserCodeDeliveryDetails

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

the class AuthComponentTest method resetPasswordWithOptions.

/**
 * Tests that the resetPassword method of the Auth wrapper of AWSMobileClient (AMC) calls AMC.forgotPassword with
 * the username and options it received.
 * Also ensures that in the onResult case, the success callback receives a valid AuthResetPasswordResult and in
 * the onError case, the error call back receives an AuthException with the root cause attached.
 * @throws AuthException test fails if this gets thrown since method should succeed
 */
@Test
public void resetPasswordWithOptions() throws AuthException {
    ForgotPasswordResult amcResult = new ForgotPasswordResult(ForgotPasswordState.CONFIRMATION_CODE);
    amcResult.setParameters(new UserCodeDeliveryDetails(DESTINATION, DELIVERY_MEDIUM, ATTRIBUTE_NAME));
    AWSCognitoAuthResetPasswordOptions.CognitoBuilder options = AWSCognitoAuthResetPasswordOptions.builder();
    Map<String, String> metadata = new HashMap<String, String>();
    metadata.put("key", "value");
    options.metadata(metadata);
    AWSCognitoAuthResetPasswordOptions builtOptions = options.build();
    doAnswer(invocation -> {
        Callback<ForgotPasswordResult> callback = invocation.getArgument(2);
        callback.onResult(amcResult);
        return null;
    }).when(mobileClient).forgotPassword(any(), any(), Mockito.<Callback<ForgotPasswordResult>>any());
    AuthResetPasswordResult result = synchronousAuth.resetPassword(USERNAME, builtOptions);
    assertFalse(result.isPasswordReset());
    assertEquals(AuthResetPasswordStep.CONFIRM_RESET_PASSWORD_WITH_CODE, result.getNextStep().getResetPasswordStep());
    validateCodeDeliveryDetails(result.getNextStep().getCodeDeliveryDetails());
    verify(mobileClient).forgotPassword(eq(USERNAME), eq(metadata), Mockito.<Callback<ForgotPasswordResult>>any());
}
Also used : HashMap(java.util.HashMap) UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) ForgotPasswordResult(com.amazonaws.mobile.client.results.ForgotPasswordResult) RandomString(com.amplifyframework.testutils.random.RandomString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AWSCognitoAuthResetPasswordOptions(com.amplifyframework.auth.cognito.options.AWSCognitoAuthResetPasswordOptions) AuthResetPasswordResult(com.amplifyframework.auth.result.AuthResetPasswordResult) 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