1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package org.xwiki.security.authorization; |
22 |
|
|
23 |
|
import java.io.File; |
24 |
|
|
25 |
|
import org.junit.Rule; |
26 |
|
import org.xwiki.model.EntityType; |
27 |
|
import org.xwiki.model.internal.DefaultModelConfiguration; |
28 |
|
import org.xwiki.model.internal.reference.DefaultEntityReferenceProvider; |
29 |
|
import org.xwiki.model.internal.reference.DefaultStringEntityReferenceResolver; |
30 |
|
import org.xwiki.model.internal.reference.DefaultStringEntityReferenceSerializer; |
31 |
|
import org.xwiki.model.reference.DocumentReference; |
32 |
|
import org.xwiki.model.reference.EntityReference; |
33 |
|
import org.xwiki.model.reference.EntityReferenceResolver; |
34 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
35 |
|
import org.xwiki.model.reference.SpaceReference; |
36 |
|
import org.xwiki.model.reference.WikiReference; |
37 |
|
import org.xwiki.security.authorization.testwikis.TestDefinition; |
38 |
|
import org.xwiki.security.authorization.testwikis.TestDefinitionParser; |
39 |
|
import org.xwiki.security.authorization.testwikis.TestDocument; |
40 |
|
import org.xwiki.security.authorization.testwikis.TestEntity; |
41 |
|
import org.xwiki.security.authorization.testwikis.TestSpace; |
42 |
|
import org.xwiki.security.authorization.testwikis.TestWiki; |
43 |
|
import org.xwiki.security.authorization.testwikis.internal.parser.DefaultTestDefinitionParser; |
44 |
|
import org.xwiki.security.authorization.testwikis.internal.parser.XWikiConstants; |
45 |
|
import org.xwiki.test.annotation.ComponentList; |
46 |
|
import org.xwiki.test.mockito.MockitoComponentManagerRule; |
47 |
|
|
48 |
|
import static org.xwiki.security.authorization.Right.ADMIN; |
49 |
|
import static org.xwiki.security.authorization.Right.CREATE_WIKI; |
50 |
|
import static org.xwiki.security.authorization.Right.CREATOR; |
51 |
|
import static org.xwiki.security.authorization.Right.DELETE; |
52 |
|
import static org.xwiki.security.authorization.Right.ILLEGAL; |
53 |
|
import static org.xwiki.security.authorization.Right.LOGIN; |
54 |
|
import static org.xwiki.security.authorization.Right.PROGRAM; |
55 |
|
import static org.xwiki.security.authorization.Right.REGISTER; |
56 |
|
import static org.xwiki.security.authorization.Right.SCRIPT; |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
@version |
64 |
|
@since |
65 |
|
|
66 |
|
@ComponentList({DefaultStringEntityReferenceResolver.class, DefaultStringEntityReferenceSerializer.class, |
67 |
|
DefaultEntityReferenceProvider.class, DefaultModelConfiguration.class}) |
|
|
| 37.2% |
Uncovered Elements: 86 (137) |
Complexity: 50 |
Complexity Density: 0.72 |
|
68 |
|
public abstract class AbstractAuthorizationTestCase |
69 |
|
{ |
70 |
|
|
71 |
|
protected static final DocumentReference SUPERADMIN = new DocumentReference("anyWiki", "anySpace", "SuperAdmin"); |
72 |
|
|
73 |
|
|
74 |
|
protected static final RightSet ALL_RIGHTS = new RightSet(); |
75 |
|
|
76 |
|
|
77 |
|
protected static final RightSet ALL_RIGHTS_EXCEPT_PROGRAMING = new RightSet(); |
78 |
|
|
79 |
|
|
80 |
|
protected static final RightSet ALL_RIGHTS_EXCEPT_PROGRAMING_AND_CREATE_WIKI = new RightSet(); |
81 |
|
|
82 |
|
|
83 |
|
protected static final RightSet ALL_RIGHTS_EXCEPT_ADMIN_AND_CREATE_WIKI = new RightSet(); |
84 |
|
|
85 |
|
|
86 |
|
protected static final RightSet ALL_SPACE_RIGHTS = new RightSet(); |
87 |
|
|
88 |
|
|
89 |
|
protected static final RightSet DEFAULT_DOCUMENT_RIGHTS = new RightSet(); |
90 |
|
|
91 |
|
|
92 |
|
protected static final RightSet ALL_DOCUMENT_RIGHTS = new RightSet(); |
93 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (32) |
Complexity: 14 |
Complexity Density: 0.88 |
|
94 |
1 |
static {... |
95 |
1 |
for(Right right : Right.values()) { |
96 |
12 |
if (right != ILLEGAL && right != CREATOR) { |
97 |
10 |
ALL_RIGHTS.add(right); |
98 |
10 |
if (right != PROGRAM) { |
99 |
9 |
ALL_RIGHTS_EXCEPT_PROGRAMING.add(right); |
100 |
9 |
if (right != CREATE_WIKI) { |
101 |
8 |
ALL_RIGHTS_EXCEPT_PROGRAMING_AND_CREATE_WIKI.add(right); |
102 |
|
} |
103 |
9 |
if (right != ADMIN) { |
104 |
8 |
if (right != CREATE_WIKI){ |
105 |
7 |
ALL_RIGHTS_EXCEPT_ADMIN_AND_CREATE_WIKI.add(right); |
106 |
7 |
if (right != LOGIN && right != REGISTER) { |
107 |
5 |
ALL_DOCUMENT_RIGHTS.add(right); |
108 |
|
} |
109 |
7 |
if (right != DELETE && right != SCRIPT) { |
110 |
5 |
DEFAULT_DOCUMENT_RIGHTS.add(right); |
111 |
|
} |
112 |
|
} |
113 |
|
} |
114 |
9 |
if (right != LOGIN && right != REGISTER && right != CREATE_WIKI) { |
115 |
6 |
ALL_SPACE_RIGHTS.add(right); |
116 |
|
} |
117 |
|
} |
118 |
|
} |
119 |
|
} |
120 |
|
} |
121 |
|
|
122 |
|
@Rule |
123 |
|
public final MockitoComponentManagerRule componentManager = new MockitoComponentManagerRule(); |
124 |
|
|
125 |
|
|
126 |
|
protected TestDefinition testDefinition; |
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
@param |
131 |
|
@return@link |
132 |
|
@throws |
133 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
134 |
14 |
protected TestDefinition initialiseWikiMock(String filename) throws Exception... |
135 |
|
{ |
136 |
14 |
TestDefinitionParser parser = new DefaultTestDefinitionParser(); |
137 |
|
|
138 |
14 |
EntityReferenceResolver<String> resolver = |
139 |
|
componentManager.getInstance(EntityReferenceResolver.TYPE_STRING); |
140 |
14 |
EntityReferenceSerializer<String> serializer = |
141 |
|
componentManager.getInstance(EntityReferenceSerializer.TYPE_STRING); |
142 |
|
|
143 |
14 |
testDefinition = parser.parse("testwikis" + File.separator + filename + ".xml", resolver, serializer); |
144 |
14 |
return testDefinition; |
145 |
|
} |
146 |
|
|
147 |
|
|
148 |
|
@param |
149 |
|
@return |
150 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
151 |
104 |
protected DocumentReference getXUser(String name)... |
152 |
|
{ |
153 |
104 |
return new DocumentReference(name, getXSpace(XWikiConstants.XWIKI_SPACE)); |
154 |
|
} |
155 |
|
|
156 |
|
|
157 |
|
@param |
158 |
|
@param |
159 |
|
@return |
160 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
161 |
38 |
protected DocumentReference getUser(String name, String wiki)... |
162 |
|
{ |
163 |
38 |
return new DocumentReference(name, |
164 |
|
new SpaceReference(XWikiConstants.XWIKI_SPACE, testDefinition.getWiki(wiki).getWikiReference())); |
165 |
|
} |
166 |
|
|
167 |
|
|
168 |
|
@return |
169 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
170 |
0 |
protected WikiReference getXWiki()... |
171 |
|
{ |
172 |
0 |
return testDefinition.getMainWiki().getWikiReference(); |
173 |
|
} |
174 |
|
|
175 |
|
|
176 |
|
@param |
177 |
|
@return |
178 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
179 |
16 |
protected WikiReference getWiki(String name)... |
180 |
|
{ |
181 |
16 |
return new WikiReference(name); |
182 |
|
} |
183 |
|
|
184 |
|
|
185 |
|
@param |
186 |
|
@return |
187 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
188 |
104 |
protected SpaceReference getXSpace(String space)... |
189 |
|
{ |
190 |
104 |
return new SpaceReference(space, testDefinition.getMainWiki().getWikiReference()); |
191 |
|
} |
192 |
|
|
193 |
|
|
194 |
|
@param |
195 |
|
@param |
196 |
|
@return |
197 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
198 |
0 |
protected SpaceReference getSpace(String space, String wiki)... |
199 |
|
{ |
200 |
0 |
return new SpaceReference(space, new WikiReference(wiki)); |
201 |
|
} |
202 |
|
|
203 |
|
|
204 |
|
@param |
205 |
|
@param |
206 |
|
@return |
207 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
208 |
39 |
protected DocumentReference getXDoc(String name, String space)... |
209 |
|
{ |
210 |
39 |
return new DocumentReference(name, new SpaceReference(space, testDefinition.getMainWiki().getWikiReference())); |
211 |
|
} |
212 |
|
|
213 |
|
|
214 |
|
@param |
215 |
|
@param |
216 |
|
@param |
217 |
|
@return |
218 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
219 |
67 |
protected DocumentReference getDoc(String name, String space, String wiki)... |
220 |
|
{ |
221 |
67 |
return new DocumentReference(name, new SpaceReference(space, new WikiReference(wiki))); |
222 |
|
} |
223 |
|
|
224 |
|
|
225 |
|
@param |
226 |
|
@return |
227 |
|
|
|
|
| 0% |
Uncovered Elements: 23 (23) |
Complexity: 8 |
Complexity Density: 0.73 |
|
228 |
0 |
protected String getUserReadableName(DocumentReference user) {... |
229 |
0 |
if (user == null) { |
230 |
0 |
return "Public"; |
231 |
|
} |
232 |
|
|
233 |
0 |
TestEntity userEntity = testDefinition.searchEntity(user); |
234 |
0 |
String result = (userEntity != null && userEntity instanceof TestDocument) |
235 |
|
? ((TestDocument) userEntity).getDescription() : null; |
236 |
0 |
result = (result != null) ? result : user.getName(); |
237 |
|
|
238 |
0 |
TestWiki wiki = testDefinition.getWiki(user.getWikiReference()); |
239 |
0 |
String name = (wiki != null) ? wiki.getDescription() : null; |
240 |
0 |
name = (name != null) ? name : user.getWikiReference().getName(); |
241 |
0 |
if (!name.startsWith("any")) { |
242 |
0 |
result += " from " + name; |
243 |
|
} |
244 |
|
|
245 |
0 |
return result; |
246 |
|
} |
247 |
|
|
248 |
|
|
249 |
|
@param |
250 |
|
@return |
251 |
|
|
|
|
| 0% |
Uncovered Elements: 57 (57) |
Complexity: 19 |
Complexity Density: 0.66 |
|
252 |
0 |
protected String getEntityReadableName(EntityReference entity) {... |
253 |
0 |
if (entity == null) { |
254 |
0 |
return "Main Wiki"; |
255 |
|
} |
256 |
|
|
257 |
0 |
StringBuilder result = null; |
258 |
|
|
259 |
0 |
if (entity.getType() == EntityType.DOCUMENT) { |
260 |
0 |
TestEntity docEntity = testDefinition.searchEntity(entity); |
261 |
0 |
String name = (docEntity != null && docEntity instanceof TestDocument) |
262 |
|
? ((TestDocument)docEntity).getDescription() : null; |
263 |
0 |
name = (name != null) ? name : entity.getName(); |
264 |
0 |
result = new StringBuilder(name); |
265 |
0 |
entity = entity.getParent(); |
266 |
|
} |
267 |
|
|
268 |
0 |
if (entity.getType() == EntityType.SPACE) { |
269 |
0 |
TestEntity spaceEntity = testDefinition.searchEntity(entity); |
270 |
0 |
String name = (spaceEntity != null && spaceEntity instanceof TestSpace) |
271 |
|
? ((TestSpace) spaceEntity).getDescription() : null; |
272 |
0 |
name = (name != null) ? name : entity.getName(); |
273 |
0 |
if (result == null || !name.startsWith("any")) { |
274 |
0 |
if (result != null) { |
275 |
0 |
result.append(" in "); |
276 |
0 |
result.append(name); |
277 |
|
} else { |
278 |
0 |
result = new StringBuilder(name); |
279 |
|
} |
280 |
|
} |
281 |
0 |
entity = entity.getParent(); |
282 |
|
} |
283 |
|
|
284 |
0 |
if (entity.getType() == EntityType.WIKI) { |
285 |
0 |
TestEntity wikiEntity = testDefinition.getWiki(new WikiReference(entity)); |
286 |
0 |
String name = (wikiEntity != null) ? ((TestWiki) wikiEntity).getDescription() : null; |
287 |
0 |
name = (name != null) ? name : entity.getName(); |
288 |
0 |
if (result == null || !name.startsWith("any")) { |
289 |
0 |
if (result != null) { |
290 |
0 |
result.append(" from "); |
291 |
0 |
result.append(name); |
292 |
|
} else { |
293 |
0 |
result = new StringBuilder(name); |
294 |
|
} |
295 |
|
} |
296 |
|
} |
297 |
|
|
298 |
0 |
return result.toString(); |
299 |
|
} |
300 |
|
} |