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

File XWikiRightServiceTest.java

 

Code metrics

0
92
10
1
295
174
10
0.11
9.2
10
1

Classes

Class Line # Actions
XWikiRightServiceTest 38 92 0% 10 0
1.0100%
 

Contributing tests

This file is covered by 10 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    package org.xwiki.security.authorization.internal;
21   
22    import org.junit.Assert;
23    import org.junit.Test;
24    import org.xwiki.model.reference.DocumentReference;
25    import org.xwiki.security.authorization.AbstractLegacyWikiTestCase;
26    import org.xwiki.security.authorization.AuthorizationManager;
27    import org.xwiki.security.authorization.testwikibuilding.LegacyTestWiki;
28    import org.xwiki.security.internal.XWikiConstants;
29   
30    import com.xpn.xwiki.XWikiContext;
31    import com.xpn.xwiki.doc.XWikiDocument;
32   
33    /**
34    * Unit tests for {@link com.xpn.xwiki.user.impl.xwiki.XWikiRightServiceImpl}.
35    *
36    * @version $Id: fde1b1a71a9f67d38670aca7c00ddb1b81df3ddf $
37    */
 
38    public class XWikiRightServiceTest extends AbstractLegacyWikiTestCase
39    {
 
40  1 toggle @Test
41    public void testUserFromAnotherWiki1() throws Exception
42    {
43  1 LegacyTestWiki testWiki = newTestWiki("userFromAnotherWiki1.xml", true);
44   
45  1 XWikiContext ctx = testWiki.getXWikiContext();
46  1 ctx.setWikiId("wiki");
47   
48  1 assertAccessLevelTrue("User from another wiki has right on a local wiki", "view", "wiki:XWiki.user",
49    "wiki2:Space.Page", ctx);
50   
51  1 assertAccessLevelTrue("User from another wiki has right on a local wiki", "view", "XWiki.user",
52    "wiki2:Space.Page", ctx);
53    }
54   
 
55  1 toggle @Test
56    public void testUserFromAnotherWiki2() throws Exception
57    {
58  1 LegacyTestWiki testWiki = newTestWiki("userFromAnotherWiki2.xml", true);
59   
60  1 XWikiContext ctx = testWiki.getXWikiContext();
61   
62  1 ctx.setWikiId("wiki");
63   
64  1 assertAccessLevelTrue("User from another wiki does not have right on a local wiki when tested from user wiki",
65    "view", "wiki:XWiki.user", "wiki2:Space.Page", ctx);
66   
67  1 assertAccessLevelTrue("User from another wiki does not have right on a local wiki when tested from user wiki",
68    "view", "XWiki.user", "wiki2:Space.Page", ctx);
69   
70  1 ctx.setWikiId("wiki2");
71   
72  1 assertAccessLevelTrue("User from another wiki does not have right on a local wiki when tested from local wiki",
73    "view", "wiki:XWiki.user", "wiki2:Space.Page", ctx);
74   
75  1 assertAccessLevelTrue("User from another wiki does not have right on a local wiki when tested from local wiki",
76    "view", "wiki:XWiki.user", "Space.Page", ctx);
77   
78    }
79   
 
80  1 toggle @Test
81    public void testGroupFromAnotherWiki1() throws Exception
82    {
83  1 LegacyTestWiki testWiki = newTestWiki("groupFromAnotherWiki1.xml", true);
84   
85  1 XWikiContext ctx = testWiki.getXWikiContext();
86   
87  1 ctx.setWikiId("wiki");
88   
89  1 assertAccessLevelTrue(
90    "User group from another wiki does not have right on a local wiki when tested from user wiki", "view",
91    "wiki:XWiki.user", "wiki2:Space.Page", ctx);
92   
93  1 assertAccessLevelTrue(
94    "User group from another wiki does not have right on a local wiki when tested from user wiki", "view",
95    "XWiki.user", "wiki2:Space.Page", ctx);
96   
97  1 ctx.setWikiId("wiki2");
98   
99  1 assertAccessLevelTrue("User from another wiki does not have right on a local wiki when tested from local wiki",
100    "view", "wiki:XWiki.user", "wiki2:Space.Page", ctx);
101   
102  1 assertAccessLevelTrue("User from another wiki does not have right on a local wiki when tested from local wiki",
103    "view", "wiki:XWiki.user", "Space.Page", ctx);
104   
105    }
106   
 
107  1 toggle @Test
108    public void testWikiOwnerFromAnotherWiki() throws Exception
109    {
110  1 LegacyTestWiki testWiki = newTestWiki("userFromAnotherWiki2.xml", true);
111   
112  1 XWikiContext ctx = testWiki.getXWikiContext();
113   
114  1 ctx.setWikiId("wiki");
115   
116  1 assertAccessLevelTrue(
117    "Wiki owner from another wiki does not have right on a local wiki when tested from user wiki", "view",
118    "wiki:XWiki.user", "wiki2:Space.Page", ctx);
119   
120  1 assertAccessLevelTrue(
121    "Wiki owner from another wiki does not have right on a local wiki when tested from user wiki", "view",
122    "XWiki.user", "wiki2:Space.Page", ctx);
123   
124  1 ctx.setWikiId("wiki2");
125   
126  1 assertAccessLevelTrue(
127    "Wiki owner from another wiki does not have right on a local wiki when tested from local wiki", "view",
128    "wiki:XWiki.user", "wiki2:Space.Page", ctx);
129   
130  1 assertAccessLevelTrue(
131    "Wiki owner from another wiki does not have right on a local wiki when tested from local wiki", "view",
132    "wiki:XWiki.user", "Space.Page", ctx);
133   
134    }
135   
136    /**
137    * Test that programming rights are checked on the context user when no context document is set.
138    *
139    * @throws com.xpn.xwiki.XWikiException on error
140    */
 
141  1 toggle @Test
142    public void testProgrammingRightsWhenNoContextDocumentIsSet() throws Exception
143    {
144  1 LegacyTestWiki testWiki = newTestWiki("programmingRights.xml", true);
145   
146  1 XWikiContext ctx = testWiki.getXWikiContext();
147   
148  1 ctx.setWikiId("wiki");
149   
150  1 setContext(ctx);
151   
152  1 testWiki.setSdoc((String) null);
153   
154    // XWiki.Programmer should have PR, as per the global rights.
155  1 testWiki.setUser("XWiki.programmer");
156  1 Assert.assertTrue(getLegacyImpl().hasProgrammingRights(ctx));
157  1 Assert.assertTrue(getCachingImpl().hasProgrammingRights(ctx));
158   
159    // Guests should not have PR
160  1 testWiki.setUser(XWikiConstants.GUEST_USER_FULLNAME);
161  1 Assert.assertFalse(getLegacyImpl().hasProgrammingRights(ctx));
162  1 Assert.assertFalse(getCachingImpl().hasProgrammingRights(ctx));
163   
164    // superadmin should always have PR
165  1 testWiki.setUser(XWikiConstants.XWIKI_SPACE + '.' + AuthorizationManager.SUPERADMIN_USER);
166  1 Assert.assertTrue(getLegacyImpl().hasProgrammingRights(ctx));
167  1 Assert.assertTrue(getCachingImpl().hasProgrammingRights(ctx));
168   
169    }
170   
 
171  1 toggle @Test
172    public void testProgrammingRightsWhenCustomSecureDocIsSet() throws Exception
173    {
174  1 LegacyTestWiki testWiki = newTestWiki("programmingRights.xml", true);
175   
176  1 XWikiContext ctx = testWiki.getXWikiContext();
177   
178  1 ctx.setWikiId("wiki");
179   
180  1 setContext(ctx);
181   
182  1 XWikiDocument sdoc = new XWikiDocument(new DocumentReference("wiki", "space", "page"));
183  1 sdoc.setContentAuthorReference(new DocumentReference(ctx.getMainXWiki(), "XWiki", "superadmin"));
184  1 testWiki.setSdoc(sdoc);
185   
186    // XWiki.Programmer should have PR, as per the global rights.
187  1 sdoc.setContentAuthorReference(new DocumentReference(ctx.getMainXWiki(), "XWiki", "programmer"));
188  1 Assert.assertTrue(getLegacyImpl().hasProgrammingRights(ctx));
189  1 Assert.assertTrue(getCachingImpl().hasProgrammingRights(ctx));
190   
191    // Guests should not have PR
192  1 sdoc.setContentAuthorReference(null);
193  1 Assert.assertFalse(getLegacyImpl().hasProgrammingRights(ctx));
194  1 Assert.assertFalse(getCachingImpl().hasProgrammingRights(ctx));
195   
196    // superadmin should always have PR
197  1 sdoc.setContentAuthorReference(new DocumentReference(ctx.getMainXWiki(), "XWiki", "superadmin"));
198  1 Assert.assertTrue(getLegacyImpl().hasProgrammingRights(ctx));
199  1 Assert.assertTrue(getCachingImpl().hasProgrammingRights(ctx));
200   
201    }
202   
 
203  1 toggle @Test
204    public void testGuestRightsOnEmptyWiki() throws Exception
205    {
206  1 LegacyTestWiki testWiki = newTestWiki("empty.xml", true);
207   
208  1 XWikiContext ctx = testWiki.getXWikiContext();
209   
210  1 ctx.setWikiId("wiki");
211   
212  1 assertAccessLevelTrue("Guest does not have view right on empty wiki.", "view", "wiki:XWiki.XWikiGuest",
213    "wiki:Space.Page", ctx);
214   
215  1 assertAccessLevelTrue("Guest does not have edit right on empty wiki.", "edit", "wiki:XWiki.XWikiGuest",
216    "wiki:Space.Page", ctx);
217   
218  1 assertAccessLevelFalseExpectedDifference("Guest should not have delete right on empty wiki.", "delete",
219    "wiki:XWiki.XWikiGuest", "wiki:Space.Page", ctx);
220   
221  1 assertAccessLevelFalseExpectedDifference("Guest should not have admin right on empty wiki.", "admin",
222    "wiki:XWiki.XWikiGuest", "wiki:Space.Page", ctx);
223   
224  1 assertAccessLevelFalse("Guest should not have programming right on empty wiki.", "programming",
225    "wiki:XWiki.XWikiGuest", "wiki:Space.Page", ctx);
226   
227  1 testWiki.setUser("wiki:XWiki.XWikiGuest");
228   
229  1 assertWikiAdminRightsFalseExpectedDifference("Guest should not have admin right on empty wiki.", ctx);
230   
231  1 testWiki.setDoc("wiki:Space.Page");
232   
233  1 assertAdminRightsFalseExpectedDifference("Guest should not have admin right on empty wiki.", ctx);
234    }
235   
 
236  1 toggle @Test
237    public void testGlobalUserInLocalGroup() throws Exception
238    {
239  1 LegacyTestWiki testWiki = newTestWiki("glocalUserInLocalGroup.xml", true);
240   
241  1 XWikiContext ctx = testWiki.getXWikiContext();
242   
243  1 ctx.setWikiId("wiki2");
244   
245  1 assertAccessLevelTrue("User from global wiki should have right on local wiki through local group", "view",
246    "wiki:XWiki.user", "wiki2:Space.Page", ctx);
247    }
248   
 
249  1 toggle @Test
250    public void testGlobalGroupInLocalGroup() throws Exception
251    {
252  1 LegacyTestWiki testWiki = newTestWiki("glocalGroupInLocalGroup.xml", true);
253   
254  1 XWikiContext ctx = testWiki.getXWikiContext();
255   
256  1 ctx.setWikiId("wiki2");
257   
258  1 assertAccessLevelTrue("Users from global wiki should have right on local wiki through local group", "view",
259    "wiki:XWiki.user", "wiki2:Space.Page", ctx);
260   
261  1 assertAccessLevelTrue("Groups from global wiki should have right on local wiki through local group", "view",
262    "wiki:XWiki.group", "wiki2:Space.Page", ctx);
263    }
264   
 
265  1 toggle @Test
266    public void testRelativeDocumentReference() throws Exception
267    {
268  1 LegacyTestWiki testWiki = newTestWiki("denieddocument.xml", true);
269   
270  1 XWikiContext ctx = testWiki.getXWikiContext();
271  1 ctx.setWikiId("wiki");
272  1 testWiki.setDoc("wiki:Space.Page");
273   
274    // view
275   
276  1 assertAccessLevelFalse("User has right on the denied document", "view", "wiki:XWiki.user", "wiki:Space.Page",
277    ctx);
278   
279  1 assertAccessLevelFalse("User has right on the denied document", "view", "wiki:XWiki.user", "Space.Page", ctx);
280   
281  1 assertAccessLevelFalse("User has right on the denied document", "view", "wiki:XWiki.user", "Page", ctx);
282   
283  1 assertAccessLevelTrue("User does not have right on the document space", "view", "wiki:XWiki.user", "", ctx);
284   
285    // edit
286   
287  1 assertAccessLevelFalse("User has right on the denied document", "edit", "XWiki.user", "wiki:Space.Page", ctx);
288   
289  1 assertAccessLevelFalse("User has right on the denied document", "edit", "XWiki.user", "Space.Page", ctx);
290   
291  1 assertAccessLevelFalse("User has right on the denied document", "edit", "XWiki.user", "Page", ctx);
292   
293  1 assertAccessLevelTrue("User does not have right on the document space", "edit", "XWiki.user", "", ctx);
294    }
295    }