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

File DefaultSecurityCacheLoaderTest.java

 

Code metrics

0
46
3
1
162
115
4
0.09
15.33
3
1.33

Classes

Class Line # Actions
DefaultSecurityCacheLoaderTest 71 46 0% 4 1
0.9795918598%
 

Contributing tests

This file is covered by 1 test. .

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   
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.apache.commons.lang3.exception.ExceptionUtils;
30    import org.junit.Before;
31    import org.junit.Rule;
32    import org.junit.Test;
33    import org.mockito.invocation.InvocationOnMock;
34    import org.mockito.stubbing.Answer;
35    import org.xwiki.model.reference.DocumentReference;
36    import org.xwiki.model.reference.EntityReference;
37    import org.xwiki.model.reference.WikiReference;
38    import org.xwiki.security.DefaultSecurityReferenceFactory;
39    import org.xwiki.security.GroupSecurityReference;
40    import org.xwiki.security.SecurityReference;
41    import org.xwiki.security.SecurityReferenceFactory;
42    import org.xwiki.security.UserSecurityReference;
43    import org.xwiki.security.authorization.AuthorizationException;
44    import org.xwiki.security.authorization.AuthorizationSettler;
45    import org.xwiki.security.authorization.SecurityAccessEntry;
46    import org.xwiki.security.authorization.SecurityEntryReader;
47    import org.xwiki.security.authorization.SecurityRuleEntry;
48    import org.xwiki.security.authorization.cache.ConflictingInsertionException;
49    import org.xwiki.security.authorization.cache.SecurityCacheLoader;
50    import org.xwiki.security.authorization.cache.SecurityCacheRulesInvalidator;
51    import org.xwiki.security.internal.UserBridge;
52    import org.xwiki.security.internal.XWikiBridge;
53    import org.xwiki.test.AllLogRule;
54    import org.xwiki.test.annotation.ComponentList;
55    import org.xwiki.test.mockito.MockitoComponentManagerRule;
56   
57    import static org.junit.Assert.assertEquals;
58    import static org.junit.Assert.assertTrue;
59    import static org.junit.Assert.fail;
60    import static org.mockito.ArgumentMatchers.any;
61    import static org.mockito.Mockito.doThrow;
62    import static org.mockito.Mockito.mock;
63    import static org.mockito.Mockito.when;
64   
65    /**
66    * Unit tests for {@link DefaultSecurityCacheLoader}.
67    *
68    * @version $Id: ac457cc13aba3e1fc2de3d63e6398f8e38cc8eb2 $
69    */
70    @ComponentList({ DefaultSecurityCacheLoader.class, DefaultSecurityReferenceFactory.class })
 
71    public class DefaultSecurityCacheLoaderTest
72    {
73    @Rule
74    public MockitoComponentManagerRule mocker = new MockitoComponentManagerRule();
75   
76    @Rule
77    public AllLogRule logRule = new AllLogRule();
78   
79    private SecurityCacheLoader securityCacheLoader;
80   
81    private SecurityReferenceFactory securityReferenceFactory;
82   
 
83  1 toggle @Before
84    public void setUp() throws Exception
85    {
86  1 XWikiBridge bridge = mocker.registerMockComponent(XWikiBridge.class);
87  1 when(bridge.getMainWikiReference()).thenReturn(new WikiReference("wiki"));
88  1 when(bridge.toCompatibleEntityReference(any(EntityReference.class))).thenAnswer(new Answer<EntityReference>()
89    {
 
90  1 toggle @Override
91    public EntityReference answer(InvocationOnMock invocation) throws Throwable
92    {
93  1 return invocation.getArgument(0);
94    }
95    });
96  1 securityReferenceFactory = mocker.getInstance(SecurityReferenceFactory.class);
97   
98  1 mocker.registerMockComponent(SecurityCache.class);
99  1 mocker.registerMockComponent(SecurityCacheRulesInvalidator.class);
100  1 mocker.registerMockComponent(SecurityEntryReader.class);
101  1 mocker.registerMockComponent(UserBridge.class);
102  1 mocker.registerMockComponent(AuthorizationSettler.class);
103  1 securityCacheLoader = mocker.getInstance(SecurityCacheLoader.class);
104    }
105   
 
106  1 toggle @Test
107    public void loadWithConflictingInsertionException() throws Exception
108    {
109  1 DocumentReference userReference = new DocumentReference("wiki", "Users", "mflorea");
110  1 UserSecurityReference user = securityReferenceFactory.newUserReference(userReference);
111   
112  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Document");
113  1 SecurityReference entity = securityReferenceFactory.newEntityReference(documentReference);
114   
115  1 SecurityRuleEntry documentEntry = mock(SecurityRuleEntry.class, "document");
116  1 when(documentEntry.getReference()).thenReturn(entity);
117  1 when(documentEntry.isEmpty()).thenReturn(true);
118   
119  1 SecurityRuleEntry spaceEntry = mock(SecurityRuleEntry.class, "space");
120  1 when(spaceEntry.getReference()).thenReturn(entity.getParentSecurityReference());
121  1 when(spaceEntry.isEmpty()).thenReturn(true);
122   
123  1 SecurityRuleEntry wikiEntry = mock(SecurityRuleEntry.class, "wiki");
124  1 when(wikiEntry.getReference()).thenReturn(entity.getParentSecurityReference().getParentSecurityReference());
125  1 when(wikiEntry.isEmpty()).thenReturn(true);
126   
127  1 SecurityCache securityCache = mocker.getInstance(SecurityCache.class);
128  1 when(securityCache.get(entity)).thenReturn(documentEntry);
129  1 when(securityCache.get(entity.getParentSecurityReference())).thenReturn(spaceEntry);
130  1 when(securityCache.get(entity.getParentSecurityReference().getParentSecurityReference())).thenReturn(wikiEntry);
131  1 when(securityCache.getGroupsFor(user, null)).thenReturn(null);
132   
133  1 UserBridge userBridge = mocker.getInstance(UserBridge.class);
134  1 DocumentReference groupReference = new DocumentReference("wiki", "Groups", "AllGroup");
135  1 Set<GroupSecurityReference> groups =
136    Collections.singleton(securityReferenceFactory.newGroupReference(groupReference));
137  1 when(userBridge.getAllGroupsFor(user, userReference.getWikiReference())).thenReturn(groups);
138   
139  1 SecurityAccessEntry securityAccessEntry = mock(SecurityAccessEntry.class);
140   
141  1 AuthorizationSettler authorizationSettler = mocker.getInstance(AuthorizationSettler.class);
142  1 Deque<SecurityRuleEntry> securityRuleEntries =
143    new LinkedList<SecurityRuleEntry>(Arrays.asList(documentEntry, spaceEntry, wikiEntry));
144  1 when(authorizationSettler.settle(user, groups, securityRuleEntries)).thenReturn(securityAccessEntry);
145   
146  1 doThrow(ConflictingInsertionException.class).when(securityCache).add(securityAccessEntry);
147  1 doThrow(ConflictingInsertionException.class).when(securityCache).add(securityAccessEntry, null);
148   
149  1 try {
150  1 securityCacheLoader.load(user, entity);
151  0 fail();
152    } catch (AuthorizationException e) {
153  1 assertEquals("Failed to load the cache in 5 attempts. Giving up. when checking "
154    + "access to [wiki:Space.Document] for user [wiki:Users.mflorea]", e.getMessage());
155  1 assertTrue(ExceptionUtils.getRootCauseMessage(e).contains("ConflictingInsertionException"));
156    }
157   
158    // Assert that we've also emitted a log
159  1 assertEquals(1, this.logRule.size());
160  1 assertEquals("Failed to load the cache in 5 attempts. Giving up.", this.logRule.getMessage(0));
161    }
162    }