Parameterize sitekey

This commit is contained in:
Chris Eager
2022-02-28 09:49:31 -08:00
committed by Chris Eager
parent 3a1c716c73
commit 935e268dec
7 changed files with 115 additions and 59 deletions

View File

@@ -0,0 +1,63 @@
/*
* Copyright 2022 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.whispersystems.textsecuregcm.recaptcha;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.whispersystems.textsecuregcm.recaptcha.EnterpriseRecaptchaClient.SEPARATOR;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import javax.ws.rs.BadRequestException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
class EnterpriseRecaptchaClientTest {
private static final String SITE_KEY = "site-key";
private static final String TOKEN = "some-token";
@ParameterizedTest
@MethodSource
void parseInputToken(final String input, final String expectedToken, final String siteKey,
@Nullable final String expectedAction) {
final String[] parts = EnterpriseRecaptchaClient.parseInputToken(input);
assertEquals(siteKey, parts[0]);
assertEquals(expectedAction, parts[1]);
assertEquals(expectedToken, parts[2]);
}
@Test
void parseInputTokenBadRequest() {
assertThrows(BadRequestException.class, () -> {
EnterpriseRecaptchaClient.parseInputToken(TOKEN);
});
}
static Stream<Arguments> parseInputToken() {
return Stream.of(
Arguments.of(
String.join(SEPARATOR, SITE_KEY, TOKEN),
TOKEN,
SITE_KEY,
null),
Arguments.of(
String.join(SEPARATOR, SITE_KEY, "an-action", TOKEN),
TOKEN,
SITE_KEY,
"an-action"),
Arguments.of(
String.join(SEPARATOR, SITE_KEY, "an-action", TOKEN, "something-else"),
TOKEN + SEPARATOR + "something-else",
SITE_KEY,
"an-action")
);
}
}

View File

@@ -8,7 +8,7 @@ package org.whispersystems.textsecuregcm.recaptcha;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.whispersystems.textsecuregcm.recaptcha.TransitionalRecaptchaClient.SEPARATOR;
import static org.whispersystems.textsecuregcm.recaptcha.EnterpriseRecaptchaClient.SEPARATOR;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
@@ -36,8 +36,7 @@ class TransitionalRecaptchaClientTest {
@ParameterizedTest
@MethodSource
void testVerify(final String inputToken, final boolean expectLegacy, final String expectedToken,
final String expectedAction) {
void testVerify(final String inputToken, final boolean expectLegacy, final String expectedToken) {
transitionalRecaptchaClient.verify(inputToken, IP_ADDRESS);
@@ -46,7 +45,7 @@ class TransitionalRecaptchaClientTest {
verify(legacyRecaptchaClient).verify(expectedToken, IP_ADDRESS);
} else {
verifyNoInteractions(legacyRecaptchaClient);
verify(enterpriseRecaptchaClient).verify(expectedToken, IP_ADDRESS, expectedAction);
verify(enterpriseRecaptchaClient).verify(expectedToken, IP_ADDRESS);
}
}
@@ -56,23 +55,20 @@ class TransitionalRecaptchaClientTest {
Arguments.of(
TOKEN,
true,
TOKEN,
null),
TOKEN),
Arguments.of(
String.join(SEPARATOR, PREFIX, TOKEN),
false,
TOKEN,
null),
TOKEN),
Arguments.of(
String.join(SEPARATOR, PREFIX, "an-action", TOKEN),
String.join(SEPARATOR, PREFIX, "site-key", "an-action", TOKEN),
false,
TOKEN,
String.join(SEPARATOR, "site-key", "an-action", TOKEN),
"an-action"),
Arguments.of(
String.join(SEPARATOR, PREFIX, "an-action", TOKEN, "something-else"),
String.join(SEPARATOR, PREFIX, "site-key", "an-action", TOKEN, "something-else"),
false,
TOKEN + SEPARATOR + "something-else",
"an-action")
"site-key" + SEPARATOR + "an-action" + SEPARATOR + TOKEN + SEPARATOR + "something-else")
);
}