1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.security.authorization

File AbstractAuthorizationTestCase.java

 

Coverage histogram

../../../../img/srcFileCovDistChart4.png
80% of files have more coverage

Code metrics

56
69
12
1
300
178
50
0.72
5.75
12
4.17

Classes

Class Line # Actions
AbstractAuthorizationTestCase 68 69 0% 50 86
0.3722627837.2%
 

Contributing tests

This file is covered by 14 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Abstract class that should be inherited when writing tests case based on XML based mocked wikis for testing
61    * authorization.
62    *
63    * @version $Id: 7cd007d945881d23206e40f7a0ef3f7149659186 $
64    * @since 5.0M2
65    */
66    @ComponentList({DefaultStringEntityReferenceResolver.class, DefaultStringEntityReferenceSerializer.class,
67    DefaultEntityReferenceProvider.class, DefaultModelConfiguration.class})
 
68    public abstract class AbstractAuthorizationTestCase
69    {
70    /** SuperAdmin user. */
71    protected static final DocumentReference SUPERADMIN = new DocumentReference("anyWiki", "anySpace", "SuperAdmin");
72   
73    /** VIEW, EDIT, COMMENT, DELETE, REGISTER, LOGIN, SCRIPT, ADMIN, PROGRAM, CREATE_WIKI. */
74    protected static final RightSet ALL_RIGHTS = new RightSet();
75   
76    /** VIEW, EDIT, COMMENT, DELETE, REGISTER, LOGIN, SCRIPT, ADMIN, CREATE_WIKI. */
77    protected static final RightSet ALL_RIGHTS_EXCEPT_PROGRAMING = new RightSet();
78   
79    /** VIEW, EDIT, COMMENT, DELETE, REGISTER, LOGIN, SCRIPT, ADMIN. */
80    protected static final RightSet ALL_RIGHTS_EXCEPT_PROGRAMING_AND_CREATE_WIKI = new RightSet();
81   
82    /** VIEW, EDIT, COMMENT, DELETE, REGISTER, LOGIN, SCRIPT. */
83    protected static final RightSet ALL_RIGHTS_EXCEPT_ADMIN_AND_CREATE_WIKI = new RightSet();
84   
85    /** VIEW, EDIT, COMMENT, DELETE, SCRIPT, ADMIN. */
86    protected static final RightSet ALL_SPACE_RIGHTS = new RightSet();
87   
88    /** VIEW, EDIT, COMMENT, REGISTER, LOGIN. */
89    protected static final RightSet DEFAULT_DOCUMENT_RIGHTS = new RightSet();
90   
91    /** VIEW, EDIT, COMMENT, DELETE, SCRIPT. */
92    protected static final RightSet ALL_DOCUMENT_RIGHTS = new RightSet();
93   
 
94  1 toggle 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    /** Current wiki mock. */
126    protected TestDefinition testDefinition;
127   
128    /**
129    * Initialize the test by loading wikis form XML.
130    * @param filename the filename without extension
131    * @return the test wikis, also available using {@link #testDefinition}
132    * @throws Exception on error.
133    */
 
134  14 toggle 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 name user name.
149    * @return a reference to a user in the main wiki.
150    */
 
151  104 toggle protected DocumentReference getXUser(String name)
152    {
153  104 return new DocumentReference(name, getXSpace(XWikiConstants.XWIKI_SPACE));
154    }
155   
156    /**
157    * @param name user name.
158    * @param wiki subwiki name.
159    * @return a reference to a user in a sub wiki.
160    */
 
161  38 toggle 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 a reference to the main wiki.
169    */
 
170  0 toggle protected WikiReference getXWiki()
171    {
172  0 return testDefinition.getMainWiki().getWikiReference();
173    }
174   
175    /**
176    * @param name subwiki name.
177    * @return a reference to a subwiki.
178    */
 
179  16 toggle protected WikiReference getWiki(String name)
180    {
181  16 return new WikiReference(name);
182    }
183   
184    /**
185    * @param space space name.
186    * @return a reference to a space in the main wiki.
187    */
 
188  104 toggle protected SpaceReference getXSpace(String space)
189    {
190  104 return new SpaceReference(space, testDefinition.getMainWiki().getWikiReference());
191    }
192   
193    /**
194    * @param space space name.
195    * @param wiki subwiki name.
196    * @return a reference to a space in a subwiki.
197    */
 
198  0 toggle protected SpaceReference getSpace(String space, String wiki)
199    {
200  0 return new SpaceReference(space, new WikiReference(wiki));
201    }
202   
203    /**
204    * @param name document name.
205    * @param space space name.
206    * @return a reference to a document in a space of the main wiki.
207    */
 
208  39 toggle 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 name document name.
215    * @param space space name.
216    * @param wiki subwiki name.
217    * @return a reference to a document in a space of a subwiki.
218    */
 
219  67 toggle 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 user user reference.
226    * @return a pretty name for the user based on entities "alt" attributes or their names.
227    */
 
228  0 toggle 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 entity user reference.
250    * @return a pretty name for the user based on entities "alt" attributes or their names.
251    */
 
252  0 toggle 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    }