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());
}
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"));
}
});
}
Aggregations