| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
@link |
| 42 |
|
|
| 43 |
|
@version |
| 44 |
|
@since |
| 45 |
|
|
| 46 |
|
@ComponentList({ |
| 47 |
|
DefaultVfsPermissionChecker.class, |
| 48 |
|
AttachVfsPermissionChecker.class |
| 49 |
|
}) |
| |
|
| 92.6% |
Uncovered Elements: 2 (27) |
Complexity: 6 |
Complexity Density: 0.26 |
|
| 50 |
|
public class CascadingVfsPermissionCheckerTest |
| 51 |
|
{ |
| 52 |
|
@Rule |
| 53 |
|
public MockitoComponentMockingRule<CascadingVfsPermissionChecker> mocker = |
| 54 |
|
new MockitoComponentMockingRule<>(CascadingVfsPermissionChecker.class); |
| 55 |
|
|
| |
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
1PASS
|
|
| 56 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
| 69 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 86 |
1 |
@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 |
|
|
| |
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
1PASS
|
|
| 98 |
1 |
@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 |
|
} |