Search in sources :

Example 76 with Context

use of com.enonic.xp.context.Context in project xp by enonic.

the class RepositoryServiceImplTest method update_attachment.

@Test
void update_attachment() throws Exception {
    final String repoId = "repo-with-attachment";
    doCreateRepo(repoId);
    final BinaryReference binaryRef = BinaryReference.from("image1.jpg");
    ByteSource binarySource = ByteSource.wrap("this-is-the-binary-data-for-image1".getBytes());
    Context mockCurrentContext = ContextBuilder.create().branch("master").repositoryId(repoId).authInfo(REPO_TEST_DEFAULT_USER_AUTHINFO).build();
    PropertyTree data = new PropertyTree();
    data.setBinaryReference("someIcon", binaryRef);
    mockCurrentContext.runWith(() -> repositoryService.updateRepository(UpdateRepositoryParams.create().repositoryId(RepositoryId.from(repoId)).editor(edit -> {
        edit.data = data;
        edit.binaryAttachments = ImmutableList.of(new BinaryAttachment(binaryRef, binarySource));
    }).build()));
    createAdminContext().runWith(() -> {
        repositoryService.invalidateAll();
    });
    ByteSource persistedAttachment = mockCurrentContext.callWith(() -> repositoryService.getBinary(RepositoryId.from(repoId), BinaryReference.from("image1.jpg")));
    assertTrue(binarySource.contentEquals(persistedAttachment));
}
Also used : Context(com.enonic.xp.context.Context) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) IdProviderKey(com.enonic.xp.security.IdProviderKey) CreateNodeParams(com.enonic.xp.node.CreateNodeParams) DeleteRepositoryParams(com.enonic.xp.repository.DeleteRepositoryParams) Node(com.enonic.xp.node.Node) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Branch(com.enonic.xp.branch.Branch) NodeHelper(com.enonic.xp.repo.impl.node.NodeHelper) RepositoryId(com.enonic.xp.repository.RepositoryId) ImmutableList(com.google.common.collect.ImmutableList) CreateBranchParams(com.enonic.xp.repository.CreateBranchParams) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ContextAccessor(com.enonic.xp.context.ContextAccessor) ContextBuilder(com.enonic.xp.context.ContextBuilder) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ByteSource(com.google.common.io.ByteSource) SystemConstants(com.enonic.xp.security.SystemConstants) PropertyTree(com.enonic.xp.data.PropertyTree) BinaryReference(com.enonic.xp.util.BinaryReference) DeleteBranchParams(com.enonic.xp.repository.DeleteBranchParams) User(com.enonic.xp.security.User) BinaryAttachment(com.enonic.xp.node.BinaryAttachment) NodePath(com.enonic.xp.node.NodePath) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) AccessControlList(com.enonic.xp.security.acl.AccessControlList) CreateRepositoryParams(com.enonic.xp.repository.CreateRepositoryParams) UpdateRepositoryParams(com.enonic.xp.repository.UpdateRepositoryParams) Test(org.junit.jupiter.api.Test) PrincipalKey(com.enonic.xp.security.PrincipalKey) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) RoleKeys(com.enonic.xp.security.RoleKeys) Context(com.enonic.xp.context.Context) Assertions.assertDoesNotThrow(org.junit.jupiter.api.Assertions.assertDoesNotThrow) AccessControlEntry(com.enonic.xp.security.acl.AccessControlEntry) Repository(com.enonic.xp.repository.Repository) PropertyTree(com.enonic.xp.data.PropertyTree) ByteSource(com.google.common.io.ByteSource) BinaryReference(com.enonic.xp.util.BinaryReference) BinaryAttachment(com.enonic.xp.node.BinaryAttachment) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest)

Example 77 with Context

use of com.enonic.xp.context.Context in project xp by enonic.

the class ScheduleAuditLogSupportImpl method create.

@Override
public void create(final CreateScheduledJobParams params, final ScheduledJob job) {
    final Context context = scheduleContext();
    executor.execute(() -> doCreate(params, job, context));
}
Also used : Context(com.enonic.xp.context.Context)

Example 78 with Context

use of com.enonic.xp.context.Context in project xp by enonic.

the class SchedulerServiceImplTest method createWithoutUser.

@Test
void createWithoutUser() {
    final ScheduledJobName name = ScheduledJobName.from("test");
    final CronCalendar calendar = calendarService.cron("* * * * *", TimeZone.getDefault());
    final CreateScheduledJobParams params = CreateScheduledJobParams.create().name(name).calendar(calendar).descriptor(DescriptorKey.from(ApplicationKey.from("com.enonic.app.features"), "landing")).config(new PropertyTree()).build();
    Context context = ContextAccessor.current();
    final AuthenticationInfo authenticationInfo = AuthenticationInfo.copyOf(AuthenticationInfo.unAuthenticated()).user(null).principals(context.getAuthInfo().getPrincipals()).principals(RoleKeys.ADMIN).build();
    context = ContextBuilder.from(context).authInfo(authenticationInfo).build();
    final ScheduledJob scheduledJob = context.callWith(() -> schedulerService.create(params));
    assertEquals(User.ANONYMOUS.getKey(), scheduledJob.getCreator());
    assertEquals(User.ANONYMOUS.getKey(), scheduledJob.getModifier());
}
Also used : CreateScheduledJobParams(com.enonic.xp.scheduler.CreateScheduledJobParams) Context(com.enonic.xp.context.Context) CronCalendar(com.enonic.xp.scheduler.CronCalendar) ScheduledJobName(com.enonic.xp.scheduler.ScheduledJobName) PropertyTree(com.enonic.xp.data.PropertyTree) ScheduledJob(com.enonic.xp.scheduler.ScheduledJob) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest)

Example 79 with Context

use of com.enonic.xp.context.Context in project xp by enonic.

the class ContextMapperTest method test.

@Test
public void test() {
    User user = User.create().login(PrincipalKey.ofSuperUser().getId()).displayName("Super User").key(PrincipalKey.ofSuperUser()).build();
    AuthenticationInfo authInfo = AuthenticationInfo.create().user(user).principals(RoleKeys.ADMIN, RoleKeys.EVERYONE).build();
    Context context = ContextBuilder.create().repositoryId(RepositoryId.from("repository.id")).branch(Branch.create().value("master").build()).authInfo(authInfo).attribute("attrAsString", "value").attribute("attrAsInteger", Integer.MAX_VALUE).attribute("attrAsLong", Long.MIN_VALUE).attribute("attrAsBoolean", true).attribute("authInfoDetails", authInfo).attribute("testMapper", new TestMapper()).build();
    context.getLocalScope().setAttribute("attrAsString", "localValue");
    context.getLocalScope().setAttribute("attr1", "localValue");
    context.getLocalScope().setSession(new SessionMock());
    context.getLocalScope().getSession().setAttribute("attrAsString", "sessionValue");
    context.getLocalScope().getSession().setAttribute("attr2", "sessionValue");
    JsonMapGenerator generator = new JsonMapGenerator();
    new ContextMapper(context).serialize(generator);
    JsonNode actualJson = (JsonNode) generator.getRoot();
    JsonNode attributes = actualJson.get("attributes");
    assertNull(attributes.get("authInfoDetails"));
    assertNull(attributes.get(Branch.class.getName()));
    assertNull(attributes.get(RepositoryId.class.getName()));
    assertNull(attributes.get(AuthenticationInfo.class.getName()));
    assertEquals("value", attributes.get("attrAsString").asText());
    assertEquals(Integer.MAX_VALUE, attributes.get("attrAsInteger").asInt());
    assertTrue(attributes.get("attrAsBoolean").asBoolean());
    assertEquals(Long.MIN_VALUE, attributes.get("attrAsLong").asLong());
    assertNotNull(attributes.get("testMapper"));
    assertEquals("localValue", attributes.get("attr1").asText());
    assertEquals("sessionValue", attributes.get("attr2").asText());
}
Also used : Context(com.enonic.xp.context.Context) User(com.enonic.xp.security.User) JsonMapGenerator(com.enonic.xp.script.serializer.JsonMapGenerator) JsonNode(com.fasterxml.jackson.databind.JsonNode) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) SessionMock(com.enonic.xp.session.SessionMock) Test(org.junit.jupiter.api.Test)

Example 80 with Context

use of com.enonic.xp.context.Context in project xp by enonic.

the class PortalUrlServiceImpl method runWithAdminRole.

private <T> T runWithAdminRole(final Callable<T> callable) {
    final Context context = ContextAccessor.current();
    final AuthenticationInfo authenticationInfo = AuthenticationInfo.copyOf(context.getAuthInfo()).principals(RoleKeys.ADMIN).build();
    return ContextBuilder.from(context).authInfo(authenticationInfo).build().callWith(callable);
}
Also used : Context(com.enonic.xp.context.Context) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo)

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