Skip to content

Commit

Permalink
Merge pull request #311 from auth0/unchecked-warnings
Browse files Browse the repository at this point in the history
Add unchecked warnings failures for src
  • Loading branch information
jimmyjames committed Nov 2, 2020
2 parents c1b5d7d + f13132d commit 2fa6fbb
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 18 deletions.
11 changes: 6 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ jacocoTestReport {
compileJava {
sourceCompatibility '1.7'
targetCompatibility '1.7'

options.compilerArgs << "-Xlint:deprecation" << "-Xlint:unchecked" << "-Werror"
}

compileTestJava {
options.compilerArgs << "-Xlint:deprecation" << "-Werror"
}

buildscript {
Expand All @@ -45,11 +51,6 @@ buildscript {
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4'
classpath 'gradle.plugin.com.auth0.gradle:oss-library:0.9.0'
}

tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:deprecation"
options.compilerArgs << "-Werror"
}
}

repositories {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/auth0/exception/APIException.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ private static String obtainExceptionMessage(Map<String, Object> values) {
Object description = values.get("description");
if (description instanceof String) {
return (String) description;
} else {
} else if (description instanceof Map){
@SuppressWarnings("unchecked")
PasswordStrengthErrorParser policy = new PasswordStrengthErrorParser((Map<String, Object>) description);
return policy.getDescription();
}
Expand Down
94 changes: 82 additions & 12 deletions src/test/java/com/auth0/exception/APIExceptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.Before;
import org.junit.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -13,16 +14,85 @@ public class APIExceptionTest {

private Map<String, Object> values;

private final static int ERROR_CODE = 42;
private final static String EXPECTED_ERROR_MESSAGE_PREFIX = "Request failed with status code " + ERROR_CODE + ": ";

@Before
public void setUp() {
values = new HashMap<>();
}

@Test
public void shouldGetMessageFromErrorDescription() {
values.put("error_description", "some error description");
values.put("description", "some description");
values.put("message", "some message");
values.put("error", "some error");
APIException apiException = new APIException(values, ERROR_CODE);
assertThat(apiException.getMessage(), is(EXPECTED_ERROR_MESSAGE_PREFIX + "some error description"));
}

@Test
public void shouldGetMessageFromDescriptionAsString() {
values.put("message", "some message");
values.put("error", "some error");
APIException apiException = new APIException(values, ERROR_CODE);
assertThat(apiException.getMessage(), is(EXPECTED_ERROR_MESSAGE_PREFIX + "some message"));
}

@Test
public void shouldGetMessageFromDescriptionAsMap() {
Map<String, Object> rules = new HashMap<>();
rules.put("verified", false);
rules.put("code", "lengthAtLeast");
rules.put("format", Collections.singletonList(8));
rules.put("message", "some password length message");

Map<String, Object> passwordStrengthError = new HashMap<>();
passwordStrengthError.put("rules", Collections.singletonList(rules));

values.put("description", passwordStrengthError);
values.put("message", "some message");
values.put("error", "some error");

APIException apiException = new APIException(values, ERROR_CODE);
assertThat(apiException.getMessage(), is(EXPECTED_ERROR_MESSAGE_PREFIX + "some password length message"));
}

@Test
public void shouldGetDefaultMessageWhenDescriptionNotMapOrString() {
values.put("description", Collections.singletonList("some description"));

APIException apiException = new APIException(values, ERROR_CODE);
assertThat(apiException.getMessage(), is(EXPECTED_ERROR_MESSAGE_PREFIX + "Unknown exception"));
}

@Test
public void shouldGetMessageFromMessage() {
values.put("message", "some message");
values.put("error", "some error");
APIException apiException = new APIException(values, ERROR_CODE);
assertThat(apiException.getMessage(), is(EXPECTED_ERROR_MESSAGE_PREFIX + "some message"));
}

@Test
public void shouldGetMessageFromError() {
values.put("error", "some error");
APIException apiException = new APIException(values, ERROR_CODE);
assertThat(apiException.getMessage(), is(EXPECTED_ERROR_MESSAGE_PREFIX + "some error"));
}

@Test
public void shouldGetDefaultMessage() {
APIException apiException = new APIException(values, ERROR_CODE);
assertThat(apiException.getMessage(), is(EXPECTED_ERROR_MESSAGE_PREFIX + "Unknown exception"));
}

@Test
public void shouldBeMfaRequiredWithMfaTokenError() {
values.put("error", "mfa_required");
values.put("mfa_token", "some-mfa-token");
APIException apiException = new APIException(values, 42);
APIException apiException = new APIException(values, ERROR_CODE);
assertThat(apiException.isMultifactorRequired(), is(true));
assertThat(apiException.getValue("mfa_token"), is("some-mfa-token"));
}
Expand All @@ -31,82 +101,82 @@ public void shouldBeMfaRequiredWithMfaTokenError() {
public void shouldBeInvalidCredentialsErrorWhenInvalidGrant() {
values.put("error", "invalid_grant");
values.put("error_description", "Wrong email or password.");
APIException apiException = new APIException(values, 42);
APIException apiException = new APIException(values, ERROR_CODE);
assertThat(apiException.isInvalidCredentials(), is(true));
}

@Test
public void shouldBeInvalidCredentialsErrorWhenInvalidUserPassword() {
values.put("error", "invalid_user_password");
values.put("error_description", "Wrong email or password.");
APIException apiException = new APIException(values, 42);
APIException apiException = new APIException(values, ERROR_CODE);
assertThat(apiException.isInvalidCredentials(), is(true));
}

@Test
public void shouldNotBeInvalidCredentialsErrorWhenWrongDescription() {
values.put("error", "invalid_grant");
values.put("error_description", "some message");
APIException apiException = new APIException(values, 42);
APIException apiException = new APIException(values, ERROR_CODE);
assertThat(apiException.isInvalidCredentials(), is(false));
}

@Test
public void shouldBeAccessDeniedError() {
values.put("error", "access_denied");
APIException apiException = new APIException(values, 42);
APIException apiException = new APIException(values, ERROR_CODE);
assertThat(apiException.isAccessDenied(), is(true));
}

@Test
public void shouldBeMfaEnrollementRequiredError() {
values.put("error", "unsupported_challenge_type");
APIException apiException = new APIException(values, 42);
APIException apiException = new APIException(values, ERROR_CODE);
assertThat(apiException.isMultifactorEnrollRequired(), is(true));
}

@Test
public void shouldBeMfaTokenInvalidErrorForMalformedToken() {
values.put("error", "invalid_grant");
values.put("error_description", "Malformed mfa_token");
APIException apiException = new APIException(values, 42);
APIException apiException = new APIException(values, ERROR_CODE);
assertThat(apiException.isMultifactorTokenInvalid(), is(true));
}

@Test
public void shouldBeMfaTokenInvalidErrorForExpiredToken() {
values.put("error", "expired_token");
values.put("error_description", "mfa_token is expired");
APIException apiException = new APIException(values, 1);
APIException apiException = new APIException(values, ERROR_CODE);
assertThat(apiException.isMultifactorTokenInvalid(), is(true));
}

@Test
public void shouldNotBeMfaTokenInvalidErrorForExpiredTokenWhenWrongDescription() {
values.put("error", "expired_token");
values.put("error_description", "some message");
APIException apiException = new APIException(values, 1);
APIException apiException = new APIException(values, ERROR_CODE);
assertThat(apiException.isMultifactorTokenInvalid(), is(false));
}

@Test
public void shouldNotBeMfaTokenInvalidErrorForMalformedTokenWhenWrongDescription() {
values.put("error", "invalid_grant");
values.put("error_description", "some message");
APIException apiException = new APIException(values, 1);
APIException apiException = new APIException(values, ERROR_CODE);
assertThat(apiException.isMultifactorTokenInvalid(), is(false));
}

@Test
public void shouldBeMfaEnrollementRequired() {
values.put("error", "unsupported_challenge_type");
APIException apiException = new APIException(values, 401);
APIException apiException = new APIException(values, ERROR_CODE);
assertThat(apiException.isMultifactorEnrollRequired(), is(true));
}

@Test
public void checkErrorTypesShouldHandleNullError() {
APIException apiException = new APIException(values, 42);
APIException apiException = new APIException(values, ERROR_CODE);
assertThat(apiException.isAccessDenied(), is(false));
assertThat(apiException.isInvalidCredentials(), is(false));
assertThat(apiException.isMultifactorRequired(), is(false));
Expand Down

0 comments on commit 2fa6fbb

Please sign in to comment.