1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.wiki.template.script; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.Collection; |
24 |
|
import java.util.List; |
25 |
|
|
26 |
|
import javax.inject.Provider; |
27 |
|
|
28 |
|
import org.junit.Before; |
29 |
|
import org.junit.Rule; |
30 |
|
import org.junit.Test; |
31 |
|
import org.xwiki.component.util.DefaultParameterizedType; |
32 |
|
import org.xwiki.context.Execution; |
33 |
|
import org.xwiki.context.ExecutionContext; |
34 |
|
import org.xwiki.job.event.status.JobStatus; |
35 |
|
import org.xwiki.model.reference.DocumentReference; |
36 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
37 |
|
import org.xwiki.model.reference.WikiReference; |
38 |
|
import org.xwiki.security.authorization.AccessDeniedException; |
39 |
|
import org.xwiki.security.authorization.AuthorizationManager; |
40 |
|
import org.xwiki.security.authorization.Right; |
41 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
42 |
|
import org.xwiki.wiki.descriptor.WikiDescriptor; |
43 |
|
import org.xwiki.wiki.descriptor.WikiDescriptorManager; |
44 |
|
import org.xwiki.wiki.manager.WikiManagerException; |
45 |
|
import org.xwiki.wiki.provisioning.WikiProvisioningJob; |
46 |
|
import org.xwiki.wiki.template.WikiTemplateManager; |
47 |
|
import org.xwiki.wiki.template.WikiTemplateManagerException; |
48 |
|
|
49 |
|
import com.xpn.xwiki.XWikiContext; |
50 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
51 |
|
|
52 |
|
import static org.junit.Assert.assertEquals; |
53 |
|
import static org.junit.Assert.assertFalse; |
54 |
|
import static org.junit.Assert.assertNull; |
55 |
|
import static org.junit.Assert.assertTrue; |
56 |
|
import static org.mockito.ArgumentMatchers.anyList; |
57 |
|
import static org.mockito.ArgumentMatchers.eq; |
58 |
|
import static org.mockito.Mockito.doThrow; |
59 |
|
import static org.mockito.Mockito.mock; |
60 |
|
import static org.mockito.Mockito.verify; |
61 |
|
import static org.mockito.Mockito.when; |
62 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (147) |
Complexity: 21 |
Complexity Density: 0.17 |
|
63 |
|
public class WikiTemplateManagerScriptTest |
64 |
|
{ |
65 |
|
@Rule |
66 |
|
public MockitoComponentMockingRule<WikiTemplateManagerScript> mocker = |
67 |
|
new MockitoComponentMockingRule(WikiTemplateManagerScript.class); |
68 |
|
|
69 |
|
private WikiTemplateManager wikiTemplateManager; |
70 |
|
|
71 |
|
private WikiDescriptorManager wikiDescriptorManager; |
72 |
|
|
73 |
|
private AuthorizationManager authorizationManager; |
74 |
|
|
75 |
|
private Provider<XWikiContext> xcontextProvider; |
76 |
|
|
77 |
|
private EntityReferenceSerializer<String> entityReferenceSerializer; |
78 |
|
|
79 |
|
private XWikiContext xcontext; |
80 |
|
|
81 |
|
private Execution execution; |
82 |
|
|
83 |
|
private DocumentReference currentUserRef; |
84 |
|
|
85 |
|
private XWikiDocument currentDoc; |
86 |
|
|
87 |
|
private ExecutionContext executionContext; |
88 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 1 |
Complexity Density: 0.06 |
|
89 |
17 |
@Before... |
90 |
|
public void setUp() throws Exception |
91 |
|
{ |
92 |
17 |
wikiTemplateManager = mocker.getInstance(WikiTemplateManager.class); |
93 |
17 |
wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class); |
94 |
17 |
authorizationManager = mocker.getInstance(AuthorizationManager.class); |
95 |
17 |
entityReferenceSerializer = mocker.getInstance(new DefaultParameterizedType(null, |
96 |
|
EntityReferenceSerializer.class, String.class)); |
97 |
17 |
xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER); |
98 |
17 |
xcontext = mock(XWikiContext.class); |
99 |
17 |
when(xcontextProvider.get()).thenReturn(xcontext); |
100 |
17 |
execution = mocker.getInstance(Execution.class); |
101 |
17 |
executionContext = new ExecutionContext(); |
102 |
17 |
when(execution.getContext()).thenReturn(executionContext); |
103 |
|
|
104 |
17 |
currentUserRef = new DocumentReference("mainWiki", "XWiki", "User"); |
105 |
17 |
when(xcontext.getUserReference()).thenReturn(currentUserRef); |
106 |
|
|
107 |
17 |
currentDoc = mock(XWikiDocument.class); |
108 |
17 |
when(xcontext.getDoc()).thenReturn(currentDoc); |
109 |
|
|
110 |
17 |
when(xcontext.getMainXWiki()).thenReturn("mainWiki"); |
111 |
|
|
112 |
17 |
when(entityReferenceSerializer.serialize(currentUserRef)).thenReturn("mainWiki:XWiki.User"); |
113 |
|
} |
114 |
|
|
115 |
|
|
116 |
|
@return |
117 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
118 |
2 |
private Exception currentScriptHasNotProgrammingRight() throws AccessDeniedException... |
119 |
|
{ |
120 |
2 |
DocumentReference authorDocRef = new DocumentReference("mainWiki", "XWiki", "Admin"); |
121 |
2 |
when(currentDoc.getAuthorReference()).thenReturn(authorDocRef); |
122 |
2 |
DocumentReference currentDocRef = new DocumentReference("subwiki", "Test", "test"); |
123 |
2 |
when(currentDoc.getDocumentReference()).thenReturn(currentDocRef); |
124 |
|
|
125 |
2 |
Exception exception = new AccessDeniedException(Right.PROGRAM, authorDocRef, currentDocRef); |
126 |
2 |
doThrow(exception).when(authorizationManager).checkAccess(Right.PROGRAM, authorDocRef, currentDocRef); |
127 |
|
|
128 |
2 |
return exception; |
129 |
|
} |
130 |
|
|
131 |
|
|
132 |
|
@return |
133 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
134 |
1 |
private Exception currentUserHasNotAdminRight() throws AccessDeniedException... |
135 |
|
{ |
136 |
1 |
WikiReference wiki = new WikiReference("wikiId"); |
137 |
1 |
Exception exception = new AccessDeniedException(Right.ADMIN, currentUserRef, wiki); |
138 |
1 |
doThrow(exception).when(authorizationManager).checkAccess(eq(Right.ADMIN), eq(currentUserRef), eq(wiki)); |
139 |
|
|
140 |
1 |
return exception; |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
|
@return |
145 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
146 |
1 |
private Exception currentUserHasNotCreateWikiRight() throws AccessDeniedException... |
147 |
|
{ |
148 |
1 |
WikiReference wiki = new WikiReference("mainWiki"); |
149 |
1 |
Exception exception = new AccessDeniedException(Right.CREATE_WIKI, currentUserRef, wiki); |
150 |
1 |
doThrow(exception).when(authorizationManager).checkAccess(eq(Right.CREATE_WIKI), eq(currentUserRef), eq(wiki)); |
151 |
|
|
152 |
1 |
return exception; |
153 |
|
} |
154 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
155 |
1 |
@Test... |
156 |
|
public void getTemplates() throws Exception |
157 |
|
{ |
158 |
1 |
Collection<WikiDescriptor> templates = new ArrayList<WikiDescriptor>(); |
159 |
1 |
WikiDescriptor descriptor = new WikiDescriptor("templateId", "templateAlias"); |
160 |
1 |
templates.add(descriptor); |
161 |
|
|
162 |
1 |
when(wikiTemplateManager.getTemplates()).thenReturn(templates); |
163 |
|
|
164 |
1 |
Collection<WikiDescriptor> results = mocker.getComponentUnderTest().getTemplates(); |
165 |
1 |
assertEquals(templates, results); |
166 |
|
} |
167 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
168 |
1 |
@Test... |
169 |
|
public void getTemplatesError() throws Exception |
170 |
|
{ |
171 |
1 |
Exception exception = new WikiTemplateManagerException("Error in getTemplates"); |
172 |
1 |
when(wikiTemplateManager.getTemplates()).thenThrow(exception); |
173 |
|
|
174 |
1 |
Collection<WikiDescriptor> results = mocker.getComponentUnderTest().getTemplates(); |
175 |
1 |
assertTrue(results.isEmpty()); |
176 |
1 |
assertEquals(exception, mocker.getComponentUnderTest().getLastError()); |
177 |
1 |
verify(mocker.getMockedLogger()).error("Error while getting all the wiki templates.", exception); |
178 |
|
} |
179 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
180 |
1 |
@Test... |
181 |
|
public void setTemplateWhenCurrentUserIsOwner() throws Exception |
182 |
|
{ |
183 |
1 |
WikiDescriptor wikiDescriptor = new WikiDescriptor("wikiId", "wikiAlias"); |
184 |
1 |
wikiDescriptor.setOwnerId("mainWiki:XWiki.User"); |
185 |
1 |
when(wikiDescriptorManager.getById("wikiId")).thenReturn(wikiDescriptor); |
186 |
|
|
187 |
|
|
188 |
1 |
boolean result = mocker.getComponentUnderTest().setTemplate("wikiId", true); |
189 |
1 |
assertTrue(result); |
190 |
1 |
verify(wikiTemplateManager).setTemplate("wikiId", true); |
191 |
|
|
192 |
|
|
193 |
1 |
result = mocker.getComponentUnderTest().setTemplate("wikiId", false); |
194 |
1 |
assertTrue(result); |
195 |
1 |
verify(wikiTemplateManager).setTemplate("wikiId", false); |
196 |
|
} |
197 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
198 |
1 |
@Test... |
199 |
|
public void setTemplateWithoutPR() throws Exception |
200 |
|
{ |
201 |
1 |
Exception exception = currentScriptHasNotProgrammingRight(); |
202 |
|
|
203 |
1 |
boolean result = mocker.getComponentUnderTest().setTemplate("wikiId", true); |
204 |
1 |
assertFalse(result); |
205 |
1 |
assertEquals(exception, mocker.getComponentUnderTest().getLastError()); |
206 |
1 |
verify(mocker.getMockedLogger()).error("Access denied for [mainWiki:XWiki.User] to change the template value" + |
207 |
|
" of the wiki [wikiId]. The user has not the right to perform this operation or the script has not " + |
208 |
|
"the programming right.", exception); |
209 |
|
} |
210 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
211 |
1 |
@Test... |
212 |
|
public void setTemplateWithoutAdminRight() throws Exception |
213 |
|
{ |
214 |
1 |
Exception exception = currentUserHasNotAdminRight(); |
215 |
|
|
216 |
1 |
WikiDescriptor wikiDescriptor = new WikiDescriptor("wikiId", "wikiAlias"); |
217 |
1 |
when(wikiDescriptorManager.getById("wikiId")).thenReturn(wikiDescriptor); |
218 |
|
|
219 |
1 |
boolean result = mocker.getComponentUnderTest().setTemplate("wikiId", true); |
220 |
1 |
assertFalse(result); |
221 |
1 |
assertEquals(exception, mocker.getComponentUnderTest().getLastError()); |
222 |
1 |
verify(mocker.getMockedLogger()).error("Access denied for [mainWiki:XWiki.User] to change the template value" + |
223 |
|
" of the wiki [wikiId]. The user has not the right to perform this operation or the script has not " + |
224 |
|
"the programming right.", exception); |
225 |
|
} |
226 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
227 |
1 |
@Test... |
228 |
|
public void setTemplateErrorWithDescriptorManager() throws Exception |
229 |
|
{ |
230 |
1 |
Exception exception = new WikiManagerException("error in getById"); |
231 |
1 |
when(wikiDescriptorManager.getById("wikiId")).thenThrow(exception); |
232 |
|
|
233 |
1 |
boolean result = mocker.getComponentUnderTest().setTemplate("wikiId", true); |
234 |
1 |
assertFalse(result); |
235 |
1 |
assertEquals(exception, mocker.getComponentUnderTest().getLastError()); |
236 |
1 |
verify(mocker.getMockedLogger()).error("Failed to get the descriptor of the wiki [wikiId].", exception); |
237 |
|
} |
238 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
239 |
1 |
@Test... |
240 |
|
public void setTemplateErrorWithTemplateManager() throws Exception |
241 |
|
{ |
242 |
1 |
WikiDescriptor wikiDescriptor = new WikiDescriptor("wikiId", "wikiAlias"); |
243 |
1 |
when(wikiDescriptorManager.getById("wikiId")).thenReturn(wikiDescriptor); |
244 |
|
|
245 |
1 |
Exception exception = new WikiTemplateManagerException("error in setTemplate"); |
246 |
1 |
doThrow(exception).when(wikiTemplateManager).setTemplate("wikiId", true); |
247 |
|
|
248 |
1 |
boolean result = mocker.getComponentUnderTest().setTemplate("wikiId", true); |
249 |
1 |
assertFalse(result); |
250 |
1 |
assertEquals(exception, mocker.getComponentUnderTest().getLastError()); |
251 |
1 |
verify(mocker.getMockedLogger()).error("Failed to set the template value [true] for the wiki [wikiId].", |
252 |
|
exception); |
253 |
|
} |
254 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
255 |
1 |
@Test... |
256 |
|
public void isTemplate() throws Exception |
257 |
|
{ |
258 |
1 |
when(wikiTemplateManager.isTemplate("wikiTemplate")).thenReturn(true); |
259 |
1 |
when(wikiTemplateManager.isTemplate("subwiki")).thenReturn(false); |
260 |
|
|
261 |
1 |
assertTrue(mocker.getComponentUnderTest().isTemplate("wikiTemplate")); |
262 |
1 |
assertFalse(mocker.getComponentUnderTest().isTemplate("subwiki")); |
263 |
|
} |
264 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
265 |
1 |
@Test... |
266 |
|
public void isTemplateError() throws Exception |
267 |
|
{ |
268 |
1 |
Exception exception = new WikiTemplateManagerException("error in isTemplate"); |
269 |
|
|
270 |
1 |
when(wikiTemplateManager.isTemplate("wikiTemplate")).thenThrow(exception); |
271 |
|
|
272 |
1 |
assertNull(mocker.getComponentUnderTest().isTemplate("wikiTemplate")); |
273 |
1 |
assertEquals(exception, mocker.getComponentUnderTest().getLastError()); |
274 |
1 |
verify(mocker.getMockedLogger()).error("Failed to get if the wiki [wikiTemplate] is a template or not.", |
275 |
|
exception); |
276 |
|
} |
277 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
278 |
1 |
@Test... |
279 |
|
public void createWikiFromTemplate() throws Exception |
280 |
|
{ |
281 |
|
|
282 |
1 |
boolean result = mocker.getComponentUnderTest().createWikiFromTemplate("newWikiId", "newWikiAlias", |
283 |
|
"templateId", "ownerId", true); |
284 |
|
|
285 |
|
|
286 |
1 |
assertTrue(result); |
287 |
1 |
verify(wikiTemplateManager).createWikiFromTemplate("newWikiId", "newWikiAlias", "templateId", "ownerId", true); |
288 |
|
} |
289 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
290 |
1 |
@Test... |
291 |
|
public void createWikiFromTemplateWithoutPR() throws Exception |
292 |
|
{ |
293 |
1 |
Exception exception = currentScriptHasNotProgrammingRight(); |
294 |
|
|
295 |
|
|
296 |
1 |
boolean result = mocker.getComponentUnderTest().createWikiFromTemplate("newWikiId", "newWikiAlias", |
297 |
|
"templateId", "ownerId", true); |
298 |
|
|
299 |
|
|
300 |
1 |
assertFalse(result); |
301 |
1 |
assertEquals(exception, mocker.getComponentUnderTest().getLastError()); |
302 |
1 |
verify(mocker.getMockedLogger()).error( |
303 |
|
"Error, you or this script does not have the right to create a wiki from a template.", exception); |
304 |
|
} |
305 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
306 |
1 |
@Test... |
307 |
|
public void createWikiFromTemplateWithoutCreateRight() throws Exception |
308 |
|
{ |
309 |
1 |
Exception exception = currentUserHasNotCreateWikiRight(); |
310 |
|
|
311 |
|
|
312 |
1 |
boolean result = mocker.getComponentUnderTest().createWikiFromTemplate("newWikiId", "newWikiAlias", |
313 |
|
"templateId", "ownerId", true); |
314 |
|
|
315 |
|
|
316 |
1 |
assertFalse(result); |
317 |
1 |
assertEquals(exception, mocker.getComponentUnderTest().getLastError()); |
318 |
1 |
verify(mocker.getMockedLogger()).error( |
319 |
|
"Error, you or this script does not have the right to create a wiki from a template.", exception); |
320 |
|
} |
321 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
322 |
1 |
@Test... |
323 |
|
public void createWikiFromTemplateError() throws Exception |
324 |
|
{ |
325 |
1 |
Exception exception = new WikiTemplateManagerException("error in createWikiFromTemplate."); |
326 |
|
|
327 |
1 |
when(wikiTemplateManager.createWikiFromTemplate("newWikiId", "newWikiAlias", "templateId", |
328 |
|
"ownerId", true)).thenThrow(exception); |
329 |
|
|
330 |
|
|
331 |
1 |
boolean result = mocker.getComponentUnderTest().createWikiFromTemplate("newWikiId", "newWikiAlias", |
332 |
|
"templateId", "ownerId", true); |
333 |
|
|
334 |
|
|
335 |
1 |
assertFalse(result); |
336 |
1 |
assertEquals(exception, mocker.getComponentUnderTest().getLastError()); |
337 |
1 |
verify(mocker.getMockedLogger()).error("Failed to create the wiki from the template.", exception); |
338 |
|
} |
339 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
340 |
1 |
@Test... |
341 |
|
public void getLastException() throws Exception |
342 |
|
{ |
343 |
1 |
Exception exception = new Exception("test"); |
344 |
1 |
executionContext.setProperty(WikiTemplateManagerScript.CONTEXT_LASTEXCEPTION, exception); |
345 |
1 |
assertEquals(exception, mocker.getComponentUnderTest().getLastException()); |
346 |
|
} |
347 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
348 |
1 |
@Test... |
349 |
|
public void getWikiProvisioningJobStatus() throws Exception |
350 |
|
{ |
351 |
1 |
WikiProvisioningJob job = mock(WikiProvisioningJob.class); |
352 |
1 |
when(wikiTemplateManager.getWikiProvisioningJob(anyList())).thenReturn(job); |
353 |
1 |
JobStatus status = mock(JobStatus.class); |
354 |
1 |
when(job.getStatus()).thenReturn(status); |
355 |
|
|
356 |
1 |
List<String> jobId = new ArrayList<String>(); |
357 |
1 |
JobStatus result = mocker.getComponentUnderTest().getWikiProvisioningJobStatus(jobId); |
358 |
|
|
359 |
1 |
assertEquals(status, result); |
360 |
|
} |
361 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
362 |
1 |
@Test... |
363 |
|
public void getWikiProvisioningJobStatusWithBadId() throws Exception |
364 |
|
{ |
365 |
1 |
List<String> jobId = new ArrayList<String>(); |
366 |
1 |
JobStatus result = mocker.getComponentUnderTest().getWikiProvisioningJobStatus(jobId); |
367 |
|
|
368 |
1 |
assertEquals(null, result); |
369 |
|
} |
370 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
371 |
1 |
@Test... |
372 |
|
public void getWikiProvisioningJobStatusWithException() throws Exception |
373 |
|
{ |
374 |
1 |
Exception exception = new WikiTemplateManagerException("test"); |
375 |
1 |
when(wikiTemplateManager.getWikiProvisioningJob(anyList())).thenThrow(exception); |
376 |
|
|
377 |
1 |
List<String> jobId = new ArrayList<String>(); |
378 |
1 |
JobStatus result = mocker.getComponentUnderTest().getWikiProvisioningJobStatus(jobId); |
379 |
|
|
380 |
1 |
assertEquals(null, result); |
381 |
1 |
assertEquals(exception, mocker.getComponentUnderTest().getLastError()); |
382 |
1 |
verify(mocker.getMockedLogger()).error("Failed to get tge wiki provisioning job.", exception); |
383 |
|
|
384 |
|
} |
385 |
|
|
386 |
|
} |