|
| 1 | +package org.jenkinsci.plugins.rolestrategy; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.Matchers.is; |
| 5 | +import static org.hamcrest.Matchers.oneOf; |
| 6 | + |
| 7 | +import java.net.URL; |
| 8 | +import java.util.Collections; |
| 9 | + |
| 10 | +import org.junit.Rule; |
| 11 | +import org.junit.Test; |
| 12 | +import org.jvnet.hudson.test.JenkinsRule.WebClient; |
| 13 | + |
| 14 | +import com.gargoylesoftware.htmlunit.HttpMethod; |
| 15 | +import com.gargoylesoftware.htmlunit.WebRequest; |
| 16 | +import com.gargoylesoftware.htmlunit.WebResponse; |
| 17 | +import com.gargoylesoftware.htmlunit.html.HtmlPage; |
| 18 | +import com.gargoylesoftware.htmlunit.util.NameValuePair; |
| 19 | +import com.synopsys.arc.jenkins.plugins.ownership.OwnershipDescription; |
| 20 | +import com.synopsys.arc.jenkins.plugins.ownership.nodes.OwnerNodeProperty; |
| 21 | + |
| 22 | +import hudson.model.Node; |
| 23 | +import io.jenkins.plugins.casc.misc.ConfiguredWithCode; |
| 24 | +import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule; |
| 25 | + |
| 26 | +public class OwnershipTest |
| 27 | +{ |
| 28 | + @Rule |
| 29 | + public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule(); |
| 30 | + |
| 31 | + @Test |
| 32 | + @ConfiguredWithCode("OwnershipTest.yml") |
| 33 | + public void currentUserIsPrimaryOwnerGrantsPermissions() throws Exception { |
| 34 | + Node n = j.createOnlineSlave(); |
| 35 | + n.getNodeProperties().add(new OwnerNodeProperty(n, new OwnershipDescription(true, "nodePrimaryTester", null))); |
| 36 | + String nodeUrl = n.toComputer().getUrl(); |
| 37 | + |
| 38 | + WebClient wc = j.createWebClient(); |
| 39 | + wc.withThrowExceptionOnFailingStatusCode(false); |
| 40 | + wc.login("nodePrimaryTester", "nodePrimaryTester"); |
| 41 | + HtmlPage managePage = wc.withThrowExceptionOnFailingStatusCode(false).goTo(String.format("%sconfigure", nodeUrl)); |
| 42 | + assertThat(managePage.getWebResponse().getStatusCode(), is(200)); |
| 43 | + URL testUrl = wc.createCrumbedUrl(String.format("%sdisconnect", nodeUrl)); |
| 44 | + WebRequest request = new WebRequest(testUrl, HttpMethod.POST); |
| 45 | + NameValuePair param = new NameValuePair("offlineMessage", "Disconnect for Test"); |
| 46 | + request.setRequestParameters(Collections.singletonList(param)); |
| 47 | + WebResponse response = wc.loadWebResponse(request); |
| 48 | + assertThat(response.getStatusCode(), is(200)); |
| 49 | + |
| 50 | + testUrl = wc.createCrumbedUrl(String.format("%slaunchSlaveAgent", nodeUrl)); |
| 51 | + request = new WebRequest(testUrl, HttpMethod.POST); |
| 52 | + response = wc.loadWebResponse(request); |
| 53 | + assertThat(response.getStatusCode(), is(oneOf(200, 302))); |
| 54 | + |
| 55 | + testUrl = wc.createCrumbedUrl(String.format("%sdoDelete", nodeUrl)); |
| 56 | + request = new WebRequest(testUrl, HttpMethod.POST); |
| 57 | + response = wc.loadWebResponse(request); |
| 58 | + assertThat(response.getStatusCode(), is(200)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + @ConfiguredWithCode("OwnershipTest.yml") |
| 63 | + public void currentUserIsSecondaryOwnerGrantsPermissions() throws Exception { |
| 64 | + Node n = j.createOnlineSlave(); |
| 65 | + n.getNodeProperties().add(new OwnerNodeProperty(n, new OwnershipDescription(true, "nodePrimaryTester", Collections.singleton("nodeSecondaryTester")))); |
| 66 | + String nodeUrl = n.toComputer().getUrl(); |
| 67 | + |
| 68 | + WebClient wc = j.createWebClient(); |
| 69 | + wc.withThrowExceptionOnFailingStatusCode(false); |
| 70 | + wc.login("nodeSecondaryTester", "nodeSecondaryTester"); |
| 71 | + HtmlPage managePage = wc.withThrowExceptionOnFailingStatusCode(false).goTo(String.format("%sconfigure", nodeUrl)); |
| 72 | + assertThat(managePage.getWebResponse().getStatusCode(), is(200)); |
| 73 | + URL testUrl = wc.createCrumbedUrl(String.format("%sdisconnect", nodeUrl)); |
| 74 | + WebRequest request = new WebRequest(testUrl, HttpMethod.POST); |
| 75 | + NameValuePair param = new NameValuePair("offlineMessage", "Disconnect for Test"); |
| 76 | + request.setRequestParameters(Collections.singletonList(param)); |
| 77 | + WebResponse response = wc.loadWebResponse(request); |
| 78 | + assertThat(response.getStatusCode(), is(200)); |
| 79 | + |
| 80 | + testUrl = wc.createCrumbedUrl(String.format("%slaunchSlaveAgent", nodeUrl)); |
| 81 | + request = new WebRequest(testUrl, HttpMethod.POST); |
| 82 | + response = wc.loadWebResponse(request); |
| 83 | + assertThat(response.getStatusCode(), is(oneOf(200, 302))); |
| 84 | + |
| 85 | + testUrl = wc.createCrumbedUrl(String.format("%sdoDelete", nodeUrl)); |
| 86 | + request = new WebRequest(testUrl, HttpMethod.POST); |
| 87 | + response = wc.loadWebResponse(request); |
| 88 | + assertThat(response.getStatusCode(), is(200)); |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments