Search in sources :

Example 6 with PresetData

use of org.jvnet.hudson.test.recipes.PresetData in project jenkins by jenkinsci.

the class SecurityContextExecutorServiceTest method testCallableCollectionAgainstAllContexts.

@Test
@PresetData(PresetData.DataSet.NO_ANONYMOUS_READACCESS)
public void testCallableCollectionAgainstAllContexts() throws Exception {
    Collection<Callable<SecurityContext>> callables = new ArrayList<>();
    Callable<SecurityContext> c = new Callable<SecurityContext>() {

        @Override
        public SecurityContext call() {
            return SecurityContextHolder.getContext();
        }
    };
    callables.add(c);
    callables.add(c);
    callables.add(c);
    SecurityContextHolder.setContext(systemContext);
    Collection<Future<SecurityContext>> results = wrappedService.invokeAll(callables);
    for (Future<SecurityContext> result : results) {
        // Assert each thread context was identical to the initial service context
        SecurityContext value = result.get();
        assertEquals(systemContext, value);
    }
    SecurityContextHolder.setContext(userContext);
    results = wrappedService.invokeAll(callables);
    for (Future<SecurityContext> result : results) {
        // Assert each thread context was identical to the initial service context
        assertEquals(userContext, result.get());
    }
    SecurityContextHolder.setContext(nullContext);
    results = wrappedService.invokeAll(callables);
    for (Future<SecurityContext> result : results) {
        // Assert each thread context was identical to the initial service context
        assertEquals(nullContext, result.get());
    }
}
Also used : ArrayList(java.util.ArrayList) SecurityContext(org.springframework.security.core.context.SecurityContext) Future(java.util.concurrent.Future) Callable(java.util.concurrent.Callable) Test(org.junit.Test) PresetData(org.jvnet.hudson.test.recipes.PresetData)

Example 7 with PresetData

use of org.jvnet.hudson.test.recipes.PresetData in project jenkins by jenkinsci.

the class LayoutTest method rejectedLinks.

@Issue("JENKINS-21254")
@PresetData(PresetData.DataSet.NO_ANONYMOUS_READACCESS)
@Test
public void rejectedLinks() throws Exception {
    JenkinsRule.WebClient wc = r.createWebClient();
    String prefix = r.contextPath + '/';
    for (DomElement e : wc.goTo("login").getElementsByTagName("link")) {
        String href = ((HtmlLink) e).getHrefAttribute();
        if (!href.startsWith(prefix)) {
            System.err.println("ignoring " + href);
            continue;
        }
        System.err.println("checking " + href);
        wc.goTo(href.substring(prefix.length()), null);
    }
}
Also used : DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HtmlLink(com.gargoylesoftware.htmlunit.html.HtmlLink) JenkinsRule(org.jvnet.hudson.test.JenkinsRule) Issue(org.jvnet.hudson.test.Issue) PresetData(org.jvnet.hudson.test.recipes.PresetData) Test(org.junit.Test)

Example 8 with PresetData

use of org.jvnet.hudson.test.recipes.PresetData in project jenkins by jenkinsci.

the class DefaultCrumbIssuerTest method apiJson.

@PresetData(PresetData.DataSet.ANONYMOUS_READONLY)
@Test
public void apiJson() throws Exception {
    WebClient wc = r.createWebClient();
    String json = wc.goTo("crumbIssuer/api/json", "application/json").getWebResponse().getContentAsString();
    JSONObject jsonObject = JSONObject.fromObject(json);
    assertEquals(r.jenkins.getCrumbIssuer().getCrumbRequestField(), jsonObject.getString("crumbRequestField"));
    assertTrue(jsonObject.getString("crumb").matches("[0-9a-f]+"));
    wc.assertFails("crumbIssuer/api/json?jsonp=hack", HttpURLConnection.HTTP_FORBIDDEN);
}
Also used : JSONObject(net.sf.json.JSONObject) Matchers.containsString(org.hamcrest.Matchers.containsString) WebClient(org.jvnet.hudson.test.JenkinsRule.WebClient) PresetData(org.jvnet.hudson.test.recipes.PresetData) Test(org.junit.Test)

Example 9 with PresetData

use of org.jvnet.hudson.test.recipes.PresetData in project jenkins by jenkinsci.

the class DefaultCrumbIssuerTest method apiXml.

@PresetData(PresetData.DataSet.ANONYMOUS_READONLY)
@Test
public void apiXml() throws Exception {
    WebClient wc = r.createWebClient();
    r.assertXPathValue(wc.goToXml("crumbIssuer/api/xml"), "//crumbRequestField", r.jenkins.getCrumbIssuer().getCrumbRequestField());
    String text = wc.goTo("crumbIssuer/api/xml?xpath=concat(//crumbRequestField,'=',//crumb)", "text/plain").getWebResponse().getContentAsString();
    assertTrue(text, text.matches("\\Q" + r.jenkins.getCrumbIssuer().getCrumbRequestField() + "\\E=[0-9a-f]+"));
    text = wc.goTo("crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)", "text/plain").getWebResponse().getContentAsString();
    assertTrue(text, text.matches("\\Q" + r.jenkins.getCrumbIssuer().getCrumbRequestField() + "\\E:[0-9a-f]+"));
    text = wc.goTo("crumbIssuer/api/xml?xpath=/*/crumbRequestField/text()", "text/plain").getWebResponse().getContentAsString();
    assertEquals(r.jenkins.getCrumbIssuer().getCrumbRequestField(), text);
    text = wc.goTo("crumbIssuer/api/xml?xpath=/*/crumb/text()", "text/plain").getWebResponse().getContentAsString();
    assertTrue(text, text.matches("[0-9a-f]+"));
    wc.assertFails("crumbIssuer/api/xml?xpath=concat('hack=\"',//crumb,'\"')", HttpURLConnection.HTTP_FORBIDDEN);
    wc.assertFails("crumbIssuer/api/xml?xpath=concat(\"hack='\",//crumb,\"'\")", HttpURLConnection.HTTP_FORBIDDEN);
    // 37.5% chance that crumb ~ /[a-f].+/
    wc.assertFails("crumbIssuer/api/xml?xpath=concat('{',//crumb,':1}')", HttpURLConnection.HTTP_FORBIDDEN);
    // ditto
    wc.assertFails("crumbIssuer/api/xml?xpath=concat('hack.',//crumb,'=1')", HttpURLConnection.HTTP_FORBIDDEN);
    r.jenkins.getCrumbIssuer().getDescriptor().setCrumbRequestField("_crumb");
    // perhaps interpretable as JS number
    wc.assertFails("crumbIssuer/api/xml?xpath=concat(//crumbRequestField,'=',//crumb)", HttpURLConnection.HTTP_FORBIDDEN);
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) WebClient(org.jvnet.hudson.test.JenkinsRule.WebClient) PresetData(org.jvnet.hudson.test.recipes.PresetData) Test(org.junit.Test)

Example 10 with PresetData

use of org.jvnet.hudson.test.recipes.PresetData in project jenkins by jenkinsci.

the class LoginTest method loginRememberMe.

/**
 * Test 'remember me' cookie
 */
@Test
@PresetData(DataSet.SECURED_ACEGI)
public void loginRememberMe() throws Exception {
    WebClient wc = j.createWebClient();
    HtmlFormUtil.submit(prepareLoginFormWithRememberMeChecked(wc), null);
    assertNotNull(getRememberMeCookie(wc));
}
Also used : WebClient(org.jvnet.hudson.test.JenkinsRule.WebClient) Test(org.junit.Test) PresetData(org.jvnet.hudson.test.recipes.PresetData)

Aggregations

PresetData (org.jvnet.hudson.test.recipes.PresetData)18 Test (org.junit.Test)17 WebClient (org.jvnet.hudson.test.JenkinsRule.WebClient)10 JenkinsRule (org.jvnet.hudson.test.JenkinsRule)4 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 FailingHttpStatusCodeException (com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException)2 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)2 HtmlForm (com.gargoylesoftware.htmlunit.html.HtmlForm)2 HtmlLink (com.gargoylesoftware.htmlunit.html.HtmlLink)2 Future (java.util.concurrent.Future)2 Issue (org.jvnet.hudson.test.Issue)2 Page (com.gargoylesoftware.htmlunit.Page)1 XmlPage (com.gargoylesoftware.htmlunit.xml.XmlPage)1 Launcher (hudson.Launcher)1 Proc (hudson.Proc)1 FreeStyleProject (hudson.model.FreeStyleProject)1 Slave (hudson.model.Slave)1 Channel (hudson.remoting.Channel)1 GlobalMatrixAuthorizationStrategy (hudson.security.GlobalMatrixAuthorizationStrategy)1