Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from daniel-beck/prevent-mistake
[JENKINS-46832] Prevent initial misconfiguration by making sure there's an admin
- Loading branch information
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/test/java/hudson/security/ProjectMatrixAuthorizationStrategyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package hudson.security; | ||
|
||
import com.gargoylesoftware.htmlunit.html.HtmlElement; | ||
import com.gargoylesoftware.htmlunit.html.HtmlForm; | ||
import com.gargoylesoftware.htmlunit.html.HtmlLabel; | ||
import hudson.model.User; | ||
import jenkins.model.Jenkins; | ||
import org.junit.Assert; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
public class ProjectMatrixAuthorizationStrategyTest { | ||
|
||
@Rule | ||
public JenkinsRule r = new JenkinsRule(); | ||
|
||
@Test | ||
public void submitEmptyPropertyEnsuresPermissionsForSubmitter() throws Exception { | ||
HudsonPrivateSecurityRealm realm = new HudsonPrivateSecurityRealm(false); | ||
realm.createAccount("alice","alice"); | ||
realm.createAccount("bob","bob"); | ||
r.jenkins.setSecurityRealm(realm); | ||
|
||
r.jenkins.setAuthorizationStrategy(new FullControlOnceLoggedInAuthorizationStrategy()); | ||
|
||
// ensure logged in users are admins, but anon is not | ||
try (ACLContext _ = ACL.as(User.get("alice"))) { | ||
Assert.assertTrue("alice is admin", r.jenkins.hasPermission(Jenkins.ADMINISTER)); | ||
} | ||
try (ACLContext _ = ACL.as(User.get("bob"))) { | ||
Assert.assertTrue("bob is admin", r.jenkins.hasPermission(Jenkins.ADMINISTER)); | ||
} | ||
Assert.assertFalse("anon is not admin", r.jenkins.getACL().hasPermission(Jenkins.ANONYMOUS, Jenkins.ADMINISTER)); | ||
|
||
JenkinsRule.WebClient wc = r.createWebClient().login("alice"); | ||
HtmlForm form = wc.goTo("configureSecurity").getFormByName("config"); | ||
|
||
// TODO this must be possible in a nicer way | ||
HtmlElement label = form.getElementsByTagName("label").stream().filter( | ||
lbl -> lbl.asText().contains(GlobalMatrixAuthorizationStrategy.DESCRIPTOR.getDisplayName())).findAny().get(); | ||
((HtmlLabel)label).click(); | ||
r.submit(form); | ||
|
||
try (ACLContext _ = ACL.as(User.get("alice"))) { | ||
// ensure that the user submitting the empty matrix will be admin | ||
Assert.assertTrue("alice is admin", r.jenkins.hasPermission(Jenkins.ADMINISTER)); | ||
} | ||
try (ACLContext _ = ACL.as(User.get("bob"))) { | ||
Assert.assertFalse("bob is not admin", r.jenkins.hasPermission(Jenkins.ADMINISTER)); | ||
} | ||
Assert.assertFalse("anon is not admin", r.jenkins.getACL().hasPermission(Jenkins.ANONYMOUS, Jenkins.ADMINISTER)); | ||
} | ||
|
||
@Test | ||
public void submitEmptyPropertyEnsuresPermissionsForAnonymousSubmitter() throws Exception { | ||
// prepare form to have options visible | ||
r.jenkins.setSecurityRealm(new HudsonPrivateSecurityRealm(true)); | ||
r.jenkins.setAuthorizationStrategy(new AuthorizationStrategy.Unsecured()); | ||
|
||
Assert.assertTrue("anon is admin", r.jenkins.getACL().hasPermission(Jenkins.ANONYMOUS, Jenkins.ADMINISTER)); | ||
|
||
JenkinsRule.WebClient wc = r.createWebClient(); | ||
HtmlForm form = wc.goTo("configureSecurity").getFormByName("config"); | ||
|
||
// TODO this must be possible in a nicer way | ||
HtmlElement label = form.getElementsByTagName("label").stream().filter( | ||
lbl -> lbl.asText().contains(GlobalMatrixAuthorizationStrategy.DESCRIPTOR.getDisplayName())).findAny().get(); | ||
((HtmlLabel)label).click(); | ||
r.submit(form); | ||
|
||
Assert.assertTrue("anon is admin", r.jenkins.getACL().hasPermission(Jenkins.ANONYMOUS, Jenkins.ADMINISTER)); | ||
Assert.assertTrue(r.jenkins.getAuthorizationStrategy() instanceof GlobalMatrixAuthorizationStrategy); | ||
} | ||
} |