Search in sources :

Example 1 with AWSCognitoAuthSignUpOptions

use of com.amplifyframework.auth.cognito.options.AWSCognitoAuthSignUpOptions in project amplify-android by aws-amplify.

the class AuthComponentTest method signUp.

/**
 * Tests that the signUp method of the Auth wrapper of AWSMobileClient (AMC) calls AMC.signUp() with the username
 * and password it received and, if included, the userAttributes and validationData sent in the options object.
 * Also ensures that in the onResult case, the success callback receives a valid AuthSignUpResult.
 * @throws AuthException test fails if this gets thrown since method should succeed
 */
@Test
public void signUp() throws AuthException {
    SignUpResult amcResult = new SignUpResult(false, new UserCodeDeliveryDetails(DESTINATION, DELIVERY_MEDIUM, ATTRIBUTE_NAME), USER_SUB);
    doAnswer(invocation -> {
        Callback<SignUpResult> callback = invocation.getArgument(5);
        callback.onResult(amcResult);
        return null;
    }).when(mobileClient).signUp(any(), any(), any(), any(), any(), Mockito.<Callback<SignUpResult>>any());
    AWSCognitoAuthSignUpOptions options = AWSCognitoAuthSignUpOptions.builder().userAttribute(AuthUserAttributeKey.email(), ATTRIBUTE_VAL).validationData(VALIDATIONDATA).clientMetadata(CLIENTMETADATA).build();
    AuthSignUpResult result = synchronousAuth.signUp(USERNAME, PASSWORD, options);
    validateSignUpResult(result, AuthSignUpStep.CONFIRM_SIGN_UP_STEP);
    Map<String, String> expectedAttributeMap = Collections.singletonMap(ATTRIBUTE_KEY, ATTRIBUTE_VAL);
    verify(mobileClient).signUp(eq(USERNAME), eq(PASSWORD), eq(expectedAttributeMap), eq(VALIDATIONDATA), eq(CLIENTMETADATA), Mockito.<Callback<SignUpResult>>any());
}
Also used : AWSCognitoAuthSignUpOptions(com.amplifyframework.auth.cognito.options.AWSCognitoAuthSignUpOptions) UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) AuthSignUpResult(com.amplifyframework.auth.result.AuthSignUpResult) RandomString(com.amplifyframework.testutils.random.RandomString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AuthSignUpResult(com.amplifyframework.auth.result.AuthSignUpResult) SignUpResult(com.amazonaws.mobile.client.results.SignUpResult) Test(org.junit.Test)

Example 2 with AWSCognitoAuthSignUpOptions

use of com.amplifyframework.auth.cognito.options.AWSCognitoAuthSignUpOptions 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

SignUpResult (com.amazonaws.mobile.client.results.SignUpResult)2 AWSCognitoAuthSignUpOptions (com.amplifyframework.auth.cognito.options.AWSCognitoAuthSignUpOptions)2 AuthSignUpResult (com.amplifyframework.auth.result.AuthSignUpResult)2 UserCodeDeliveryDetails (com.amazonaws.mobile.client.results.UserCodeDeliveryDetails)1 AuthNavigationException (com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthNavigationException)1 NotAuthorizedException (com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException)1 AmplifyException (com.amplifyframework.AmplifyException)1 AuthException (com.amplifyframework.auth.AuthException)1 AuthUserAttribute (com.amplifyframework.auth.AuthUserAttribute)1 RandomString (com.amplifyframework.testutils.random.RandomString)1 HashMap (java.util.HashMap)1 JSONException (org.json.JSONException)1 Test (org.junit.Test)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1