Skip to content

Commit

Permalink
feat: improve error message on invalid site-key
Browse files Browse the repository at this point in the history
  • Loading branch information
CAMOBAP committed Jul 3, 2024
1 parent efd18b2 commit 9853372
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
10 changes: 10 additions & 0 deletions sdk/src/main/java/com/hcaptcha/sdk/HCaptchaConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,15 @@ public HCaptchaConfigBuilder apiEndpoint(String url) { //NOSONAR
this.jsSrc(url);
return this;
}

public HCaptchaConfigBuilder siteKey(@NonNull String siteKey) {
try {
java.util.UUID.fromString(siteKey);
this.siteKey = siteKey;
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("hCaptcha site-key must be a valid UUID", e);
}
return this;
}
}
}
6 changes: 5 additions & 1 deletion sdk/src/test/java/com/hcaptcha/sdk/HCaptchaConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

public class HCaptchaConfigTest {

public static final String MOCK_SITE_KEY = "mocked-site-key";
public static final String MOCK_SITE_KEY = "00000000-0000-0000-0000-000000000000";

@Test
public void custom_locale() {
Expand Down Expand Up @@ -90,4 +90,8 @@ public void serialization() throws Exception {
deserializedObject.toBuilder().retryPredicate(null).build());
}

@Test(expected = IllegalArgumentException.class)
public void invalid_site_key() {
HCaptchaConfig.builder().siteKey("invalid-uuid-site-key").build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class HCaptchaJSInterfaceTest {
@Captor
ArgumentCaptor<HCaptchaException> exceptionCaptor;

HCaptchaConfig testConfig = HCaptchaConfig.builder().siteKey("0000-1111-2222-3333").build();
HCaptchaConfig testConfig = HCaptchaConfig.builder().siteKey(HCaptchaConfigTest.MOCK_SITE_KEY).build();

@Before
public void init() {
Expand All @@ -48,7 +48,7 @@ public void init() {

@Test
public void full_config_serialization() throws JSONException {
final String siteKey = "0000-1111-2222-3333";
final String siteKey = HCaptchaConfigTest.MOCK_SITE_KEY;
final String locale = "ro";
final HCaptchaOrientation orientation = HCaptchaOrientation.PORTRAIT;
final HCaptchaSize size = HCaptchaSize.NORMAL;
Expand Down Expand Up @@ -108,7 +108,7 @@ public void full_config_serialization() throws JSONException {

@Test
public void subset_config_serialization() throws JSONException {
final String siteKey = "0000-1111-2222-3333";
final String siteKey = HCaptchaConfigTest.MOCK_SITE_KEY;
final String locale = "ro";
final HCaptchaSize size = HCaptchaSize.NORMAL;
final HCaptchaOrientation orientation = HCaptchaOrientation.LANDSCAPE;
Expand Down
5 changes: 3 additions & 2 deletions sdk/src/test/java/com/hcaptcha/sdk/HCaptchaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.mockito.junit.MockitoJUnitRunner;

import java.util.Locale;
import java.util.UUID;


@RunWith(MockitoJUnitRunner.class)
Expand Down Expand Up @@ -201,7 +202,7 @@ public void test_setup_config() throws Exception {
@Test
public void test_verify_config_has_priority_over_setup_config() throws Exception {
final HCaptchaConfig verifyConfig = HCaptchaConfig.builder()
.siteKey(HCaptchaConfigTest.MOCK_SITE_KEY + "-on-verify")
.siteKey(HCaptchaConfigTest.MOCK_SITE_KEY)
.size(HCaptchaSize.INVISIBLE)
.loading(false)
.build();
Expand All @@ -222,7 +223,7 @@ public void test_verify_config_has_priority_over_setup_config() throws Exception

@Test
public void test_verify_site_key_has_priority_over_setup_config() throws Exception {
final String siteKey = HCaptchaConfigTest.MOCK_SITE_KEY + "-on-verify";
final String siteKey = UUID.randomUUID().toString();
HCaptcha.getClient(fragmentActivity)
.setup(config)
.verifyWithHCaptcha(siteKey);
Expand Down

0 comments on commit 9853372

Please sign in to comment.