Skip to content

Commit

Permalink
Add username/password support to CredentialsTest
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkEWaite committed Jul 25, 2015
1 parent c15236a commit ff9c219
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions src/test/java/org/jenkinsci/plugins/gitclient/CredentialsTest.java
Expand Up @@ -3,6 +3,8 @@
import au.com.bytecode.opencsv.CSVReader;
import com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import com.google.common.io.Files;
import hudson.plugins.git.GitException;
import hudson.util.LogTaskListener;
Expand Down Expand Up @@ -50,6 +52,7 @@ public class CredentialsTest {
private final String gitImpl;
private final String gitRepoURL;
private final String username;
private final String password;
private final File privateKey;

private GitClient git;
Expand Down Expand Up @@ -78,14 +81,15 @@ private static PrintStream log() {
return StreamTaskListener.fromStdout().getLogger();
}

public CredentialsTest(String gitImpl, String gitRepoUrl, String username, File privateKey) {
public CredentialsTest(String gitImpl, String gitRepoUrl, String username, String password, File privateKey) {
this.gitImpl = gitImpl;
this.gitRepoURL = gitRepoUrl;
if (privateKey == null && defaultPrivateKey.exists()) {
privateKey = defaultPrivateKey;
}
this.privateKey = privateKey;
this.username = username;
this.password = password;
log().println("Repo: " + gitRepoUrl);
}

Expand Down Expand Up @@ -138,7 +142,7 @@ protected void clearExpectedLogSubstring() {
this.expectedLogSubstrings = new ArrayList<String>();
}

private BasicSSHUserPrivateKey newCredential(File privateKey, String username) throws IOException {
private BasicSSHUserPrivateKey newPrivateKeyCredential(String username, File privateKey) throws IOException {
CredentialsScope scope = CredentialsScope.GLOBAL;
String id = "private-key-" + privateKey.getPath();
String privateKeyData = Files.toString(privateKey, Charset.forName("UTF-8"));
Expand All @@ -148,6 +152,13 @@ private BasicSSHUserPrivateKey newCredential(File privateKey, String username) t
return new BasicSSHUserPrivateKey(scope, id, username, privateKeySource, passphrase, description);
}

private StandardUsernamePasswordCredentials newUsernamePasswordCredential(String username, String password) {
CredentialsScope scope = CredentialsScope.GLOBAL;
String id = "username-" + username + "-password-" + password;
StandardUsernamePasswordCredentials usernamePasswordCredential = new UsernamePasswordCredentialsImpl(scope, id, "desc: " + id, username, password);
return usernamePasswordCredential;
}

private static boolean isCredentialsSupported() throws IOException, InterruptedException {
CliGitAPIImpl cli = (CliGitAPIImpl) Git.with(null, new hudson.EnvVars()).in(currDir).using("git").getClient();
return cli.isAtLeastVersion(1, 7, 9, 0);
Expand All @@ -166,7 +177,7 @@ public static Collection gitRepoUrls() throws MalformedURLException, FileNotFoun
if (defaultPrivateKey.exists()) {
String username = System.getProperty("user.name");
String url = "https://github.com/jenkinsci/git-client-plugin.git";
Object[] masterRepo = {implementation, url, username, defaultPrivateKey};
Object[] masterRepo = {implementation, url, username, null, defaultPrivateKey};
repos.add(masterRepo);
}

Expand All @@ -186,13 +197,15 @@ public static Collection gitRepoUrls() throws MalformedURLException, FileNotFoun
String repoURL = entry[0];
String username = entry[1];
File privateKey = new File(authDataDir, entry[2]);
if (privateKey.exists()) {
Object[] repo = {implementation, repoURL, username, privateKey};
repos.add(repo);
} else {
Object[] repo = {implementation, repoURL, username, null};
repos.add(repo);
if (!privateKey.exists()) {
privateKey = null;
}
String password = null;
if (entry.length > 3) {
password = entry[3];
}
Object[] repo = {implementation, repoURL, username, password, privateKey};
repos.add(repo);
}
}
}
Expand Down Expand Up @@ -223,7 +236,11 @@ public void testFetchWithCredentials() throws URISyntaxException, GitException,
refSpecs.add(new RefSpec("+refs/heads/*:refs/remotes/" + origin + "/*"));
git.init_().workspace(repo.getAbsolutePath()).execute();
assertFalse("readme in " + repo + ", has " + listDir(repo), readme.exists());
git.addDefaultCredentials(newCredential(privateKey, username));
if (password != null) {
git.addDefaultCredentials(newUsernamePasswordCredential(username, password));
} else {
git.addDefaultCredentials(newPrivateKeyCredential(username, privateKey));
}
git.fetch_().from(new URIish(gitRepoURL), refSpecs).execute();
git.setRemoteUrl(origin, gitRepoURL);
ObjectId master = git.getHeadRev(gitRepoURL, "master");
Expand All @@ -239,7 +256,11 @@ public void testFetchWithCredentials() throws URISyntaxException, GitException,
public void testCloneWithCredentials() throws URISyntaxException, GitException, InterruptedException, MalformedURLException, IOException {
File readme = new File(repo, "README.md");
String origin = "origin";
git.addCredentials(gitRepoURL, newCredential(privateKey, username));
if (password != null) {
git.addDefaultCredentials(newUsernamePasswordCredential(username, password));
} else {
git.addDefaultCredentials(newPrivateKeyCredential(username, privateKey));
}
CloneCommand cmd = git.clone_().url(gitRepoURL).repositoryName(origin);
if (gitImpl.equals("git")) {
// Reduce network transfer by using a local reference repository
Expand Down

0 comments on commit ff9c219

Please sign in to comment.