|
17 | 17 | * CDDL HEADER END
|
18 | 18 | */
|
19 | 19 |
|
20 |
| -/* |
| 20 | + /* |
21 | 21 | * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
|
22 | 22 | */
|
23 | 23 | package org.opensolaris.opengrok.web;
|
|
26 | 26 | import java.io.FileNotFoundException;
|
27 | 27 | import java.io.IOException;
|
28 | 28 | import java.nio.file.Files;
|
| 29 | +import java.util.ArrayList; |
29 | 30 | import java.util.Arrays;
|
| 31 | +import java.util.List; |
| 32 | +import java.util.Map; |
30 | 33 | import javax.servlet.http.HttpServletRequest;
|
31 | 34 | import org.junit.AfterClass;
|
32 | 35 | import org.junit.BeforeClass;
|
33 | 36 | import org.junit.Rule;
|
34 | 37 | import org.junit.Test;
|
| 38 | +import org.opensolaris.opengrok.authorization.AuthControlFlag; |
| 39 | +import org.opensolaris.opengrok.authorization.AuthorizationFramework; |
| 40 | +import org.opensolaris.opengrok.authorization.AuthorizationPlugin; |
| 41 | +import org.opensolaris.opengrok.authorization.TestPlugin; |
35 | 42 | import org.opensolaris.opengrok.condition.ConditionalRun;
|
36 | 43 | import org.opensolaris.opengrok.condition.ConditionalRunRule;
|
37 | 44 | import org.opensolaris.opengrok.condition.RepositoryInstalled;
|
| 45 | +import org.opensolaris.opengrok.configuration.Project; |
38 | 46 | import org.opensolaris.opengrok.configuration.RuntimeEnvironment;
|
39 | 47 | import org.opensolaris.opengrok.history.Annotation;
|
40 | 48 | import org.opensolaris.opengrok.history.HistoryGuru;
|
@@ -135,6 +143,73 @@ public void canProcessXref() {
|
135 | 143 | assertCanProcess(null, "/source", "/xref", "/mercurial/xyz/");
|
136 | 144 | }
|
137 | 145 |
|
| 146 | + /** |
| 147 | + * Testing the root of /xref for authorization filtering. |
| 148 | + */ |
| 149 | + @Test |
| 150 | + public void testGetResourceFileList() { |
| 151 | + RuntimeEnvironment env = RuntimeEnvironment.getInstance(); |
| 152 | + |
| 153 | + // backup original values |
| 154 | + String oldSourceRootPath = env.getSourceRootPath(); |
| 155 | + AuthorizationFramework oldAuthorizationFramework = env.getAuthorizationFramework(); |
| 156 | + Map<String, Project> oldProjects = env.getProjects(); |
| 157 | + |
| 158 | + // set up the source root directory containing some projects |
| 159 | + env.setSourceRoot(repository.getSourceRoot()); |
| 160 | + |
| 161 | + // enable projects |
| 162 | + for (String file : new File(repository.getSourceRoot()).list()) { |
| 163 | + env.getProjects().put(file, new Project(file)); |
| 164 | + } |
| 165 | + |
| 166 | + HttpServletRequest req = createRequest("/source", "/xref", ""); |
| 167 | + PageConfig cfg = PageConfig.get(req); |
| 168 | + List<String> allFiles = new ArrayList<>(cfg.getResourceFileList()); |
| 169 | + |
| 170 | + /** |
| 171 | + * Check if there are some files (the "5" here is just a sufficient |
| 172 | + * value for now which won't break any future repository tests) without |
| 173 | + * any authorization. |
| 174 | + */ |
| 175 | + assertTrue(allFiles.size() > 5); |
| 176 | + assertTrue(allFiles.contains("git")); |
| 177 | + assertTrue(allFiles.contains("mercurial")); |
| 178 | + |
| 179 | + /** |
| 180 | + * Now set up the same projects with authorization plugin enabling only |
| 181 | + * some of them. |
| 182 | + * <pre> |
| 183 | + * - disabling "git" |
| 184 | + * - disabling "mercurial" |
| 185 | + * </pre> |
| 186 | + */ |
| 187 | + env.setAuthorizationFramework(new AuthorizationFramework(null)); |
| 188 | + env.getAuthorizationFramework().getStack() |
| 189 | + .add(new AuthorizationPlugin(AuthControlFlag.REQUIRED, new TestPlugin() { |
| 190 | + @Override |
| 191 | + public boolean isAllowed(HttpServletRequest request, Project project) { |
| 192 | + return !project.getName().startsWith("git") |
| 193 | + && !project.getName().startsWith("mercurial"); |
| 194 | + } |
| 195 | + })); |
| 196 | + |
| 197 | + req = createRequest("/source", "/xref", ""); |
| 198 | + cfg = PageConfig.get(req); |
| 199 | + List<String> filteredFiles = new ArrayList<>(cfg.getResourceFileList()); |
| 200 | + // list subtraction - retains only disabled files |
| 201 | + allFiles.removeAll(filteredFiles); |
| 202 | + |
| 203 | + assertEquals(2, allFiles.size()); |
| 204 | + assertTrue(allFiles.contains("git")); |
| 205 | + assertTrue(allFiles.contains("mercurial")); |
| 206 | + |
| 207 | + // restore original values |
| 208 | + env.setAuthorizationFramework(oldAuthorizationFramework); |
| 209 | + env.setSourceRoot(oldSourceRootPath); |
| 210 | + env.setProjects(oldProjects); |
| 211 | + } |
| 212 | + |
138 | 213 | @Test
|
139 | 214 | public void testGetIntParam() {
|
140 | 215 | String[] attrs = {"a", "b", "c", "d", "e", "f", "g", "h"};
|
@@ -193,7 +268,6 @@ public String getParameter(String name) {
|
193 | 268 | }
|
194 | 269 | }
|
195 | 270 |
|
196 |
| - |
197 | 271 | @Test
|
198 | 272 | @ConditionalRun(condition = RepositoryInstalled.GitInstalled.class)
|
199 | 273 | public void testGetAnnotation() {
|
|
0 commit comments