Throw checked exceptions from CaptchaChecker

This commit is contained in:
Ravi Khadiwala authored and Jon Chambers committed 2026-06-11 11:33:56 -04:00
1 parent 82e3c16fba
commit 660011017d
11 files changed
+65 -29

No files matched your search

@@ -79,7 +79,7 @@ public class CaptchaCheckerTest {
final String input,
final String expectedToken,
final String siteKey,
final Action expectedAction) throws IOException {
final Action expectedAction) throws IOException, InvalidCaptchaArgumentException {
final CaptchaClient captchaClient = mockClient(PREFIX);
new CaptchaChecker(null, PREFIX -> captchaClient).verify(Optional.empty(), expectedAction, input, null, USER_AGENT);
verify(captchaClient, times(1)).verify(any(), eq(siteKey), eq(expectedAction), eq(expectedToken), any(), eq(USER_AGENT));
@@ -103,7 +103,7 @@ public class CaptchaCheckerTest {
}
@Test
public void choose() throws IOException {
public void choose() throws IOException, InvalidCaptchaArgumentException {
String ainput = String.join(SEPARATOR, PREFIX_A, CHALLENGE_SITE_KEY, "challenge", TOKEN);
String binput = String.join(SEPARATOR, PREFIX_B, CHALLENGE_SITE_KEY, "challenge", TOKEN);
final CaptchaClient a = mockClient(PREFIX_A);
@@ -134,13 +134,13 @@ public class CaptchaCheckerTest {
@MethodSource
public void badArgs(final String input) throws IOException {
final CaptchaClient cc = mockClient(PREFIX);
assertThrows(BadRequestException.class,
assertThrows(InvalidCaptchaArgumentException.class,
() -> new CaptchaChecker(null, prefix -> PREFIX.equals(prefix) ? cc : null).verify(Optional.of(ACI), Action.CHALLENGE, input, null, USER_AGENT));
}
@Test
public void testShortened() throws IOException {
public void testShortened() throws IOException, InvalidCaptchaArgumentException {
final CaptchaClient captchaClient = mockClient(PREFIX);
final ShortCodeExpander retriever = mock(ShortCodeExpander.class);
when(retriever.retrieve("abc")).thenReturn(Optional.of(TOKEN));
@@ -33,6 +33,7 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.whispersystems.textsecuregcm.auth.AuthenticatedDevice;
import org.whispersystems.textsecuregcm.captcha.InvalidCaptchaArgumentException;
import org.whispersystems.textsecuregcm.limits.RateLimitChallengeManager;
import org.whispersystems.textsecuregcm.mappers.RateLimitExceededExceptionMapper;
import org.whispersystems.textsecuregcm.push.NotPushRegisteredException;
@@ -119,7 +120,8 @@ class ChallengeControllerTest {
@ParameterizedTest
@ValueSource(booleans = { true, false } )
void testHandleCaptcha(boolean hasThreshold) throws RateLimitExceededException, IOException {
void testHandleCaptcha(boolean hasThreshold)
throws RateLimitExceededException, IOException, InvalidCaptchaArgumentException {
final String captchaChallengeJson = """
{
"type": "captcha",
@@ -149,7 +151,7 @@ class ChallengeControllerTest {
}
@Test
void testHandleInvalidCaptcha() throws RateLimitExceededException, IOException {
void testHandleInvalidCaptcha() throws RateLimitExceededException, IOException, InvalidCaptchaArgumentException {
final String captchaChallengeJson = """
{
"type": "captcha",
@@ -170,7 +172,7 @@ class ChallengeControllerTest {
}
@Test
void testHandleCaptchaRateLimited() throws RateLimitExceededException, IOException {
void testHandleCaptchaRateLimited() throws RateLimitExceededException, IOException, InvalidCaptchaArgumentException {
final String captchaChallengeJson = """
{
"type": "captcha",
@@ -17,6 +17,7 @@ import org.mockito.Mock;
import org.signal.chat.challenge.AnswerChallengeRequest;
import org.signal.chat.challenge.AnswerChallengeResponse;
import org.signal.chat.challenge.ChallengeGrpc;
import org.whispersystems.textsecuregcm.captcha.InvalidCaptchaArgumentException;
import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException;
import org.whispersystems.textsecuregcm.limits.RateLimitChallengeManager;
import org.whispersystems.textsecuregcm.spam.ChallengeConstraintChecker;
@@ -84,7 +85,8 @@ public class ChallengeGrpcServiceTest extends
@ParameterizedTest
@ValueSource(booleans = {true, false})
void handleCaptchaChallengeResponse(final boolean captchaSuccess) throws RateLimitExceededException, IOException {
void handleCaptchaChallengeResponse(final boolean captchaSuccess)
throws RateLimitExceededException, IOException, InvalidCaptchaArgumentException {
when(challengeConstraintChecker.challengeConstraintsGrpc(account)).thenReturn(
new ChallengeConstraintChecker.ChallengeConstraints(true, Optional.empty()));
when(rateLimitChallengeManager.answerCaptchaChallenge(any(), any(), any(), any(), any())).thenReturn(
@@ -100,7 +102,8 @@ public class ChallengeGrpcServiceTest extends
}
@Test
void handleCaptchaChallengeResponseRateLimitExceeded() throws RateLimitExceededException, IOException {
void handleCaptchaChallengeResponseRateLimitExceeded()
throws RateLimitExceededException, IOException, InvalidCaptchaArgumentException {
when(challengeConstraintChecker.challengeConstraintsGrpc(account)).thenReturn(
new ChallengeConstraintChecker.ChallengeConstraints(true, Optional.empty()));
final Duration retryAfter = Duration.ofMinutes(1);
@@ -26,6 +26,7 @@ import org.junit.jupiter.params.provider.ValueSource;
import org.whispersystems.textsecuregcm.captcha.Action;
import org.whispersystems.textsecuregcm.captcha.AssessmentResult;
import org.whispersystems.textsecuregcm.captcha.CaptchaChecker;
import org.whispersystems.textsecuregcm.captcha.InvalidCaptchaArgumentException;
import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException;
import org.whispersystems.textsecuregcm.spam.ChallengeType;
import org.whispersystems.textsecuregcm.spam.RateLimitChallengeListener;
@@ -80,7 +81,7 @@ class RateLimitChallengeManagerTest {
@ParameterizedTest
@MethodSource
void answerCaptchaChallenge(Optional<Float> scoreThreshold, float actualScore, boolean expectSuccess)
throws RateLimitExceededException, IOException {
throws RateLimitExceededException, IOException, InvalidCaptchaArgumentException {
final Account account = mock(Account.class);
when(account.getNumber()).thenReturn("+18005551234");
when(account.getUuid()).thenReturn(UUID.randomUUID());