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

Tested REST submission #11

Merged
merged 2 commits into from
Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ pipeline {
- [X] implementation
- [X] inline help text
- [X] `withFileParameter` compatibility
- [ ] manual test
- [X] manual test
- [X] automated test
- [ ] sanity check against `artifact-manager-s3`
- [ ] suppress download link when stash is missing
- [ ] archived file parameter (compatible with freestyle, and suitable if you want to ensure parameters are kept after the build ends)
- [ ] implementation
- [ ] inline help text
Expand All @@ -66,10 +67,10 @@ pipeline {
- [X] implementation
- [ ] manual test
- [X] automated test
- [ ] `POST` submission
- [X] `POST` submission
- [X] implementation
- [ ] manual test
- [ ] automated test
- [X] manual test
- [X] automated test
- [X] `withFileParameter` wrapper step
- [X] implementation
- [X] inline help text
Expand All @@ -87,10 +88,6 @@ pipeline {
- [X] tests using Declarative syntax
- [ ] tests using `build-token-root`

## Getting started

TODO

## LICENSE

Licensed under MIT, see [LICENSE](LICENSE.md)
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,20 @@

package io.jenkins.plugins.file_parameters;

import com.gargoylesoftware.htmlunit.FormEncodingType;
import com.gargoylesoftware.htmlunit.HttpMethod;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.util.KeyDataPair;
import com.gargoylesoftware.htmlunit.util.NameValuePair;
import hudson.cli.CLICommandInvoker;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.User;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.net.URL;
import java.util.Collections;
import jenkins.model.Jenkins;
import org.apache.commons.io.FileUtils;
import static org.hamcrest.MatcherAssert.assertThat;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
Expand All @@ -35,15 +46,19 @@
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.MockAuthorizationStrategy;

public class AbstractFileParameterDefinitionTest {

@ClassRule public static BuildWatcher buildWatcher = new BuildWatcher();

@Rule public JenkinsRule r = new JenkinsRule();

@Rule public TemporaryFolder tmp = new TemporaryFolder();

// adapted from BuildCommandTest.fileParameter
@Test public void cli() throws Exception {
WorkflowJob p = r.createProject(WorkflowJob.class, "myjob");
Expand All @@ -58,4 +73,24 @@ public class AbstractFileParameterDefinitionTest {
r.assertLogContains("received: dXBsb2FkZWQgY29udGVudCBoZXJl", b);
}

@Test public void rest() throws Exception {
r.jenkins.setSecurityRealm(r.createDummySecurityRealm());
r.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy().grant(Jenkins.ADMINISTER).everywhere().to("admin"));
WorkflowJob p = r.createProject(WorkflowJob.class, "myjob");
p.addProperty(new ParametersDefinitionProperty(new Base64FileParameterDefinition("FILE", null)));
p.setDefinition(new CpsFlowDefinition("echo(/received: $FILE/)", true));
// Like: curl -u $auth -F FILE=@/tmp/f $jenkins/job/myjob/buildWithParameters
WebRequest req = new WebRequest(new URL(r.getURL() + "job/myjob/buildWithParameters"), HttpMethod.POST);
File f = tmp.newFile();
FileUtils.write(f, "uploaded content here", "UTF-8");
req.setEncodingType(FormEncodingType.MULTIPART);
req.setRequestParameters(Collections.<NameValuePair>singletonList(new KeyDataPair("FILE", f, "FILE", "text/plain", "UTF-8")));
User.getById("admin", true); // TODO workaround for https://github.com/jenkinsci/jenkins-test-harness/pull/273
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r.createWebClient().withBasicApiToken("admin").getPage(req);
r.waitUntilNoActivity();
WorkflowRun b = p.getBuildByNumber(1);
assertNotNull(b);
r.assertLogContains("received: dXBsb2FkZWQgY29udGVudCBoZXJl", b);
}

}