use of com.enonic.xp.context.Context in project xp by enonic.
the class PageHandlerTest method getContentExistsButInsufficientRights.
@Test
public void getContentExistsButInsufficientRights() {
final AuthenticationInfo authenticationInfo = AuthenticationInfo.create().user(User.ANONYMOUS).build();
final Context authenticatedContext = ContextBuilder.from(ContextAccessor.current()).authInfo(authenticationInfo).build();
final ContentPath path = ContentPath.from("/site/somepath/content");
when(this.contentService.getByPath(path)).thenThrow(new ContentNotFoundException(path, Branch.from("draft")));
when(this.contentService.contentExists(path)).thenReturn(true);
this.request.setContentPath(path);
final WebException e = assertThrows(WebException.class, () -> authenticatedContext.callWith(() -> this.handler.handle(this.request, PortalResponse.create().build(), null)));
assertEquals(HttpStatus.FORBIDDEN, e.getStatus());
assertEquals("You don't have permission to access [/site/somepath/content]", e.getMessage());
}
use of com.enonic.xp.context.Context in project xp by enonic.
the class IdProviderRequestWrapperTest method getUserPrincipal.
@Test
void getUserPrincipal() {
final User user = User.create().key(PrincipalKey.ofUser(IdProviderKey.createDefault(), "userId")).login("usr").build();
final AuthenticationInfo authenticationInfo = AuthenticationInfo.create().user(user).build();
final Context context = ContextBuilder.create().build();
final Session session = new SessionMock();
context.getLocalScope().setSession(session);
session.setAttribute(authenticationInfo);
final Principal principal = context.callWith(() -> new IdProviderRequestWrapper(request).getUserPrincipal());
assertEquals(principal, user);
verifyNoInteractions(request);
}
use of com.enonic.xp.context.Context in project xp by enonic.
the class IdProviderRequestWrapperTest method isUserInRole.
@Test
void isUserInRole() {
final User user = User.create().key(PrincipalKey.ofUser(IdProviderKey.createDefault(), "userId")).login("usr").build();
final AuthenticationInfo authenticationInfo = AuthenticationInfo.create().user(user).principals(RoleKeys.ADMIN).build();
final Context context = ContextBuilder.create().build();
final Session session = new SessionMock();
context.getLocalScope().setSession(session);
session.setAttribute(authenticationInfo);
final Boolean isAdmin = context.callWith(() -> new IdProviderRequestWrapper(request).isUserInRole(RoleKeys.ADMIN.getId()));
assertTrue(isAdmin);
verifyNoInteractions(request);
}
use of com.enonic.xp.context.Context in project xp by enonic.
the class WebExceptionTest method forbidden_403_for_authenticated.
@Test
void forbidden_403_for_authenticated() {
// for already authenticated users forbidden must not allow ID Provider to re-authenticate
final AuthenticationInfo authenticationInfo = AuthenticationInfo.create().user(User.ANONYMOUS).build();
final Context authenticatedContext = ContextBuilder.from(ContextAccessor.current()).authInfo(authenticationInfo).build();
final WebException webException = authenticatedContext.callWith(() -> WebException.forbidden("some message"));
assertEquals(HttpStatus.FORBIDDEN, webException.getStatus());
}
use of com.enonic.xp.context.Context in project xp by enonic.
the class ContentInitializer method doInitialize.
@Override
public void doInitialize() {
createAdminContext(ContentConstants.BRANCH_MASTER).runWith(() -> {
initializeRepository();
createDraftBranch();
});
final Context adminDraft = createAdminContext(ContentConstants.BRANCH_DRAFT);
adminDraft.runWith(this::initContentNode);
}
Aggregations