|
| 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.scheduler.ui; |
| 21 | + |
| 22 | +import java.util.List; |
| 23 | +import java.util.stream.Stream; |
| 24 | + |
| 25 | +import org.jsoup.nodes.Document; |
| 26 | +import org.jsoup.nodes.Element; |
| 27 | +import org.junit.jupiter.api.BeforeEach; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.junit.jupiter.params.ParameterizedTest; |
| 30 | +import org.junit.jupiter.params.provider.Arguments; |
| 31 | +import org.junit.jupiter.params.provider.MethodSource; |
| 32 | +import org.mockito.Mock; |
| 33 | +import org.quartz.Trigger; |
| 34 | +import org.xwiki.csrf.script.CSRFTokenScriptService; |
| 35 | +import org.xwiki.model.reference.DocumentReference; |
| 36 | +import org.xwiki.query.internal.ScriptQuery; |
| 37 | +import org.xwiki.query.script.QueryManagerScriptService; |
| 38 | +import org.xwiki.rendering.RenderingScriptServiceComponentList; |
| 39 | +import org.xwiki.rendering.internal.configuration.DefaultRenderingConfigurationComponentList; |
| 40 | +import org.xwiki.rendering.internal.macro.message.ErrorMessageMacro; |
| 41 | +import org.xwiki.rendering.internal.macro.message.InfoMessageMacro; |
| 42 | +import org.xwiki.rendering.internal.macro.message.WarningMessageMacro; |
| 43 | +import org.xwiki.script.service.ScriptService; |
| 44 | +import org.xwiki.test.annotation.ComponentList; |
| 45 | +import org.xwiki.test.page.HTML50ComponentList; |
| 46 | +import org.xwiki.test.page.PageTest; |
| 47 | +import org.xwiki.test.page.TestNoScriptMacro; |
| 48 | +import org.xwiki.test.page.XWikiSyntax21ComponentList; |
| 49 | + |
| 50 | +import com.xpn.xwiki.XWikiContext; |
| 51 | +import com.xpn.xwiki.api.Object; |
| 52 | +import com.xpn.xwiki.doc.XWikiDocument; |
| 53 | +import com.xpn.xwiki.objects.BaseObject; |
| 54 | +import com.xpn.xwiki.plugin.scheduler.JobState; |
| 55 | +import com.xpn.xwiki.plugin.scheduler.SchedulerPluginApi; |
| 56 | +import com.xpn.xwiki.plugin.scheduler.internal.SchedulerJobClassDocumentInitializer; |
| 57 | + |
| 58 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 59 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 60 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 61 | +import static org.mockito.ArgumentMatchers.any; |
| 62 | +import static org.mockito.ArgumentMatchers.anyString; |
| 63 | +import static org.mockito.ArgumentMatchers.eq; |
| 64 | +import static org.mockito.Mockito.doReturn; |
| 65 | +import static org.mockito.Mockito.mock; |
| 66 | +import static org.mockito.Mockito.never; |
| 67 | +import static org.mockito.Mockito.verify; |
| 68 | +import static org.mockito.Mockito.when; |
| 69 | + |
| 70 | +/** |
| 71 | + * Page tests for {@code Scheduler.WebHome}. |
| 72 | + * |
| 73 | + * @version $Id$ |
| 74 | + */ |
| 75 | +@ComponentList({ |
| 76 | + InfoMessageMacro.class, |
| 77 | + ErrorMessageMacro.class, |
| 78 | + SchedulerJobClassDocumentInitializer.class, |
| 79 | + TestNoScriptMacro.class, |
| 80 | + WarningMessageMacro.class |
| 81 | +}) |
| 82 | +@RenderingScriptServiceComponentList |
| 83 | +@DefaultRenderingConfigurationComponentList |
| 84 | +@HTML50ComponentList |
| 85 | +@XWikiSyntax21ComponentList |
| 86 | +class SchedulerPageTest extends PageTest |
| 87 | +{ |
| 88 | + private static final String WIKI_NAME = "xwiki"; |
| 89 | + |
| 90 | + private static final String XWIKI_SPACE = "Scheduler"; |
| 91 | + |
| 92 | + private static final DocumentReference SCHEDULER_WEB_HOME = |
| 93 | + new DocumentReference(WIKI_NAME, XWIKI_SPACE, "WebHome"); |
| 94 | + |
| 95 | + private static final String CSRF_TOKEN = "a0a0a0a0"; |
| 96 | + |
| 97 | + private QueryManagerScriptService queryService; |
| 98 | + |
| 99 | + private CSRFTokenScriptService tokenService; |
| 100 | + |
| 101 | + private SchedulerPluginApi schedulerPluginApi; |
| 102 | + |
| 103 | + @Mock |
| 104 | + private ScriptQuery query; |
| 105 | + |
| 106 | + private Object testJobObjectApi; |
| 107 | + |
| 108 | + @BeforeEach |
| 109 | + void setUp() throws Exception |
| 110 | + { |
| 111 | + // Mock the Query Service to return a job. |
| 112 | + this.queryService = this.oldcore.getMocker().registerMockComponent(ScriptService.class, "query", |
| 113 | + QueryManagerScriptService.class, |
| 114 | + true); |
| 115 | + when(this.queryService.xwql(anyString())).thenReturn(this.query); |
| 116 | + when(this.query.execute()).thenReturn(List.of("Scheduler.TestJob")); |
| 117 | + |
| 118 | + // Mock the Token Service to get a consistent CSRF token throughout the tests. |
| 119 | + this.tokenService = this.oldcore.getMocker().registerMockComponent(ScriptService.class, "csrf", |
| 120 | + CSRFTokenScriptService.class, true); |
| 121 | + when(this.tokenService.getToken()).thenReturn(CSRF_TOKEN); |
| 122 | + when(this.tokenService.isTokenValid(CSRF_TOKEN)).thenReturn(true); |
| 123 | + |
| 124 | + // Spy the Scheduler Plugin to obtain a mocked API. |
| 125 | + this.schedulerPluginApi = mock(SchedulerPluginApi.class); |
| 126 | + doReturn(this.schedulerPluginApi).when(this.oldcore.getSpyXWiki()).getPluginApi(eq("scheduler"), |
| 127 | + any(XWikiContext.class)); |
| 128 | + |
| 129 | + this.xwiki.initializeMandatoryDocuments(this.context); |
| 130 | + |
| 131 | + // Create a new job and keep a reference to its API. |
| 132 | + XWikiDocument testJob = new XWikiDocument(new DocumentReference("xwiki", "Scheduler", "TestJob")); |
| 133 | + BaseObject testJobObject = testJob.newXObject(SchedulerJobClassDocumentInitializer.XWIKI_JOB_CLASSREFERENCE, |
| 134 | + this.context); |
| 135 | + this.xwiki.saveDocument(testJob, this.context); |
| 136 | + this.testJobObjectApi = new Object(testJobObject, this.context); |
| 137 | + |
| 138 | + // Fake programming access level to display the complete page. |
| 139 | + when(this.oldcore.getMockRightService().hasAccessLevel(eq("programming"), anyString(), anyString(), |
| 140 | + any(XWikiContext.class))).thenReturn(true); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Verify that the trigger operation is not called in the Scheduler Plugin API when the CSRF token is invalid, and |
| 145 | + * that the corresponding error message is properly displayed. |
| 146 | + */ |
| 147 | + @Test |
| 148 | + void checkInvalidCSRFToken() throws Exception |
| 149 | + { |
| 150 | + String wrongToken = "wrong token"; |
| 151 | + |
| 152 | + this.request.put("do", "trigger"); |
| 153 | + this.request.put("which", "Scheduler.TestJob"); |
| 154 | + this.request.put("form_token", wrongToken); |
| 155 | + Document result = renderHTMLPage(SCHEDULER_WEB_HOME); |
| 156 | + |
| 157 | + verify(this.schedulerPluginApi, never()).triggerJob(any(Object.class)); |
| 158 | + verify(this.tokenService).isTokenValid(wrongToken); |
| 159 | + assertEquals("xe.scheduler.invalidToken", result.getElementsByClass("errormessage").text()); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Verify that the trigger operation is correctly called in the Scheduler Plugin API when the CSRF token is valid, |
| 164 | + * and that no error displays. |
| 165 | + */ |
| 166 | + @Test |
| 167 | + void checkValidCSRFToken() throws Exception |
| 168 | + { |
| 169 | + when(this.schedulerPluginApi.triggerJob(this.testJobObjectApi)).thenReturn(true); |
| 170 | + |
| 171 | + this.request.put("do", "trigger"); |
| 172 | + this.request.put("which", "Scheduler.TestJob"); |
| 173 | + this.request.put("form_token", CSRF_TOKEN); |
| 174 | + Document result = renderHTMLPage(SCHEDULER_WEB_HOME); |
| 175 | + |
| 176 | + verify(this.schedulerPluginApi).triggerJob(this.testJobObjectApi); |
| 177 | + verify(this.tokenService).isTokenValid(CSRF_TOKEN); |
| 178 | + assertTrue(result.getElementsByClass("errormessage").isEmpty()); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * List every possible action that can be applied to a job depending on its current status. |
| 183 | + * |
| 184 | + * @return a {@link Stream} of {@link org.junit.jupiter.params.provider.Arguments} for every combination of job |
| 185 | + * status and action |
| 186 | + */ |
| 187 | + static Stream<Arguments> jobStatusAndActionProvider() |
| 188 | + { |
| 189 | + return Stream.of( |
| 190 | + Arguments.of(new JobState(Trigger.TriggerState.NONE), "trigger"), |
| 191 | + Arguments.of(new JobState(Trigger.TriggerState.NONE), "schedule"), |
| 192 | + Arguments.of(new JobState(Trigger.TriggerState.NORMAL), "pause"), |
| 193 | + Arguments.of(new JobState(Trigger.TriggerState.NORMAL), "unschedule"), |
| 194 | + Arguments.of(new JobState(Trigger.TriggerState.PAUSED), "resume"), |
| 195 | + Arguments.of(new JobState(Trigger.TriggerState.PAUSED), "unschedule"), |
| 196 | + Arguments.of(new JobState(Trigger.TriggerState.NONE), "delete")); |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * Verify that each action URL on the page contains the right CSRF token. |
| 201 | + * |
| 202 | + * @param status the status of the job |
| 203 | + * @param action the action to verify |
| 204 | + */ |
| 205 | + @ParameterizedTest |
| 206 | + @MethodSource("jobStatusAndActionProvider") |
| 207 | + void checkCSRFTokenPresenceInActionURL(JobState status, String action) throws Exception |
| 208 | + { |
| 209 | + // Set the status of the displayed job to control which action URLs will be rendered. |
| 210 | + when(this.schedulerPluginApi.getJobStatus(this.testJobObjectApi)).thenReturn(status); |
| 211 | + |
| 212 | + Document result = renderHTMLPage(SCHEDULER_WEB_HOME); |
| 213 | + verify(this.schedulerPluginApi).getJobStatus(this.testJobObjectApi); |
| 214 | + Element actionLink = result.selectFirst(String.format("td a:contains(actions.%s)", action)); |
| 215 | + assertNotNull(actionLink); |
| 216 | + |
| 217 | + // Check the presence of the CSRF token for the given action. |
| 218 | + assertEquals(String.format("path:/xwiki/bin/view/Scheduler/?do=%s&which=Scheduler" |
| 219 | + + ".TestJob&form_token=%s", action, CSRF_TOKEN), actionLink.attr("href")); |
| 220 | + } |
| 221 | + |
| 222 | + /** |
| 223 | + * Verify that the names of jobs are properly escaped in each action URL. |
| 224 | + * |
| 225 | + * @param status the status of the job |
| 226 | + * @param action the action to verify |
| 227 | + */ |
| 228 | + @ParameterizedTest |
| 229 | + @MethodSource("jobStatusAndActionProvider") |
| 230 | + void checkEscapingInJobNames(JobState status, String action) throws Exception |
| 231 | + { |
| 232 | + // Use the `noscript` macro to make sure that no code injection occurs. |
| 233 | + String jobName = "\">]]{{/html}}{{noscript /}}"; |
| 234 | + String escapedJobName = "%22%3E%5D%5D%7B%7B%2Fhtml%7D%7D%7B%7Bnoscript%20%2F%7D%7D"; |
| 235 | + |
| 236 | + // Create a new job with a name that needs escaping and get a reference to its API. |
| 237 | + XWikiDocument escapedJob = new XWikiDocument(new DocumentReference("xwiki", "Scheduler", jobName)); |
| 238 | + BaseObject escapedJobObject = |
| 239 | + escapedJob.newXObject(SchedulerJobClassDocumentInitializer.XWIKI_JOB_CLASSREFERENCE, this.context); |
| 240 | + Object escapedJobObjectApi = new Object(escapedJobObject, this.context); |
| 241 | + this.xwiki.saveDocument(escapedJob, this.context); |
| 242 | + |
| 243 | + // Return the name of the new job through the Query Service. |
| 244 | + when(this.query.execute()).thenReturn(List.of("Scheduler." + jobName)); |
| 245 | + |
| 246 | + // Set the status of the new job to control which action URLs will be rendered. |
| 247 | + when(this.schedulerPluginApi.getJobStatus(escapedJobObjectApi)).thenReturn(status); |
| 248 | + |
| 249 | + Document result = renderHTMLPage(SCHEDULER_WEB_HOME); |
| 250 | + Element actionLink = result.selectFirst(String.format("td a:contains(actions.%s)", action)); |
| 251 | + assertNotNull(actionLink); |
| 252 | + |
| 253 | + // Check the proper escaping of the job name for the given action. |
| 254 | + assertEquals(String.format("path:/xwiki/bin/view/Scheduler/?do=%s&which=Scheduler" |
| 255 | + + ".%s&form_token=%s", action, escapedJobName, CSRF_TOKEN), actionLink.attr("href")); |
| 256 | + } |
| 257 | +} |
0 commit comments