Search in sources :

Example 31 with StaplerRequest

use of org.kohsuke.stapler.StaplerRequest in project blueocean-plugin by jenkinsci.

the class GithubScmContentProviderTest method unauthorizedAccessToContentForMbpShouldFail.

@Test
public void unauthorizedAccessToContentForMbpShouldFail() 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(GithubEnterpriseScm.ID);
    MultiBranchProject mbp = mockMbp(aliceCredentialId, alice, 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");
}
Also used : User(hudson.model.User) ServiceException(io.jenkins.blueocean.commons.ServiceException) StaplerRequest(org.kohsuke.stapler.StaplerRequest) Mailer(hudson.tasks.Mailer) MultiBranchProject(jenkins.branch.MultiBranchProject) Test(org.junit.Test)

Example 32 with StaplerRequest

use of org.kohsuke.stapler.StaplerRequest in project blueocean-plugin by jenkinsci.

the class GithubScmContentProviderTest method saveContentNewBranchToMbp.

@Test
public void saveContentNewBranchToMbp() throws UnirestException, IOException {
    String credentialId = createGithubCredential(user);
    StaplerRequest staplerRequest = mockStapler();
    GitContent content = new GitContent.Builder().autoCreateBranch(true).base64Data("c2xlZXAgMTUKbm9kZSB7CiAgY2hlY2tvdXQgc2NtCiAgc2ggJ2xzIC1sJwp9\\nCnNsZWVwIDE1Cg==\\n").branch("test2").message("another commit").sourceBranch("master").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(credentialId, user, GithubScm.DOMAIN_NAME);
    String request = "{\n" + "  \"content\" : {\n" + "    \"message\" : \"first commit\",\n" + "    \"path\" : \"Jenkinsfile\",\n" + "    \"branch\" : \"test2\",\n" + "    \"sourceBranch\" : \"master\",\n" + "    \"repo\" : \"PR-demo\",\n" + "    \"sha\" : \"e23b8ef5c2c4244889bf94db6c05cc08ea138aef\",\n" + "    \"base64Data\" : " + "\"c2xlZXAgMTUKbm9kZSB7CiAgY2hlY2tvdXQgc2NtCiAgc2ggJ2xzIC1sJwp9\\nCnNsZWVwIDE1Cg==\\n\"" + "  }\n" + "}";
    when(staplerRequest.getReader()).thenReturn(new BufferedReader(new StringReader(request), request.length()));
    GithubFile file = (GithubFile) new GithubScmContentProvider().saveContent(staplerRequest, mbp);
    assertEquals("Jenkinsfile", file.getContent().getName());
    assertEquals("e23b8ef5c2c4244889bf94db6c05cc08ea138aef", file.getContent().getSha());
    assertEquals("PR-demo", file.getContent().getRepo());
    assertEquals("cloudbeers", file.getContent().getOwner());
}
Also used : JSONObject(net.sf.json.JSONObject) StaplerRequest(org.kohsuke.stapler.StaplerRequest) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) MultiBranchProject(jenkins.branch.MultiBranchProject) GitContent(io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent) Test(org.junit.Test)

Example 33 with StaplerRequest

use of org.kohsuke.stapler.StaplerRequest in project blueocean-plugin by jenkinsci.

the class OrganizationFolderTest method testOrganizationFolderFactoryNoPermissionsFolder.

@Test(expected = ServiceException.ForbiddenException.class)
public void testOrganizationFolderFactoryNoPermissionsFolder() throws Exception {
    List<OrganizationFolderPipelineImpl.OrganizationFolderFactory> organizationFolderFactoryList = ExtensionList.lookup(OrganizationFolderPipelineImpl.OrganizationFolderFactory.class);
    OrganizationFolderFactoryTestImpl organizationFolderFactoryTest = ((ExtensionList<OrganizationFolderPipelineImpl.OrganizationFolderFactory>) organizationFolderFactoryList).get(OrganizationFolderFactoryTestImpl.class);
    assertNotNull(organizationFolderFactoryTest);
    OrganizationFolderPipelineImpl folderPipeline = organizationFolderFactoryTest.getFolder(orgFolder, new Reachable() {

        @Override
        public Link getLink() {
            return organization.getLink().rel("/pipelines/");
        }
    }, mockOrganization());
    assertNotNull(folderPipeline);
    assertNotNull(folderPipeline.getQueue());
    assertNotNull(folderPipeline.getQueue().iterator());
    // Make sure the user does not have permissions to that folder
    PowerMockito.when(orgFolder.getACL()).thenReturn(new ACL() {

        @Override
        public boolean hasPermission(Authentication arg0, Permission arg1) {
            return false;
        }
    });
    ScmResourceImpl scmResource = new ScmResourceImpl(orgFolder, folderPipeline);
    StaplerRequest staplerRequest = PowerMockito.mock(StaplerRequest.class);
    assertEquals("hello", scmResource.getContent(staplerRequest));
}
Also used : StaplerRequest(org.kohsuke.stapler.StaplerRequest) ACL(hudson.security.ACL) Authentication(org.acegisecurity.Authentication) ExtensionList(hudson.ExtensionList) Reachable(io.jenkins.blueocean.rest.Reachable) Permission(hudson.security.Permission) Link(io.jenkins.blueocean.rest.hal.Link) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 34 with StaplerRequest

use of org.kohsuke.stapler.StaplerRequest in project blueocean-plugin by jenkinsci.

the class OrganizationFolderTest method testOrganizationFolderFactory.

@Test
public void testOrganizationFolderFactory() throws Exception {
    List<OrganizationFolderPipelineImpl.OrganizationFolderFactory> organizationFolderFactoryList = ExtensionList.lookup(OrganizationFolderPipelineImpl.OrganizationFolderFactory.class);
    OrganizationFolderFactoryTestImpl organizationFolderFactoryTest = ((ExtensionList<OrganizationFolderPipelineImpl.OrganizationFolderFactory>) organizationFolderFactoryList).get(OrganizationFolderFactoryTestImpl.class);
    assertNotNull(organizationFolderFactoryTest);
    OrganizationFolderPipelineImpl folderPipeline = organizationFolderFactoryTest.getFolder(orgFolder, () -> organization.getLink().rel("/pipelines/"), mockOrganization());
    assertNotNull(folderPipeline);
    assertNotNull(folderPipeline.getQueue());
    assertNotNull(folderPipeline.getQueue().iterator());
    // Make sure the user does has permissions to that folder
    PowerMockito.when(orgFolder.getACL()).thenReturn(new ACL() {

        @Override
        public boolean hasPermission(Authentication arg0, Permission arg1) {
            return true;
        }
    });
    ScmResourceImpl scmResource = new ScmResourceImpl(orgFolder, folderPipeline);
    StaplerRequest staplerRequest = PowerMockito.mock(StaplerRequest.class);
    assertEquals("hello", scmResource.getContent(staplerRequest));
}
Also used : Authentication(org.acegisecurity.Authentication) ExtensionList(hudson.ExtensionList) Permission(hudson.security.Permission) StaplerRequest(org.kohsuke.stapler.StaplerRequest) ACL(hudson.security.ACL) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 35 with StaplerRequest

use of org.kohsuke.stapler.StaplerRequest in project blueocean-plugin by jenkinsci.

the class BlueTestResultContainerImpl method iterator.

@NonNull
@Override
public Iterator<BlueTestResult> iterator() {
    Result resolved = resolve();
    if (resolved.summary == null || resolved.results == null) {
        throw new NotFoundException("no tests");
    }
    StaplerRequest request = Stapler.getCurrentRequest();
    if (request != null) {
        String status = request.getParameter("status");
        String state = request.getParameter("state");
        String age = request.getParameter("age");
        return getBlueTestResultIterator(resolved.results, status, state, age);
    }
    return resolved.results.iterator();
}
Also used : StaplerRequest(org.kohsuke.stapler.StaplerRequest) NotFoundException(io.jenkins.blueocean.commons.ServiceException.NotFoundException) BlueTestResult(io.jenkins.blueocean.rest.model.BlueTestResult) Result(io.jenkins.blueocean.rest.factory.BlueTestResultFactory.Result) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Aggregations

StaplerRequest (org.kohsuke.stapler.StaplerRequest)79 Test (org.junit.Test)45 MultiBranchProject (jenkins.branch.MultiBranchProject)28 JSONObject (net.sf.json.JSONObject)20 GitContent (io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent)18 ServiceException (io.jenkins.blueocean.commons.ServiceException)17 BufferedReader (java.io.BufferedReader)16 StringReader (java.io.StringReader)16 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)15 User (hudson.model.User)14 Mailer (hudson.tasks.Mailer)13 StaplerResponse (org.kohsuke.stapler.StaplerResponse)8 File (java.io.File)7 BitbucketScmSaveFileRequest (io.jenkins.blueocean.blueocean_bitbucket_pipeline.BitbucketScmSaveFileRequest)6 ScmFile (io.jenkins.blueocean.rest.impl.pipeline.scm.ScmFile)5 HashMap (java.util.HashMap)5 List (java.util.List)5 Nonnull (javax.annotation.Nonnull)5 Item (hudson.model.Item)4 ArrayList (java.util.ArrayList)4