Skip to content

Commit

Permalink
Merge pull request #4 from armfergom/JENKINS-34877
Browse files Browse the repository at this point in the history
[JENKINS-34877] Fix PCT against 2.x and migrate to 2.9 parent pom
  • Loading branch information
jglick committed May 19, 2016
2 parents 792b70b + b613c48 commit 3f3c7fd
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -2,3 +2,6 @@ target
work
.idea/
*.iml
.classpath
.project
.settings
17 changes: 7 additions & 10 deletions pom.xml
Expand Up @@ -3,9 +3,8 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.580.1</version>
<version>2.9</version>
</parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plain-credentials</artifactId>
<version>1.2-SNAPSHOT</version>
<packaging>hpi</packaging>
Expand All @@ -18,12 +17,17 @@
<url>http://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>repo.jenkins-ci.org</id>
<url>http://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>
<properties>
<jenkins.version>1.580.1</jenkins.version>
<java.level>6</java.level>
</properties>
<licenses>
<license>
<name>MIT</name>
Expand All @@ -35,27 +39,20 @@
<developerConnection>scm:git:git@github.com:jenkinsci/${project.artifactId}-plugin.git</developerConnection>
<url>https://github.com/jenkinsci/${project.artifactId}-plugin</url>
<tag>HEAD</tag>
</scm>
</scm>
<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials</artifactId>
<version>1.21</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jenkins-ci.tools</groupId>
<artifactId>maven-hpi-plugin</artifactId>
<configuration>
<compatibleSinceVersion>1.0-beta-4</compatibleSinceVersion>
<loggers>
<org.jenkinsci.plugins.plaincredentials>FINE</org.jenkinsci.plugins.plaincredentials>
</loggers>
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/index.jelly
@@ -1,3 +1,4 @@
<?jelly escape-by-default='true'?>
<div>
Allows use of plain strings and files as credentials.
</div>
120 changes: 120 additions & 0 deletions src/test/java/org/jenkinsci/plugins/plaincredentials/BaseTest.java
@@ -0,0 +1,120 @@
package org.jenkinsci.plugins.plaincredentials;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.List;

import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl;
import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.CredentialsStore;
import com.cloudbees.plugins.credentials.domains.Domain;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import com.cloudbees.plugins.credentials.impl.BaseStandardCredentials;
import com.trilead.ssh2.crypto.Base64;

import hudson.security.ACL;
import hudson.util.Secret;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

public class BaseTest {

private static final String UPDATED_CRED_ID = "Custom-ID-Updated";
private static final String CRED_ID = "Custom-ID";

@Rule
public JenkinsRule r = new JenkinsRule();

private CredentialsStore store;

@Before
public void setup(){
store = CredentialsProvider.lookupStores(r.jenkins).iterator().next();
}

@Test
public void secretTextBaseTest() throws IOException {
StringCredentialsImpl credential = new StringCredentialsImpl(CredentialsScope.GLOBAL, CRED_ID, "Test Secret Text", Secret.fromString("password"));
StringCredentialsImpl updatedCredential = new StringCredentialsImpl(credential.getScope(), UPDATED_CRED_ID, credential.getDescription(), credential.getSecret());
testCreateUpdateDelete(credential, updatedCredential);
}

@Test
public void secretFileBaseTest() throws IOException, URISyntaxException {
DiskFileItem fileItem = createEmptyFileItem();

FileCredentialsImpl credential = new FileCredentialsImpl(CredentialsScope.GLOBAL, CRED_ID, "Test Secret file", fileItem, "keys.txt", Base64.encode(fileItem.get()).toString());
FileCredentialsImpl updatedCredential = new FileCredentialsImpl(credential.getScope(), UPDATED_CRED_ID, credential.getDescription(), fileItem, credential.getFileName(), credential.getData());
testCreateUpdateDelete(credential, updatedCredential);
}

/**
* Creates, updates and deletes credentials and perform different assertions
*
* @param credential the credential to create
* @param updatedCredential the credential that will replace the first one during update
* @throws IOException
*/
private <T extends BaseStandardCredentials> void testCreateUpdateDelete(T credential, T updatedCredential) throws IOException {
// Add a credential
store.addCredentials(Domain.global(), credential);

// Look up all credentials
List<BaseStandardCredentials> credentials = CredentialsProvider.lookupCredentials(BaseStandardCredentials.class, r.jenkins, ACL.SYSTEM, Collections.<DomainRequirement>emptyList());

// There is one credential
assertThat(credentials.size(), is(1));
BaseStandardCredentials cred = credentials.get(0);
assertThat(cred, instanceOf(credential.getClass()));
assertThat(cred.getId(), is(CRED_ID));
// Update credential
store.updateCredentials(Domain.global(), cred, updatedCredential);

// Look up all credentials again
credentials = CredentialsProvider.lookupCredentials(BaseStandardCredentials.class, r.jenkins, ACL.SYSTEM, Collections.<DomainRequirement>emptyList());

// There is still 1 credential but the ID has been updated
assertThat(credentials.size(), is(1));
cred = credentials.get(0);
assertThat(cred, instanceOf(credential.getClass()));
assertThat(cred.getId(), is(UPDATED_CRED_ID));

// Delete credential
store.removeCredentials(Domain.global(), cred);

// Look up all credentials again
credentials = CredentialsProvider.lookupCredentials(BaseStandardCredentials.class, r.jenkins, ACL.SYSTEM, Collections.<DomainRequirement>emptyList());

// There are no credentials anymore
assertThat(credentials.size(), is(0));
}

/**
* Creates an empty FileItem for testing purposes
*
* @param fileName
* @return
* @throws URISyntaxException
* @throws FileNotFoundException
* @throws IOException
*/
private DiskFileItem createEmptyFileItem() throws URISyntaxException, FileNotFoundException, IOException {
DiskFileItem fileItem = (DiskFileItem) new DiskFileItemFactory().createItem("fileData", "text/plain", true, "fileName");
OutputStream os = fileItem.getOutputStream();
os.flush();
return fileItem;
}
}
Expand Up @@ -41,8 +41,7 @@ public class FileCredentialsTest {
@Test(expected = IllegalArgumentException.class)
@Issue("JENKINS-30926")
public void shouldThrowAnExceptionIfFileNameIsBlank() throws IOException {
FileCredentials fileCredentials = new FileCredentialsImpl(CredentialsScope.GLOBAL, "1", "",
new StubFileItem(), "", "");
new FileCredentialsImpl(CredentialsScope.GLOBAL, "1", "", new StubFileItem(), "", "");
}

private class StubFileItem implements FileItem {
Expand Down

0 comments on commit 3f3c7fd

Please sign in to comment.