Search in sources :

Example 11 with MockAuthorizationStrategy

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();
}
Also used : WebRequest(com.gargoylesoftware.htmlunit.WebRequest) MockAuthorizationStrategy(org.jvnet.hudson.test.MockAuthorizationStrategy) Matchers.containsString(org.hamcrest.Matchers.containsString) WebClient(org.jvnet.hudson.test.JenkinsRule.WebClient) URL(java.net.URL)

Example 12 with MockAuthorizationStrategy

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);
}
Also used : MockAuthorizationStrategy(org.jvnet.hudson.test.MockAuthorizationStrategy)

Example 13 with MockAuthorizationStrategy

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);
}
Also used : MockAuthorizationStrategy(org.jvnet.hudson.test.MockAuthorizationStrategy)

Example 14 with MockAuthorizationStrategy

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());
    });
}
Also used : WebRequest(com.gargoylesoftware.htmlunit.WebRequest) FailingHttpStatusCodeException(com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException) MockAuthorizationStrategy(org.jvnet.hudson.test.MockAuthorizationStrategy) ElementNotFoundException(com.gargoylesoftware.htmlunit.ElementNotFoundException) JenkinsRule(org.jvnet.hudson.test.JenkinsRule) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 15 with MockAuthorizationStrategy

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());
        }
    });
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Statement(org.junit.runners.model.Statement) RestartableJenkinsRule(org.jvnet.hudson.test.RestartableJenkinsRule) JenkinsRule(org.jvnet.hudson.test.JenkinsRule) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) HtmlForm(com.gargoylesoftware.htmlunit.html.HtmlForm) MockAuthorizationStrategy(org.jvnet.hudson.test.MockAuthorizationStrategy) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Aggregations

MockAuthorizationStrategy (org.jvnet.hudson.test.MockAuthorizationStrategy)17 Test (org.junit.Test)14 JenkinsRule (org.jvnet.hudson.test.JenkinsRule)8 Matchers.containsString (org.hamcrest.Matchers.containsString)5 WebClient (org.jvnet.hudson.test.JenkinsRule.WebClient)5 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)4 Issue (org.jvnet.hudson.test.Issue)4 CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)3 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)3 WebRequest (com.gargoylesoftware.htmlunit.WebRequest)2 RestartableJenkinsRule (org.jvnet.hudson.test.RestartableJenkinsRule)2 ElementNotFoundException (com.gargoylesoftware.htmlunit.ElementNotFoundException)1 FailingHttpStatusCodeException (com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException)1 HtmlForm (com.gargoylesoftware.htmlunit.html.HtmlForm)1 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)1 ACLContext (hudson.security.ACLContext)1 URL (java.net.URL)1 ScriptApproval (org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval)1 Statement (org.junit.runners.model.Statement)1