1919 */
2020package com .xpn .xwiki .web ;
2121
22+ import java .util .Map ;
23+
2224import javax .inject .Named ;
2325
2426import org .junit .jupiter .api .BeforeEach ;
2527import org .junit .jupiter .api .Test ;
2628import org .mockito .Mock ;
29+ import org .xwiki .csrf .CSRFToken ;
2730import org .xwiki .model .reference .DocumentReference ;
2831import org .xwiki .store .TemporaryAttachmentSessionsManager ;
2932import org .xwiki .test .junit5 .mockito .InjectMockComponents ;
3942import com .xpn .xwiki .test .junit5 .mockito .OldcoreTest ;
4043import com .xpn .xwiki .test .reference .ReferenceComponentList ;
4144
45+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4246import static org .junit .jupiter .api .Assertions .assertSame ;
4347import static org .mockito .Mockito .mock ;
4448import static org .mockito .Mockito .when ;
@@ -75,6 +79,9 @@ public class EditActionTest
7579 @ Named ("document" )
7680 private UserReferenceSerializer <DocumentReference > documentReferenceUserReferenceSerializer ;
7781
82+ @ MockComponent
83+ private CSRFToken csrf ;
84+
7885 @ Mock
7986 private XWikiRequest request ;
8087
@@ -85,6 +92,9 @@ public void beforeEach()
8592 when (this .documentReferenceUserReferenceSerializer .serialize (USER_REFERENCE )).thenReturn (USER_DOCUMENT_REFERENCE );
8693
8794 this .oldcore .getXWikiContext ().setUserReference (USER_DOCUMENT_REFERENCE );
95+
96+ this .oldcore .getXWikiContext ().setRequest (new XWikiServletRequestStub .Builder ().
97+ setRequestParameters (Map .of ("form_token" , new String [] {"tokenvalue" })).build ());
8898 }
8999
90100 private String initAndRenderAction () throws XWikiException
@@ -141,7 +151,18 @@ void documentAuthorsWhenDocumentExist() throws XWikiException
141151 }
142152
143153 @ Test
144- void documentAuthorsWhenDocumentExistAndContentIsModified () throws XWikiException
154+ void documentAuthorsWhenDocumentExistAndContentIsModifiedAndInvalidValidCSRF () throws XWikiException
155+ {
156+ documentAuthorsWhenDocumentExistAndContentIsModified (false );
157+ }
158+
159+ @ Test
160+ void documentAuthorsWhenDocumentExistAndContentIsModifiedAndValidCSRF () throws XWikiException
161+ {
162+ documentAuthorsWhenDocumentExistAndContentIsModified (true );
163+ }
164+
165+ void documentAuthorsWhenDocumentExistAndContentIsModified (boolean validToken ) throws XWikiException
145166 {
146167 XWikiDocument document = this .oldcore .getSpyXWiki ().getDocument (new DocumentReference ("wiki" , "space" , "page" ),
147168 this .oldcore .getXWikiContext ());
@@ -158,6 +179,8 @@ void documentAuthorsWhenDocumentExistAndContentIsModified() throws XWikiExceptio
158179
159180 when (this .request .getParameter ("content" )).thenReturn ("modified content" );
160181
182+ when (this .csrf .isTokenValid ("tokenvalue" )).thenReturn (validToken );
183+
161184 initAndRenderAction ();
162185
163186 document = this .oldcore .getXWikiContext ().getDoc ();
@@ -166,5 +189,6 @@ void documentAuthorsWhenDocumentExistAndContentIsModified() throws XWikiExceptio
166189 assertSame (OTHERUSER_REFERENCE , document .getAuthors ().getCreator ());
167190 assertSame (OTHERUSER_REFERENCE , document .getAuthors ().getEffectiveMetadataAuthor ());
168191 assertSame (OTHERUSER_REFERENCE , document .getAuthors ().getOriginalMetadataAuthor ());
192+ assertEquals (!validToken , document .isRestricted ());
169193 }
170194}
0 commit comments