Skip to content

Commit

Permalink
Add missing test, better exception name, fix test throws declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
martinda committed Jan 3, 2022
1 parent 8ad6593 commit 2679337
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import static com.google.common.io.BaseEncoding.base64;

import com.cdancy.jenkins.rest.auth.AuthenticationType;
import com.cdancy.jenkins.rest.exception.UndetectableCredentialTypeException;
import com.cdancy.jenkins.rest.exception.UndetectableIdentityException;

import java.nio.charset.StandardCharsets;
import java.util.Objects;
Expand Down Expand Up @@ -121,7 +121,7 @@ private String extractIdentity(final String credentialString) {
decoded = credentialString;
}
if (!decoded.contains(":")) {
throw new UndetectableCredentialTypeException("Unable to detect the type of credential being used from '" + credentialString + "'. Supported types are a user:password, or a user:apiToken, or their base64 encoded value.");
throw new UndetectableIdentityException("Unable to detect the identity being used in '" + credentialString + "'. Supported types are a user:password, or a user:apiToken, or their base64 encoded value.");
}
if (decoded.equals(":")) {
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@
* When this fails, this exception is thrown.
*
*/
public class UndetectableCredentialTypeException extends RuntimeException {
public class UndetectableIdentityException extends RuntimeException {
private static final long serialVersionUID = 1L;

public UndetectableCredentialTypeException() { super(); }
public UndetectableIdentityException() { super(); }

public UndetectableCredentialTypeException(final String arg0, final Throwable arg1) {
public UndetectableIdentityException(final String arg0, final Throwable arg1) {
super(arg0, arg1);
}

public UndetectableCredentialTypeException(final String arg0) {
public UndetectableIdentityException(final String arg0) {
super(arg0);
}

public UndetectableCredentialTypeException(final Throwable arg0) {
public UndetectableIdentityException(final Throwable arg0) {
super(arg0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import com.cdancy.jenkins.rest.JenkinsAuthentication;
import com.cdancy.jenkins.rest.auth.AuthenticationType;
import com.cdancy.jenkins.rest.exception.UndetectableCredentialTypeException;
import com.cdancy.jenkins.rest.exception.UndetectableIdentityException;

import org.jclouds.http.HttpRequest;

Expand All @@ -38,7 +38,7 @@
public class JenkinsAuthenticationMockTest {

@Test
public void testAnonymousAuthentication() throws Exception {
public void testAnonymousAuthentication() {
JenkinsAuthentication ja = JenkinsAuthentication.builder().build();
assertEquals(ja.identity, "anonymous");
assertEquals(ja.authType(), AuthenticationType.Anonymous);
Expand All @@ -47,7 +47,7 @@ public void testAnonymousAuthentication() throws Exception {
}

@Test
public void testUsernamePasswordAuthentication() throws Exception {
public void testUsernamePasswordAuthentication() {
JenkinsAuthentication ja = JenkinsAuthentication.builder()
.credentials("user:password")
.build();
Expand All @@ -58,7 +58,7 @@ public void testUsernamePasswordAuthentication() throws Exception {
}

@Test
public void testUsernameApiTokenAuthentication() throws Exception {
public void testUsernameApiTokenAuthentication() {
JenkinsAuthentication ja = JenkinsAuthentication.builder()
.apiToken("user:token")
.build();
Expand All @@ -69,7 +69,7 @@ public void testUsernameApiTokenAuthentication() throws Exception {
}

@Test
public void testEncodedUsernamePasswordAuthentication() throws Exception {
public void testEncodedUsernamePasswordAuthentication() {
String encoded = base64().encode("user:password".getBytes());
JenkinsAuthentication ja = JenkinsAuthentication.builder()
.credentials(encoded)
Expand All @@ -81,7 +81,7 @@ public void testEncodedUsernamePasswordAuthentication() throws Exception {
}

@Test
public void testEncodedUsernameApiTokenAuthentication() throws Exception {
public void testEncodedUsernameApiTokenAuthentication() {
String encoded = base64().encode("user:token".getBytes());
JenkinsAuthentication ja = JenkinsAuthentication.builder()
.apiToken(encoded)
Expand Down Expand Up @@ -113,4 +113,17 @@ public void testEmptyUsernameApiToken() {
assertEquals(ja.authValue(), base64().encode(":".getBytes()));
assertEquals(ja.credential, ja.authValue());
}

@Test
public void testUndetectableCredential() {
String invalid = base64().encode("no_colon_here".getBytes());
try {
JenkinsAuthentication ja = JenkinsAuthentication.builder()
.apiToken(invalid)
.build();
} catch (UndetectableIdentityException ex) {
assertEquals(ex.getMessage(),
"Unable to detect the identity being used in '" + invalid + "'. Supported types are a user:password, or a user:apiToken, or their base64 encoded value.");
}
}
}

0 comments on commit 2679337

Please sign in to comment.