Search in sources :

Example 1 with AuthUserAttributeKey

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

the class RxAuthBindingTest method testResendUserAttributeConfirmationCode.

/**
 * Tests that a successful request to resend user attribute confirmation code will propagate a
 * completion back through the binding.
 * @throws InterruptedException If test observer is interrupted while awaiting terminal event
 */
@Test
public void testResendUserAttributeConfirmationCode() throws InterruptedException {
    // Arrange an invocation of the success Action
    AuthUserAttributeKey attributeKey = AuthUserAttributeKey.custom(ATTRIBUTE_KEY);
    AuthCodeDeliveryDetails expected = new AuthCodeDeliveryDetails(DESTINATION, DeliveryMedium.EMAIL, ATTRIBUTE_NAME);
    doAnswer(invocation -> {
        Consumer<AuthCodeDeliveryDetails> onCompletion = invocation.getArgument(1);
        onCompletion.accept(expected);
        return null;
    }).when(delegate).resendUserAttributeConfirmationCode(any(), anyConsumer(), anyConsumer());
    // Act: call the binding
    TestObserver<AuthCodeDeliveryDetails> observer = auth.resendUserAttributeConfirmationCode(attributeKey).test();
    // Assert: Completable completes with success
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertNoErrors().assertValue(expected);
}
Also used : AuthCodeDeliveryDetails(com.amplifyframework.auth.AuthCodeDeliveryDetails) AuthUserAttributeKey(com.amplifyframework.auth.AuthUserAttributeKey) Test(org.junit.Test)

Example 2 with AuthUserAttributeKey

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

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

the class AuthComponentTest method resendUserAttributeConfirmationCode.

/**
 * Tests the resendUserAttributeConfirmationCode method of the Auth wrapper of AWSMobileClient (AMC) Calls
 * AMC.verifyUserAttribute with the user attribute key to be verified.
 * @throws AuthException test fails if this gets thrown since method should succeed
 */
@Test
public void resendUserAttributeConfirmationCode() throws AuthException {
    AuthUserAttributeKey attributeKey = AuthUserAttributeKey.custom(ATTRIBUTE_KEY);
    UserCodeDeliveryDetails userCodeDeliveryDetails = new UserCodeDeliveryDetails(DESTINATION, DELIVERY_MEDIUM, ATTRIBUTE_NAME);
    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);
    validateCodeDeliveryDetails(result);
    verify(mobileClient).verifyUserAttribute(eq(attributeKey.getKeyString()), any(), Mockito.<Callback<UserCodeDeliveryDetails>>any());
}
Also used : AuthCodeDeliveryDetails(com.amplifyframework.auth.AuthCodeDeliveryDetails) UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) AuthUserAttributeKey(com.amplifyframework.auth.AuthUserAttributeKey) Test(org.junit.Test)

Example 4 with AuthUserAttributeKey

use of com.amplifyframework.auth.AuthUserAttributeKey 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 5 with AuthUserAttributeKey

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

the class AuthComponentTest method confirmUserAttribute.

/**
 * Tests the confirmUserAttribute method of the Auth wrapper of AWSMobileClient (AMC) Calls
 * AMC.confirmUpdateUserAttribute with the user attribute key and confirmation code.
 * @throws AuthException test fails if this gets thrown since method should succeed
 */
@Test
public void confirmUserAttribute() throws AuthException {
    AuthUserAttributeKey attributeKey = AuthUserAttributeKey.custom(ATTRIBUTE_KEY);
    doAnswer(invocation -> {
        Callback<Void> callback = invocation.getArgument(2);
        callback.onResult(null);
        return null;
    }).when(mobileClient).confirmUpdateUserAttribute(any(), any(), Mockito.<Callback<Void>>any());
    synchronousAuth.confirmUserAttribute(attributeKey, CONFIRMATION_CODE);
    verify(mobileClient).confirmUpdateUserAttribute(eq(attributeKey.getKeyString()), eq(CONFIRMATION_CODE), Mockito.<Callback<Void>>any());
}
Also used : AuthUserAttributeKey(com.amplifyframework.auth.AuthUserAttributeKey) Test(org.junit.Test)

Aggregations

AuthUserAttributeKey (com.amplifyframework.auth.AuthUserAttributeKey)8 Test (org.junit.Test)8 UserCodeDeliveryDetails (com.amazonaws.mobile.client.results.UserCodeDeliveryDetails)4 AuthCodeDeliveryDetails (com.amplifyframework.auth.AuthCodeDeliveryDetails)4 HashMap (java.util.HashMap)4 AuthUserAttribute (com.amplifyframework.auth.AuthUserAttribute)3 AuthUpdateAttributeResult (com.amplifyframework.auth.result.AuthUpdateAttributeResult)3 RandomString (com.amplifyframework.testutils.random.RandomString)3 ArrayList (java.util.ArrayList)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 List (java.util.List)2 AWSCognitoAuthResendUserAttributeConfirmationCodeOptions (com.amplifyframework.auth.cognito.options.AWSCognitoAuthResendUserAttributeConfirmationCodeOptions)1 AWSCognitoAuthUpdateUserAttributesOptions (com.amplifyframework.auth.cognito.options.AWSCognitoAuthUpdateUserAttributesOptions)1 AuthNextUpdateAttributeStep (com.amplifyframework.auth.result.step.AuthNextUpdateAttributeStep)1 Action (com.amplifyframework.core.Action)1 Matchers.anyAction (com.amplifyframework.rx.Matchers.anyAction)1 Map (java.util.Map)1