Skip to content

Commit

Permalink
Global users aren't able to cast the vote on subwiki's poll xwikisas#111
Browse files Browse the repository at this point in the history


* Refactoring
  • Loading branch information
KebabRonin committed Feb 21, 2025
1 parent b39cb9d commit 5966f72
Showing 1 changed file with 12 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,39 +105,31 @@ public void configure() throws Exception
@Test
void saveXPollAnswersWithEditRightTest() throws XWikiRestException
{
when(this.contextualAuthorizationManager.hasAccess(eq(Right.VIEW), any(DocumentReference.class))).thenReturn(true);
when(this.contextualAuthorizationManager.hasAccess(eq(Right.EDIT), any(DocumentReference.class))).thenReturn(true);
when(this.xWikiContext.getUserReference()).thenReturn(userDocumentReference);
setMockUserRights(true, true, userDocumentReference);
Response response = this.resource.vote("wiki", "space", "page", new Vote());
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
}

@Test
void saveXPollAnswersWithoutEditRightTest() throws XWikiRestException
{
when(this.contextualAuthorizationManager.hasAccess(eq(Right.VIEW), any(DocumentReference.class))).thenReturn(true);
when(this.contextualAuthorizationManager.hasAccess(eq(Right.EDIT), any(DocumentReference.class))).thenReturn(false);
when(this.xWikiContext.getUserReference()).thenReturn(userDocumentReference);
setMockUserRights(true, false, userDocumentReference);
Response response = this.resource.vote("wiki", "space", "page", new Vote());
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
}

@Test
void saveXPollAnswersWithoutViewRightTest() throws XWikiRestException
{
when(this.contextualAuthorizationManager.hasAccess(eq(Right.VIEW), any(DocumentReference.class))).thenReturn(false);
when(this.contextualAuthorizationManager.hasAccess(eq(Right.EDIT), any(DocumentReference.class))).thenReturn(false);
when(this.xWikiContext.getUserReference()).thenReturn(userDocumentReference);
setMockUserRights(false, false, userDocumentReference);
Response response = this.resource.vote("wiki", "space", "page", new Vote());
assertEquals(Response.Status.FORBIDDEN.getStatusCode(), response.getStatus());
}

@Test
void saveXPollAnswersLoggedOutTest() throws XWikiRestException
{
when(this.contextualAuthorizationManager.hasAccess(eq(Right.VIEW), any(DocumentReference.class))).thenReturn(true);
when(this.contextualAuthorizationManager.hasAccess(eq(Right.EDIT), any(DocumentReference.class))).thenReturn(false);
when(this.xWikiContext.getUserReference()).thenReturn(null);
setMockUserRights(true, false, null);
Response response = this.resource.vote("wiki", "space", "page", new Vote());
assertEquals(Response.Status.FORBIDDEN.getStatusCode(), response.getStatus());
}
Expand All @@ -146,8 +138,7 @@ void saveXPollAnswersLoggedOutTest() throws XWikiRestException
void saveXPollButManagerThrowsException() throws XPollException, XWikiRestException
{
DocumentReference docRef = new DocumentReference("xwiki", "Main", "WebHome");
when(this.xWikiContext.getUserReference()).thenReturn(userDocumentReference);
when(this.contextualAuthorizationManager.hasAccess(Right.VIEW, docRef)).thenReturn(true);
setMockUserRights(true, false, userDocumentReference);
when(this.serializer.serialize(null, new WikiReference("wiki"))).thenReturn("userIdentifier");

Vote vote = new Vote();
Expand All @@ -157,4 +148,11 @@ void saveXPollButManagerThrowsException() throws XPollException, XWikiRestExcept
Response response = resource.vote("xwiki", "Main", "WebHome", vote);
assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
}

private void setMockUserRights(boolean viewRight, boolean editRight, DocumentReference userReference)
{
when(this.contextualAuthorizationManager.hasAccess(eq(Right.VIEW), any(DocumentReference.class))).thenReturn(viewRight);
when(this.contextualAuthorizationManager.hasAccess(eq(Right.EDIT), any(DocumentReference.class))).thenReturn(editRight);
when(this.xWikiContext.getUserReference()).thenReturn(userReference);
}
}

0 comments on commit 5966f72

Please sign in to comment.