| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
package org.xwiki.security.authorization.cache.internal; |
| 22 |
|
|
| 23 |
|
import java.util.Arrays; |
| 24 |
|
import java.util.Collections; |
| 25 |
|
import java.util.Deque; |
| 26 |
|
import java.util.LinkedList; |
| 27 |
|
import java.util.Set; |
| 28 |
|
|
| 29 |
|
import org.junit.Assert; |
| 30 |
|
import org.junit.Before; |
| 31 |
|
import org.junit.Rule; |
| 32 |
|
import org.junit.Test; |
| 33 |
|
import org.xwiki.model.reference.DocumentReference; |
| 34 |
|
import org.xwiki.model.reference.WikiReference; |
| 35 |
|
import org.xwiki.security.DefaultSecurityReferenceFactory; |
| 36 |
|
import org.xwiki.security.GroupSecurityReference; |
| 37 |
|
import org.xwiki.security.SecurityReference; |
| 38 |
|
import org.xwiki.security.SecurityReferenceFactory; |
| 39 |
|
import org.xwiki.security.UserSecurityReference; |
| 40 |
|
import org.xwiki.security.authorization.AuthorizationException; |
| 41 |
|
import org.xwiki.security.authorization.AuthorizationSettler; |
| 42 |
|
import org.xwiki.security.authorization.SecurityAccessEntry; |
| 43 |
|
import org.xwiki.security.authorization.SecurityEntryReader; |
| 44 |
|
import org.xwiki.security.authorization.SecurityRuleEntry; |
| 45 |
|
import org.xwiki.security.authorization.cache.ConflictingInsertionException; |
| 46 |
|
import org.xwiki.security.authorization.cache.SecurityCacheLoader; |
| 47 |
|
import org.xwiki.security.authorization.cache.SecurityCacheRulesInvalidator; |
| 48 |
|
import org.xwiki.security.internal.UserBridge; |
| 49 |
|
import org.xwiki.security.internal.XWikiBridge; |
| 50 |
|
import org.xwiki.test.LogRule; |
| 51 |
|
import org.xwiki.test.annotation.ComponentList; |
| 52 |
|
import org.xwiki.test.mockito.MockitoComponentManagerRule; |
| 53 |
|
|
| 54 |
|
import static org.mockito.Mockito.doThrow; |
| 55 |
|
import static org.mockito.Mockito.mock; |
| 56 |
|
import static org.mockito.Mockito.when; |
| 57 |
|
|
| 58 |
|
|
| 59 |
|
@link |
| 60 |
|
|
| 61 |
|
@version |
| 62 |
|
|
| 63 |
|
@ComponentList({DefaultSecurityCacheLoader.class, DefaultSecurityReferenceFactory.class}) |
| |
|
| 97.7% |
Uncovered Elements: 1 (43) |
Complexity: 3 |
Complexity Density: 0.07 |
|
| 64 |
|
public class DefaultSecurityCacheLoaderTest |
| 65 |
|
{ |
| 66 |
|
@Rule |
| 67 |
|
public MockitoComponentManagerRule mocker = new MockitoComponentManagerRule(); |
| 68 |
|
|
| 69 |
|
@Rule |
| 70 |
|
public LogRule logCapture = new LogRule(); |
| 71 |
|
|
| 72 |
|
private SecurityCacheLoader securityCacheLoader; |
| 73 |
|
|
| 74 |
|
private SecurityReferenceFactory securityReferenceFactory; |
| 75 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
| 76 |
1 |
@Before... |
| 77 |
|
public void setUp() throws Exception |
| 78 |
|
{ |
| 79 |
1 |
XWikiBridge bridge = mocker.registerMockComponent(XWikiBridge.class); |
| 80 |
1 |
when(bridge.getMainWikiReference()).thenReturn(new WikiReference("wiki")); |
| 81 |
1 |
securityReferenceFactory = mocker.getInstance(SecurityReferenceFactory.class); |
| 82 |
|
|
| 83 |
1 |
mocker.registerMockComponent(SecurityCache.class); |
| 84 |
1 |
mocker.registerMockComponent(SecurityCacheRulesInvalidator.class); |
| 85 |
1 |
mocker.registerMockComponent(SecurityEntryReader.class); |
| 86 |
1 |
mocker.registerMockComponent(UserBridge.class); |
| 87 |
1 |
mocker.registerMockComponent(AuthorizationSettler.class); |
| 88 |
1 |
securityCacheLoader = mocker.getInstance(SecurityCacheLoader.class); |
| 89 |
|
} |
| 90 |
|
|
| |
|
| 96.9% |
Uncovered Elements: 1 (32) |
Complexity: 2 |
Complexity Density: 0.06 |
1PASS
|
|
| 91 |
1 |
@Test... |
| 92 |
|
public void loadWithConflictingInsertionException() throws Exception |
| 93 |
|
{ |
| 94 |
1 |
DocumentReference userReference = new DocumentReference("wiki", "Users", "mflorea"); |
| 95 |
1 |
UserSecurityReference user = securityReferenceFactory.newUserReference(userReference); |
| 96 |
|
|
| 97 |
1 |
DocumentReference documentReference = new DocumentReference("wiki", "Space", "Document"); |
| 98 |
1 |
SecurityReference entity = securityReferenceFactory.newEntityReference(documentReference); |
| 99 |
|
|
| 100 |
1 |
SecurityRuleEntry documentEntry = mock(SecurityRuleEntry.class, "document"); |
| 101 |
1 |
when(documentEntry.getReference()).thenReturn(entity); |
| 102 |
1 |
when(documentEntry.isEmpty()).thenReturn(true); |
| 103 |
|
|
| 104 |
1 |
SecurityRuleEntry spaceEntry = mock(SecurityRuleEntry.class, "space"); |
| 105 |
1 |
when(spaceEntry.getReference()).thenReturn(entity.getParentSecurityReference()); |
| 106 |
1 |
when(spaceEntry.isEmpty()).thenReturn(true); |
| 107 |
|
|
| 108 |
1 |
SecurityRuleEntry wikiEntry = mock(SecurityRuleEntry.class, "wiki"); |
| 109 |
1 |
when(wikiEntry.getReference()).thenReturn(entity.getParentSecurityReference().getParentSecurityReference()); |
| 110 |
1 |
when(wikiEntry.isEmpty()).thenReturn(true); |
| 111 |
|
|
| 112 |
1 |
SecurityCache securityCache = mocker.getInstance(SecurityCache.class); |
| 113 |
1 |
when(securityCache.get(entity)).thenReturn(documentEntry); |
| 114 |
1 |
when(securityCache.get(entity.getParentSecurityReference())).thenReturn(spaceEntry); |
| 115 |
1 |
when(securityCache.get(entity.getParentSecurityReference().getParentSecurityReference())).thenReturn(wikiEntry); |
| 116 |
1 |
when(securityCache.getGroupsFor(user, null)).thenReturn(null); |
| 117 |
|
|
| 118 |
1 |
UserBridge userBridge = mocker.getInstance(UserBridge.class); |
| 119 |
1 |
DocumentReference groupReference = new DocumentReference("wiki", "Groups", "AllGroup"); |
| 120 |
1 |
Set<GroupSecurityReference> groups = |
| 121 |
|
Collections.singleton(securityReferenceFactory.newGroupReference(groupReference)); |
| 122 |
1 |
when(userBridge.getAllGroupsFor(user, userReference.getWikiReference())).thenReturn(groups); |
| 123 |
|
|
| 124 |
1 |
SecurityAccessEntry securityAccessEntry = mock(SecurityAccessEntry.class); |
| 125 |
|
|
| 126 |
1 |
AuthorizationSettler authorizationSettler = mocker.getInstance(AuthorizationSettler.class); |
| 127 |
1 |
Deque<SecurityRuleEntry> securityRuleEntries = |
| 128 |
|
new LinkedList<SecurityRuleEntry>(Arrays.asList(documentEntry, spaceEntry, wikiEntry)); |
| 129 |
1 |
when(authorizationSettler.settle(user, groups, securityRuleEntries)).thenReturn(securityAccessEntry); |
| 130 |
|
|
| 131 |
1 |
doThrow(ConflictingInsertionException.class).when(securityCache).add(securityAccessEntry); |
| 132 |
1 |
doThrow(ConflictingInsertionException.class).when(securityCache).add(securityAccessEntry, null); |
| 133 |
|
|
| 134 |
1 |
try { |
| 135 |
1 |
securityCacheLoader.load(user, entity); |
| 136 |
0 |
Assert.fail(); |
| 137 |
|
} catch (AuthorizationException e) { |
| 138 |
1 |
Assert.assertEquals("Failed to load the cache in 5 attempts. Giving up. when checking " |
| 139 |
|
+ "access to [wiki:Space.Document] for user [wiki:Users.mflorea]", e.getMessage()); |
| 140 |
|
} |
| 141 |
|
} |
| 142 |
|
} |