use of org.kohsuke.stapler.StaplerRequest in project blueocean-plugin by jenkinsci.
the class BitbucketCloudScmContentProviderTest method unauthorizedSaveContentShouldFail.
@Test
public void unauthorizedSaveContentShouldFail() throws UnirestException, IOException {
User alice = User.get("alice");
alice.setFullName("Alice Cooper");
alice.addProperty(new Mailer.UserProperty("alice@jenkins-ci.org"));
String aliceCredentialId = createCredential(BitbucketCloudScm.ID, alice);
StaplerRequest staplerRequest = mockStapler();
MultiBranchProject mbp = mockMbp(aliceCredentialId, alice);
GitContent content = new GitContent.Builder().autoCreateBranch(true).base64Data("bm9kZXsKICBlY2hvICdoZWxsbyB3b3JsZCEnCn0K").branch("master").message("new commit").owner("TESTP").path("README.md").repo("pipeline-demo-test").build();
when(staplerRequest.bindJSON(Mockito.eq(BitbucketScmSaveFileRequest.class), Mockito.any(JSONObject.class))).thenReturn(new BitbucketScmSaveFileRequest(content));
String request = "{\n" + " \"content\" : {\n" + " \"message\" : \"new commit\",\n" + " \"path\" : \"README.md\",\n" + " \"branch\" : \"master\",\n" + " \"repo\" : \"pipeline-demo-test\",\n" + " \"base64Data\" : " + "\"bm9kZXsKICBlY2hvICdoZWxsbyB3b3JsZCEnCn0K\"" + " }\n" + "}";
when(staplerRequest.getReader()).thenReturn(new BufferedReader(new StringReader(request), request.length()));
try {
new BitbucketCloudScmContentProvider().saveContent(staplerRequest, mbp);
} catch (ServiceException.PreconditionRequired e) {
assertEquals("Can't access content from Bitbucket: no credential found", e.getMessage());
return;
}
fail("Should have failed with PreConditionException");
}
use of org.kohsuke.stapler.StaplerRequest in project blueocean-plugin by jenkinsci.
the class GithubScm method getCustomApiUri.
@Nonnull
protected String getCustomApiUri() {
StaplerRequest request = Stapler.getCurrentRequest();
Objects.requireNonNull(request, "Must be called in HTTP request context");
String apiUri = request.getParameter("apiUrl");
// if "apiUrl" parameter was supplied, parse and trim trailing slash
if (!StringUtils.isEmpty(apiUri)) {
try {
new URI(apiUri);
} catch (URISyntaxException ex) {
throw new ServiceException.BadRequestException(new ErrorMessage(400, "Invalid URI: " + apiUri));
}
apiUri = normalizeUrl(apiUri);
} else {
apiUri = "";
}
return apiUri;
}
use of org.kohsuke.stapler.StaplerRequest in project blueocean-plugin by jenkinsci.
the class GithubScmTest method getOrganizations.
@Test
public void getOrganizations() {
mockStatic(Stapler.class);
StaplerRequest staplerRequest = mock(StaplerRequest.class);
when(Stapler.getCurrentRequest()).thenReturn(staplerRequest);
when(staplerRequest.getParameter(CREDENTIAL_ID)).thenReturn("12345");
}
use of org.kohsuke.stapler.StaplerRequest in project blueocean-plugin by jenkinsci.
the class GithubScmContentProviderTest method unauthorizedAccessToContentForOrgFolderGHEShouldFail.
@Test
public void unauthorizedAccessToContentForOrgFolderGHEShouldFail() throws UnirestException, IOException {
User alice = User.get("alice");
alice.setFullName("Alice Cooper");
alice.addProperty(new Mailer.UserProperty("alice@jenkins-ci.org"));
String aliceCredentialId = createGithubEnterpriseCredential(alice);
StaplerRequest staplerRequest = mockStapler(GithubEnterpriseScm.ID);
MultiBranchProject mbp = mockMbp(aliceCredentialId, user, GithubEnterpriseScm.DOMAIN_NAME);
try {
// Bob trying to access content but his credential is not setup so should fail
new GithubScmContentProvider().getContent(staplerRequest, mbp);
} catch (ServiceException.PreconditionRequired e) {
assertEquals("Can't access content from github: no credential found", e.getMessage());
return;
}
fail("Should have failed with PreConditionException");
}
use of org.kohsuke.stapler.StaplerRequest in project blueocean-plugin by jenkinsci.
the class GithubScmContentProviderTest method unauthorizedSaveContentToOrgFolderShouldFail.
@Test
public void unauthorizedSaveContentToOrgFolderShouldFail() throws UnirestException, IOException {
User alice = User.get("alice");
alice.setFullName("Alice Cooper");
alice.addProperty(new Mailer.UserProperty("alice@jenkins-ci.org"));
String aliceCredentialId = createGithubCredential(alice);
StaplerRequest staplerRequest = mockStapler();
GitContent content = new GitContent.Builder().autoCreateBranch(true).base64Data("c2xlZXAgMTUKbm9kZSB7CiAgY2hlY2tvdXQgc2NtCiAgc2ggJ2xzIC1sJwp9\\nCnNsZWVwIDE1Cg==\\n").branch("test1").message("another commit").owner("cloudbeers").path("Jankinsfile").repo("PR-demo").sha("e23b8ef5c2c4244889bf94db6c05cc08ea138aef").build();
when(staplerRequest.bindJSON(Mockito.eq(GithubScmSaveFileRequest.class), Mockito.any(JSONObject.class))).thenReturn(new GithubScmSaveFileRequest(content));
MultiBranchProject mbp = mockMbp(aliceCredentialId, user, GithubScm.DOMAIN_NAME);
String request = "{\n" + " \"content\" : {\n" + " \"message\" : \"first commit\",\n" + " \"path\" : \"Jenkinsfile\",\n" + " \"branch\" : \"test1\",\n" + " \"repo\" : \"PR-demo\",\n" + " \"sha\" : \"e23b8ef5c2c4244889bf94db6c05cc08ea138aef\",\n" + " \"base64Data\" : " + "\"c2xlZXAgMTUKbm9kZSB7CiAgY2hlY2tvdXQgc2NtCiAgc2ggJ2xzIC1sJwp9\\nCnNsZWVwIDE1Cg==\\n\"" + " }\n" + "}";
when(staplerRequest.getReader()).thenReturn(new BufferedReader(new StringReader(request), request.length()));
try {
// Bob trying to access content but his credential is not setup so should fail
new GithubScmContentProvider().saveContent(staplerRequest, mbp);
} catch (ServiceException.PreconditionRequired e) {
assertEquals("Can't access content from github: no credential found", e.getMessage());
return;
}
fail("Should have failed with PreConditionException");
}
Aggregations