Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Sicker <boards@gmail.com>
  • Loading branch information
jvz committed Jun 21, 2019
1 parent fd31dbf commit 3e8ff4a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public CredentialsParameterValue(String name, String value, String description,
this(name, value, description, isDefaultValue, Jenkins.getAuthentication().getName());
}

private CredentialsParameterValue(String name, String value, String description, boolean isDefaultValue, String userId) {
CredentialsParameterValue(String name, String value, String description, boolean isDefaultValue, String userId) {
super(name, description);
this.value = value;
this.isDefaultValue = isDefaultValue;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.cloudbees.plugins.credentials;

import com.cloudbees.plugins.credentials.common.IdCredentials;
import com.cloudbees.plugins.credentials.domains.Domain;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import hudson.model.Cause;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.ParametersAction;
import hudson.model.ParametersDefinitionProperty;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

public class CredentialsParametersActionTest {

@ClassRule public static JenkinsRule j = new JenkinsRule();

@BeforeClass
public static void setUpClass() throws IOException {
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
CredentialsProvider.lookupStores(j.jenkins).iterator().next()
.addCredentials(Domain.global(), new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "cred-id", "global credential", "root", "correct horse battery staple"));
}

@Test
public void forRunReturnsNullWhenRunHasNoParameters() throws Exception {
final FreeStyleProject project = j.createFreeStyleProject();
final FreeStyleBuild build = j.assertBuildStatusSuccess(project.scheduleBuild2(0));
assertNull(CredentialsParametersAction.forRun(build));
}

@Test
public void forRunCopiesParametersWhenRunIsParameterized() throws Exception {
final FreeStyleProject project = j.createFreeStyleProject();
project.addProperty(new ParametersDefinitionProperty(new CredentialsParameterDefinition("cred", "", null, IdCredentials.class.getName(), true)));
final CredentialsParameterValue parameterValue = new CredentialsParameterValue("cred", "cred-id", "", false, "alpha");
final FreeStyleBuild build = j.assertBuildStatusSuccess(project.scheduleBuild2(0, new Cause.UserIdCause("alpha"), new ParametersAction(parameterValue)));
final CredentialsParametersAction action = CredentialsParametersAction.forRun(build);
assertNotNull(action);
assertEquals(parameterValue, action.findParameterByName("cred"));
}
}

0 comments on commit 3e8ff4a

Please sign in to comment.