Search in sources :

Example 21 with UserCodeDeliveryDetails

use of com.amazonaws.mobile.client.results.UserCodeDeliveryDetails 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 22 with UserCodeDeliveryDetails

use of com.amazonaws.mobile.client.results.UserCodeDeliveryDetails in project amplify-android by aws-amplify.

the class AuthComponentTest method resetPassword.

/**
 * Tests that the resetPassword method of the Auth wrapper of AWSMobileClient (AMC) calls AMC.forgotPassword with
 * the username it received.
 * Also ensures that in the onResult case, the success callback receives a valid AuthResetPasswordResult 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 resetPassword() throws AuthException {
    ForgotPasswordResult amcResult = new ForgotPasswordResult(ForgotPasswordState.CONFIRMATION_CODE);
    amcResult.setParameters(new UserCodeDeliveryDetails(DESTINATION, DELIVERY_MEDIUM, ATTRIBUTE_NAME));
    doAnswer(invocation -> {
        Callback<ForgotPasswordResult> callback = invocation.getArgument(2);
        callback.onResult(amcResult);
        return null;
    }).when(mobileClient).forgotPassword(any(), any(), Mockito.<Callback<ForgotPasswordResult>>any());
    AuthResetPasswordResult result = synchronousAuth.resetPassword(USERNAME);
    assertFalse(result.isPasswordReset());
    assertEquals(AuthResetPasswordStep.CONFIRM_RESET_PASSWORD_WITH_CODE, result.getNextStep().getResetPasswordStep());
    validateCodeDeliveryDetails(result.getNextStep().getCodeDeliveryDetails());
    verify(mobileClient).forgotPassword(eq(USERNAME), any(), Mockito.<Callback<ForgotPasswordResult>>any());
}
Also used : UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) ForgotPasswordResult(com.amazonaws.mobile.client.results.ForgotPasswordResult) AuthResetPasswordResult(com.amplifyframework.auth.result.AuthResetPasswordResult) Test(org.junit.Test)

Example 23 with UserCodeDeliveryDetails

use of com.amazonaws.mobile.client.results.UserCodeDeliveryDetails in project amplify-android by aws-amplify.

the class AuthComponentTest method resendUserAttributeConfirmationCodeWithOptions.

/**
 * Tests the resendUserAttributeConfirmationCode method of the Auth wrapper of AWSMobileClient (AMC) Calls
 * AMC.verifyUserAttribute with the user attribute key to be verified and options it received.
 * @throws AuthException test fails if this gets thrown since method should succeed
 */
@Test
public void resendUserAttributeConfirmationCodeWithOptions() throws AuthException {
    AuthUserAttributeKey attributeKey = AuthUserAttributeKey.custom(ATTRIBUTE_KEY);
    UserCodeDeliveryDetails userCodeDeliveryDetails = new UserCodeDeliveryDetails(DESTINATION, DELIVERY_MEDIUM, ATTRIBUTE_NAME);
    AWSCognitoAuthResendUserAttributeConfirmationCodeOptions.CognitoBuilder options = AWSCognitoAuthResendUserAttributeConfirmationCodeOptions.builder();
    Map<String, String> metadata = new HashMap<String, String>();
    metadata.put("key", "value");
    options.metadata(metadata);
    AWSCognitoAuthResendUserAttributeConfirmationCodeOptions builtOptions = options.build();
    doAnswer(invocation -> {
        Callback<UserCodeDeliveryDetails> callback = invocation.getArgument(2);
        callback.onResult(userCodeDeliveryDetails);
        return null;
    }).when(mobileClient).verifyUserAttribute(any(), any(), Mockito.<Callback<UserCodeDeliveryDetails>>any());
    AuthCodeDeliveryDetails result = synchronousAuth.resendUserAttributeConfirmationCode(attributeKey, builtOptions);
    validateCodeDeliveryDetails(result);
    verify(mobileClient).verifyUserAttribute(eq(attributeKey.getKeyString()), eq(metadata), Mockito.<Callback<UserCodeDeliveryDetails>>any());
}
Also used : AuthCodeDeliveryDetails(com.amplifyframework.auth.AuthCodeDeliveryDetails) AWSCognitoAuthResendUserAttributeConfirmationCodeOptions(com.amplifyframework.auth.cognito.options.AWSCognitoAuthResendUserAttributeConfirmationCodeOptions) HashMap(java.util.HashMap) UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) AuthUserAttributeKey(com.amplifyframework.auth.AuthUserAttributeKey) RandomString(com.amplifyframework.testutils.random.RandomString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 24 with UserCodeDeliveryDetails

use of com.amazonaws.mobile.client.results.UserCodeDeliveryDetails in project aws-sdk-android by aws-amplify.

the class OAuth2Utils method _signUp.

private Runnable _signUp(final String username, final String password, final Map<String, String> userAttributes, final Map<String, String> validationData, final Map<String, String> clientMetadata, final Callback<SignUpResult> callback) {
    return new Runnable() {

        @Override
        public void run() {
            final CognitoUserAttributes cognitoUserAttr = new CognitoUserAttributes();
            for (final String key : userAttributes.keySet()) {
                cognitoUserAttr.addAttribute(key, userAttributes.get(key));
            }
            userpool.signUp(username, password, cognitoUserAttr, validationData, clientMetadata, new SignUpHandler() {

                @Override
                public void onSuccess(final CognitoUser user, final com.amazonaws.services.cognitoidentityprovider.model.SignUpResult signUpResult) {
                    signUpUser = user;
                    if (signUpResult == null) {
                        callback.onError(new Exception("SignUpResult received is null"));
                        return;
                    }
                    // CognitoUserCodeDeliveryDetails only when the user is not confirmed.
                    if (signUpResult.getCodeDeliveryDetails() == null) {
                        callback.onResult(new SignUpResult(signUpResult.getUserConfirmed(), null, signUpResult.getUserSub()));
                    } else {
                        UserCodeDeliveryDetails userCodeDeliveryDetails = new UserCodeDeliveryDetails(signUpResult.getCodeDeliveryDetails().getDestination(), signUpResult.getCodeDeliveryDetails().getDeliveryMedium(), signUpResult.getCodeDeliveryDetails().getAttributeName());
                        callback.onResult(new SignUpResult(signUpResult.getUserConfirmed(), userCodeDeliveryDetails, signUpResult.getUserSub()));
                    }
                }

                @Override
                public void onFailure(Exception exception) {
                    callback.onError(exception);
                }
            });
        }
    };
}
Also used : CognitoUserCodeDeliveryDetails(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails) UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) JSONException(org.json.JSONException) InvalidUserPoolConfigurationException(com.amazonaws.services.cognitoidentityprovider.model.InvalidUserPoolConfigurationException) AmazonClientException(com.amazonaws.AmazonClientException) NotAuthorizedException(com.amazonaws.services.cognitoidentity.model.NotAuthorizedException) CognitoUserAttributes(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserAttributes) ReturningRunnable(com.amazonaws.mobile.client.internal.ReturningRunnable) SignUpHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.SignUpHandler) CognitoUser(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser) SignUpResult(com.amazonaws.mobile.client.results.SignUpResult)

Example 25 with UserCodeDeliveryDetails

use of com.amazonaws.mobile.client.results.UserCodeDeliveryDetails in project aws-sdk-android by aws-amplify.

the class OAuth2Utils method _forgotPassword.

private Runnable _forgotPassword(final String username, final Map<String, String> clientMetadata, final Callback<ForgotPasswordResult> callback) {
    return new Runnable() {

        @Override
        public void run() {
            forgotPasswordCallback = new InternalCallback<ForgotPasswordResult>(callback);
            userpool.getUser(username).forgotPasswordInBackground(clientMetadata, new ForgotPasswordHandler() {

                @Override
                public void onSuccess() {
                    forgotPasswordCallback.onResult(new ForgotPasswordResult(ForgotPasswordState.DONE));
                }

                @Override
                public void getResetCode(ForgotPasswordContinuation continuation) {
                    forgotPasswordContinuation = continuation;
                    ForgotPasswordResult result = new ForgotPasswordResult(ForgotPasswordState.CONFIRMATION_CODE);
                    CognitoUserCodeDeliveryDetails parameters = continuation.getParameters();
                    result.setParameters(new UserCodeDeliveryDetails(parameters.getDestination(), parameters.getDeliveryMedium(), parameters.getAttributeName()));
                    forgotPasswordCallback.onResult(result);
                }

                @Override
                public void onFailure(Exception exception) {
                    forgotPasswordCallback.onError(exception);
                }
            });
        }
    };
}
Also used : CognitoUserCodeDeliveryDetails(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails) ForgotPasswordHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.ForgotPasswordHandler) ReturningRunnable(com.amazonaws.mobile.client.internal.ReturningRunnable) ForgotPasswordContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.ForgotPasswordContinuation) CognitoUserCodeDeliveryDetails(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails) UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) ForgotPasswordResult(com.amazonaws.mobile.client.results.ForgotPasswordResult) JSONException(org.json.JSONException) InvalidUserPoolConfigurationException(com.amazonaws.services.cognitoidentityprovider.model.InvalidUserPoolConfigurationException) AmazonClientException(com.amazonaws.AmazonClientException) NotAuthorizedException(com.amazonaws.services.cognitoidentity.model.NotAuthorizedException)

Aggregations

UserCodeDeliveryDetails (com.amazonaws.mobile.client.results.UserCodeDeliveryDetails)28 Test (org.junit.Test)18 HashMap (java.util.HashMap)14 RandomString (com.amplifyframework.testutils.random.RandomString)11 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)11 JSONException (org.json.JSONException)8 SignInResult (com.amazonaws.mobile.client.results.SignInResult)7 SignUpResult (com.amazonaws.mobile.client.results.SignUpResult)7 ArrayList (java.util.ArrayList)7 List (java.util.List)7 CognitoUserCodeDeliveryDetails (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails)6 AuthUserAttribute (com.amplifyframework.auth.AuthUserAttribute)6 AuthSignUpResult (com.amplifyframework.auth.result.AuthSignUpResult)6 AuthUpdateAttributeResult (com.amplifyframework.auth.result.AuthUpdateAttributeResult)6 AmazonClientException (com.amazonaws.AmazonClientException)5 ReturningRunnable (com.amazonaws.mobile.client.internal.ReturningRunnable)5 NotAuthorizedException (com.amazonaws.services.cognitoidentity.model.NotAuthorizedException)5 InvalidUserPoolConfigurationException (com.amazonaws.services.cognitoidentityprovider.model.InvalidUserPoolConfigurationException)5 Tokens (com.amazonaws.mobile.client.results.Tokens)4 AuthSignInResult (com.amplifyframework.auth.result.AuthSignInResult)4