|
32 | 32 | import static org.junit.jupiter.api.Assertions.assertFalse; |
33 | 33 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
34 | 34 | import static org.junit.jupiter.api.Assertions.assertNull; |
| 35 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
35 | 36 | import static org.junit.jupiter.api.Assertions.assertTrue; |
36 | 37 | import static org.junit.jupiter.api.Assumptions.assumeFalse; |
37 | 38 |
|
|
48 | 49 | import jenkins.model.Jenkins; |
49 | 50 | import net.sf.json.JSONNull; |
50 | 51 | import net.sf.json.JSONObject; |
| 52 | +import org.htmlunit.FailingHttpStatusCodeException; |
| 53 | +import org.htmlunit.Page; |
51 | 54 | import org.htmlunit.WebResponse; |
52 | 55 | import org.junit.jupiter.api.BeforeEach; |
53 | 56 | import org.junit.jupiter.api.Test; |
@@ -228,4 +231,51 @@ private String getRemoteFS(Node node, String user) throws Exception { |
228 | 231 | return pathObj.toString(); |
229 | 232 | } |
230 | 233 | } |
| 234 | + |
| 235 | + @Test |
| 236 | + void testAgentSecretWithAgentConnectPermission() throws Exception { |
| 237 | + DumbSlave testAgent = j.createOnlineSlave(); |
| 238 | + |
| 239 | + // Setup user with Agent/Connect permission |
| 240 | + String userWithConnect = "user-with-connect"; |
| 241 | + MockAuthorizationStrategy authStrategy = new MockAuthorizationStrategy(); |
| 242 | + authStrategy.grant(Computer.CONNECT, Jenkins.READ).everywhere().to(userWithConnect); |
| 243 | + |
| 244 | + j.jenkins.setSecurityRealm(j.createDummySecurityRealm()); |
| 245 | + j.jenkins.setAuthorizationStrategy(authStrategy); |
| 246 | + |
| 247 | + JenkinsRule.WebClient wc = j.createWebClient(); |
| 248 | + wc.login(userWithConnect); |
| 249 | + |
| 250 | + Page page = wc.goTo("computer/" + testAgent.getNodeName() + "/agent-secret", "text/plain"); |
| 251 | + WebResponse response = page.getWebResponse(); |
| 252 | + |
| 253 | + // Verify response |
| 254 | + assertEquals(200, response.getStatusCode()); |
| 255 | + |
| 256 | + String secret = response.getContentAsString().trim(); |
| 257 | + assertNotNull(secret); |
| 258 | + assertEquals(testAgent.getComputer().getJnlpMac(), secret); |
| 259 | + } |
| 260 | + |
| 261 | + @Test |
| 262 | + void testAgentSecretWithoutAgentConnectPermission() throws Exception { |
| 263 | + DumbSlave testAgent = j.createOnlineSlave(); |
| 264 | + |
| 265 | + // Setup user without Agent/Connect permission |
| 266 | + String userWithoutConnect = "user-without-connect"; |
| 267 | + MockAuthorizationStrategy authStrategy = new MockAuthorizationStrategy(); |
| 268 | + |
| 269 | + j.jenkins.setSecurityRealm(j.createDummySecurityRealm()); |
| 270 | + j.jenkins.setAuthorizationStrategy(authStrategy); |
| 271 | + |
| 272 | + JenkinsRule.WebClient wc = j.createWebClient(); |
| 273 | + // Expect 403 Forbidden |
| 274 | + FailingHttpStatusCodeException e = assertThrows(FailingHttpStatusCodeException.class, () -> { |
| 275 | + wc.goTo("computer/" + testAgent.getNodeName() + "/agent-secret", "text/plain"); |
| 276 | + }); |
| 277 | + |
| 278 | + // Verify it's a 403 Forbidden |
| 279 | + assertEquals(403, e.getStatusCode()); |
| 280 | + } |
231 | 281 | } |
0 commit comments