|
| 1 | +package io.jenkins.plugins.casc.permissions; |
| 2 | + |
| 3 | +import com.gargoylesoftware.htmlunit.html.HtmlPage; |
| 4 | +import com.google.common.collect.ImmutableMap; |
| 5 | +import java.util.Map; |
| 6 | +import jenkins.model.Jenkins; |
| 7 | +import org.junit.Rule; |
| 8 | +import org.junit.Test; |
| 9 | +import org.jvnet.hudson.test.JenkinsRule; |
| 10 | +import org.jvnet.hudson.test.JenkinsRule.WebClient; |
| 11 | +import org.jvnet.hudson.test.MockAuthorizationStrategy; |
| 12 | + |
| 13 | +import static io.jenkins.plugins.casc.permissions.Action.APPLY_NEW_CONFIGURATION; |
| 14 | +import static io.jenkins.plugins.casc.permissions.Action.DOWNLOAD_CONFIGURATION; |
| 15 | +import static io.jenkins.plugins.casc.permissions.Action.RELOAD_EXISTING_CONFIGURATION; |
| 16 | +import static io.jenkins.plugins.casc.permissions.Action.VIEW_CONFIGURATION; |
| 17 | +import static java.lang.String.format; |
| 18 | +import static java.net.HttpURLConnection.HTTP_FORBIDDEN; |
| 19 | +import static java.net.HttpURLConnection.HTTP_OK; |
| 20 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 21 | +import static org.hamcrest.Matchers.containsString; |
| 22 | +import static org.hamcrest.Matchers.is; |
| 23 | +import static org.hamcrest.Matchers.not; |
| 24 | +import static org.junit.Assert.assertEquals; |
| 25 | + |
| 26 | +public class PermissionsTest { |
| 27 | + |
| 28 | + private static final String RELATIVE_PATH_MANAGE_PAGE = "manage"; |
| 29 | + private static final String RELATIVE_PATH_CASC_PAGE = "configuration-as-code"; |
| 30 | + |
| 31 | + @Rule |
| 32 | + public JenkinsRule j = new JenkinsRule(); |
| 33 | + |
| 34 | + @Test |
| 35 | + public void checkPermissionsForReader() throws Exception { |
| 36 | + final String READER = "reader"; |
| 37 | + j.jenkins.setSecurityRealm(j.createDummySecurityRealm()); |
| 38 | + j.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy() |
| 39 | + .grant(Jenkins.READ).everywhere().to(READER) |
| 40 | + ); |
| 41 | + |
| 42 | + JenkinsRule.WebClient webClient = j.createWebClient() |
| 43 | + .withThrowExceptionOnFailingStatusCode(false); |
| 44 | + |
| 45 | + webClient.login(READER); |
| 46 | + assertCannotAccessPage(webClient, RELATIVE_PATH_CASC_PAGE); |
| 47 | + assertCannotAccessPage(webClient, RELATIVE_PATH_MANAGE_PAGE); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void checkPermissionsForSystemReader() throws Exception { |
| 52 | + final String SYSTEM_READER = "systemReader"; |
| 53 | + j.jenkins.setSecurityRealm(j.createDummySecurityRealm()); |
| 54 | + j.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy() |
| 55 | + .grant(Jenkins.READ).everywhere().to(SYSTEM_READER) |
| 56 | + .grant(Jenkins.SYSTEM_READ).everywhere().to(SYSTEM_READER) |
| 57 | + ); |
| 58 | + |
| 59 | + JenkinsRule.WebClient webClient = j.createWebClient() |
| 60 | + .withThrowExceptionOnFailingStatusCode(false); |
| 61 | + |
| 62 | + assertUserPermissions( |
| 63 | + webClient, |
| 64 | + SYSTEM_READER, |
| 65 | + ImmutableMap.<Action, Boolean>builder() |
| 66 | + .put(VIEW_CONFIGURATION, true) |
| 67 | + .put(DOWNLOAD_CONFIGURATION, true) |
| 68 | + .put(APPLY_NEW_CONFIGURATION, false) |
| 69 | + .put(RELOAD_EXISTING_CONFIGURATION, false) |
| 70 | + .build() |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void checkPermissionsForManager() throws Exception { |
| 76 | + final String MANAGER = "manager"; |
| 77 | + j.jenkins.setSecurityRealm(j.createDummySecurityRealm()); |
| 78 | + j.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy() |
| 79 | + .grant(Jenkins.READ).everywhere().to(MANAGER) |
| 80 | + .grant(Jenkins.MANAGE).everywhere().to(MANAGER) |
| 81 | + ); |
| 82 | + |
| 83 | + JenkinsRule.WebClient webClient = j.createWebClient() |
| 84 | + .withThrowExceptionOnFailingStatusCode(false); |
| 85 | + |
| 86 | + assertUserPermissions( |
| 87 | + webClient, |
| 88 | + MANAGER, |
| 89 | + ImmutableMap.<Action, Boolean>builder() |
| 90 | + .put(RELOAD_EXISTING_CONFIGURATION, true) |
| 91 | + .build() |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void checkPermissionsForAdmin() throws Exception { |
| 97 | + final String ADMIN = "admin"; |
| 98 | + j.jenkins.setSecurityRealm(j.createDummySecurityRealm()); |
| 99 | + j.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy() |
| 100 | + .grant(Jenkins.ADMINISTER).everywhere().to(ADMIN) |
| 101 | + ); |
| 102 | + |
| 103 | + JenkinsRule.WebClient webClient = j.createWebClient() |
| 104 | + .withThrowExceptionOnFailingStatusCode(false); |
| 105 | + |
| 106 | + assertUserPermissions( |
| 107 | + webClient, |
| 108 | + ADMIN, |
| 109 | + ImmutableMap.<Action, Boolean>builder() |
| 110 | + .put(VIEW_CONFIGURATION, true) |
| 111 | + .put(DOWNLOAD_CONFIGURATION, true) |
| 112 | + .put(APPLY_NEW_CONFIGURATION, true) |
| 113 | + .put(RELOAD_EXISTING_CONFIGURATION, true) |
| 114 | + .build() |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + private void assertUserPermissions(WebClient webClient, String user, |
| 119 | + Map<Action, Boolean> allowedActions) throws Exception { |
| 120 | + |
| 121 | + webClient.login(user); |
| 122 | + assertCascTileShows(webClient); |
| 123 | + |
| 124 | + HtmlPage cascPage = assertCanAccessPage(webClient, RELATIVE_PATH_CASC_PAGE); |
| 125 | + allowedActions.forEach( |
| 126 | + (action, isAllowed) -> assertActionAvailable(cascPage, action, isAllowed) |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + private HtmlPage assertCanAccessPage(WebClient webClient, String relativePath) |
| 131 | + throws Exception { |
| 132 | + HtmlPage page = webClient.goTo(relativePath); |
| 133 | + assertEquals(HTTP_OK, page.getWebResponse().getStatusCode()); |
| 134 | + return page; |
| 135 | + } |
| 136 | + |
| 137 | + private void assertCannotAccessPage(WebClient webClient, String relativePath) throws Exception { |
| 138 | + final HtmlPage page = webClient.goTo(relativePath); |
| 139 | + final int statusCode = page.getWebResponse().getStatusCode(); |
| 140 | + assertThat( |
| 141 | + format("Page %s should not be accessible", relativePath), |
| 142 | + statusCode, |
| 143 | + is(HTTP_FORBIDDEN) |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + private void assertCascTileShows(WebClient webClient) throws Exception { |
| 148 | + HtmlPage managePage = assertCanAccessPage(webClient, RELATIVE_PATH_MANAGE_PAGE); |
| 149 | + final String pageContent = managePage.getWebResponse().getContentAsString(); |
| 150 | + assertThat( |
| 151 | + "The user should have access to the CasC tile in management page", |
| 152 | + pageContent, |
| 153 | + containsString("Configuration as Code") |
| 154 | + ); |
| 155 | + } |
| 156 | + |
| 157 | + private void assertActionAvailable(HtmlPage page, Action action, boolean shouldContain) { |
| 158 | + String responseContent = page.getWebResponse().getContentAsString(); |
| 159 | + if (shouldContain) { |
| 160 | + assertThat( |
| 161 | + format("Action %s should be available", action.name()), |
| 162 | + responseContent, |
| 163 | + containsString(action.buttonText) |
| 164 | + ); |
| 165 | + } else { |
| 166 | + assertThat( |
| 167 | + format("Action %s should not be available", action.name()), |
| 168 | + responseContent, |
| 169 | + not(containsString(action.buttonText)) |
| 170 | + ); |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments