Search in sources :

Example 46 with Context

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());
}
Also used : Context(com.enonic.xp.context.Context) WebException(com.enonic.xp.web.WebException) ContentNotFoundException(com.enonic.xp.content.ContentNotFoundException) ContentPath(com.enonic.xp.content.ContentPath) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Test(org.junit.jupiter.api.Test)

Example 47 with Context

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);
}
Also used : Context(com.enonic.xp.context.Context) User(com.enonic.xp.security.User) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Principal(java.security.Principal) Session(com.enonic.xp.session.Session) SessionMock(com.enonic.xp.session.SessionMock) Test(org.junit.jupiter.api.Test)

Example 48 with Context

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);
}
Also used : Context(com.enonic.xp.context.Context) User(com.enonic.xp.security.User) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Session(com.enonic.xp.session.Session) SessionMock(com.enonic.xp.session.SessionMock) Test(org.junit.jupiter.api.Test)

Example 49 with Context

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());
}
Also used : Context(com.enonic.xp.context.Context) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Test(org.junit.jupiter.api.Test)

Example 50 with Context

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);
}
Also used : Context(com.enonic.xp.context.Context)

Aggregations

Context (com.enonic.xp.context.Context)101 Test (org.junit.jupiter.api.Test)35 AuthenticationInfo (com.enonic.xp.security.auth.AuthenticationInfo)21 Node (com.enonic.xp.node.Node)16 InternalContext (com.enonic.xp.repo.impl.InternalContext)16 User (com.enonic.xp.security.User)13 Content (com.enonic.xp.content.Content)11 PropertyTree (com.enonic.xp.data.PropertyTree)11 PrincipalKey (com.enonic.xp.security.PrincipalKey)10 Branch (com.enonic.xp.branch.Branch)9 AbstractNodeTest (com.enonic.xp.repo.impl.node.AbstractNodeTest)9 ContextAccessor (com.enonic.xp.context.ContextAccessor)8 NodePath (com.enonic.xp.node.NodePath)8 ContextBuilder (com.enonic.xp.context.ContextBuilder)7 ContentId (com.enonic.xp.content.ContentId)6 CreateNodeParams (com.enonic.xp.node.CreateNodeParams)6 Repository (com.enonic.xp.repository.Repository)6 LogAuditLogParams (com.enonic.xp.audit.LogAuditLogParams)5 ProcessUpdateParams (com.enonic.xp.content.processor.ProcessUpdateParams)5 NodeComparison (com.enonic.xp.node.NodeComparison)5