|
| 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.realtime.ui; |
| 21 | + |
| 22 | +import javax.inject.Provider; |
| 23 | + |
| 24 | +import org.jsoup.nodes.Document; |
| 25 | +import org.junit.jupiter.api.BeforeEach; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.xwiki.csrf.script.CSRFTokenScriptService; |
| 28 | +import org.xwiki.model.reference.DocumentReference; |
| 29 | +import org.xwiki.rendering.RenderingScriptServiceComponentList; |
| 30 | +import org.xwiki.rendering.internal.configuration.DefaultRenderingConfigurationComponentList; |
| 31 | +import org.xwiki.script.service.ScriptService; |
| 32 | +import org.xwiki.test.page.HTML50ComponentList; |
| 33 | +import org.xwiki.test.page.PageTest; |
| 34 | +import org.xwiki.test.page.XWikiSyntax21ComponentList; |
| 35 | + |
| 36 | +import com.xpn.xwiki.XWikiContext; |
| 37 | +import com.xpn.xwiki.user.api.XWikiRightService; |
| 38 | + |
| 39 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 40 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 41 | +import static org.mockito.Mockito.mock; |
| 42 | +import static org.mockito.Mockito.never; |
| 43 | +import static org.mockito.Mockito.spy; |
| 44 | +import static org.mockito.Mockito.verify; |
| 45 | +import static org.mockito.Mockito.when; |
| 46 | + |
| 47 | +@RenderingScriptServiceComponentList |
| 48 | +@DefaultRenderingConfigurationComponentList |
| 49 | +@HTML50ComponentList |
| 50 | +@XWikiSyntax21ComponentList |
| 51 | +class ConvertHTMLPageTest extends PageTest |
| 52 | +{ |
| 53 | + |
| 54 | + private static final String WIKI_NAME = "xwiki"; |
| 55 | + |
| 56 | + private static final String XWIKI_SPACE = "RTFrontend"; |
| 57 | + |
| 58 | + private static final DocumentReference RTF_FRONTEND_CONVERT_HTML = |
| 59 | + new DocumentReference(WIKI_NAME, XWIKI_SPACE, "ConvertHTML"); |
| 60 | + |
| 61 | + private static final String CSRF_TOKEN = "a0a0a0a0"; |
| 62 | + |
| 63 | + private CSRFTokenScriptService tokenService; |
| 64 | + |
| 65 | + @BeforeEach |
| 66 | + void setUp() throws Exception |
| 67 | + { |
| 68 | + // Mock the Token Service to get a consistent CSRF token throughout the tests. |
| 69 | + this.tokenService = this.oldcore.getMocker().registerMockComponent(ScriptService.class, "csrf", |
| 70 | + CSRFTokenScriptService.class, true); |
| 71 | + when(this.tokenService.isTokenValid(CSRF_TOKEN)).thenReturn(true); |
| 72 | + |
| 73 | + this.xwiki.initializeMandatoryDocuments(this.context); |
| 74 | + |
| 75 | + this.context = mock(XWikiContext.class); |
| 76 | + Provider<XWikiContext> xcontextProvider = |
| 77 | + this.componentManager.registerMockComponent(XWikiContext.TYPE_PROVIDER); |
| 78 | + when(xcontextProvider.get()).thenReturn(this.context); |
| 79 | + when(this.context.getRequest()).thenReturn(this.request); |
| 80 | + when(this.context.getResponse()).thenReturn(this.response); |
| 81 | + when(this.context.getWiki()).thenReturn(this.xwiki); |
| 82 | + |
| 83 | + // Fake programming access level to display the complete page. |
| 84 | + XWikiRightService rightService = this.oldcore.getMockRightService(); |
| 85 | + when(this.xwiki.getRightService()).thenReturn(rightService); |
| 86 | + when(rightService.hasProgrammingRights(this.context)).thenReturn(true); |
| 87 | + this.response = spy(this.response); |
| 88 | + when(this.context.getResponse()).thenReturn(this.response); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void checkValidCSRFToken() throws Exception |
| 93 | + { |
| 94 | + when(this.context.getAction()).thenReturn("get"); |
| 95 | + this.request.put("text", "Hello"); |
| 96 | + this.request.put("form_token", CSRF_TOKEN); |
| 97 | + Document result = renderHTMLPage(RTF_FRONTEND_CONVERT_HTML); |
| 98 | + |
| 99 | + verify(this.response, never()).setStatus(anyInt()); |
| 100 | + verify(this.tokenService).isTokenValid(CSRF_TOKEN); |
| 101 | + assertEquals("$xwiki.getDocument('CKEditor.ContentSheet').getRenderedContent()", |
| 102 | + result.getElementsByTag("body").text()); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void checkInvalidCSRFToken() throws Exception |
| 107 | + { |
| 108 | + String wrongToken = "wrong_token"; |
| 109 | + when(this.context.getAction()).thenReturn("get"); |
| 110 | + this.request.put("text", "Hello"); |
| 111 | + this.request.put("form_token", wrongToken); |
| 112 | + Document result = renderHTMLPage(RTF_FRONTEND_CONVERT_HTML); |
| 113 | + |
| 114 | + verify(this.response).sendError(403, "rtfFrontend.convertHtml.invalidCsrfToken"); |
| 115 | + verify(this.tokenService).isTokenValid(wrongToken); |
| 116 | + assertEquals("", result.getElementsByTag("body").text()); |
| 117 | + } |
| 118 | +} |
0 commit comments