Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,68 +1,66 @@
package org.jenkinsci.plugins.plaincredentials;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.List;

import com.cloudbees.plugins.credentials.SecretBytes;
import org.apache.commons.fileupload.FileItem;
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.SecretBytes;
import com.cloudbees.plugins.credentials.domains.Domain;
import com.cloudbees.plugins.credentials.impl.BaseStandardCredentials;

import hudson.model.FileParameterValue.FileItemImpl;
import hudson.model.FileParameterValue;
import hudson.security.ACL;
import hudson.util.Secret;
import org.apache.commons.fileupload.FileItem;
import org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl;
import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

import java.nio.file.Files;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class BaseTest {
@WithJenkins
class BaseTest {

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

@Rule
public JenkinsRule r = new JenkinsRule();
private JenkinsRule r;

private CredentialsStore store;

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

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

@Test
public void secretFileBaseTest() throws IOException, URISyntaxException {
void secretFileBaseTest() throws Exception {
secretFileTest(false);
}

@Test
public void secretFileBaseTestWithDeprecatedCtor() throws IOException, URISyntaxException {
void secretFileBaseTestWithDeprecatedCtor() throws Exception {
secretFileTest(true);
}

private void secretFileTest(boolean useDeprecatedConstructor) throws IOException, URISyntaxException {
FileItem fileItem = createEmptyFileItem();
private void secretFileTest(boolean useDeprecatedConstructor) throws Exception {
FileItem fileItem = FileItem.fromFileUpload2FileItem(createEmptyFileItem());

FileCredentialsImpl credential;

Expand All @@ -81,14 +79,14 @@ private void secretFileTest(boolean useDeprecatedConstructor) throws IOException
*
* @param credential the credential to create
* @param updatedCredential the credential that will replace the first one during update
* @throws IOException
* @throws Exception
*/
private <T extends BaseStandardCredentials> void testCreateUpdateDelete(T credential, T updatedCredential) throws IOException {
private <T extends BaseStandardCredentials> void testCreateUpdateDelete(T credential, T updatedCredential) throws Exception {
// Add a credential
store.addCredentials(Domain.global(), credential);

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

// There is one credential
assertThat(credentials.size(), is(1));
Expand All @@ -99,7 +97,7 @@ private <T extends BaseStandardCredentials> void testCreateUpdateDelete(T creden
store.updateCredentials(Domain.global(), cred, updatedCredential);

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

// There is still 1 credential but the description has been updated
assertThat(credentials.size(), is(1));
Expand All @@ -112,7 +110,7 @@ private <T extends BaseStandardCredentials> void testCreateUpdateDelete(T creden
store.removeCredentials(Domain.global(), cred);

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

// There are no credentials anymore
assertThat(credentials.size(), is(0));
Expand All @@ -122,9 +120,9 @@ private <T extends BaseStandardCredentials> void testCreateUpdateDelete(T creden
* Creates an empty FileItem for testing purposes
*
* @return
* @throws IOException
* @throws Exception
*/
private FileItem createEmptyFileItem() throws IOException {
return new FileItemImpl(Files.createTempFile("credential-test", null).toFile());
private static FileParameterValue.FileItemImpl2 createEmptyFileItem() throws Exception {
return new FileParameterValue.FileItemImpl2(Files.createTempFile("credential-test", null).toFile());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,33 @@

import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import hudson.security.ACL;
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode;
import org.apache.commons.io.IOUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.nio.charset.StandardCharsets;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
*/
public class ConfigurationAsCodeTest {

@Rule
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();
@WithJenkinsConfiguredWithCode
class ConfigurationAsCodeTest {

@Test
@ConfiguredWithCode("ConfigurationAsCode.yaml")
public void should_configure_file_credentials() throws Exception {
void should_configure_file_credentials(JenkinsConfiguredWithCodeRule j) throws Exception {
FileCredentials credentials = CredentialsMatchers.firstOrNull(
CredentialsProvider.lookupCredentials(FileCredentials.class, j.jenkins, ACL.SYSTEM, (DomainRequirement) null),
CredentialsProvider.lookupCredentialsInItemGroup(FileCredentials.class, j.jenkins, ACL.SYSTEM2, null),
CredentialsMatchers.withId("secret-file"));
assertNotNull(credentials);
assertEquals("Some secret file", credentials.getDescription());
assertEquals("my-secret-file", credentials.getFileName());
assertEquals("FOO_BAR", IOUtils.toString(credentials.getContent()));
assertEquals("FOO_BAR", IOUtils.toString(credentials.getContent(), StandardCharsets.UTF_8));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,30 @@
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemHeaders;
import org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import org.jvnet.hudson.test.JenkinsRule;

public class FileCredentialsTest {
import static org.junit.jupiter.api.Assertions.assertThrows;

@Rule
public JenkinsRule r = new JenkinsRule();
@WithJenkins
class FileCredentialsTest {

@Test(expected = IllegalArgumentException.class)
@Test
@Issue("JENKINS-30926")
public void shouldThrowAnExceptionIfFileNameIsBlank() throws IOException {
new FileCredentialsImpl(CredentialsScope.GLOBAL, "1", "", new StubFileItem(), "", SecretBytes.fromString(""));
void shouldThrowAnExceptionIfFileNameIsBlank(JenkinsRule r) {
assertThrows(IllegalArgumentException.class, () -> new FileCredentialsImpl(CredentialsScope.GLOBAL, "1", "", new StubFileItem(), "", SecretBytes.fromString("")));
}

private class StubFileItem implements FileItem {
private static class StubFileItem implements FileItem {

@Override
public InputStream getInputStream() throws IOException {
public InputStream getInputStream() {
return null;
}

Expand Down Expand Up @@ -83,7 +81,7 @@ public byte[] get() {
}

@Override
public String getString(String encoding) throws UnsupportedEncodingException {
public String getString(String encoding) {
return null;
}

Expand All @@ -93,7 +91,7 @@ public String getString() {
}

@Override
public void write(File file) throws Exception {
public void write(File file) {

}

Expand Down Expand Up @@ -123,7 +121,7 @@ public void setFormField(boolean state) {
}

@Override
public OutputStream getOutputStream() throws IOException {
public OutputStream getOutputStream() {
return null;
}

Expand Down
Loading