Search in sources :

Example 1 with AuthNextUpdateAttributeStep

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

the class RxAuthBindingTest method testUpdateUserAttribute.

/**
 * Tests that a successful request to update a user attribute will propagate a completion
 * back through the binding.
 * @throws InterruptedException If test observer is interrupted while awaiting terminal event
 */
@Test
public void testUpdateUserAttribute() throws InterruptedException {
    // Arrange an invocation of the success Action
    AuthUserAttribute attribute = new AuthUserAttribute(AuthUserAttributeKey.custom(ATTRIBUTE_KEY), ATTRIBUTE_VAL);
    AuthUpdateAttributeResult expected = new AuthUpdateAttributeResult(true, new AuthNextUpdateAttributeStep(AuthUpdateAttributeStep.DONE, Collections.emptyMap(), null));
    doAnswer(invocation -> {
        Consumer<AuthUpdateAttributeResult> onCompletion = invocation.getArgument(1);
        onCompletion.accept(expected);
        return null;
    }).when(delegate).updateUserAttribute(any(), anyConsumer(), anyConsumer());
    // Act: call the binding
    TestObserver<AuthUpdateAttributeResult> observer = auth.updateUserAttribute(attribute).test();
    // Assert: Completable completes with success
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertNoErrors().assertValue(expected);
}
Also used : AuthUserAttribute(com.amplifyframework.auth.AuthUserAttribute) AuthUpdateAttributeResult(com.amplifyframework.auth.result.AuthUpdateAttributeResult) AuthNextUpdateAttributeStep(com.amplifyframework.auth.result.step.AuthNextUpdateAttributeStep) Test(org.junit.Test)

Example 2 with AuthNextUpdateAttributeStep

use of com.amplifyframework.auth.result.step.AuthNextUpdateAttributeStep 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 3 with AuthNextUpdateAttributeStep

use of com.amplifyframework.auth.result.step.AuthNextUpdateAttributeStep 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 4 with AuthNextUpdateAttributeStep

use of com.amplifyframework.auth.result.step.AuthNextUpdateAttributeStep 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)4 AuthNextUpdateAttributeStep (com.amplifyframework.auth.result.step.AuthNextUpdateAttributeStep)4 AuthUserAttribute (com.amplifyframework.auth.AuthUserAttribute)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 UserCodeDeliveryDetails (com.amazonaws.mobile.client.results.UserCodeDeliveryDetails)2 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 List (java.util.List)2 Map (java.util.Map)2 JSONException (org.json.JSONException)2 Test (org.junit.Test)2 AuthCodeDeliveryDetails (com.amplifyframework.auth.AuthCodeDeliveryDetails)1 AuthUserAttributeKey (com.amplifyframework.auth.AuthUserAttributeKey)1 AWSCognitoAuthUpdateUserAttributeOptions (com.amplifyframework.auth.cognito.options.AWSCognitoAuthUpdateUserAttributeOptions)1 AWSCognitoAuthUpdateUserAttributesOptions (com.amplifyframework.auth.cognito.options.AWSCognitoAuthUpdateUserAttributesOptions)1