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));
}
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));
}
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());
}
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());
}
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);
}
Aggregations