Search in sources :

Example 1 with SignUpHandler

use of com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.SignUpHandler in project aws-sdk-android by aws-amplify.

the class SignUpActivity method signUp.

/**
 * Retrieve input and return to caller.
 * @param view the Android View
 */
public void signUp(final View view) {
    final String username = signUpView.getUserName();
    final String password = signUpView.getPassword();
    final String givenName = signUpView.getGivenName();
    final String email = signUpView.getEmail();
    final String phone = signUpView.getPhone();
    Log.d(LOG_TAG, "username = " + username);
    Log.d(LOG_TAG, "given_name = " + givenName);
    Log.d(LOG_TAG, "email = " + email);
    Log.d(LOG_TAG, "phone = " + phone);
    if (username.isEmpty()) {
        showError(getString(R.string.sign_up_username_missing));
        return;
    }
    if (password.length() < 6) {
        showError(getString(R.string.password_length_validation_failed));
        return;
    }
    final CognitoUserAttributes userAttributes = new CognitoUserAttributes();
    userAttributes.addAttribute(GIVEN_NAME, givenName);
    userAttributes.addAttribute(EMAIL_ADDRESS, email);
    if (!phone.isEmpty()) {
        userAttributes.addAttribute(PHONE_NUMBER, phone);
    }
    final AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle(getString(R.string.sign_up_in_progress)).setMessage(getString(R.string.please_wait)).setNeutralButton(android.R.string.ok, null);
    final AlertDialog alertDialog = builder.show();
    mUserPool.signUpInBackground(username, password, userAttributes, null, new SignUpHandler() {

        @Override
        public void onSuccess(CognitoUser user, SignUpResult signUpResult) {
            alertDialog.dismiss();
            final Intent intent = new Intent();
            intent.putExtra(USERNAME, username);
            intent.putExtra(PASSWORD, password);
            intent.putExtra(IS_SIGN_UP_CONFIRMED, signUpResult.getUserConfirmed());
            intent.putExtra(CONFIRMATION_DESTINATION, signUpResult.getCodeDeliveryDetails().getDestination());
            setResult(RESULT_OK, intent);
            finish();
        }

        @Override
        public void onFailure(Exception exception) {
            alertDialog.dismiss();
            showError(exception.getLocalizedMessage() != null ? getErrorMessageFromException(exception) : "");
        }
    });
}
Also used : AlertDialog(android.app.AlertDialog) CognitoUserAttributes(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserAttributes) SignUpHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.SignUpHandler) Intent(android.content.Intent) CognitoUser(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser) CognitoUserPoolsSignInProvider.getErrorMessageFromException(com.amazonaws.mobile.auth.userpools.CognitoUserPoolsSignInProvider.getErrorMessageFromException) SignUpResult(com.amazonaws.services.cognitoidentityprovider.model.SignUpResult)

Example 2 with SignUpHandler

use of com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.SignUpHandler in project aws-sdk-android by aws-amplify.

the class CognitoIdentityProviderUserPoolTest method signUpResourceNotFoundException.

// Register with invalid clientId
@Test
public void signUpResourceNotFoundException() throws Exception {
    testPool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET, mockCSIClient);
    ResourceNotFoundException exception = new ResourceNotFoundException("registration failed");
    doThrow(exception).when(mockCSIClient).signUp(any(SignUpRequest.class));
    testPool.signUp(TEST_USER_NAME, "null", TEST_IN_USER_ATTRIBUTES_LIST, TEST_IN_VALIDATION_DATA, new SignUpHandler() {

        @Override
        public void onSuccess(CognitoUser user, SignUpResult signUpResult) {
            assertNotNull(user);
            assertEquals(TEST_USER_NAME, user.getUserId());
        }

        @Override
        public void onFailure(Exception exception) {
            assertNotNull(exception);
            assertTrue(exception instanceof ResourceNotFoundException);
        }
    });
}
Also used : CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) SignUpHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.SignUpHandler) SignUpRequest(com.amazonaws.services.cognitoidentityprovider.model.SignUpRequest) ResourceNotFoundException(com.amazonaws.services.cognitoidentityprovider.model.ResourceNotFoundException) CognitoUser(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser) UnexpectedLambdaException(com.amazonaws.services.cognitoidentityprovider.model.UnexpectedLambdaException) ResourceNotFoundException(com.amazonaws.services.cognitoidentityprovider.model.ResourceNotFoundException) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) SignUpResult(com.amazonaws.services.cognitoidentityprovider.model.SignUpResult) Test(org.junit.Test)

Example 3 with SignUpHandler

use of com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.SignUpHandler in project aws-sdk-android by aws-amplify.

the class CognitoIdentityProviderUserPoolTest method signUpPasswordException.

// Register with invalid password
@Test
public void signUpPasswordException() throws Exception {
    testPool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET, mockCSIClient);
    InvalidParameterException exception = new InvalidParameterException("registration failed");
    doThrow(exception).when(mockCSIClient).signUp(any(SignUpRequest.class));
    testPool.signUp(TEST_USER_NAME, "null", TEST_IN_USER_ATTRIBUTES_LIST, TEST_IN_VALIDATION_DATA, new SignUpHandler() {

        @Override
        public void onSuccess(CognitoUser user, SignUpResult signUpResult) {
            assertNotNull(user);
            assertNotNull(signUpResult.getUserConfirmed());
            assertEquals(TEST_USER_NAME, user.getUserId());
        }

        @Override
        public void onFailure(Exception exception) {
            assertNotNull(exception);
            assertTrue(exception instanceof InvalidParameterException);
        }
    });
}
Also used : InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) SignUpHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.SignUpHandler) SignUpRequest(com.amazonaws.services.cognitoidentityprovider.model.SignUpRequest) CognitoUser(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser) UnexpectedLambdaException(com.amazonaws.services.cognitoidentityprovider.model.UnexpectedLambdaException) ResourceNotFoundException(com.amazonaws.services.cognitoidentityprovider.model.ResourceNotFoundException) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) SignUpResult(com.amazonaws.services.cognitoidentityprovider.model.SignUpResult) Test(org.junit.Test)

Example 4 with SignUpHandler

use of com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.SignUpHandler in project aws-sdk-android by aws-amplify.

the class CognitoIdentityProviderUserPoolTest method signUpUserInCurrentThread.

@Test
public void signUpUserInCurrentThread() throws Exception {
    testPool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET, mockCSIClient);
    doReturn(TEST_REGISTER_USER_RESPONSE).when(mockCSIClient).signUp(any(SignUpRequest.class));
    expected_result_boolean = true;
    testPool.signUp(TEST_USER_NAME, TEST_USER_PASSWORD, TEST_IN_USER_ATTRIBUTES_LIST, null, new SignUpHandler() {

        @Override
        public void onSuccess(CognitoUser user, SignUpResult signUpResult) {
            // Verify the arguments
            ArgumentCaptor<SignUpRequest> argumentCaptor = ArgumentCaptor.forClass(SignUpRequest.class);
            verify(mockCSIClient).signUp(argumentCaptor.capture());
            SignUpRequest requestSent = argumentCaptor.getValue();
            assertNotNull(requestSent);
            assertEquals(TEST_USER_NAME, requestSent.getUsername());
            assertEquals(TEST_USER_PASSWORD, requestSent.getPassword());
            assertNotNull(requestSent.getUserAttributes());
            assertEquals(2, requestSent.getUserAttributes().size());
            assertNull(requestSent.getValidationData());
            // Verify results
            assertNotNull(user);
            assertNotNull(signUpResult.getUserConfirmed());
            assertNotNull(signUpResult.getCodeDeliveryDetails());
            assertEquals(TEST_USER_NAME, user.getUserId());
            assertEquals(signUpResult.getUserConfirmed(), expected_result_boolean);
        }

        @Override
        public void onFailure(Exception exception) {
            assertNull(exception);
        }
    });
}
Also used : CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) ArgumentCaptor(org.mockito.ArgumentCaptor) SignUpHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.SignUpHandler) SignUpRequest(com.amazonaws.services.cognitoidentityprovider.model.SignUpRequest) CognitoUser(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser) UnexpectedLambdaException(com.amazonaws.services.cognitoidentityprovider.model.UnexpectedLambdaException) ResourceNotFoundException(com.amazonaws.services.cognitoidentityprovider.model.ResourceNotFoundException) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) SignUpResult(com.amazonaws.services.cognitoidentityprovider.model.SignUpResult) Test(org.junit.Test)

Example 5 with SignUpHandler

use of com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.SignUpHandler in project aws-sdk-android by aws-amplify.

the class CognitoIdentityProviderUserPoolTest method signUpUserWithoutPPInCurrentThread.

@Test
public void signUpUserWithoutPPInCurrentThread() throws Exception {
    // Test shared Pinpoint context
    awsKeyValueStorageUtility.put(PP_UNIQUE_ID_KEY, TEST_PP_ENDPOINT_1);
    // Test new user pool creation with PP app id.
    CognitoUserPool testPool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET, mockCSIClient);
    doReturn(TEST_REGISTER_USER_RESPONSE).when(mockCSIClient).signUp(any(SignUpRequest.class));
    expected_result_boolean = true;
    testPool.signUp(TEST_USER_NAME, TEST_USER_PASSWORD, TEST_IN_USER_ATTRIBUTES_LIST, null, new SignUpHandler() {

        @Override
        public void onSuccess(CognitoUser user, SignUpResult signUpResult) {
            // Verify the arguments
            ArgumentCaptor<SignUpRequest> argumentCaptor = ArgumentCaptor.forClass(SignUpRequest.class);
            verify(mockCSIClient).signUp(argumentCaptor.capture());
            SignUpRequest requestSent = argumentCaptor.getValue();
            assertNotNull(requestSent);
            assertEquals(TEST_USER_NAME, requestSent.getUsername());
            assertEquals(TEST_USER_PASSWORD, requestSent.getPassword());
            assertNotNull(requestSent.getUserAttributes());
            assertEquals(2, requestSent.getUserAttributes().size());
            assertNull(requestSent.getValidationData());
            assertNull(requestSent.getAnalyticsMetadata());
            // Verify results
            assertNotNull(user);
            assertNotNull(signUpResult.getUserConfirmed());
            assertNotNull(signUpResult.getCodeDeliveryDetails());
            assertEquals(TEST_USER_NAME, user.getUserId());
            assertEquals(signUpResult.getUserConfirmed(), expected_result_boolean);
        }

        @Override
        public void onFailure(Exception exception) {
            assertNull(exception);
        }
    });
}
Also used : CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) ArgumentCaptor(org.mockito.ArgumentCaptor) SignUpHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.SignUpHandler) SignUpRequest(com.amazonaws.services.cognitoidentityprovider.model.SignUpRequest) CognitoUser(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser) UnexpectedLambdaException(com.amazonaws.services.cognitoidentityprovider.model.UnexpectedLambdaException) ResourceNotFoundException(com.amazonaws.services.cognitoidentityprovider.model.ResourceNotFoundException) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) SignUpResult(com.amazonaws.services.cognitoidentityprovider.model.SignUpResult) Test(org.junit.Test)

Aggregations

SignUpHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.SignUpHandler)10 CognitoUser (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser)9 SignUpResult (com.amazonaws.services.cognitoidentityprovider.model.SignUpResult)9 CognitoUserPool (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool)7 InvalidParameterException (com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException)7 ResourceNotFoundException (com.amazonaws.services.cognitoidentityprovider.model.ResourceNotFoundException)7 SignUpRequest (com.amazonaws.services.cognitoidentityprovider.model.SignUpRequest)7 UnexpectedLambdaException (com.amazonaws.services.cognitoidentityprovider.model.UnexpectedLambdaException)7 Test (org.junit.Test)7 ArgumentCaptor (org.mockito.ArgumentCaptor)4 CognitoUserAttributes (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserAttributes)2 AlertDialog (android.app.AlertDialog)1 Intent (android.content.Intent)1 Handler (android.os.Handler)1 AmazonClientException (com.amazonaws.AmazonClientException)1 CognitoUserPoolsSignInProvider.getErrorMessageFromException (com.amazonaws.mobile.auth.userpools.CognitoUserPoolsSignInProvider.getErrorMessageFromException)1 ReturningRunnable (com.amazonaws.mobile.client.internal.ReturningRunnable)1 SignUpResult (com.amazonaws.mobile.client.results.SignUpResult)1 UserCodeDeliveryDetails (com.amazonaws.mobile.client.results.UserCodeDeliveryDetails)1 CognitoUserCodeDeliveryDetails (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails)1