Search in sources :

Example 6 with AuthUpdateAttributeResult

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

the class RxAuthBindingTest method testUpdateUserAttributes.

/**
 * Tests that a successful request to update user attributes will propagate a completion
 * back through the binding.
 * @throws InterruptedException If test observer is interrupted while awaiting terminal event
 */
@Test
public void testUpdateUserAttributes() throws InterruptedException {
    // Arrange an invocation of the success Action
    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<AuthUserAttributeKey, AuthUpdateAttributeResult> attributeResultMap = new HashMap<>();
    attributeResultMap.put(attributeKey, new AuthUpdateAttributeResult(true, new AuthNextUpdateAttributeStep(AuthUpdateAttributeStep.CONFIRM_ATTRIBUTE_WITH_CODE, Collections.emptyMap(), new AuthCodeDeliveryDetails(DESTINATION, DeliveryMedium.EMAIL, ATTRIBUTE_NAME))));
    attributeResultMap.put(attributeKeyWithoutCode, new AuthUpdateAttributeResult(true, new AuthNextUpdateAttributeStep(AuthUpdateAttributeStep.DONE, Collections.emptyMap(), null)));
    doAnswer(invocation -> {
        Consumer<Map<AuthUserAttributeKey, AuthUpdateAttributeResult>> onCompletion = invocation.getArgument(1);
        onCompletion.accept(attributeResultMap);
        return null;
    }).when(delegate).updateUserAttributes(any(), anyConsumer(), anyConsumer());
    // Act: call the binding
    TestObserver<Map<AuthUserAttributeKey, AuthUpdateAttributeResult>> observer = auth.updateUserAttributes(attributes).test();
    // Assert: Completable completes with success
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertNoErrors().assertValue(attributeResultMap);
}
Also used : AuthUserAttribute(com.amplifyframework.auth.AuthUserAttribute) AuthUpdateAttributeResult(com.amplifyframework.auth.result.AuthUpdateAttributeResult) AuthNextUpdateAttributeStep(com.amplifyframework.auth.result.step.AuthNextUpdateAttributeStep) AuthCodeDeliveryDetails(com.amplifyframework.auth.AuthCodeDeliveryDetails) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AuthUserAttributeKey(com.amplifyframework.auth.AuthUserAttributeKey) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 7 with AuthUpdateAttributeResult

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

the class AWSCognitoAuthPlugin method updateUserAttributes.

@Override
public void updateUserAttributes(@NonNull List<AuthUserAttribute> attributes, @NonNull AuthUpdateUserAttributesOptions options, @NonNull Consumer<Map<AuthUserAttributeKey, AuthUpdateAttributeResult>> onSuccess, @NonNull Consumer<AuthException> onError) {
    final Map<String, String> clientMetadata = new HashMap<>();
    if (options instanceof AWSCognitoAuthUpdateUserAttributesOptions) {
        AWSCognitoAuthUpdateUserAttributesOptions cognitoOptions = (AWSCognitoAuthUpdateUserAttributesOptions) options;
        clientMetadata.putAll(cognitoOptions.getMetadata());
    }
    Map<String, String> attributesMap = new HashMap<>();
    for (AuthUserAttribute attribute : attributes) {
        attributesMap.put(attribute.getKey().getKeyString(), attribute.getValue());
    }
    awsMobileClient.updateUserAttributes(attributesMap, clientMetadata, new Callback<List<UserCodeDeliveryDetails>>() {

        @Override
        public void onResult(List<UserCodeDeliveryDetails> result) {
            Map<String, UserCodeDeliveryDetails> codeDetailsMap = new HashMap<>();
            Map<AuthUserAttributeKey, AuthUpdateAttributeResult> resultMap = new HashMap<>();
            for (UserCodeDeliveryDetails details : result) {
                codeDetailsMap.put(details.getAttributeName(), details);
            }
            for (String attributeKey : attributesMap.keySet()) {
                if (codeDetailsMap.containsKey(attributeKey)) {
                    resultMap.put(AuthUserAttributeKey.custom(attributeKey), new AuthUpdateAttributeResult(true, new AuthNextUpdateAttributeStep(AuthUpdateAttributeStep.CONFIRM_ATTRIBUTE_WITH_CODE, Collections.emptyMap(), convertCodeDeliveryDetails(codeDetailsMap.get(attributeKey)))));
                } else {
                    resultMap.put(AuthUserAttributeKey.custom(attributeKey), new AuthUpdateAttributeResult(true, new AuthNextUpdateAttributeStep(AuthUpdateAttributeStep.DONE, Collections.emptyMap(), null)));
                }
            }
            onSuccess.accept(resultMap);
        }

        @Override
        public void onError(Exception error) {
            onError.accept(new AuthException("Failed to update user attributes", error, "See attached exception for more details"));
        }
    });
}
Also used : AuthUserAttribute(com.amplifyframework.auth.AuthUserAttribute) 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) AWSCognitoAuthUpdateUserAttributesOptions(com.amplifyframework.auth.cognito.options.AWSCognitoAuthUpdateUserAttributesOptions) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) HashMap(java.util.HashMap)

Example 8 with AuthUpdateAttributeResult

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

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