Skip to content

Commit

Permalink
Add test for include user credentials checkbox
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Sicker <boards@gmail.com>
  • Loading branch information
jvz committed Jul 10, 2019
1 parent e703faa commit 8e5d692
Showing 1 changed file with 46 additions and 2 deletions.
Expand Up @@ -27,6 +27,10 @@

import com.cloudbees.plugins.credentials.domains.Domain;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlElementUtil;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
Expand All @@ -35,15 +39,18 @@
import hudson.model.ParameterValue;
import hudson.model.ParametersAction;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.User;
import hudson.security.ACL;
import hudson.security.ACLContext;
import hudson.tasks.Builder;
import jenkins.model.Jenkins;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import static org.junit.Assert.*;

Expand All @@ -55,7 +62,7 @@ public class CredentialsParameterDefinitionTest {
@Test public void defaultValue() throws Exception {
FreeStyleProject p = r.createFreeStyleProject();

CredentialsStore store = CredentialsProvider.lookupStores(Jenkins.getInstance()).iterator().next();
CredentialsStore store = CredentialsProvider.lookupStores(r.jenkins).iterator().next();
store.addCredentials(Domain.global(),
new UsernamePasswordCredentialsImpl(
CredentialsScope.GLOBAL, "id", "description", "username", "password"));
Expand All @@ -80,4 +87,41 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
return true;
}
}

@Test public void onlyIncludeUserCredentialsWhenChecked() throws Exception {
r.jenkins.setSecurityRealm(r.createDummySecurityRealm());

String globalCredentialId = UUID.randomUUID().toString();
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(),
new UsernamePasswordCredentialsImpl(
CredentialsScope.GLOBAL, globalCredentialId, "global credentials", "root", "qwerty"));

User beth = User.getOrCreateByIdOrFullName("beth");
String userCredentialId = UUID.randomUUID().toString();
try (ACLContext ignored = ACL.as(beth)) {
CredentialsProvider.lookupStores(beth).iterator().next().addCredentials(Domain.global(),
new UsernamePasswordCredentialsImpl(
CredentialsScope.USER, userCredentialId, "user credentials", "root", "a much better password than qwerty"));
}

FreeStyleProject p = r.createFreeStyleProject();
p.addProperty(new ParametersDefinitionProperty(new CredentialsParameterDefinition("credential", "description",
globalCredentialId, UsernamePasswordCredentialsImpl.class.getName(), true)));

JenkinsRule.WebClient wc = r.createWebClient()
.withThrowExceptionOnFailingStatusCode(false) // build with parameters returns 405 success for fun reasons
.login("beth");
HtmlForm form = wc.getPage(p, "build?delay=0sec").getFormByName("parameters");

HtmlCheckBoxInput checkbox = form.getInputByName("includeUser");
assertFalse("List user credentials checkbox should not be checked by default", checkbox.isChecked());
HtmlDivision div = form.getOneHtmlElementByAttribute("div", "class", "warning user-credentials-caution");
assertFalse("Caution message about user credentials should not be displayed yet", div.isDisplayed());
form.getSelectByName("_.value").getOptions().forEach(option -> assertNotEquals("No user credential should be an option yet", userCredentialId, option.getValueAttribute()));

HtmlElementUtil.click(checkbox);
assertTrue("Caution message about user credentials should be displayed after checking the box", div.isDisplayed());
form.getSelectByName("_.value").getOptions().stream().filter(option -> option.getValueAttribute().equals(userCredentialId)).findAny()
.orElseThrow(() -> new AssertionError("No credential found matching user credential id " + userCredentialId));
}
}

0 comments on commit 8e5d692

Please sign in to comment.