Skip to content

Commit

Permalink
XWIKI-21884: Uploading files during page creation step will mark XWik…
Browse files Browse the repository at this point in the history
…iGuest as creator

* adjust unit test to verify also creator
  • Loading branch information
oanalavinia committed Feb 14, 2024
1 parent 07170be commit 68072ac
Showing 1 changed file with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.xwiki.attachment.validation.AttachmentValidationException;
import org.xwiki.attachment.validation.AttachmentValidator;
import org.xwiki.model.reference.AttachmentReference;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.DocumentReferenceResolver;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.model.reference.EntityReferenceSerializer;
import org.xwiki.model.reference.WikiReference;
import org.xwiki.query.Query;
import org.xwiki.rest.internal.resources.AbstractAttachmentsResourceTest;
import org.xwiki.rest.model.jaxb.Attachment;
Expand All @@ -50,6 +52,8 @@
import org.xwiki.security.authorization.Right;
import org.xwiki.test.junit5.mockito.InjectMockComponents;
import org.xwiki.test.junit5.mockito.MockComponent;
import org.xwiki.user.UserReference;
import org.xwiki.user.UserReferenceResolver;

import com.xpn.xwiki.doc.XWikiAttachment;
import com.xpn.xwiki.doc.XWikiDocument;
Expand All @@ -75,6 +79,8 @@
@OldcoreTest
class AttachmentsResourceImplTest extends AbstractAttachmentsResourceTest
{
private static final DocumentReference USER_REFERENCE = new DocumentReference("test", "XWiki", "User");

@InjectMockComponents
AttachmentsResourceImpl attachmentsResource;

Expand All @@ -91,6 +97,17 @@ class AttachmentsResourceImplTest extends AbstractAttachmentsResourceTest
@MockComponent
private AttachmentValidator attachmentValidator;

@MockComponent
@Named("document")
private UserReferenceResolver<DocumentReference> documentReferenceUserReferenceResolver;

@MockComponent
@Named("compactwiki")
EntityReferenceSerializer<String> compactWikiEntityReferenceSerializer;

@Mock
private UserReference userReference;

@BeforeEach
@Override
public void setUp() throws Exception
Expand Down Expand Up @@ -159,6 +176,13 @@ void getAttachments() throws Exception
@Test
void createAttachment() throws Exception
{
this.xcontext.setMainXWiki("test");
this.xcontext.setWikiId("test");
this.xcontext.setUserReference(USER_REFERENCE);
when(this.documentReferenceUserReferenceResolver.resolve(USER_REFERENCE)).thenReturn(this.userReference);
when(this.compactWikiEntityReferenceSerializer.serialize(USER_REFERENCE, new WikiReference("test"))).thenReturn(
"XWiki.User");

DocumentReference documentReference = new DocumentReference("test", Arrays.asList("Path", "To"), "Page");
XWikiDocument cachedDocument = prepareXWikiDocument(documentReference, "test:Path.To.Page", true, true, false);

Expand All @@ -180,11 +204,12 @@ void createAttachment() throws Exception
// The cached document should not have been modified.
assertNull(cachedDocument.getAttachment("myBio.txt"));

XWikiAttachment xwikiAttachment =
this.xwiki.getDocument(documentReference, this.xcontext).getAttachment("myBio.txt");
XWikiDocument xwikiDocument = this.xwiki.getDocument(documentReference, this.xcontext);
XWikiAttachment xwikiAttachment = xwikiDocument.getAttachment("myBio.txt");
assertEquals("myBio.txt", xwikiAttachment.getFilename());
assertEquals("text/plain", xwikiAttachment.getMimeType());
assertEquals("blah", IOUtils.toString(xwikiAttachment.getContentInputStream(this.xcontext)));
assertEquals(this.userReference, xwikiDocument.getAuthors().getCreator());
}

@Test
Expand Down Expand Up @@ -277,8 +302,9 @@ private XWikiDocument prepareXWikiDocument(DocumentReference documentReference,
this.xwiki.saveDocument(document, this.xcontext);
}

when(this.oldCore.getMockRightService().hasAccessLevel("view", "XWiki.XWikiGuest", serializedDocumentReference,
this.xcontext)).thenReturn(hasView);
when(this.oldCore.getMockRightService()
.hasAccessLevel("view", this.xcontext.getUser(), serializedDocumentReference, this.xcontext)).thenReturn(
hasView);
when(this.authorization.hasAccess(Right.EDIT, documentReference)).thenReturn(hasEdit);

return this.xwiki.getDocument(documentReference, this.xcontext);
Expand Down

0 comments on commit 68072ac

Please sign in to comment.