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

File CascadingVfsPermissionCheckerTest.java

 

Code metrics

0
28
5
1
130
85
7
0.25
5.6
5
1.4

Classes

Class Line # Actions
CascadingVfsPermissionCheckerTest 55 28 0% 7 2
0.9393939493.9%
 

Contributing tests

This file is covered by 4 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.vfs.internal;
21   
22    import java.net.URI;
23   
24    import javax.inject.Provider;
25   
26    import org.junit.Rule;
27    import org.junit.Test;
28    import org.xwiki.model.reference.AttachmentReference;
29    import org.xwiki.model.reference.AttachmentReferenceResolver;
30    import org.xwiki.model.reference.DocumentReference;
31    import org.xwiki.security.authorization.AuthorizationManager;
32    import org.xwiki.security.authorization.ContextualAuthorizationManager;
33    import org.xwiki.security.authorization.Right;
34    import org.xwiki.test.annotation.BeforeComponent;
35    import org.xwiki.test.annotation.ComponentList;
36    import org.xwiki.test.mockito.MockitoComponentMockingRule;
37    import org.xwiki.vfs.VfsException;
38    import org.xwiki.vfs.VfsResourceReference;
39    import org.xwiki.vfs.internal.attach.AttachVfsPermissionChecker;
40   
41    import com.xpn.xwiki.XWikiContext;
42   
43    import static org.junit.Assert.assertEquals;
44    import static org.junit.Assert.fail;
45    import static org.mockito.Mockito.mock;
46    import static org.mockito.Mockito.when;
47   
48    /**
49    * Unit tests for {@link CascadingVfsPermissionChecker}.
50    *
51    * @version $Id: a0e3998b5e90cec1c710ea56cb2e1d5aa60357dd $
52    * @since 7.4M2
53    */
54    @ComponentList({ DefaultVfsPermissionChecker.class, AttachVfsPermissionChecker.class })
 
55    public class CascadingVfsPermissionCheckerTest
56    {
57    @Rule
58    public MockitoComponentMockingRule<CascadingVfsPermissionChecker> mocker =
59    new MockitoComponentMockingRule<>(CascadingVfsPermissionChecker.class);
60   
61    private DocumentReference contextUser = new DocumentReference("wiki", "space", "page");
62   
 
63  4 toggle @BeforeComponent
64    public void beforeComponent() throws Exception
65    {
66  4 Provider<XWikiContext> provider = this.mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
67  4 XWikiContext xcontext = mock(XWikiContext.class);
68  4 when(provider.get()).thenReturn(xcontext);
69  4 when(xcontext.getUserReference()).thenReturn(contextUser);
70    }
71   
 
72  1 toggle @Test
73    public void checkPermissionWhenReservedScheme() throws Exception
74    {
75  1 VfsResourceReference reference = new VfsResourceReference(URI.create("cascading:whatever"), "whatever");
76   
77  1 try {
78  1 this.mocker.getComponentUnderTest().checkPermission(reference);
79  0 fail("Should have raised exception");
80    } catch (VfsException expected) {
81  1 assertEquals("[cascading] is a reserved VFS URI scheme and cannot be used.", expected.getMessage());
82    }
83    }
84   
 
85  1 toggle @Test
86    public void checkPermissionWithAttachSchemeChecker() throws Exception
87    {
88  1 VfsResourceReference reference = new VfsResourceReference(URI.create("attach:whatever"), "whatever");
89   
90  1 AttachmentReferenceResolver<String> resolver =
91    this.mocker.registerMockComponent(AttachmentReferenceResolver.TYPE_STRING);
92  1 DocumentReference attachmentDocumentReference = new DocumentReference("wiki", "space", "page");
93  1 AttachmentReference attachmentReference = new AttachmentReference("file", attachmentDocumentReference);
94  1 when(resolver.resolve("whatever")).thenReturn(attachmentReference);
95   
96  1 ContextualAuthorizationManager authorizationManager =
97    this.mocker.registerMockComponent(ContextualAuthorizationManager.class);
98  1 when(authorizationManager.hasAccess(Right.VIEW, attachmentReference)).thenReturn(true);
99   
100  1 this.mocker.getComponentUnderTest().checkPermission(reference);
101    }
102   
 
103  1 toggle @Test
104    public void checkPermissionWhenNoSpecificSchemeCheckerAndAllowed() throws Exception
105    {
106  1 VfsResourceReference reference = new VfsResourceReference(URI.create("customscheme:whatever"), "whatever");
107   
108  1 AuthorizationManager authorizationManager = this.mocker.registerMockComponent(AuthorizationManager.class);
109  1 when(authorizationManager.hasAccess(Right.PROGRAM, this.contextUser, null)).thenReturn(true);
110   
111  1 this.mocker.getComponentUnderTest().checkPermission(reference);
112    }
113   
 
114  1 toggle @Test
115    public void checkPermissionWhenNoSpecificSchemeCheckerAndNotAllowed() throws Exception
116    {
117  1 VfsResourceReference reference = new VfsResourceReference(URI.create("customscheme:whatever"), "whatever");
118   
119  1 AuthorizationManager authorizationManager = this.mocker.registerMockComponent(AuthorizationManager.class);
120  1 when(authorizationManager.hasAccess(Right.PROGRAM, this.contextUser, null)).thenReturn(false);
121   
122  1 try {
123  1 this.mocker.getComponentUnderTest().checkPermission(reference);
124  0 fail("Should have raised exception");
125    } catch (VfsException expected) {
126  1 assertEquals("Current logged-in user ([" + this.contextUser
127    + "]) needs to have Programming Rights to use the [customscheme] VFS", expected.getMessage());
128    }
129    }
130    }