Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(authenticator): Add default error string for LimitExceededException #182

Merged
merged 3 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.amplifyframework.auth.cognito.exceptions.service.CodeMismatchExceptio
import com.amplifyframework.auth.cognito.exceptions.service.CodeValidationException
import com.amplifyframework.auth.cognito.exceptions.service.InvalidParameterException
import com.amplifyframework.auth.cognito.exceptions.service.InvalidPasswordException
import com.amplifyframework.auth.cognito.exceptions.service.LimitExceededException
import com.amplifyframework.auth.cognito.exceptions.service.PasswordResetRequiredException
import com.amplifyframework.auth.cognito.exceptions.service.UserNotConfirmedException
import com.amplifyframework.auth.cognito.exceptions.service.UserNotFoundException
Expand Down Expand Up @@ -71,6 +72,7 @@ import com.amplifyframework.ui.authenticator.util.CodeSentMessage
import com.amplifyframework.ui.authenticator.util.ExpiredCodeMessage
import com.amplifyframework.ui.authenticator.util.InvalidConfigurationException
import com.amplifyframework.ui.authenticator.util.InvalidLoginMessage
import com.amplifyframework.ui.authenticator.util.LimitExceededMessage
import com.amplifyframework.ui.authenticator.util.MissingConfigurationException
import com.amplifyframework.ui.authenticator.util.NetworkErrorMessage
import com.amplifyframework.ui.authenticator.util.PasswordResetMessage
Expand Down Expand Up @@ -560,6 +562,7 @@ internal class AuthenticatorViewModel(
is CodeDeliveryFailureException -> sendMessage(CannotSendCodeMessage(error))
is CodeExpiredException -> sendMessage(ExpiredCodeMessage(error))
is CodeValidationException -> sendMessage(UnknownErrorMessage(error))
is LimitExceededException -> sendMessage(LimitExceededMessage(error))
is UnknownException -> {
if (error.isConnectivityIssue()) {
sendMessage(NetworkErrorMessage(error))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,10 @@ internal class ExpiredCodeMessage(
internal class NetworkErrorMessage(
override val cause: AuthException
) : AuthenticatorMessageImpl(R.string.amplify_ui_authenticator_error_network), AuthenticatorMessage.Error

/**
* User tried an action too many times.
*/
internal class LimitExceededMessage(
override val cause: AuthException
) : AuthenticatorMessageImpl(R.string.amplify_ui_authenticator_error_limit_exceeded), AuthenticatorMessage.Error
1 change: 1 addition & 0 deletions authenticator/src/main/res/values/errors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
<string name="amplify_ui_authenticator_error_send_code">Could not send confirmation code</string>
<string name="amplify_ui_authenticator_error_expired_code">Code has expired</string>
<string name="amplify_ui_authenticator_error_network">Please check your connectivity</string>
<string name="amplify_ui_authenticator_error_limit_exceeded">You\'ve reached the request limit. Please try again later.</string>
<string name="amplify_ui_authenticator_error_unknown">Sorry, something went wrong</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import aws.smithy.kotlin.runtime.http.HttpException
import com.amplifyframework.auth.AuthUserAttributeKey.email
import com.amplifyframework.auth.AuthUserAttributeKey.emailVerified
import com.amplifyframework.auth.MFAType
import com.amplifyframework.auth.cognito.exceptions.service.LimitExceededException
import com.amplifyframework.auth.exceptions.SessionExpiredException
import com.amplifyframework.auth.exceptions.UnknownException
import com.amplifyframework.auth.result.AuthResetPasswordResult
Expand All @@ -33,6 +34,7 @@ import com.amplifyframework.ui.authenticator.util.AmplifyResult.Error
import com.amplifyframework.ui.authenticator.util.AmplifyResult.Success
import com.amplifyframework.ui.authenticator.util.AuthConfigurationResult
import com.amplifyframework.ui.authenticator.util.AuthProvider
import com.amplifyframework.ui.authenticator.util.LimitExceededMessage
import com.amplifyframework.ui.authenticator.util.NetworkErrorMessage
import com.amplifyframework.ui.testing.CoroutineTestRule
import io.kotest.matchers.shouldBe
Expand Down Expand Up @@ -490,6 +492,18 @@ class AuthenticatorViewModelTest {
viewModel.confirmResetPassword("username", "password", "code")
viewModel.currentStep shouldBe AuthenticatorStep.SignIn
}

@Test
fun `Password reset results in limit exceeded message`() = runTest {
coEvery { authProvider.fetchAuthSession() } returns Success(mockAuthSession(isSignedIn = false))
coEvery { authProvider.resetPassword(any()) } returns Error(LimitExceededException(null))

viewModel.start(mockAuthenticatorConfiguration(initialStep = AuthenticatorStep.PasswordReset))

viewModel.shouldEmitMessage<LimitExceededMessage> {
viewModel.resetPassword("username")
}
}
//endregion
//region helpers
private val AuthenticatorViewModel.currentStep: AuthenticatorStep
Expand Down