1
package hudson.security;
3
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
4
import com.gargoylesoftware.htmlunit.html.HtmlButton;
5
import com.gargoylesoftware.htmlunit.html.HtmlForm;
6
import com.gargoylesoftware.htmlunit.html.HtmlPage;
7
import hudson.model.Item;
8
import org.jvnet.hudson.test.HudsonTestCase;
9
import org.jvnet.hudson.test.recipes.LocalData;
15
public class ExtendedReadPermissionTest extends HudsonTestCase {
16
private boolean enabled;
19
protected void setUp() throws Exception {
21
enabled = Item.EXTENDED_READ.getEnabled();
25
protected void tearDown() throws Exception {
26
Item.EXTENDED_READ.setEnabled(enabled);
31
* alice: Job/Configure+Read
33
* charlie: Job/ExtendedRead+Read
36
private void setPermissionEnabled(boolean enabled) throws Exception {
37
Item.EXTENDED_READ.setEnabled(enabled);
42
public void testReadOnlyConfigAccessWithPermissionEnabled() throws Exception {
43
setPermissionEnabled(true);
45
AuthorizationStrategy as = hudson.getAuthorizationStrategy();
46
assertTrue("Expecting GlobalMatrixAuthorizationStrategy", (as instanceof GlobalMatrixAuthorizationStrategy));
47
GlobalMatrixAuthorizationStrategy gas = (GlobalMatrixAuthorizationStrategy)as;
48
assertTrue("Charlie should have extended read for this test", gas.hasExplicitPermission("charlie",Item.EXTENDED_READ));
50
WebClient wc = new WebClient().login("charlie","charlie");
51
HtmlPage page = wc.goTo("job/a/configure");
52
HtmlForm form = page.getFormByName("config");
53
HtmlButton saveButton = getButtonByCaption(form,"Save");
54
assertNull(saveButton);
58
public void testReadOnlyConfigAccessWithPermissionDisabled() throws Exception {
59
setPermissionEnabled(false);
61
AuthorizationStrategy as = hudson.getAuthorizationStrategy();
62
assertTrue("Expecting GlobalMatrixAuthorizationStrategy", (as instanceof GlobalMatrixAuthorizationStrategy));
63
GlobalMatrixAuthorizationStrategy gas = (GlobalMatrixAuthorizationStrategy)as;
64
assertFalse("Charlie should not have extended read for this test", gas.hasExplicitPermission("charlie",Item.EXTENDED_READ));
66
WebClient wc = new WebClient().login("charlie","charlie");
68
HtmlPage page = wc.goTo("job/a/configure");
70
catch (FailingHttpStatusCodeException e) {
71
assertEquals(403,e.getStatusCode());
75
fail("Charlie should not have been able to access the configuration page");
79
public void testNoConfigAccessWithPermissionEnabled() throws Exception {
80
setPermissionEnabled(true);
82
AuthorizationStrategy as = hudson.getAuthorizationStrategy();
83
assertTrue("Expecting GlobalMatrixAuthorizationStrategy", (as instanceof GlobalMatrixAuthorizationStrategy));
84
GlobalMatrixAuthorizationStrategy gas = (GlobalMatrixAuthorizationStrategy)as;
85
assertFalse("Bob should not have extended read for this test", gas.hasExplicitPermission("bob",Item.EXTENDED_READ));
87
WebClient wc = new WebClient().login("bob","bob");
89
HtmlPage page = wc.goTo("job/a/configure");
91
catch (FailingHttpStatusCodeException e) {
92
assertEquals(403,e.getStatusCode());
96
fail("Bob should not have been able to access the configuration page");
101
public void testConfigureLink() throws Exception {
106
public void testViewConfigurationLink() throws Exception {
111
public void testMatrixWithPermissionEnabled() throws Exception {
115
public void testMatrixWithPermissionDisabled() throws Exception {