Search in sources :

Example 1 with AWSCognitoAuthUpdateUserAttributeOptions

use of com.amplifyframework.auth.cognito.options.AWSCognitoAuthUpdateUserAttributeOptions in project amplify-android by aws-amplify.

the class AuthComponentTest method updateUserAttributeWithOptions.

/**
 * Tests that updateUserAttribute method of the Auth wrapper of AWSMobileClient (AMC) calls
 * AMC.updateUserAttributes with the user attribute and options 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 updateUserAttributeWithOptions() 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));
    AWSCognitoAuthUpdateUserAttributeOptions.CognitoBuilder options = AWSCognitoAuthUpdateUserAttributeOptions.builder();
    Map<String, String> metadata = new HashMap<String, String>();
    metadata.put("key", "value");
    options.metadata(metadata);
    AWSCognitoAuthUpdateUserAttributeOptions 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());
    AuthUpdateAttributeResult result = synchronousAuth.updateUserAttribute(attribute, builtOptions);
    assertTrue(result.isUpdated());
    assertEquals(AuthUpdateAttributeStep.CONFIRM_ATTRIBUTE_WITH_CODE, result.getNextStep().getUpdateAttributeStep());
    validateCodeDeliveryDetails(result.getNextStep().getCodeDeliveryDetails());
    verify(mobileClient).updateUserAttributes(eq(attributeMap), eq(metadata), Mockito.<Callback<List<UserCodeDeliveryDetails>>>any());
}
Also used : AuthUserAttribute(com.amplifyframework.auth.AuthUserAttribute) AuthUpdateAttributeResult(com.amplifyframework.auth.result.AuthUpdateAttributeResult) HashMap(java.util.HashMap) 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) AWSCognitoAuthUpdateUserAttributeOptions(com.amplifyframework.auth.cognito.options.AWSCognitoAuthUpdateUserAttributeOptions) Test(org.junit.Test)

Example 2 with AWSCognitoAuthUpdateUserAttributeOptions

use of com.amplifyframework.auth.cognito.options.AWSCognitoAuthUpdateUserAttributeOptions in project amplify-android by aws-amplify.

the class AWSCognitoAuthPlugin method updateUserAttribute.

@Override
public void updateUserAttribute(@NonNull AuthUserAttribute attribute, @NonNull AuthUpdateUserAttributeOptions options, @NonNull Consumer<AuthUpdateAttributeResult> onSuccess, @NonNull Consumer<AuthException> onError) {
    final Map<String, String> clientMetadata = new HashMap<>();
    if (options instanceof AWSCognitoAuthUpdateUserAttributeOptions) {
        AWSCognitoAuthUpdateUserAttributeOptions cognitoOptions = (AWSCognitoAuthUpdateUserAttributeOptions) options;
        clientMetadata.putAll(cognitoOptions.getMetadata());
    }
    awsMobileClient.updateUserAttributes(Collections.singletonMap(attribute.getKey().getKeyString(), attribute.getValue()), clientMetadata, new Callback<List<UserCodeDeliveryDetails>>() {

        @Override
        public void onResult(List<UserCodeDeliveryDetails> result) {
            if (result.size() == 0) {
                onSuccess.accept(new AuthUpdateAttributeResult(true, new AuthNextUpdateAttributeStep(AuthUpdateAttributeStep.DONE, Collections.emptyMap(), null)));
            } else {
                onSuccess.accept(new AuthUpdateAttributeResult(true, new AuthNextUpdateAttributeStep(AuthUpdateAttributeStep.CONFIRM_ATTRIBUTE_WITH_CODE, Collections.emptyMap(), convertCodeDeliveryDetails(result.get(0)))));
            }
        }

        @Override
        public void onError(Exception error) {
            onError.accept(new AuthException("Failed to update user attributes", error, "See attached exception for more details"));
        }
    });
}
Also used : AuthUpdateAttributeResult(com.amplifyframework.auth.result.AuthUpdateAttributeResult) AuthNextUpdateAttributeStep(com.amplifyframework.auth.result.step.AuthNextUpdateAttributeStep) HashMap(java.util.HashMap) UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) AuthException(com.amplifyframework.auth.AuthException) AuthException(com.amplifyframework.auth.AuthException) JSONException(org.json.JSONException) AmplifyException(com.amplifyframework.AmplifyException) NotAuthorizedException(com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException) AuthNavigationException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthNavigationException) ArrayList(java.util.ArrayList) List(java.util.List) AWSCognitoAuthUpdateUserAttributeOptions(com.amplifyframework.auth.cognito.options.AWSCognitoAuthUpdateUserAttributeOptions)

Aggregations

UserCodeDeliveryDetails (com.amazonaws.mobile.client.results.UserCodeDeliveryDetails)2 AWSCognitoAuthUpdateUserAttributeOptions (com.amplifyframework.auth.cognito.options.AWSCognitoAuthUpdateUserAttributeOptions)2 AuthUpdateAttributeResult (com.amplifyframework.auth.result.AuthUpdateAttributeResult)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 AuthNavigationException (com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthNavigationException)1 NotAuthorizedException (com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException)1 AmplifyException (com.amplifyframework.AmplifyException)1 AuthException (com.amplifyframework.auth.AuthException)1 AuthUserAttribute (com.amplifyframework.auth.AuthUserAttribute)1 AuthNextUpdateAttributeStep (com.amplifyframework.auth.result.step.AuthNextUpdateAttributeStep)1 RandomString (com.amplifyframework.testutils.random.RandomString)1 JSONException (org.json.JSONException)1 Test (org.junit.Test)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1