Search in sources :

Example 6 with CognitoUserCodeDeliveryDetails

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

the class CognitoIdentityProviderUpdateDetailsTest method updateUserAttributeInBackgroundWithCachedTokensServiceException.

// Get user attributes - in background
@Test
public void updateUserAttributeInBackgroundWithCachedTokensServiceException() {
    testPool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET, mockCSIClient);
    testUser = testPool.getUser(TEST_USER_NAME);
    // Set mock result for the change password request API call
    InvalidParameterException exception = new InvalidParameterException("failed");
    doThrow(exception).when(mockCSIClient).updateUserAttributes(any(UpdateUserAttributesRequest.class));
    // Store tokens in shared preferences
    SharedPreferences sharedPreferences = appContext.getSharedPreferences("CognitoIdentityProviderCache", Context.MODE_PRIVATE);
    sharedPreferences.edit().putString("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "idToken", getValidJWT(3600L)).commit();
    sharedPreferences.edit().putString("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "accessToken", getValidJWT(3600L)).commit();
    sharedPreferences.edit().putString("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "refreshToken", TEST_CACHED_RTOKEN).commit();
    CognitoUserAttributes attributes = new CognitoUserAttributes();
    attributes.addAttribute("TestAttribute1", "TestData1");
    attributes.addAttribute("TestAttribute2", "TestData2");
    testUser.updateAttributesInBackground(attributes, new UpdateAttributesHandler() {

        @Override
        public void onSuccess(List<CognitoUserCodeDeliveryDetails> var1) {
            assertNotNull(var1);
        }

        @Override
        public void onFailure(Exception exception) {
            assertNotNull(exception);
        }
    });
}
Also used : CognitoUserCodeDeliveryDetails(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) UpdateUserAttributesRequest(com.amazonaws.services.cognitoidentityprovider.model.UpdateUserAttributesRequest) UpdateAttributesHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler) SharedPreferences(android.content.SharedPreferences) CognitoUserAttributes(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserAttributes) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) Test(org.junit.Test)

Example 7 with CognitoUserCodeDeliveryDetails

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

the class CognitoIdentityProviderUpdateDetailsTest method updateUserAttributeInBackgroundWithCachedTokens.

// Get user attributes - in background
@Test
public void updateUserAttributeInBackgroundWithCachedTokens() {
    testPool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET, mockCSIClient);
    testUser = testPool.getUser(TEST_USER_NAME);
    // Set mock result for the change password request API call
    doReturn(TEST_VALID_UPDATE_USER_RESULT).when(mockCSIClient).updateUserAttributes(any(UpdateUserAttributesRequest.class));
    // Store tokens in shared preferences
    SharedPreferences sharedPreferences = appContext.getSharedPreferences("CognitoIdentityProviderCache", Context.MODE_PRIVATE);
    sharedPreferences.edit().putString("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "idToken", getValidJWT(3600L)).commit();
    sharedPreferences.edit().putString("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "accessToken", getValidJWT(3600L)).commit();
    sharedPreferences.edit().putString("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "refreshToken", TEST_CACHED_RTOKEN).commit();
    CognitoUserAttributes attributes = new CognitoUserAttributes();
    attributes.addAttribute("TestAttribute1", "TestData1");
    attributes.addAttribute("TestAttribute2", "TestData2");
    testUser.updateAttributesInBackground(attributes, new UpdateAttributesHandler() {

        @Override
        public void onSuccess(List<CognitoUserCodeDeliveryDetails> var1) {
            // Extract the arguments passed to get user API call
            ArgumentCaptor<GetUserRequest> argumentCaptor = ArgumentCaptor.forClass(GetUserRequest.class);
            verify(mockCSIClient).getUser(argumentCaptor.capture());
            GetUserRequest requestSent = argumentCaptor.getValue();
            // Verify the arguments passed to the API call
            assertNotNull(requestSent);
            assertNotNull(requestSent.getAccessToken());
            // Verify result
            assertNotNull(var1);
        }

        @Override
        public void onFailure(Exception exception) {
            assertNotNull(exception);
        }
    });
}
Also used : CognitoUserCodeDeliveryDetails(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails) CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) ArgumentCaptor(org.mockito.ArgumentCaptor) GetUserRequest(com.amazonaws.services.cognitoidentityprovider.model.GetUserRequest) UpdateUserAttributesRequest(com.amazonaws.services.cognitoidentityprovider.model.UpdateUserAttributesRequest) UpdateAttributesHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler) SharedPreferences(android.content.SharedPreferences) CognitoUserAttributes(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserAttributes) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) Test(org.junit.Test)

Example 8 with CognitoUserCodeDeliveryDetails

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

the class CognitoIdentityProviderUpdateDetailsTest method updateUserAttributeInCurrentThreadWithCachedTokens.

// Get user attributes -
@Test
public void updateUserAttributeInCurrentThreadWithCachedTokens() {
    testPool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET, mockCSIClient);
    testUser = testPool.getUser(TEST_USER_NAME);
    // Set mock result for the change password request API call
    doReturn(TEST_VALID_UPDATE_USER_RESULT).when(mockCSIClient).updateUserAttributes(any(UpdateUserAttributesRequest.class));
    // Store tokens in shared preferences
    SharedPreferences sharedPreferences = appContext.getSharedPreferences("CognitoIdentityProviderCache", Context.MODE_PRIVATE);
    sharedPreferences.edit().putString("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "idToken", getValidJWT(3600L)).commit();
    sharedPreferences.edit().putString("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "accessToken", getValidJWT(3600L)).commit();
    sharedPreferences.edit().putString("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "refreshToken", TEST_CACHED_RTOKEN).commit();
    CognitoUserAttributes attributes = new CognitoUserAttributes();
    attributes.addAttribute("TestAttribute1", "TestData1");
    attributes.addAttribute("TestAttribute2", "TestData2");
    testUser.updateAttributes(attributes, new UpdateAttributesHandler() {

        @Override
        public void onSuccess(List<CognitoUserCodeDeliveryDetails> var1) {
            // Extract the arguments passed to get user API call
            ArgumentCaptor<GetUserRequest> argumentCaptor = ArgumentCaptor.forClass(GetUserRequest.class);
            verify(mockCSIClient).getUser(argumentCaptor.capture());
            GetUserRequest requestSent = argumentCaptor.getValue();
            // Verify the arguments passed to the API call
            assertNotNull(requestSent);
            assertNotNull(requestSent.getAccessToken());
            // Verify result
            assertNotNull(var1);
        }

        @Override
        public void onFailure(Exception exception) {
            assertNotNull(exception);
        }
    });
}
Also used : CognitoUserCodeDeliveryDetails(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails) CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) ArgumentCaptor(org.mockito.ArgumentCaptor) GetUserRequest(com.amazonaws.services.cognitoidentityprovider.model.GetUserRequest) UpdateUserAttributesRequest(com.amazonaws.services.cognitoidentityprovider.model.UpdateUserAttributesRequest) UpdateAttributesHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler) SharedPreferences(android.content.SharedPreferences) CognitoUserAttributes(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserAttributes) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) Test(org.junit.Test)

Example 9 with CognitoUserCodeDeliveryDetails

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

the class CognitoIdentityProviderUpdateDetailsTest method updateUserAttributeInBackgroundWithNoCachedTokens.

@Test
public void updateUserAttributeInBackgroundWithNoCachedTokens() {
    testPool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET, mockCSIClient);
    testUser = testPool.getUser(TEST_USER_NAME);
    // Set mock result for the change password request API call
    doReturn(TEST_VALID_UPDATE_USER_RESULT).when(mockCSIClient).updateUserAttributes(any(UpdateUserAttributesRequest.class));
    CognitoUserAttributes attributes = new CognitoUserAttributes();
    attributes.addAttribute("TestAttribute1", "TestData1");
    attributes.addAttribute("TestAttribute2", "TestData2");
    testUser.updateAttributesInBackground(attributes, new UpdateAttributesHandler() {

        @Override
        public void onSuccess(List<CognitoUserCodeDeliveryDetails> var1) {
            assertNotNull(var1);
        }

        @Override
        public void onFailure(Exception exception) {
            assertNotNull(exception);
        }
    });
}
Also used : CognitoUserCodeDeliveryDetails(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails) CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) UpdateUserAttributesRequest(com.amazonaws.services.cognitoidentityprovider.model.UpdateUserAttributesRequest) UpdateAttributesHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler) CognitoUserAttributes(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserAttributes) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) Test(org.junit.Test)

Example 10 with CognitoUserCodeDeliveryDetails

use of com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails 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)

Aggregations

CognitoUserCodeDeliveryDetails (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails)12 CognitoUserAttributes (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserAttributes)8 UpdateAttributesHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler)7 InvalidParameterException (com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException)7 CognitoUserPool (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool)6 UpdateUserAttributesRequest (com.amazonaws.services.cognitoidentityprovider.model.UpdateUserAttributesRequest)6 JSONException (org.json.JSONException)6 Test (org.junit.Test)6 AmazonClientException (com.amazonaws.AmazonClientException)5 ReturningRunnable (com.amazonaws.mobile.client.internal.ReturningRunnable)5 UserCodeDeliveryDetails (com.amazonaws.mobile.client.results.UserCodeDeliveryDetails)5 NotAuthorizedException (com.amazonaws.services.cognitoidentity.model.NotAuthorizedException)5 InvalidUserPoolConfigurationException (com.amazonaws.services.cognitoidentityprovider.model.InvalidUserPoolConfigurationException)5 SharedPreferences (android.content.SharedPreferences)4 SignInResult (com.amazonaws.mobile.client.results.SignInResult)2 CognitoDevice (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoDevice)2 CognitoUser (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser)2 CognitoUserSession (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserSession)2 AuthenticationContinuation (com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationContinuation)2 AuthenticationDetails (com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationDetails)2