|
24 | 24 |
|
25 | 25 | package org.jenkinsci.plugins.workflow.cps; |
26 | 26 |
|
| 27 | +import hudson.util.VersionNumber; |
| 28 | +import org.htmlunit.FailingHttpStatusCodeException; |
27 | 29 | import org.htmlunit.HttpMethod; |
28 | 30 | import org.htmlunit.WebRequest; |
29 | 31 | import org.htmlunit.html.HtmlCheckBoxInput; |
|
41 | 43 | import org.apache.tools.ant.filters.StringInputStream; |
42 | 44 | import org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval; |
43 | 45 | import org.jenkinsci.plugins.scriptsecurity.scripts.languages.GroovyLanguage; |
| 46 | +import org.jenkinsci.plugins.workflow.cps.config.CPSConfiguration; |
44 | 47 | import org.jenkinsci.plugins.workflow.job.WorkflowJob; |
45 | 48 | import org.junit.Rule; |
46 | 49 | import org.junit.Test; |
47 | 50 | import org.jvnet.hudson.test.Issue; |
48 | 51 | import org.jvnet.hudson.test.JenkinsRule; |
49 | 52 | import org.jvnet.hudson.test.MockAuthorizationStrategy; |
50 | 53 |
|
| 54 | +import java.nio.charset.StandardCharsets; |
51 | 55 | import java.util.List; |
52 | 56 |
|
53 | 57 | import static org.hamcrest.MatcherAssert.assertThat; |
| 58 | +import static org.hamcrest.Matchers.containsStringIgnoringCase; |
| 59 | +import static org.hamcrest.Matchers.equalTo; |
54 | 60 | import static org.hamcrest.Matchers.hasSize; |
| 61 | +import static org.hamcrest.Matchers.not; |
| 62 | +import static org.hamcrest.Matchers.notNullValue; |
| 63 | +import static org.hamcrest.Matchers.nullValue; |
55 | 64 | import static org.junit.Assert.assertEquals; |
56 | 65 | import static org.junit.Assert.assertFalse; |
57 | 66 | import static org.junit.Assert.assertTrue; |
| 67 | +import static org.junit.Assert.fail; |
58 | 68 |
|
59 | 69 | public class CpsFlowDefinitionTest { |
60 | 70 |
|
@@ -280,4 +290,84 @@ public void cpsScriptSubmissionViaRest() throws Exception { |
280 | 290 | assertFalse(ScriptApproval.get().isScriptApproved(configuredViaRestByNonAdmin, GroovyLanguage.get())); |
281 | 291 | wc.close(); |
282 | 292 | } |
| 293 | + |
| 294 | + @Test |
| 295 | + public void cpsScriptSandboxHide() throws Exception { |
| 296 | + jenkins.jenkins.setSecurityRealm(jenkins.createDummySecurityRealm()); |
| 297 | + |
| 298 | + MockAuthorizationStrategy mockStrategy = new MockAuthorizationStrategy(); |
| 299 | + mockStrategy.grant(Jenkins.READ).everywhere().to("devel"); |
| 300 | + for (Permission p : Item.PERMISSIONS.getPermissions()) { |
| 301 | + mockStrategy.grant(p).everywhere().to("devel"); |
| 302 | + } |
| 303 | + mockStrategy.grant(Jenkins.ADMINISTER).everywhere().to("admin"); |
| 304 | + jenkins.jenkins.setAuthorizationStrategy(mockStrategy); |
| 305 | + |
| 306 | + WorkflowJob p = jenkins.createProject(WorkflowJob.class); |
| 307 | + p.setDefinition(new CpsFlowDefinition("echo 'Hello'", true)); |
| 308 | + |
| 309 | + JenkinsRule.WebClient wcDevel = jenkins.createWebClient(); |
| 310 | + |
| 311 | + // non-admins can see the sandbox checkbox in jobs by default |
| 312 | + wcDevel.login("devel"); |
| 313 | + { |
| 314 | + HtmlForm config = wcDevel.getPage(p, "configure").getFormByName("config"); |
| 315 | + assertThat(config.getVisibleText(), containsStringIgnoringCase("Use Groovy Sandbox")); |
| 316 | + } |
| 317 | + |
| 318 | + // non-admins cannot see the sandbox checkbox in jobs if hideSandbox is On globally |
| 319 | + CPSConfiguration.get().setHideSandbox(true); |
| 320 | + { |
| 321 | + HtmlForm config = wcDevel.getPage(p, "configure").getFormByName("config"); |
| 322 | + assertThat(config.getVisibleText(), not(containsStringIgnoringCase("Use Groovy Sandbox"))); |
| 323 | + |
| 324 | + // but, when the sandbox is disabled the checkbox is shown so users can enable it |
| 325 | + p.setDefinition(new CpsFlowDefinition("echo 'Hello'", false)); |
| 326 | + config = wcDevel.getPage(p, "configure").getFormByName("config"); |
| 327 | + assertThat(config.getVisibleText(), containsStringIgnoringCase("Use Groovy Sandbox")); |
| 328 | + } |
| 329 | + |
| 330 | + // admins can always see the sandbox checkbox |
| 331 | + CPSConfiguration.get().setHideSandbox(false); |
| 332 | + wcDevel.login("admin"); |
| 333 | + { |
| 334 | + HtmlForm config = wcDevel.getPage(p, "configure").getFormByName("config"); |
| 335 | + assertThat(config.getVisibleText(), containsStringIgnoringCase("Use Groovy Sandbox")); |
| 336 | + } |
| 337 | + |
| 338 | + // even when set to hide globally |
| 339 | + CPSConfiguration.get().setHideSandbox(true); |
| 340 | + { |
| 341 | + HtmlForm config = wcDevel.getPage(p, "configure").getFormByName("config"); |
| 342 | + assertThat(config.getVisibleText(), containsStringIgnoringCase("Use Groovy Sandbox")); |
| 343 | + } |
| 344 | + |
| 345 | + // regular users cannot save jobs if the sandbox is disabled |
| 346 | + p.setDefinition(new CpsFlowDefinition("echo 'Hello'", false)); |
| 347 | + wcDevel.login("devel"); |
| 348 | + { |
| 349 | + HtmlForm config = wcDevel.getPage(p, "configure").getFormByName("config"); |
| 350 | + assertThat(config.getVisibleText(), containsStringIgnoringCase("Use Groovy Sandbox")); |
| 351 | + List<HtmlInput> sandboxes = config.getInputsByName("_.sandbox"); |
| 352 | + // Get the last one, because previous ones might be from Lockable Resources during PCT. |
| 353 | + HtmlCheckBoxInput sandbox = (HtmlCheckBoxInput) sandboxes.get(sandboxes.size() - 1); |
| 354 | + assertFalse("Sandbox is disabled", sandbox.isChecked()); |
| 355 | + VersionNumber jenkinsVersion = new VersionNumber(Jenkins.VERSION); |
| 356 | + int expectedStatus = 500; |
| 357 | + if (jenkinsVersion.isNewerThanOrEqualTo(new VersionNumber("2.470"))) { // TODO pending https://github.com/jenkinsci/jenkins/pull/9495 in baseline |
| 358 | + expectedStatus = 400; |
| 359 | + } |
| 360 | + try { |
| 361 | + jenkins.submit(config); |
| 362 | + fail("Expected HTTP " + expectedStatus); |
| 363 | + } catch (FailingHttpStatusCodeException e) { |
| 364 | + // good, expected |
| 365 | + assertThat(e.getStatusCode(), equalTo(expectedStatus)); |
| 366 | + if (expectedStatus == 400) { |
| 367 | + assertThat(e.getResponse().getContentAsString(StandardCharsets.UTF_8), containsStringIgnoringCase("Sandbox cannot be disabled")); |
| 368 | + } |
| 369 | + } |
| 370 | + |
| 371 | + } |
| 372 | + } |
283 | 373 | } |
0 commit comments