Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #22 from ikedam/feature/JENKINS-31318_UserPermission
[JENKINS-31318] Retry button invisible with "Build" project role
  • Loading branch information
ikedam committed Dec 6, 2015
2 parents d46742e + 91d88d9 commit 888e8f4
Show file tree
Hide file tree
Showing 2 changed files with 201 additions and 4 deletions.
Expand Up @@ -5,12 +5,14 @@
import java.util.List;

import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerResponse;

import hudson.model.AbstractBuild;
import hudson.model.Action;
import hudson.model.CauseAction;
import hudson.model.Item;
import hudson.model.Job;
import hudson.model.ParametersAction;

import jenkins.model.Jenkins;
Expand All @@ -20,23 +22,33 @@
*/
public class NaginatorRetryAction implements Action {

private boolean hasPermission() {
Job<?, ?> job = Stapler.getCurrentRequest().findAncestorObject(Job.class);
if (job != null) {
return job.getACL().hasPermission(Item.BUILD);
}

Jenkins j = Jenkins.getInstance();
return (j != null)?j.hasPermission(Item.BUILD):false;
}

public String getIconFileName() {
return Jenkins.getInstance().hasPermission(Item.BUILD) ?
return hasPermission() ?
"refresh.png" : null;
}

public String getDisplayName() {
return Jenkins.getInstance().hasPermission(Item.BUILD) ?
return hasPermission() ?
"Retry" : null;
}

public String getUrlName() {
return Jenkins.getInstance().hasPermission(Item.BUILD) ?
return hasPermission() ?
"retry" : null;
}

public void doIndex(StaplerResponse res, @AncestorInPath AbstractBuild build) throws IOException {
Jenkins.getInstance().checkPermission(Item.BUILD);
build.getACL().checkPermission(Item.BUILD);
NaginatorRetryAction.scheduleBuild(build, 0, NaginatorListener.calculateRetryCount(build));
res.sendRedirect2(build.getUpUrl());
}
Expand Down
@@ -0,0 +1,185 @@
/*
* The MIT License
*
* Copyright (c) 2015 IKEDA Yasuyuki
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.chikli.hudson.plugin.naginator;

import static org.junit.Assert.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import jenkins.model.Jenkins;
import hudson.Functions;
import hudson.model.FreeStyleBuild;
import hudson.model.Item;
import hudson.model.Result;
import hudson.model.AbstractBuild;
import hudson.model.FreeStyleProject;
import hudson.model.User;
import hudson.security.Permission;
import hudson.security.AuthorizationMatrixProperty;
import hudson.security.ProjectMatrixAuthorizationStrategy;
import net.sf.ezmorph.object.MapToDateMorpher;

import org.junit.After;
import org.junit.ClassRule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsRule.WebClient;
import org.jvnet.hudson.test.FailureBuilder;

import com.gargoylesoftware.htmlunit.ElementNotFoundException;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.google.common.collect.Sets;

/**
*
*/
public class NaginatorActionFactoryTest {
@ClassRule
public static JenkinsRule r = new JenkinsRule();

@After
public void tearDown() {
r.jenkins.setSecurityRealm(null);
r.jenkins.setAuthorizationStrategy(null);
}

private String getRetryLinkFor(AbstractBuild<?, ?> b) throws Exception {
return Functions.joinPath(r.contextPath, b.getUrl(), "retry");
}

private void assertRetryLinkExists(AbstractBuild<?, ?> b, WebClient wc) throws Exception {
wc.getPage(b).getAnchorByHref(getRetryLinkFor(b));
}

private void assertRetryLinkNotExists(AbstractBuild<?, ?> b, WebClient wc) throws Exception {
try {
HtmlAnchor a = wc.getPage(b).getAnchorByHref(getRetryLinkFor(b));
fail("Should not exist: " + a);
} catch (ElementNotFoundException e) {
// pass
}
}

private void assertRetryLinkExists(AbstractBuild<?, ?> b) throws Exception {
assertRetryLinkExists(b, r.createWebClient());
}

private void assertRetryLinkNotExists(AbstractBuild<?, ?> b) throws Exception {
assertRetryLinkNotExists(b, r.createWebClient());
}

private void assertRetryLinkExists(AbstractBuild<?, ?> b, User u) throws Exception {
assertRetryLinkExists(b, r.createWebClient().login(u.getId()));
}

private void assertRetryLinkNotExists(AbstractBuild<?, ?> b, User u) throws Exception {
assertRetryLinkNotExists(b, r.createWebClient().login(u.getId()));
}

@Test
public void testRetrylinkNotInSuccessBuild() throws Exception {
FreeStyleProject p = r.createFreeStyleProject();
FreeStyleBuild b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
assertRetryLinkNotExists(b);
}

@Test
public void testRetrylinkInFailureBuild() throws Exception {
FreeStyleProject p = r.createFreeStyleProject();
p.getBuildersList().add(new FailureBuilder());
FreeStyleBuild b = r.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0).get());
assertRetryLinkExists(b);
}

@Test
public void testRetrylinkInOptInProject() throws Exception {
FreeStyleProject p = r.createFreeStyleProject();
p.addProperty(new NaginatorOptOutProperty(false));
p.getBuildersList().add(new FailureBuilder());
FreeStyleBuild b = r.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0).get());
assertRetryLinkExists(b);
}

@Test
public void testRetrylinkNotInOptOutProject() throws Exception {
FreeStyleProject p = r.createFreeStyleProject();
p.addProperty(new NaginatorOptOutProperty(true));
p.getBuildersList().add(new FailureBuilder());
FreeStyleBuild b = r.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0).get());
assertRetryLinkNotExists(b);
}

@Test
public void testRetrylinkForPermittedUser() throws Exception {
r.jenkins.setSecurityRealm(r.createDummySecurityRealm());
ProjectMatrixAuthorizationStrategy pmas = new ProjectMatrixAuthorizationStrategy();
pmas.add(Jenkins.READ, "user1");
pmas.add(Item.READ, "user1");
pmas.add(Item.BUILD, "user1");
r.jenkins.setAuthorizationStrategy(pmas);

FreeStyleProject p = r.createFreeStyleProject();
p.getBuildersList().add(new FailureBuilder());
FreeStyleBuild b = r.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0).get());
assertRetryLinkExists(b, User.get("user1"));
}


@Test
public void testRetrylinkNotForNonPermittedUser() throws Exception {
r.jenkins.setSecurityRealm(r.createDummySecurityRealm());
ProjectMatrixAuthorizationStrategy pmas = new ProjectMatrixAuthorizationStrategy();
pmas.add(Jenkins.READ, "user1");
pmas.add(Item.READ, "user1");
//pmas.add(Item.BUILD, "user1");
r.jenkins.setAuthorizationStrategy(pmas);

FreeStyleProject p = r.createFreeStyleProject();
p.getBuildersList().add(new FailureBuilder());
FreeStyleBuild b = r.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0).get());
assertRetryLinkNotExists(b, User.get("user1"));
}

@Test
public void testRetrylinkForPermittedUserByProject() throws Exception {
r.jenkins.setSecurityRealm(r.createDummySecurityRealm());
ProjectMatrixAuthorizationStrategy pmas = new ProjectMatrixAuthorizationStrategy();
pmas.add(Jenkins.READ, "user1");
r.jenkins.setAuthorizationStrategy(pmas);

FreeStyleProject p = r.createFreeStyleProject();

Map<Permission, Set<String>> authMap = new HashMap<Permission, Set<String>>();
authMap.put(Item.READ, Sets.newHashSet("user1"));
authMap.put(Item.BUILD, Sets.newHashSet("user1"));
p.addProperty(new AuthorizationMatrixProperty(authMap));

p.getBuildersList().add(new FailureBuilder());
FreeStyleBuild b = r.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0).get());
assertRetryLinkExists(b, User.get("user1"));
}
}

0 comments on commit 888e8f4

Please sign in to comment.