Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge back BuildAuthorizationTokenSEC2558Test #6185

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

43 changes: 43 additions & 0 deletions test/src/test/java/hudson/model/BuildAuthorizationTokenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.MockAuthorizationStrategy;

// TODO : Using the @Nested class might be cleaner for SECURITY-2558 tests
public class BuildAuthorizationTokenTest {

@Rule
Expand All @@ -33,6 +35,47 @@ public void setupSecurity() {
.grant(Item.READ).everywhere().toEveryone());
}

@Test
@Issue("SECURITY-2558")
public void triggerJobWithoutSecurityRealm_ShouldSucceed_WithPost() throws Exception {
jr.jenkins.setSecurityRealm(null);
jr.jenkins.setAuthorizationStrategy(null);
FreeStyleProject project = createFreestyleProjectWithToken();
JenkinsRule.WebClient wc = jr.createWebClient();
wc.getPage(wc.addCrumb(new WebRequest(new URL(jr.getURL(), project.getUrl() +
"build?delay=0"),
HttpMethod.POST)));
jr.waitUntilNoActivity();
assertThat("the project should have been built", project.getBuilds(), hasSize(1));
}

@Test
@Issue("SECURITY-2558")
public void triggerJobWithoutSecurityRealm_ShouldFail_WithGet() throws Exception {
jr.jenkins.setSecurityRealm(null);
jr.jenkins.setAuthorizationStrategy(null);
FreeStyleProject project = jr.createFreeStyleProject();
JenkinsRule.WebClient wc = jr.createWebClient();
FailingHttpStatusCodeException fex = assertThrows(
"should not reach here since only POST request can",
FailingHttpStatusCodeException.class,
() -> wc.getPage(new WebRequest(new URL(jr.getURL(), project.getUrl() + "build?delay=0"), HttpMethod.GET)));
assertThat("Should fail with method not allowed", fex.getStatusCode(), is(405));
}

@Test
@Issue("SECURITY-2558")
public void triggerJobWithoutSecurityRealm_ButWithToken_ShouldSucceed_WithGet() throws Exception {
jr.jenkins.setSecurityRealm(null);
jr.jenkins.setAuthorizationStrategy(null);
FreeStyleProject project = createFreestyleProjectWithToken();
JenkinsRule.WebClient wc = jr.createWebClient();
wc.getPage(new WebRequest(new URL(jr.getURL(), project.getUrl() + "build?delay=0&token=" + token),
HttpMethod.GET));
jr.waitUntilNoActivity();
assertThat("the project should have been built", project.getBuilds(), hasSize(1));
}

@Test
public void triggerJobWithTokenShouldSucceedWithPost() throws Exception {
FreeStyleProject project = createFreestyleProjectWithToken();
Expand Down