Skip to content

Commit

Permalink
fix: missleading missing sitekey message (#137)
Browse files Browse the repository at this point in the history
* fix: update missing site-key exception message
* test: add test for missing siteKey
* fix: sonar 'primitive boolean expression' and unused TAG variable
  • Loading branch information
CAMOBAP authored Dec 22, 2023
1 parent a6026df commit f01f3b4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
8 changes: 4 additions & 4 deletions sdk/src/main/java/com/hcaptcha/sdk/HCaptcha.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

public final class HCaptcha extends Task<HCaptchaTokenResponse> implements IHCaptcha {
public static final String META_SITE_KEY = "com.hcaptcha.sdk.site-key";
public static final String TAG = "hCaptcha";

@NonNull
private final FragmentActivity activity;
Expand Down Expand Up @@ -57,8 +56,9 @@ public HCaptcha setup() {
throw new IllegalStateException(e);
}
if (siteKey == null) {
throw new IllegalStateException("Add missing " + META_SITE_KEY + " meta-data to AndroidManifest.xml"
+ " or call getClient(context, siteKey) method");
throw new IllegalStateException("The site-key is missing. You can pass it by adding "
+ META_SITE_KEY + " as meta-data to AndroidManifest.xml "
+ "or as an argument for setup/verifyWithHCaptcha methods.");
}
return setup(siteKey);
}
Expand Down Expand Up @@ -93,7 +93,7 @@ void onFailure(final HCaptchaException exception) {
}
};
try {
if (inputConfig.getHideDialog()) {
if (Boolean.TRUE.equals(inputConfig.getHideDialog())) {
// Overwrite certain config values in case the dialog is hidden to avoid behavior collision
this.config = inputConfig.toBuilder()
.size(HCaptchaSize.INVISIBLE)
Expand Down
12 changes: 12 additions & 0 deletions sdk/src/test/java/com/hcaptcha/sdk/HCaptchaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ public void test_fragment_creation_via_activity() {
any(HCaptchaStateListener.class)));
}

@Test(expected = IllegalStateException.class)
public void test_exception_on_missing_site_key() throws Exception {
final ApplicationInfo applicationInfo = mock(ApplicationInfo.class);
applicationInfo.metaData = mock(Bundle.class);

when(fragmentActivity.getPackageName()).thenReturn(TEST_PACKAGE_NAME);
when(fragmentActivity.getPackageManager()).thenReturn(packageManager);
when(packageManager.getApplicationInfo(TEST_PACKAGE_NAME, PackageManager.GET_META_DATA))
.thenReturn(applicationInfo);
HCaptcha.getClient(fragmentActivity).setup();
}

@Test
public void test_site_key_from_metadata() throws Exception {
final ApplicationInfo applicationInfo = mock(ApplicationInfo.class);
Expand Down

0 comments on commit f01f3b4

Please sign in to comment.