| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
@link |
| 53 |
|
|
| 54 |
|
@version |
| 55 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (93) |
Complexity: 11 |
Complexity Density: 0.13 |
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 1 |
Complexity Density: 0.07 |
|
| 78 |
10 |
@Before... |
| 79 |
|
public void setUp() throws Exception |
| 80 |
|
{ |
| 81 |
10 |
this.userReference = new DocumentReference("wiki", "space", "user"); |
| 82 |
|
|
| 83 |
|
|
| 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 |
|
|
| 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 |
|
|
| 99 |
10 |
this.mockAuthorization = this.mocker.getInstance(AuthorizationManager.class); |
| 100 |
|
|
| 101 |
10 |
when(mockAuthorization.hasAccess(any(), any(), any())).thenReturn(true); |
| 102 |
|
|
| 103 |
10 |
this.service = mocker.getComponentUnderTest(); |
| 104 |
|
|
| 105 |
|
|
| 106 |
10 |
this.logger = mocker.getMockedLogger(); |
| 107 |
10 |
verify(this.logger, never()).error(any(), any(IllegalAccessException.class)); |
| 108 |
|
} |
| 109 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 110 |
1 |
@Test... |
| 111 |
|
public void indexSingleReferenceChecksRights() throws Exception |
| 112 |
|
{ |
| 113 |
1 |
EntityReference wikiReference = new WikiReference("someWiki"); |
| 114 |
|
|
| 115 |
|
|
| 116 |
1 |
this.service.index(wikiReference); |
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
1 |
verify(this.mockAuthorization).hasAccess(Right.ADMIN, this.userReference, wikiReference); |
| 122 |
1 |
verify(this.mockAuthorization).hasAccess(Right.PROGRAM, this.contentAuthorReference, wikiReference); |
| 123 |
|
} |
| 124 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 125 |
1 |
@Test... |
| 126 |
|
public void indexMultipleReferencesChecksRights() throws Exception |
| 127 |
|
{ |
| 128 |
1 |
EntityReference wikiReference = new WikiReference("someWiki"); |
| 129 |
|
|
| 130 |
|
|
| 131 |
|
|
| 132 |
|
|
| 133 |
|
|
| 134 |
1 |
this.service.index(Arrays.asList(wikiReference)); |
| 135 |
|
|
| 136 |
|
|
| 137 |
|
|
| 138 |
|
|
| 139 |
1 |
verify(this.mockAuthorization).hasAccess(Right.ADMIN, this.userReference, wikiReference); |
| 140 |
1 |
verify(this.mockAuthorization).hasAccess(Right.PROGRAM, this.contentAuthorReference, wikiReference); |
| 141 |
|
} |
| 142 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 143 |
1 |
@Test... |
| 144 |
|
public void deleteSingleReferenceChecksRights() throws Exception |
| 145 |
|
{ |
| 146 |
1 |
EntityReference wikiReference = new WikiReference("someWiki"); |
| 147 |
|
|
| 148 |
|
|
| 149 |
1 |
this.service.delete(wikiReference); |
| 150 |
|
|
| 151 |
|
|
| 152 |
|
|
| 153 |
|
|
| 154 |
1 |
verify(this.mockAuthorization).hasAccess(Right.ADMIN, this.userReference, wikiReference); |
| 155 |
1 |
verify(this.mockAuthorization).hasAccess(Right.PROGRAM, this.contentAuthorReference, wikiReference); |
| 156 |
|
} |
| 157 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 158 |
1 |
@Test... |
| 159 |
|
public void deleteMultipleReferencesChecksRights() throws Exception |
| 160 |
|
{ |
| 161 |
1 |
EntityReference wikiReference = new WikiReference("someWiki"); |
| 162 |
|
|
| 163 |
|
|
| 164 |
|
|
| 165 |
|
|
| 166 |
|
|
| 167 |
1 |
this.service.delete(Arrays.asList(wikiReference)); |
| 168 |
|
|
| 169 |
|
|
| 170 |
|
|
| 171 |
|
|
| 172 |
1 |
verify(this.mockAuthorization).hasAccess(Right.ADMIN, this.userReference, wikiReference); |
| 173 |
1 |
verify(this.mockAuthorization).hasAccess(Right.PROGRAM, this.contentAuthorReference, wikiReference); |
| 174 |
|
} |
| 175 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
| 176 |
1 |
@Test... |
| 177 |
|
public void operationsChecksRightsWithOtherReferences() throws Exception |
| 178 |
|
{ |
| 179 |
1 |
EntityReference documentReference = new DocumentReference("someWiki", "space", "document"); |
| 180 |
|
|
| 181 |
|
|
| 182 |
1 |
this.service.index(documentReference); |
| 183 |
|
|
| 184 |
|
|
| 185 |
|
|
| 186 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
| 192 |
1 |
@Test... |
| 193 |
|
public void hasWikiAdminButNoProgrammingCausesRightsCheckFailure() throws Exception |
| 194 |
|
{ |
| 195 |
1 |
EntityReference wikiReference = new WikiReference("someWiki"); |
| 196 |
|
|
| 197 |
|
|
| 198 |
1 |
when(this.mockAuthorization.hasAccess(Right.PROGRAM, this.contentAuthorReference, wikiReference)).thenReturn( |
| 199 |
|
false); |
| 200 |
|
|
| 201 |
|
|
| 202 |
1 |
this.service.index(wikiReference); |
| 203 |
|
|
| 204 |
|
|
| 205 |
|
|
| 206 |
|
|
| 207 |
1 |
verify(this.mockAuthorization).hasAccess(Right.ADMIN, this.userReference, wikiReference); |
| 208 |
1 |
verify(this.mockAuthorization).hasAccess(Right.PROGRAM, this.contentAuthorReference, wikiReference); |
| 209 |
|
|
| 210 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
| 219 |
1 |
@Test... |
| 220 |
|
public void hasProgrammingButNoWikiAdminCausesRightsCheckFailure() throws Exception |
| 221 |
|
{ |
| 222 |
1 |
EntityReference wikiReference = new WikiReference("someWiki"); |
| 223 |
|
|
| 224 |
|
|
| 225 |
1 |
when(this.mockAuthorization.hasAccess(Right.ADMIN, this.userReference, wikiReference)).thenReturn(false); |
| 226 |
|
|
| 227 |
|
|
| 228 |
1 |
this.service.index(wikiReference); |
| 229 |
|
|
| 230 |
|
|
| 231 |
|
|
| 232 |
|
|
| 233 |
1 |
verify(this.mockAuthorization).hasAccess(Right.ADMIN, this.userReference, wikiReference); |
| 234 |
|
|
| 235 |
1 |
verify(this.mockAuthorization, times(0)).hasAccess(Right.PROGRAM, this.contentAuthorReference, wikiReference); |
| 236 |
|
|
| 237 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
| 246 |
1 |
@Test... |
| 247 |
|
public void openrationsOnMultipleReferencesOnTheSameWikiChecksRightsOnlyOnceForThatWiki() throws Exception |
| 248 |
|
{ |
| 249 |
|
|
| 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 |
|
|
| 256 |
1 |
this.service.index(Arrays.asList(wikiReference, spaceReference, documentReference, documentReference2)); |
| 257 |
|
|
| 258 |
|
|
| 259 |
|
|
| 260 |
|
|
| 261 |
1 |
verify(this.mockAuthorization).hasAccess(Right.ADMIN, this.userReference, wikiReference); |
| 262 |
1 |
verify(this.mockAuthorization).hasAccess(Right.PROGRAM, this.contentAuthorReference, wikiReference); |
| 263 |
|
} |
| 264 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
| 265 |
1 |
@Test... |
| 266 |
|
public void openrationsOnMultipleReferencesOnDifferentWikisChecksRightsOnEachWiki() throws Exception |
| 267 |
|
{ |
| 268 |
|
|
| 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 |
|
|
| 277 |
1 |
this.service.index(Arrays.asList(wikiReference1, spaceReference, wikiReference2, documentReference, |
| 278 |
|
documentReference2)); |
| 279 |
|
|
| 280 |
|
|
| 281 |
|
|
| 282 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
| 291 |
1 |
@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 |
|
} |