File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\test\src\test\java\hudson\security\ExtendedReadPermissionTest.java.html

1 package hudson.security;
2
3 import com.gargoylesoftware.htmlunit.html.HtmlButton;
4 import com.gargoylesoftware.htmlunit.html.HtmlForm;
5 import com.gargoylesoftware.htmlunit.html.HtmlPage;
6 import hudson.model.Item;
7 import java.net.HttpURLConnection;
8 import org.jvnet.hudson.test.HudsonTestCase;
9 import org.jvnet.hudson.test.recipes.LocalData;
10
11 /**
12 *
13 * @author dty
14 */
15 public class ExtendedReadPermissionTest extends HudsonTestCase {
16 private boolean enabled;
17
18 @Override
19 protected void setUp() throws Exception {
20 super.setUp();
21 enabled = Item.EXTENDED_READ.getEnabled();
22 }
23
24 @Override
25 protected void tearDown() throws Exception {
26 Item.EXTENDED_READ.setEnabled(enabled);
27 super.tearDown();
28 }
29
30 /**
31 * alice: Job/Configure+Read
32 * bob: Job/Read
33 * charlie: Job/ExtendedRead+Read
34 */
35
36 private void setPermissionEnabled(boolean enabled) throws Exception {
37 Item.EXTENDED_READ.setEnabled(enabled);
38 }
39
40
41 @LocalData
42 public void testReadOnlyConfigAccessWithPermissionEnabled() throws Exception {
43 setPermissionEnabled(true);
44
45 AuthorizationStrategy as = jenkins.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));
49
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);
55 }
56
57 @LocalData
58 public void testReadOnlyConfigAccessWithPermissionDisabled() throws Exception {
59 setPermissionEnabled(false);
60
61 AuthorizationStrategy as = jenkins.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));
65
66 WebClient wc = new WebClient().login("charlie","charlie");
67 wc.assertFails("job/a/configure", HttpURLConnection.HTTP_FORBIDDEN);
68 }
69
70 @LocalData
71 public void testNoConfigAccessWithPermissionEnabled() throws Exception {
72 setPermissionEnabled(true);
73
74 AuthorizationStrategy as = jenkins.getAuthorizationStrategy();
75 assertTrue("Expecting GlobalMatrixAuthorizationStrategy", (as instanceof GlobalMatrixAuthorizationStrategy));
76 GlobalMatrixAuthorizationStrategy gas = (GlobalMatrixAuthorizationStrategy)as;
77 assertFalse("Bob should not have extended read for this test", gas.hasExplicitPermission("bob",Item.EXTENDED_READ));
78
79 WebClient wc = new WebClient().login("bob","bob");
80 wc.assertFails("job/a/configure", HttpURLConnection.HTTP_FORBIDDEN);
81 }
82
83 /*
84 @LocalData
85 public void testConfigureLink() throws Exception {
86
87 }
88
89 @LocalData
90 public void testViewConfigurationLink() throws Exception {
91
92 }
93
94 @LocalData
95 public void testMatrixWithPermissionEnabled() throws Exception {
96 }
97
98 @LocalData
99 public void testMatrixWithPermissionDisabled() throws Exception {
100
101 }
102 */
103 }
104