use of org.jvnet.hudson.test.MockAuthorizationStrategy in project support-core-plugin by jenkinsci.
the class SupportActionTest method doBundle.
private WebResponse doBundle(String action, String bundle, String user, String extraBundle) throws IOException {
j.jenkins.setCrumbIssuer(null);
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
j.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy().grant(Jenkins.ADMINISTER).everywhere().to("admin").grant(Jenkins.READ).everywhere().to("user"));
WebClient wc = j.createWebClient().withBasicCredentials(user).withThrowExceptionOnFailingStatusCode(false);
String json = "?json={%22bundles%22:[{%22selected%22:+true,%22name%22:+%22" + bundle + "%22}]}";
if (extraBundle != null) {
json = "?json={%22bundles%22:[{%22selected%22:+true,%22name%22:+%22" + bundle + "%22},{%22selected%22:+true,%22name%22:+%22" + extraBundle + "%22}]}";
}
WebRequest request = new WebRequest(new URL(j.getURL() + root.getUrlName() + "/" + action + json), HttpMethod.POST);
return wc.getPage(request).getWebResponse();
}
use of org.jvnet.hudson.test.MockAuthorizationStrategy in project support-core-plugin by jenkinsci.
the class SupportTestUtils method setupAuth.
/**
* Set up a {@link hudson.security.SecurityRealm} and {@link hudson.security.AuthorizationStrategy} with two users:
* * one "privileged" user with required permissions
* * one "unprivileged" user Overall/Read and optionally a set of "test" permissions
*
* @param j the {@link JenkinsRule}
* @param userUnprivileged the id of the unprivileged user
* @param userPrivileged the id of the privileged user
* @param requiredPermissions the set of required permissions given to the unprivileged user
* @param testPermissions the set of test permissions given to the privileged user
*/
private static void setupAuth(JenkinsRule j, String userUnprivileged, String userPrivileged, Set<Permission> requiredPermissions, Set<Permission> testPermissions) {
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
MockAuthorizationStrategy auth = new MockAuthorizationStrategy().grant(Jenkins.READ).everywhere().to(userUnprivileged).grant(Jenkins.READ).everywhere().to(userPrivileged);
requiredPermissions.forEach(permission -> auth.grant(permission).everywhere().to(userPrivileged));
testPermissions.forEach(permission -> auth.grant(permission).everywhere().to(userUnprivileged));
j.jenkins.setAuthorizationStrategy(auth);
}
use of org.jvnet.hudson.test.MockAuthorizationStrategy in project promoted-builds-plugin by jenkinsci.
the class ManualConditionTest method enableSecurity.
private static void enableSecurity(JenkinsRule j) {
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
MockAuthorizationStrategy mas = new MockAuthorizationStrategy();
mas.grant(Item.BUILD, Item.READ, Jenkins.READ).everywhere().to("non-promoter", "promoter");
mas.grant(Promotion.PROMOTE).everywhere().to("promoter");
j.jenkins.setAuthorizationStrategy(mas);
}
use of org.jvnet.hudson.test.MockAuthorizationStrategy in project workflow-cps-plugin by jenkinsci.
the class CpsFlowExecutionTest method pause.
@Issue("JENKINS-25736")
@Test
public void pause() throws Throwable {
sessions.then(r -> {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
r.jenkins.setSecurityRealm(r.createDummySecurityRealm());
r.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy().grant(Jenkins.READ, Item.READ).everywhere().toEveryone().grant(Jenkins.ADMINISTER).everywhere().to("admin").grant(Item.BUILD, Item.CANCEL).onItems(p).to("dev"));
r.jenkins.save();
p.setDefinition(new CpsFlowDefinition("echo 'before'; semaphore 'one'; echo 'after'", true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
SemaphoreStep.waitForStart("one/1", b);
final CpsFlowExecution e = (CpsFlowExecution) b.getExecution();
assertFalse(e.isPaused());
JenkinsRule.WebClient wc = r.createWebClient();
String toggleUrlRel = b.getUrl() + PauseUnpauseAction.URL + "/toggle";
WebRequest wrs = new WebRequest(wc.createCrumbedUrl(toggleUrlRel), HttpMethod.POST);
try {
// like JenkinsRule.assertFails but taking a WebRequest:
fail("should have been rejected but produced: " + wc.getPage(wrs).getWebResponse().getContentAsString());
} catch (FailingHttpStatusCodeException x) {
// link not even offered
assertEquals(HttpURLConnection.HTTP_NOT_FOUND, x.getStatusCode());
}
wc.login("admin").getPage(wrs);
assertTrue(e.isPaused());
r.waitForMessage("before", b);
SemaphoreStep.success("one/1", null);
// not a very strong way of ensuring that the pause actually happens
Thread.sleep(1000);
assertTrue(b.isBuilding());
assertTrue(e.isPaused());
// link should only be displayed conditionally:
String toggleUrlAbs = r.contextPath + "/" + toggleUrlRel;
r.createWebClient().login("admin").getPage(b).getAnchorByHref(toggleUrlAbs);
try {
r.createWebClient().getPage(b).getAnchorByHref(toggleUrlAbs);
fail("link should not be present for anonymous user without CANCEL");
} catch (ElementNotFoundException x) {
// good
}
});
sessions.then(r -> {
WorkflowJob p = r.jenkins.getItemByFullName("p", WorkflowJob.class);
WorkflowRun b = p.getLastBuild();
assertTrue(b.isBuilding());
CpsFlowExecution e = (CpsFlowExecution) b.getExecution();
assertTrue(e.isPaused());
JenkinsRule.WebClient wc = r.createWebClient();
WebRequest wrs = new WebRequest(wc.createCrumbedUrl(b.getUrl() + PauseUnpauseAction.URL + "/toggle"), HttpMethod.POST);
wc.login("dev").getPage(wrs);
assertFalse(e.isPaused());
r.assertBuildStatusSuccess(r.waitForCompletion(b));
assertFalse(e.isPaused());
});
}
use of org.jvnet.hudson.test.MockAuthorizationStrategy in project workflow-cps-plugin by jenkinsci.
the class ReplayActionTest method rebuild.
@Issue("JENKINS-47339")
@Test
public void rebuild() throws Exception {
story.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
story.j.jenkins.setSecurityRealm(story.j.createDummySecurityRealm());
story.j.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy().grant(Jenkins.READ, Item.BUILD, Item.READ).everywhere().to("dev3"));
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("echo 'script to rebuild'", true));
WorkflowRun b1 = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
story.j.assertLogContains("script to rebuild", b1);
WorkflowRun b2;
{
// First time around, verify that UI elements are present and functional.
ReplayAction a = b1.getAction(ReplayAction.class);
assertNotNull(a);
assertFalse(canReplay(b1, "dev3"));
assertTrue(canRebuild(b1, "dev3"));
JenkinsRule.WebClient wc = story.j.createWebClient();
wc.login("dev3");
HtmlPage page = wc.getPage(b1, a.getUrlName());
WebAssert.assertFormNotPresent(page, "config");
HtmlForm form = page.getFormByName("rebuild");
HtmlPage redirect = story.j.submit(form);
assertEquals(p.getAbsoluteUrl(), redirect.getUrl().toString());
story.j.waitUntilNoActivity();
b2 = p.getBuildByNumber(2);
assertNotNull(b2);
}
story.j.assertLogContains("script to rebuild", story.j.assertBuildStatusSuccess(b2));
ReplayCause cause = b2.getCause(ReplayCause.class);
assertNotNull(cause);
assertEquals(1, cause.getOriginalNumber());
assertEquals(b1, cause.getOriginal());
assertEquals(b2, cause.getRun());
}
});
}
Aggregations