Search in sources :

Example 6 with AuthUserAttribute

use of com.amplifyframework.auth.AuthUserAttribute 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 7 with AuthUserAttribute

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

the class RxAuthBindingTest method testFetchUserAttributes.

/**
 * Tests that a successful request to fetch user attributes will propagate a completion
 * back through the binding.
 * @throws InterruptedException  If test observer is interrupted while awaiting terminal event
 */
@Test
public void testFetchUserAttributes() throws InterruptedException {
    // Arrange an invocation of the success Action
    List<AuthUserAttribute> expected = Collections.singletonList(new AuthUserAttribute(AuthUserAttributeKey.custom(ATTRIBUTE_KEY), ATTRIBUTE_VAL));
    doAnswer(invocation -> {
        // 0 = onComplete, 1 = onFailure
        Consumer<List<AuthUserAttribute>> onCompletion = invocation.getArgument(0);
        onCompletion.accept(expected);
        return null;
    }).when(delegate).fetchUserAttributes(anyConsumer(), anyConsumer());
    // Act: call the binding
    TestObserver<List<AuthUserAttribute>> observer = auth.fetchUserAttributes().test();
    // Assert: Completable completes with success
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertNoErrors().assertValue(expected);
}
Also used : AuthUserAttribute(com.amplifyframework.auth.AuthUserAttribute) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 8 with AuthUserAttribute

use of com.amplifyframework.auth.AuthUserAttribute 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 9 with AuthUserAttribute

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

the class AWSCognitoAuthPlugin method confirmSignIn.

@Override
public void confirmSignIn(@NonNull String confirmationCode, @NonNull AuthConfirmSignInOptions options, @NonNull Consumer<AuthSignInResult> onSuccess, @NonNull Consumer<AuthException> onException) {
    final Map<String, String> metadata = new HashMap<>();
    final Map<String, String> userAttributes = new HashMap<>();
    if (options instanceof AWSCognitoAuthConfirmSignInOptions) {
        metadata.putAll(((AWSCognitoAuthConfirmSignInOptions) options).getMetadata());
        for (AuthUserAttribute attribute : ((AWSCognitoAuthConfirmSignInOptions) options).getUserAttributes()) {
            userAttributes.put(attribute.getKey().getKeyString(), attribute.getValue());
        }
    }
    awsMobileClient.confirmSignIn(confirmationCode, metadata, userAttributes, new Callback<SignInResult>() {

        @Override
        public void onResult(SignInResult result) {
            try {
                AuthSignInResult newResult = convertSignInResult(result);
                fetchAndSetUserId(() -> onSuccess.accept(newResult));
            } catch (AuthException exception) {
                onException.accept(exception);
            }
        }

        @Override
        public void onError(Exception error) {
            onException.accept(CognitoAuthExceptionConverter.lookup(error, "Confirm sign in failed"));
        }
    });
}
Also used : AuthUserAttribute(com.amplifyframework.auth.AuthUserAttribute) HashMap(java.util.HashMap) AWSCognitoAuthConfirmSignInOptions(com.amplifyframework.auth.cognito.options.AWSCognitoAuthConfirmSignInOptions) AuthException(com.amplifyframework.auth.AuthException) AuthSignInResult(com.amplifyframework.auth.result.AuthSignInResult) 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) SignInResult(com.amazonaws.mobile.client.results.SignInResult) AuthSignInResult(com.amplifyframework.auth.result.AuthSignInResult)

Example 10 with AuthUserAttribute

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

the class AWSCognitoAuthPlugin method signUp.

@Override
public void signUp(@NonNull String username, @NonNull String password, @NonNull AuthSignUpOptions options, @NonNull final Consumer<AuthSignUpResult> onSuccess, @NonNull final Consumer<AuthException> onException) {
    Map<String, String> userAttributes = new HashMap<>();
    Map<String, String> validationData = new HashMap<>();
    Map<String, String> clientMetadata = new HashMap<>();
    if (options.getUserAttributes() != null) {
        for (AuthUserAttribute attribute : options.getUserAttributes()) {
            userAttributes.put(attribute.getKey().getKeyString(), attribute.getValue());
        }
    }
    if (options instanceof AWSCognitoAuthSignUpOptions) {
        validationData = ((AWSCognitoAuthSignUpOptions) options).getValidationData();
        clientMetadata = ((AWSCognitoAuthSignUpOptions) options).getClientMetadata();
    }
    awsMobileClient.signUp(username, password, userAttributes, validationData, clientMetadata, new Callback<SignUpResult>() {

        @Override
        public void onResult(SignUpResult result) {
            onSuccess.accept(convertSignUpResult(result, username));
        }

        @Override
        public void onError(Exception error) {
            onException.accept(CognitoAuthExceptionConverter.lookup(error, "Sign up failed"));
        }
    });
}
Also used : AuthUserAttribute(com.amplifyframework.auth.AuthUserAttribute) AWSCognitoAuthSignUpOptions(com.amplifyframework.auth.cognito.options.AWSCognitoAuthSignUpOptions) HashMap(java.util.HashMap) 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) AuthSignUpResult(com.amplifyframework.auth.result.AuthSignUpResult) SignUpResult(com.amazonaws.mobile.client.results.SignUpResult)

Aggregations

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