1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.web

File UndeleteActionTest.java

 

Code metrics

0
100
8
1
334
180
8
0.08
12.5
8
1

Classes

Class Line # Actions
UndeleteActionTest 62 100 0% 8 0
1.0100%
 

Contributing tests

This file is covered by 7 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 com.xpn.xwiki.web;
21   
22    import java.util.Arrays;
23    import java.util.List;
24    import java.util.Locale;
25   
26    import org.junit.Before;
27    import org.junit.Rule;
28    import org.junit.Test;
29    import org.xwiki.csrf.CSRFToken;
30    import org.xwiki.job.Job;
31    import org.xwiki.job.JobExecutor;
32    import org.xwiki.model.reference.DocumentReference;
33    import org.xwiki.refactoring.job.RefactoringJobs;
34    import org.xwiki.refactoring.job.RestoreRequest;
35    import org.xwiki.refactoring.script.RefactoringScriptService;
36    import org.xwiki.refactoring.script.RequestFactory;
37    import org.xwiki.script.service.ScriptService;
38    import org.xwiki.test.mockito.MockitoComponentManagerRule;
39   
40    import com.xpn.xwiki.XWiki;
41    import com.xpn.xwiki.XWikiContext;
42    import com.xpn.xwiki.doc.XWikiDeletedDocument;
43    import com.xpn.xwiki.doc.XWikiDocument;
44    import com.xpn.xwiki.user.api.XWikiRightService;
45   
46    import static org.junit.Assert.assertEquals;
47    import static org.junit.Assert.assertFalse;
48    import static org.junit.Assert.assertTrue;
49    import static org.mockito.ArgumentMatchers.any;
50    import static org.mockito.ArgumentMatchers.anyLong;
51    import static org.mockito.ArgumentMatchers.anyString;
52    import static org.mockito.Mockito.mock;
53    import static org.mockito.Mockito.never;
54    import static org.mockito.Mockito.verify;
55    import static org.mockito.Mockito.when;
56   
57    /**
58    * Unit tests for {@link UndeleteAction}.
59    *
60    * @version $Id: e08caeb4993de425c57ccef492a7632febb479b6 $
61    */
 
62    public class UndeleteActionTest
63    {
64    /**
65    * A component manager that allows us to register mock components.
66    */
67    @Rule
68    public MockitoComponentManagerRule mocker = new MockitoComponentManagerRule();
69   
70    /**
71    * The object being tested.
72    */
73    private UndeleteAction undeleteAction = new UndeleteAction();
74   
75    /**
76    * A mock {@link XWikiContext};
77    */
78    private XWikiContext context = mock(XWikiContext.class);
79   
80    private XWikiRequest request = mock(XWikiRequest.class);
81   
82    /**
83    * A mock {@link XWiki};
84    */
85    private XWiki xwiki = mock(XWiki.class);
86   
87    /**
88    * A mock {@link XWikiDocument};
89    */
90    private XWikiDocument document = mock(XWikiDocument.class);
91   
92    private XWikiRightService rightsService = mock(XWikiRightService.class);
93   
94    private RefactoringScriptService refactoringScriptService = mock(RefactoringScriptService.class);
95   
96    private RequestFactory requestFactory = mock(RequestFactory.class);
97   
98    private JobExecutor jobExecutor;
99   
100    private Job job = mock(Job.class);
101   
102    private RestoreRequest jobRequest;
103   
 
104  7 toggle @Before
105    public void setUp() throws Exception
106    {
107  7 mocker.registerMockComponent(CSRFToken.class);
108  7 mocker.registerComponent(ScriptService.class, "refactoring", refactoringScriptService);
109  7 Utils.setComponentManager(mocker);
110   
111  7 when(context.getRequest()).thenReturn(request);
112   
113  7 when(context.getWiki()).thenReturn(xwiki);
114   
115  7 when(xwiki.getRightService()).thenReturn(rightsService);
116   
117  7 when(context.getDoc()).thenReturn(document);
118  7 when(document.getDocumentReference()).thenReturn(new DocumentReference("xwiki", "Main", "DeletedDocument"));
119   
120  7 jobExecutor = mocker.registerMockComponent(JobExecutor.class);
121  7 when(jobExecutor.execute(anyString(), any())).thenReturn(job);
122   
123  7 jobRequest = mock(RestoreRequest.class);
124  7 when(refactoringScriptService.getRequestFactory()).thenReturn(requestFactory);
125  7 when(requestFactory.createRestoreRequest(any(List.class))).thenReturn(jobRequest);
126  7 when(requestFactory.createRestoreRequest(anyString())).thenReturn(jobRequest);
127    }
128   
129    /**
130    * Launches a RestoreJob with the current deleted document ID.
131    */
 
132  1 toggle @Test
133    public void restoreSingleDocument() throws Exception
134    {
135  1 CSRFToken csrfToken = mocker.getInstance(CSRFToken.class);
136  1 when(csrfToken.isTokenValid(null)).thenReturn(true);
137   
138  1 long id = 13;
139   
140  1 when(request.getParameter("id")).thenReturn(String.valueOf(id));
141   
142  1 XWikiDeletedDocument deletedDocument = mock(XWikiDeletedDocument.class);
143  1 when(deletedDocument.getLocale()).thenReturn(Locale.ROOT);
144  1 when(deletedDocument.getId()).thenReturn(id);
145  1 when(xwiki.getDeletedDocument(anyLong(), any(XWikiContext.class))).thenReturn(deletedDocument);
146   
147  1 when(rightsService.hasAccessLevel(any(), any(), any(), any())).thenReturn(true);
148   
149  1 assertFalse(undeleteAction.action(context));
150   
151  1 verify(requestFactory).createRestoreRequest(Arrays.asList(id));
152  1 verify(jobExecutor).execute(RefactoringJobs.RESTORE, jobRequest);
153  1 verify(job).join();
154    }
155   
 
156  1 toggle @Test
157    public void missingCSRFToken() throws Exception
158    {
159    // Valid Deleted document ID.
160  1 long id = 13;
161   
162  1 when(request.getParameter("id")).thenReturn(String.valueOf(id));
163   
164  1 XWikiDeletedDocument deletedDocument = mock(XWikiDeletedDocument.class);
165  1 when(xwiki.getDeletedDocument(anyLong(), any(XWikiContext.class))).thenReturn(deletedDocument);
166   
167    // Invalid CSRF token.
168  1 CSRFToken csrfToken = mocker.getInstance(CSRFToken.class);
169  1 when(csrfToken.isTokenValid(null)).thenReturn(false);
170   
171  1 assertFalse(undeleteAction.action(context));
172   
173    // Verify that the resubmission URL was retrieved to be used in the redirect.
174  1 verify(csrfToken).getResubmissionURL();
175    }
176   
177    /**
178    * When the recycle bin is disabled or when the deleted document ID is invalid, the document should not be restored.
179    */
 
180  1 toggle @Test
181    public void recycleBinDisabledOrInvalidId() throws Exception
182    {
183  1 CSRFToken csrfToken = mocker.getInstance(CSRFToken.class);
184  1 when(csrfToken.isTokenValid(null)).thenReturn(true);
185   
186  1 long id = 13;
187   
188  1 when(request.getParameter("id")).thenReturn(String.valueOf(id));
189   
190    // null is returned when the ID is invalid or the Recycle Bin is disabled.
191  1 when(xwiki.getDeletedDocument(anyLong(), any(XWikiContext.class))).thenReturn(null);
192   
193  1 assertFalse(undeleteAction.action(context));
194   
195    // Verify that we never get this far.
196  1 verify(requestFactory, never()).createRestoreRequest(Arrays.asList(id));
197    }
198   
199    /**
200    * Show the "restore" UI with the option to include the batch when restoring and to see the contents of the batch of
201    * the current deleted document.
202    */
 
203  1 toggle @Test
204    public void showBatch() throws Exception
205    {
206  1 long id = 13;
207   
208  1 when(request.getParameter("id")).thenReturn(String.valueOf(id));
209   
210  1 XWikiDeletedDocument deletedDocument = mock(XWikiDeletedDocument.class);
211  1 when(deletedDocument.getLocale()).thenReturn(Locale.ROOT);
212  1 when(deletedDocument.getId()).thenReturn(id);
213  1 when(xwiki.getDeletedDocument(anyLong(), any(XWikiContext.class))).thenReturn(deletedDocument);
214   
215  1 when(request.getParameter("showBatch")).thenReturn("true");
216   
217  1 when(rightsService.hasAccessLevel(any(), any(), any(), any())).thenReturn(true);
218   
219  1 assertTrue(undeleteAction.action(context));
220    // Render the "restore" template.
221  1 assertEquals("restore", undeleteAction.render(context));
222   
223    // Just make sure that we stop to the display, since the "confirm=true" parameter was not passed.
224  1 verify(requestFactory, never()).createRestoreRequest(Arrays.asList(id));
225    }
226   
227    /**
228    * Launches a RestoreJob with the batchId of the current deleted document.
229    */
 
230  1 toggle @Test
231    public void restoreBatch() throws Exception
232    {
233  1 CSRFToken csrfToken = mocker.getInstance(CSRFToken.class);
234  1 when(csrfToken.isTokenValid(null)).thenReturn(true);
235   
236  1 long id = 13;
237  1 String batchId = "abc123";
238   
239  1 when(request.getParameter("id")).thenReturn(String.valueOf(id));
240   
241  1 XWikiDeletedDocument deletedDocument = mock(XWikiDeletedDocument.class);
242  1 when(deletedDocument.getLocale()).thenReturn(Locale.ROOT);
243  1 when(deletedDocument.getId()).thenReturn(id);
244  1 when(deletedDocument.getBatchId()).thenReturn(batchId);
245  1 when(xwiki.getDeletedDocument(anyLong(), any(XWikiContext.class))).thenReturn(deletedDocument);
246   
247    // Go through the screen showing the option to include the batch and displaying its contents.
248  1 when(request.getParameter("showBatch")).thenReturn("true");
249   
250    // Option to include the entire batch when restoring is enabled.
251  1 when(request.getParameter("includeBatch")).thenReturn("true");
252   
253    // Confirmation button pressed.
254  1 when(request.getParameter("confirm")).thenReturn("true");
255   
256  1 when(rightsService.hasAccessLevel(any(), any(), any(), any())).thenReturn(true);
257   
258  1 assertFalse(undeleteAction.action(context));
259   
260  1 verify(requestFactory).createRestoreRequest(batchId);
261  1 verify(jobExecutor).execute(RefactoringJobs.RESTORE, jobRequest);
262  1 verify(job).join();
263    }
264   
265    /**
266    * When trying to restore, rights are checked on the current deleted document, regardless if single or batch
267    * restore.
268    */
 
269  1 toggle @Test
270    public void notAllowedToRestoreSinglePage() throws Exception
271    {
272  1 CSRFToken csrfToken = mocker.getInstance(CSRFToken.class);
273  1 when(csrfToken.isTokenValid(null)).thenReturn(true);
274   
275  1 long id = 13;
276   
277  1 when(request.getParameter("id")).thenReturn(String.valueOf(id));
278   
279  1 XWikiDeletedDocument deletedDocument = mock(XWikiDeletedDocument.class);
280  1 when(deletedDocument.getLocale()).thenReturn(Locale.ROOT);
281  1 when(deletedDocument.getId()).thenReturn(id);
282  1 when(xwiki.getDeletedDocument(anyLong(), any(XWikiContext.class))).thenReturn(deletedDocument);
283   
284  1 when(rightsService.hasAccessLevel(any(), any(), any(), any())).thenReturn(false);
285   
286  1 assertTrue(undeleteAction.action(context));
287    // Render the "accessdenied" template.
288  1 assertEquals("accessdenied", undeleteAction.render(context));
289   
290    // Just make sure we don`t go any further.
291  1 verify(requestFactory, never()).createRestoreRequest(Arrays.asList(id));
292    }
293   
294    /**
295    * When trying to restore, rights are checked on the current deleted document, regardless if single or batch
296    * restore.
297    */
 
298  1 toggle @Test
299    public void notAllowedToRestoreBatch() throws Exception
300    {
301  1 CSRFToken csrfToken = mocker.getInstance(CSRFToken.class);
302  1 when(csrfToken.isTokenValid(null)).thenReturn(true);
303   
304  1 long id = 13;
305  1 String batchId = "abc123";
306   
307  1 when(request.getParameter("id")).thenReturn(String.valueOf(id));
308   
309  1 XWikiDeletedDocument deletedDocument = mock(XWikiDeletedDocument.class);
310  1 when(deletedDocument.getLocale()).thenReturn(Locale.ROOT);
311  1 when(deletedDocument.getId()).thenReturn(id);
312  1 when(deletedDocument.getBatchId()).thenReturn(batchId);
313  1 when(xwiki.getDeletedDocument(anyLong(), any(XWikiContext.class))).thenReturn(deletedDocument);
314   
315    // Go through the screen showing the option to include the batch and displaying its contents.
316  1 when(request.getParameter("showBatch")).thenReturn("true");
317   
318    // Option to include the entire batch when restoring is enabled.
319  1 when(request.getParameter("includeBatch")).thenReturn("true");
320   
321    // Confirmation button pressed.
322  1 when(request.getParameter("confirm")).thenReturn("true");
323   
324    // No rights to restore the page when checking from the Action. The job will check individual rights.
325  1 when(rightsService.hasAccessLevel(any(), any(), any(), any())).thenReturn(false);
326   
327  1 assertTrue(undeleteAction.action(context));
328    // Render the "accessdenied" template.
329  1 assertEquals("accessdenied", undeleteAction.render(context));
330   
331    // Just make sure we don`t go any further.
332  1 verify(requestFactory, never()).createRestoreRequest(batchId);
333    }
334    }