Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-34877] Add basic test suite for creating, updating and remov…
…ing credentials.
- Loading branch information
Showing
2 changed files
with
121 additions
and
2 deletions.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
src/test/java/org/jenkinsci/plugins/plaincredentials/BaseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
testCreateUpdateDetele(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()); | ||
testCreateUpdateDetele(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 testCreateUpdateDetele(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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters