1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.search.solr.script

File SolrIndexScriptServiceTest.java

 

Code metrics

0
82
11
1
310
174
11
0.13
7.45
11
1

Classes

Class Line # Actions
SolrIndexScriptServiceTest 56 82 0% 11 0
1.0100%
 

Contributing tests

This file is covered by 10 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.search.solr.script;
21   
22    import java.util.Arrays;
23   
24    import javax.inject.Provider;
25   
26    import org.apache.solr.common.SolrDocument;
27    import org.junit.Before;
28    import org.junit.Rule;
29    import org.junit.Test;
30    import org.slf4j.Logger;
31    import org.xwiki.model.EntityType;
32    import org.xwiki.model.reference.DocumentReference;
33    import org.xwiki.model.reference.EntityReference;
34    import org.xwiki.model.reference.EntityReferenceResolver;
35    import org.xwiki.model.reference.SpaceReference;
36    import org.xwiki.model.reference.WikiReference;
37    import org.xwiki.search.solr.internal.api.FieldUtils;
38    import org.xwiki.search.solr.internal.reference.SolrEntityReferenceResolver;
39    import org.xwiki.security.authorization.AuthorizationManager;
40    import org.xwiki.security.authorization.Right;
41    import org.xwiki.test.mockito.MockitoComponentMockingRule;
42   
43    import com.xpn.xwiki.XWiki;
44    import com.xpn.xwiki.XWikiContext;
45    import com.xpn.xwiki.doc.XWikiDocument;
46   
47    import static org.junit.Assert.*;
48    import static org.mockito.ArgumentMatchers.*;
49    import static org.mockito.Mockito.*;
50   
51    /**
52    * Tests for the {@link org.xwiki.search.solr.script.SolrIndexScriptService}.
53    *
54    * @version $Id: 949b619d93901d1aed661282127b6e5933905003 $
55    */
 
56    public class SolrIndexScriptServiceTest
57    {
58    @Rule
59    public final MockitoComponentMockingRule<SolrIndexScriptService> mocker =
60    new MockitoComponentMockingRule<SolrIndexScriptService>(SolrIndexScriptService.class);
61   
62    private XWikiContext mockContext;
63   
64    private XWiki mockXWiki;
65   
66    private XWikiDocument mockCurrentDocument;
67   
68    private DocumentReference userReference;
69   
70    private DocumentReference contentAuthorReference;
71   
72    private SolrIndexScriptService service;
73   
74    private Logger logger;
75   
76    private AuthorizationManager mockAuthorization;
77   
 
78  10 toggle @Before
79    public void setUp() throws Exception
80    {
81  10 this.userReference = new DocumentReference("wiki", "space", "user");
82   
83    // Context
84  10 this.mockContext = mock(XWikiContext.class);
85  10 Provider<XWikiContext> xcontextProvider = this.mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
86  10 when(xcontextProvider.get()).thenReturn(this.mockContext);
87   
88    // XWiki
89  10 this.mockXWiki = mock(XWiki.class);
90  10 when(mockContext.getWiki()).thenReturn(this.mockXWiki);
91   
92  10 this.mockCurrentDocument = mock(XWikiDocument.class);
93  10 when(mockContext.getDoc()).thenReturn(this.mockCurrentDocument);
94   
95  10 when(mockContext.getWikiId()).thenReturn("currentWiki");
96  10 when(mockContext.getUserReference()).thenReturn(userReference);
97   
98    // RightService
99  10 this.mockAuthorization = this.mocker.getInstance(AuthorizationManager.class);
100    // By default, we have the rights.
101  10 when(mockAuthorization.hasAccess(any(), any(), any())).thenReturn(true);
102   
103  10 this.service = mocker.getComponentUnderTest();
104   
105    // Rights check success. By default we are allowed (no error is thrown)
106  10 this.logger = mocker.getMockedLogger();
107  10 verify(this.logger, never()).error(any(), any(IllegalAccessException.class));
108    }
109   
 
110  1 toggle @Test
111    public void indexSingleReferenceChecksRights() throws Exception
112    {
113  1 EntityReference wikiReference = new WikiReference("someWiki");
114   
115    // Call
116  1 this.service.index(wikiReference);
117   
118    // Assert and verify
119   
120    // Actual rights check
121  1 verify(this.mockAuthorization).hasAccess(Right.ADMIN, this.userReference, wikiReference);
122  1 verify(this.mockAuthorization).hasAccess(Right.PROGRAM, this.contentAuthorReference, wikiReference);
123    }
124   
 
125  1 toggle @Test
126    public void indexMultipleReferencesChecksRights() throws Exception
127    {
128  1 EntityReference wikiReference = new WikiReference("someWiki");
129   
130    // Call
131   
132    // Note: Faking it. just using one reference but still calling the multiple references method (which is what we
133    // wanted anyway)
134  1 this.service.index(Arrays.asList(wikiReference));
135   
136    // Assert and verify
137   
138    // Actual rights check
139  1 verify(this.mockAuthorization).hasAccess(Right.ADMIN, this.userReference, wikiReference);
140  1 verify(this.mockAuthorization).hasAccess(Right.PROGRAM, this.contentAuthorReference, wikiReference);
141    }
142   
 
143  1 toggle @Test
144    public void deleteSingleReferenceChecksRights() throws Exception
145    {
146  1 EntityReference wikiReference = new WikiReference("someWiki");
147   
148    // Call
149  1 this.service.delete(wikiReference);
150   
151    // Assert and verify
152   
153    // Actual rights check
154  1 verify(this.mockAuthorization).hasAccess(Right.ADMIN, this.userReference, wikiReference);
155  1 verify(this.mockAuthorization).hasAccess(Right.PROGRAM, this.contentAuthorReference, wikiReference);
156    }
157   
 
158  1 toggle @Test
159    public void deleteMultipleReferencesChecksRights() throws Exception
160    {
161  1 EntityReference wikiReference = new WikiReference("someWiki");
162   
163    // Call
164   
165    // Note: Faking it. just using one reference but still calling the multiple references method (which is what we
166    // wanted anyway)
167  1 this.service.delete(Arrays.asList(wikiReference));
168   
169    // Assert and verify
170   
171    // Actual rights check
172  1 verify(this.mockAuthorization).hasAccess(Right.ADMIN, this.userReference, wikiReference);
173  1 verify(this.mockAuthorization).hasAccess(Right.PROGRAM, this.contentAuthorReference, wikiReference);
174    }
175   
 
176  1 toggle @Test
177    public void operationsChecksRightsWithOtherReferences() throws Exception
178    {
179  1 EntityReference documentReference = new DocumentReference("someWiki", "space", "document");
180   
181    // Call
182  1 this.service.index(documentReference);
183   
184    // Assert and verify
185   
186    // Actual rights check
187  1 EntityReference wikiReference = documentReference.extractReference(EntityType.WIKI);
188  1 verify(this.mockAuthorization).hasAccess(Right.ADMIN, this.userReference, wikiReference);
189  1 verify(this.mockAuthorization).hasAccess(Right.PROGRAM, this.contentAuthorReference, wikiReference);
190    }
191   
 
192  1 toggle @Test
193    public void hasWikiAdminButNoProgrammingCausesRightsCheckFailure() throws Exception
194    {
195  1 EntityReference wikiReference = new WikiReference("someWiki");
196   
197    // Mock
198  1 when(this.mockAuthorization.hasAccess(Right.PROGRAM, this.contentAuthorReference, wikiReference)).thenReturn(
199    false);
200   
201    // Call
202  1 this.service.index(wikiReference);
203   
204    // Assert and verify
205   
206    // Actual rights check
207  1 verify(this.mockAuthorization).hasAccess(Right.ADMIN, this.userReference, wikiReference);
208  1 verify(this.mockAuthorization).hasAccess(Right.PROGRAM, this.contentAuthorReference, wikiReference);
209   
210    // Rights check failure
211  1 String errorMessage =
212    String.format("The user '%s' is not allowed to alter the index for the entity '%s'", userReference,
213    wikiReference);
214  1 verify(this.logger).error(eq(errorMessage), any(IllegalAccessException.class));
215  1 verify(this.mockContext).put(eq(SolrIndexScriptService.CONTEXT_LASTEXCEPTION),
216    any(IllegalAccessException.class));
217    }
218   
 
219  1 toggle @Test
220    public void hasProgrammingButNoWikiAdminCausesRightsCheckFailure() throws Exception
221    {
222  1 EntityReference wikiReference = new WikiReference("someWiki");
223   
224    // Mock
225  1 when(this.mockAuthorization.hasAccess(Right.ADMIN, this.userReference, wikiReference)).thenReturn(false);
226   
227    // Call
228  1 this.service.index(wikiReference);
229   
230    // Assert and verify
231   
232    // Actual rights check
233  1 verify(this.mockAuthorization).hasAccess(Right.ADMIN, this.userReference, wikiReference);
234    // hasProgrammingRights does not really get to be called, since hasWikiAdminRights already failed at this point
235  1 verify(this.mockAuthorization, times(0)).hasAccess(Right.PROGRAM, this.contentAuthorReference, wikiReference);
236   
237    // Rights check failure.
238  1 String errorMessage =
239    String.format("The user '%s' is not allowed to alter the index for the entity '%s'", userReference,
240    wikiReference);
241  1 verify(this.logger).error(eq(errorMessage), any(IllegalAccessException.class));
242  1 verify(this.mockContext).put(eq(SolrIndexScriptService.CONTEXT_LASTEXCEPTION),
243    any(IllegalAccessException.class));
244    }
245   
 
246  1 toggle @Test
247    public void openrationsOnMultipleReferencesOnTheSameWikiChecksRightsOnlyOnceForThatWiki() throws Exception
248    {
249    // References from the same wiki
250  1 WikiReference wikiReference = new WikiReference("wiki");
251  1 SpaceReference spaceReference = new SpaceReference("space", wikiReference);
252  1 DocumentReference documentReference = new DocumentReference("wiki", "space", "name");
253  1 DocumentReference documentReference2 = new DocumentReference("wiki", "space", "name2");
254   
255    // Call
256  1 this.service.index(Arrays.asList(wikiReference, spaceReference, documentReference, documentReference2));
257   
258    // Assert and verify
259   
260    // Actual rights check
261  1 verify(this.mockAuthorization).hasAccess(Right.ADMIN, this.userReference, wikiReference);
262  1 verify(this.mockAuthorization).hasAccess(Right.PROGRAM, this.contentAuthorReference, wikiReference);
263    }
264   
 
265  1 toggle @Test
266    public void openrationsOnMultipleReferencesOnDifferentWikisChecksRightsOnEachWiki() throws Exception
267    {
268    // References from 3 different wikis
269  1 WikiReference wikiReference1 = new WikiReference("wiki");
270  1 SpaceReference spaceReference = new SpaceReference("space", wikiReference1);
271  1 WikiReference wikiReference2 = new WikiReference("wiki2");
272  1 DocumentReference documentReference = new DocumentReference("wiki2", "space", "name");
273  1 WikiReference wikiReference3 = new WikiReference("wiki3");
274  1 DocumentReference documentReference2 = new DocumentReference("wiki3", "space", "name2");
275   
276    // Call
277  1 this.service.index(Arrays.asList(wikiReference1, spaceReference, wikiReference2, documentReference,
278    documentReference2));
279   
280    // Assert and verify
281   
282    // Actual rights check, once for each wiki.
283  1 verify(this.mockAuthorization).hasAccess(Right.ADMIN, this.userReference, wikiReference1);
284  1 verify(this.mockAuthorization).hasAccess(Right.ADMIN, this.userReference, wikiReference2);
285  1 verify(this.mockAuthorization).hasAccess(Right.ADMIN, this.userReference, wikiReference3);
286  1 verify(this.mockAuthorization).hasAccess(Right.PROGRAM, this.contentAuthorReference, wikiReference1);
287  1 verify(this.mockAuthorization).hasAccess(Right.PROGRAM, this.contentAuthorReference, wikiReference2);
288  1 verify(this.mockAuthorization).hasAccess(Right.PROGRAM, this.contentAuthorReference, wikiReference3);
289    }
290   
 
291  1 toggle @Test
292    public void resolveWithImplicitType() throws Exception
293    {
294  1 SolrDocument document = new SolrDocument();
295  1 Object[] parameters = new Object[] {};
296   
297  1 assertNull(this.service.resolve(document, parameters));
298   
299  1 document.setField(FieldUtils.TYPE, "foo");
300  1 assertNull(this.service.resolve(document, parameters));
301   
302  1 EntityReferenceResolver<SolrDocument> solrEntityReferenceResolver =
303    this.mocker.getInstance(SolrEntityReferenceResolver.TYPE);
304  1 EntityReference spaceReference = new EntityReference("bar", EntityType.SPACE);
305  1 when(solrEntityReferenceResolver.resolve(document, EntityType.SPACE, parameters)).thenReturn(spaceReference);
306   
307  1 document.setField(FieldUtils.TYPE, "SPACE");
308  1 assertSame(spaceReference, this.service.resolve(document, parameters));
309    }
310    }