use of com.amplifyframework.auth.result.AuthResetPasswordResult in project amplify-android by aws-amplify.
the class AuthComponentTest method resetPasswordWithOptions.
/**
* Tests that the resetPassword method of the Auth wrapper of AWSMobileClient (AMC) calls AMC.forgotPassword with
* the username and options it received.
* Also ensures that in the onResult case, the success callback receives a valid AuthResetPasswordResult 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 resetPasswordWithOptions() throws AuthException {
ForgotPasswordResult amcResult = new ForgotPasswordResult(ForgotPasswordState.CONFIRMATION_CODE);
amcResult.setParameters(new UserCodeDeliveryDetails(DESTINATION, DELIVERY_MEDIUM, ATTRIBUTE_NAME));
AWSCognitoAuthResetPasswordOptions.CognitoBuilder options = AWSCognitoAuthResetPasswordOptions.builder();
Map<String, String> metadata = new HashMap<String, String>();
metadata.put("key", "value");
options.metadata(metadata);
AWSCognitoAuthResetPasswordOptions builtOptions = options.build();
doAnswer(invocation -> {
Callback<ForgotPasswordResult> callback = invocation.getArgument(2);
callback.onResult(amcResult);
return null;
}).when(mobileClient).forgotPassword(any(), any(), Mockito.<Callback<ForgotPasswordResult>>any());
AuthResetPasswordResult result = synchronousAuth.resetPassword(USERNAME, builtOptions);
assertFalse(result.isPasswordReset());
assertEquals(AuthResetPasswordStep.CONFIRM_RESET_PASSWORD_WITH_CODE, result.getNextStep().getResetPasswordStep());
validateCodeDeliveryDetails(result.getNextStep().getCodeDeliveryDetails());
verify(mobileClient).forgotPassword(eq(USERNAME), eq(metadata), Mockito.<Callback<ForgotPasswordResult>>any());
}
use of com.amplifyframework.auth.result.AuthResetPasswordResult in project amplify-android by aws-amplify.
the class AWSCognitoAuthPlugin method resetPassword.
@Override
public void resetPassword(@NonNull String username, @NonNull AuthResetPasswordOptions options, @NonNull Consumer<AuthResetPasswordResult> onSuccess, @NonNull Consumer<AuthException> onException) {
final Map<String, String> clientMetadata = new HashMap<>();
if (options instanceof AWSCognitoAuthResetPasswordOptions) {
AWSCognitoAuthResetPasswordOptions cognitoOptions = (AWSCognitoAuthResetPasswordOptions) options;
clientMetadata.putAll(cognitoOptions.getMetadata());
}
awsMobileClient.forgotPassword(username, clientMetadata, new Callback<ForgotPasswordResult>() {
@Override
public void onResult(ForgotPasswordResult result) {
if (result.getState().equals(ForgotPasswordState.CONFIRMATION_CODE)) {
onSuccess.accept(new AuthResetPasswordResult(false, new AuthNextResetPasswordStep(AuthResetPasswordStep.CONFIRM_RESET_PASSWORD_WITH_CODE, Collections.emptyMap(), convertCodeDeliveryDetails(result.getParameters()))));
} else {
onException.accept(new AuthException("Received an unsupported response after triggering password recovery: " + result.getState(), "This is almost certainly a bug. Please report it as an issue in our GitHub repo."));
}
}
@Override
public void onError(Exception exception) {
onException.accept(CognitoAuthExceptionConverter.lookup(exception, "Reset password failed."));
}
});
}
use of com.amplifyframework.auth.result.AuthResetPasswordResult in project amplify-android by aws-amplify.
the class AuthComponentTest method resetPassword.
/**
* Tests that the resetPassword method of the Auth wrapper of AWSMobileClient (AMC) calls AMC.forgotPassword with
* the username it received.
* Also ensures that in the onResult case, the success callback receives a valid AuthResetPasswordResult 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 resetPassword() throws AuthException {
ForgotPasswordResult amcResult = new ForgotPasswordResult(ForgotPasswordState.CONFIRMATION_CODE);
amcResult.setParameters(new UserCodeDeliveryDetails(DESTINATION, DELIVERY_MEDIUM, ATTRIBUTE_NAME));
doAnswer(invocation -> {
Callback<ForgotPasswordResult> callback = invocation.getArgument(2);
callback.onResult(amcResult);
return null;
}).when(mobileClient).forgotPassword(any(), any(), Mockito.<Callback<ForgotPasswordResult>>any());
AuthResetPasswordResult result = synchronousAuth.resetPassword(USERNAME);
assertFalse(result.isPasswordReset());
assertEquals(AuthResetPasswordStep.CONFIRM_RESET_PASSWORD_WITH_CODE, result.getNextStep().getResetPasswordStep());
validateCodeDeliveryDetails(result.getNextStep().getCodeDeliveryDetails());
verify(mobileClient).forgotPassword(eq(USERNAME), any(), Mockito.<Callback<ForgotPasswordResult>>any());
}
use of com.amplifyframework.auth.result.AuthResetPasswordResult in project amplify-android by aws-amplify.
the class RxAuthBindingTest method testResetPasswordFails.
/**
* Tests that a failed request to reset the password will propagate a failure
* back through the binding.
* @throws InterruptedException If test observer is interrupted while awaiting terminal event
*/
@Test
public void testResetPasswordFails() throws InterruptedException {
String username = RandomString.string();
// Arrange delegate to furnish a failure
AuthException failure = new AuthException("Reset password", " has failed.");
doAnswer(invocation -> {
// 0 = username, 1 = onResult, 2 = onFailure
int positionOfFailureConsumer = 2;
Consumer<AuthException> onFailure = invocation.getArgument(positionOfFailureConsumer);
onFailure.accept(failure);
return null;
}).when(delegate).resetPassword(eq(username), anyConsumer(), anyConsumer());
// Act: call the binding
TestObserver<AuthResetPasswordResult> observer = auth.resetPassword(username).test();
// Assert: failure was furnished via Rx Single
observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
observer.assertNoValues().assertError(failure);
}
use of com.amplifyframework.auth.result.AuthResetPasswordResult in project amplify-android by aws-amplify.
the class RxAuthBindingTest method testResetPasswordSucceeds.
/**
* Tests that a successful request to reset the password will propagate a result
* back through the binding.
* @throws InterruptedException If test observer is interrupted while awaiting terminal event
*/
@Test
public void testResetPasswordSucceeds() throws InterruptedException {
String username = RandomString.string();
// Arrange delegate to furnish a result
AuthResetPasswordStep step = AuthResetPasswordStep.CONFIRM_RESET_PASSWORD_WITH_CODE;
AuthCodeDeliveryDetails details = new AuthCodeDeliveryDetails(RandomString.string(), DeliveryMedium.PHONE);
AuthNextResetPasswordStep nextStep = new AuthNextResetPasswordStep(step, Collections.emptyMap(), details);
AuthResetPasswordResult expected = new AuthResetPasswordResult(true, nextStep);
doAnswer(invocation -> {
// 0 = username, 1 = onResult, 2 = onFailure
int positionOfResultConsumer = 1;
Consumer<AuthResetPasswordResult> onResult = invocation.getArgument(positionOfResultConsumer);
onResult.accept(expected);
return null;
}).when(delegate).resetPassword(eq(username), anyConsumer(), anyConsumer());
// Act: call the binding
TestObserver<AuthResetPasswordResult> observer = auth.resetPassword(username).test();
// Assert: result was furnished via Rx Single
observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
observer.assertNoErrors().assertValue(expected);
}
Aggregations