Search in sources :

Example 1 with AuthUserAttribute

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

the class AuthComponentTest method updateUserAttribute.

/**
 * Tests that updateUserAttribute method of the Auth wrapper of AWSMobileClient (AMC) calls
 * AMC.updateUserAttributes with the user attribute 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 updateUserAttribute() 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));
    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);
    assertTrue(result.isUpdated());
    assertEquals(AuthUpdateAttributeStep.CONFIRM_ATTRIBUTE_WITH_CODE, result.getNextStep().getUpdateAttributeStep());
    validateCodeDeliveryDetails(result.getNextStep().getCodeDeliveryDetails());
    verify(mobileClient).updateUserAttributes(eq(attributeMap), any(), Mockito.<Callback<List<UserCodeDeliveryDetails>>>any());
}
Also used : AuthUserAttribute(com.amplifyframework.auth.AuthUserAttribute) AuthUpdateAttributeResult(com.amplifyframework.auth.result.AuthUpdateAttributeResult) 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) Test(org.junit.Test)

Example 2 with AuthUserAttribute

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

the class AuthComponentTest method updateUserAttributesWithOptions.

/**
 * Tests that updateUserAttributes method of the Auth wrapper of AWSMobileClient (AMC) Calls
 * AMC.updateUserAttributes with the user attributes and options it received.
 * Also ensures that in the onResult case, the success callback receives a valid Map which maps
 * AuthUserAttributeKey into 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 updateUserAttributesWithOptions() throws AuthException {
    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<String, String> attributesMap = new HashMap<>();
    for (AuthUserAttribute attribute : attributes) {
        attributesMap.put(attribute.getKey().getKeyString(), attribute.getValue());
    }
    List<UserCodeDeliveryDetails> userCodeDeliveryDetailsList = Collections.singletonList(new UserCodeDeliveryDetails(DESTINATION, DELIVERY_MEDIUM, ATTRIBUTE_NAME));
    AWSCognitoAuthUpdateUserAttributesOptions.CognitoBuilder options = AWSCognitoAuthUpdateUserAttributesOptions.builder();
    Map<String, String> metadata = new HashMap<String, String>();
    metadata.put("key", "value");
    options.metadata(metadata);
    AWSCognitoAuthUpdateUserAttributesOptions 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());
    Map<AuthUserAttributeKey, AuthUpdateAttributeResult> result = synchronousAuth.updateUserAttributes(attributes, builtOptions);
    assertTrue(result.get(attributeKey).isUpdated());
    assertTrue(result.get(attributeKeyWithoutCode).isUpdated());
    assertEquals(AuthUpdateAttributeStep.CONFIRM_ATTRIBUTE_WITH_CODE, result.get(attributeKey).getNextStep().getUpdateAttributeStep());
    assertEquals(AuthUpdateAttributeStep.DONE, result.get(attributeKeyWithoutCode).getNextStep().getUpdateAttributeStep());
    validateCodeDeliveryDetails(result.get(attributeKey).getNextStep().getCodeDeliveryDetails());
    verify(mobileClient).updateUserAttributes(eq(attributesMap), eq(metadata), Mockito.<Callback<List<UserCodeDeliveryDetails>>>any());
}
Also used : AuthUserAttribute(com.amplifyframework.auth.AuthUserAttribute) AuthUpdateAttributeResult(com.amplifyframework.auth.result.AuthUpdateAttributeResult) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) AuthUserAttributeKey(com.amplifyframework.auth.AuthUserAttributeKey) RandomString(com.amplifyframework.testutils.random.RandomString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AWSCognitoAuthUpdateUserAttributesOptions(com.amplifyframework.auth.cognito.options.AWSCognitoAuthUpdateUserAttributesOptions) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 3 with AuthUserAttribute

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

the class AuthComponentTest method updateUserAttributes.

/**
 * Tests that updateUserAttributes method of the Auth wrapper of AWSMobileClient (AMC) Calls
 * AMC.updateUserAttributes with the user attributes it received.
 * Also ensures that in the onResult case, the success callback receives a valid Map which maps
 * AuthUserAttributeKey into 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 updateUserAttributes() throws AuthException {
    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<String, String> attributesMap = new HashMap<>();
    for (AuthUserAttribute attribute : attributes) {
        attributesMap.put(attribute.getKey().getKeyString(), attribute.getValue());
    }
    List<UserCodeDeliveryDetails> userCodeDeliveryDetailsList = Collections.singletonList(new UserCodeDeliveryDetails(DESTINATION, DELIVERY_MEDIUM, ATTRIBUTE_NAME));
    doAnswer(invocation -> {
        Callback<List<UserCodeDeliveryDetails>> callback = invocation.getArgument(2);
        callback.onResult(userCodeDeliveryDetailsList);
        return null;
    }).when(mobileClient).updateUserAttributes(any(), any(), Mockito.<Callback<List<UserCodeDeliveryDetails>>>any());
    Map<AuthUserAttributeKey, AuthUpdateAttributeResult> result = synchronousAuth.updateUserAttributes(attributes);
    assertTrue(result.get(attributeKey).isUpdated());
    assertTrue(result.get(attributeKeyWithoutCode).isUpdated());
    assertEquals(AuthUpdateAttributeStep.CONFIRM_ATTRIBUTE_WITH_CODE, result.get(attributeKey).getNextStep().getUpdateAttributeStep());
    assertEquals(AuthUpdateAttributeStep.DONE, result.get(attributeKeyWithoutCode).getNextStep().getUpdateAttributeStep());
    validateCodeDeliveryDetails(result.get(attributeKey).getNextStep().getCodeDeliveryDetails());
    verify(mobileClient).updateUserAttributes(eq(attributesMap), any(), Mockito.<Callback<List<UserCodeDeliveryDetails>>>any());
}
Also used : AuthUserAttribute(com.amplifyframework.auth.AuthUserAttribute) AuthUpdateAttributeResult(com.amplifyframework.auth.result.AuthUpdateAttributeResult) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) AuthUserAttributeKey(com.amplifyframework.auth.AuthUserAttributeKey) RandomString(com.amplifyframework.testutils.random.RandomString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 4 with AuthUserAttribute

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

the class AuthComponentTest method confirmSignInWithOptions.

/**
 * Tests that the confirmSignIn method of the Auth wrapper of AWSMobileClient (AMC) calls AMC.confirmSignIn with
 * the confirmation code and options it received and the success callback receives a valid AuthSignInResult.
 * @throws AuthException test fails if this gets thrown since method should succeed
 */
@Test
// Casts final parameter to Callback to differentiate methods
@SuppressWarnings("unchecked")
public void confirmSignInWithOptions() throws AuthException {
    SignInResult amcResult = new SignInResult(SignInState.DONE, new UserCodeDeliveryDetails(DESTINATION, DELIVERY_MEDIUM, ATTRIBUTE_NAME));
    Tokens tokensResult = new Tokens(ACCESS_TOKEN, ID_TOKEN, REFRESH_TOKEN);
    AWSCognitoAuthConfirmSignInOptions.CognitoBuilder options = AWSCognitoAuthConfirmSignInOptions.builder();
    Map<String, String> metadata = new HashMap<String, String>();
    AuthUserAttribute attribute = new AuthUserAttribute(AuthUserAttributeKey.email(), "test@test.test");
    List<AuthUserAttribute> attributes = Collections.singletonList(attribute);
    Map<String, String> attributeMap = Collections.singletonMap("email", "test@test.test");
    metadata.put("key", "value");
    options.metadata(metadata);
    options.userAttributes(attributes);
    AWSCognitoAuthConfirmSignInOptions builtOptions = options.build();
    doAnswer(invocation -> {
        Callback<Tokens> callback = invocation.getArgument(0);
        callback.onResult(tokensResult);
        return null;
    }).when(mobileClient).getTokens(any());
    doAnswer(invocation -> {
        Callback<SignInResult> callback = invocation.getArgument(3);
        callback.onResult(amcResult);
        return null;
    }).when(mobileClient).confirmSignIn(any(String.class), any(), any(), (Callback<SignInResult>) any());
    AuthSignInResult result = synchronousAuth.confirmSignIn(CONFIRMATION_CODE, builtOptions);
    validateSignInResult(result, true, AuthSignInStep.DONE);
    verify(mobileClient).confirmSignIn(eq(CONFIRMATION_CODE), eq(metadata), eq(attributeMap), (Callback<SignInResult>) any());
}
Also used : AuthUserAttribute(com.amplifyframework.auth.AuthUserAttribute) HashMap(java.util.HashMap) AWSCognitoAuthConfirmSignInOptions(com.amplifyframework.auth.cognito.options.AWSCognitoAuthConfirmSignInOptions) UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) RandomString(com.amplifyframework.testutils.random.RandomString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SignInResult(com.amazonaws.mobile.client.results.SignInResult) AuthSignInResult(com.amplifyframework.auth.result.AuthSignInResult) AuthSignInResult(com.amplifyframework.auth.result.AuthSignInResult) Tokens(com.amazonaws.mobile.client.results.Tokens) Test(org.junit.Test)

Example 5 with AuthUserAttribute

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

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