Search in sources :

Example 21 with GitContent

use of io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent in project blueocean-plugin by jenkinsci.

the class BitbucketServerScmContentProviderTest 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(BitbucketServerScm.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 BitbucketServerScmContentProvider().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");
}
Also used : User(hudson.model.User) StaplerRequest(org.kohsuke.stapler.StaplerRequest) Mailer(hudson.tasks.Mailer) GitContent(io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent) JSONObject(net.sf.json.JSONObject) ServiceException(io.jenkins.blueocean.commons.ServiceException) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) BitbucketScmSaveFileRequest(io.jenkins.blueocean.blueocean_bitbucket_pipeline.BitbucketScmSaveFileRequest) MultiBranchProject(jenkins.branch.MultiBranchProject) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

GitContent (io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent)21 MultiBranchProject (jenkins.branch.MultiBranchProject)17 JSONObject (net.sf.json.JSONObject)17 Test (org.junit.Test)17 StaplerRequest (org.kohsuke.stapler.StaplerRequest)17 BufferedReader (java.io.BufferedReader)15 StringReader (java.io.StringReader)15 ServiceException (io.jenkins.blueocean.commons.ServiceException)10 User (hudson.model.User)8 ScmFile (io.jenkins.blueocean.rest.impl.pipeline.scm.ScmFile)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 Mailer (hudson.tasks.Mailer)6 BitbucketScmSaveFileRequest (io.jenkins.blueocean.blueocean_bitbucket_pipeline.BitbucketScmSaveFileRequest)5 IOException (java.io.IOException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 BbBranch (io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbBranch)1 BbSaveContentResponse (io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbSaveContentResponse)1 ErrorMessage (io.jenkins.blueocean.commons.ErrorMessage)1