Skip to content

Commit

Permalink
[JENKINS-45501] Fixes for JENKINS-9774 and JENKINS-41370
Browse files Browse the repository at this point in the history
* Now `hudson.security.ProjectMatrixAuthorizationStrategy` can be deserialized
* Bypassing the GUI on `copyJob` and issuing the proper Http request directly
  • Loading branch information
Raul Arabaolaza committed Jul 14, 2017
1 parent 380966b commit c17a394
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions src/test/java/com/cloudbees/hudson/plugins/folder/FolderTest.java
Expand Up @@ -26,10 +26,9 @@

import com.cloudbees.hudson.plugins.folder.properties.FolderCredentialsProvider;
import com.cloudbees.plugins.credentials.domains.DomainCredentials;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput;
import com.gargoylesoftware.htmlunit.HttpMethod;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.html.*;
import hudson.model.Actionable;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
Expand All @@ -47,6 +46,8 @@
import hudson.views.BuildButtonColumn;
import hudson.views.JobColumn;
import java.io.IOException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -137,22 +138,27 @@ public class FolderTest {
JenkinsRule.WebClient wc = r.createWebClient();

// "foo" should copy "child"
copyFromGUI(f, wc, "foo", "xyz");
copyViaHttp(f, wc, "foo", "xyz");
assertEquals("child",((Job)f.getItem("xyz")).getDescription());

// "/foo" should copy "top"
copyFromGUI(f, wc, "/foo", "uvw");
copyViaHttp(f, wc, "/foo", "uvw");
assertEquals("top",((Job)f.getItem("uvw")).getDescription());

}

private void copyFromGUI(Folder f, JenkinsRule.WebClient wc, String fromName, String toName) throws Exception {
HtmlPage page = wc.getPage(f, "newJob");
((HtmlInput)page.getElementById("name")).type(toName);
HtmlInput fe = (HtmlInput) page.getElementById("from");
fe.focus();
fe.type(fromName);
r.submit(page.getFormByName("createItem"));
private void copyViaHttp(Folder f, JenkinsRule.WebClient wc, String fromName, String toName) throws Exception {
// Taken from https://github.com/jenkinsci/jenkins/blob/80aa2c8e4093df270193402c3933f3f1f16271da/test/src/test/java/hudson/jobs/CreateItemTest.java#L68
r.jenkins.setCrumbIssuer(null);

URL apiURL = new URL(MessageFormat.format(
"{0}createItem?mode=copy&from={1}&name={2}",
r.jenkins.getRootUrl() + "/" + f.getUrl().toString(), fromName, toName));

WebRequest request = new WebRequest(apiURL, HttpMethod.POST);
request.setEncodingType(null);
assertTrue("Copy Job request has failed", 200 == r.createWebClient()
.getPage(request).getWebResponse().getStatusCode());
}

/**
Expand Down Expand Up @@ -336,7 +342,10 @@ private void copyFromGUI(Folder f, JenkinsRule.WebClient wc, String fromName, St
@Issue("JENKINS-32487")
@Test public void shouldAssignPropertyOwnerOnCreationAndReload() throws Exception {
Folder folder = r.jenkins.createProject(Folder.class, "myFolder");
r.jenkins.setAuthorizationStrategy(new ProjectMatrixAuthorizationStrategy());
ProjectMatrixAuthorizationStrategy as = new ProjectMatrixAuthorizationStrategy();
// Need to do this to avoid JENKINS-9774
as.add(Jenkins.ADMINISTER, "alice");
r.jenkins.setAuthorizationStrategy(as);

// We add a stub property to generate the persisted list
// Then we ensure owner is being assigned properly.
Expand All @@ -363,8 +372,11 @@ private void copyFromGUI(Folder f, JenkinsRule.WebClient wc, String fromName, St
Set<String> sids = new HashSet<String>();
sids.add("admin");
grantedPermissions.put(Jenkins.ADMINISTER, sids);
folder = r.jenkins.getItemByFullName("myFolder", Folder.class);
r.jenkins.setAuthorizationStrategy(new ProjectMatrixAuthorizationStrategy());
folder = r.jenkins.getItemByFullName("myFolder", Folder.class);
ProjectMatrixAuthorizationStrategy as = new ProjectMatrixAuthorizationStrategy();
// Need to do this to avoid JENKINS-9774
as.add(Jenkins.ADMINISTER, "alice");
r.jenkins.setAuthorizationStrategy(as);
folder.addProperty(new com.cloudbees.hudson.plugins.folder.properties.AuthorizationMatrixProperty(grantedPermissions));

// Reload folder from disk and check the state
Expand Down

0 comments on commit c17a394

Please sign in to comment.