1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.vfs.internal

File CascadingVfsPermissionCheckerTest.java

 

Code metrics

0
23
4
1
115
74
6
0.26
5.75
4
1.5

Classes

Class Line # Actions
CascadingVfsPermissionCheckerTest 50 23 0% 6 2
0.925925992.6%
 

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