Skip to content

Commit

Permalink
Adjust tests now that we have enforced security
Browse files Browse the repository at this point in the history
  • Loading branch information
lacostej committed Oct 31, 2023
1 parent 8ea1500 commit d5e0dc2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/test/java/hudson/plugins/batch_task/BatchTaskTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ public class BatchTaskTest {
*/
@Test
public void testNoBuilds() throws Exception {
r.jenkins.setCrumbIssuer(null); //Not really testing csrf right now
FreeStyleProject p = r.createFreeStyleProject();
p.addProperty(new BatchTaskProperty(new BatchTask("test", "echo hello")));
JenkinsRule.WebClient wc = r.createWebClient();
HtmlPage page = wc.getPage(p, "batchTasks/task/test/execute");
HtmlPage page = TestHelper.post(wc, p.getUrl() + "batchTasks/task/test/execute", null, null);
String path = page.getWebResponse().getWebRequest().getUrl().getPath();
assertTrue("should redirect to noBuilds page: " + path, path.endsWith("/noBuild"));
}
Expand All @@ -72,7 +73,7 @@ public void testNoBuilds() throws Exception {
*/
@Test
public void testExecute() throws Exception {

r.jenkins.setCrumbIssuer(null); //Not really testing csrf right now
r.jenkins.getGlobalNodeProperties().add(new EnvironmentVariablesNodeProperty(
new EnvironmentVariablesNodeProperty.Entry("GLOBAL", "global-property"),
new EnvironmentVariablesNodeProperty.Entry("OVERRIDE_ME", "foo")));
Expand All @@ -93,7 +94,7 @@ public void testExecute() throws Exception {
while (freeStyleBuild.isBuilding()) {
Thread.sleep(100);
}
r.createWebClient().getPage(p, "batchTasks/task/test/execute");
TestHelper.post(r.createWebClient(), p.getUrl() + "batchTasks/task/test/execute", null, null);
BatchRun run = task.getLastRun();
assertNotNull("task did not run", run);
CauseAction ca = run.getAction(CauseAction.class);
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/hudson/plugins/batch_task/TestHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package hudson.plugins.batch_task;

import org.htmlunit.FailingHttpStatusCodeException;
import org.htmlunit.HttpMethod;
import org.htmlunit.WebRequest;
import org.htmlunit.html.HtmlPage;
import org.htmlunit.util.UrlUtils;
import org.jvnet.hudson.test.JenkinsRule;

import java.io.IOException;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;

public class TestHelper {
/**
* Performs an HTTP POST request to the relative url.
*
* @param webClient the client
* @param relative the url relative to the context path
* @param expectedContentType if expecting specific content type or null if not
* @param expectedStatus if expecting a failing http status code or null if not
* @throws IOException if so
*/
public static HtmlPage post(JenkinsRule.WebClient webClient, String relative,
String expectedContentType, Integer expectedStatus) throws IOException {
WebRequest request = new WebRequest(
UrlUtils.toUrlUnsafe(webClient.getContextPath() + relative),
HttpMethod.POST);
try {
HtmlPage p = webClient.getPage(request);
if (expectedContentType != null) {
assertThat(p.getWebResponse().getContentType(), is(expectedContentType));
}
return p;
} catch (FailingHttpStatusCodeException e) {
if (expectedStatus != null) {
assertEquals(expectedStatus.intValue(), e.getStatusCode());
return null;
} else {
throw e;
}
}
}
}

0 comments on commit d5e0dc2

Please sign in to comment.