use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.
the class DefaultMailTemplateManagerTest method evaluateWhenVelocityError.
@Test
public void evaluateWhenVelocityError() throws Exception {
DocumentAccessBridge documentBridge = this.mocker.getInstance(DocumentAccessBridge.class);
DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
when(documentBridge.getProperty(same(documentReference), any(), anyInt(), eq("html"))).thenReturn("Hello <b>${name}</b> <br />${email}");
VelocityEngine velocityEngine = mock(VelocityEngine.class);
VelocityManager velocityManager = this.mocker.getInstance(VelocityManager.class);
when(velocityManager.getVelocityEngine()).thenReturn(velocityEngine);
when(velocityEvaluator.evaluateVelocity(eq("Hello <b>${name}</b> <br />${email}"), any(), any(VelocityContext.class))).thenThrow(new XWikiException(0, 0, "Error"));
try {
this.mocker.getComponentUnderTest().evaluate(documentReference, "html", Collections.<String, Object>emptyMap());
fail("Should have thrown an exception here!");
} catch (MessagingException expected) {
assertEquals("Failed to evaluate property [html] for Document [wiki:space.page] and locale [null]", expected.getMessage());
}
}
use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.
the class SecureMailTemplateManagerTest method evaluateWhenNotAuthorized.
@Test
public void evaluateWhenNotAuthorized() throws Exception {
DocumentReference reference = new DocumentReference("wiki", "space", "page");
DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
when(dab.getCurrentUserReference()).thenReturn(new DocumentReference("userwiki", "userspace", "userpage"));
try {
this.mocker.getComponentUnderTest().evaluate(reference, "property", Collections.<String, Object>emptyMap());
fail("Should have thrown an exception");
} catch (MessagingException expected) {
assertEquals("Current user [userwiki:userspace.userpage] has no permission to view Mail Template " + "Document [wiki:space.page]", expected.getMessage());
}
}
use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.
the class TemplateMimeBodyPartFactoryTest method createWithAttachmentAndTemplateAttachmentsWhenError.
@Test
public void createWithAttachmentAndTemplateAttachmentsWhenError() throws Exception {
MimeBodyPartFactory<String> htmlMimeBodyPartFactory = this.mocker.getInstance(new DefaultParameterizedType(null, MimeBodyPartFactory.class, String.class), "text/html");
Attachment attachment1 = mock(Attachment.class, "attachment1");
Map<String, Object> bodyPartParameters = new HashMap<>();
bodyPartParameters.put("velocityVariables", new HashMap<String, String>());
bodyPartParameters.put("attachments", Collections.singletonList(attachment1));
bodyPartParameters.put("includeTemplateAttachments", true);
// Mock the retrieval and conversion of attachments from the Template document
DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
when(dab.getDocumentInstance(this.documentReference)).thenThrow(new Exception("error"));
try {
this.mocker.getComponentUnderTest().create(this.documentReference, bodyPartParameters);
fail("Should have thrown an exception here");
} catch (MessagingException expected) {
assertEquals("Failed to include attachments from the Mail Template [wiki:space.page]", expected.getMessage());
}
verifyNoMoreInteractions(htmlMimeBodyPartFactory);
}
use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.
the class UsersMimeMessageIteratorTest method createMessage.
@Test
public void createMessage() throws Exception {
DocumentReference userReference1 = new DocumentReference("xwiki", "XWiki", "JohnDoe");
DocumentReference userReference2 = new DocumentReference("xwiki", "XWiki", "JaneDoe");
DocumentReference userReference3 = new DocumentReference("xwiki", "XWiki", "JonnieDoe");
List<DocumentReference> userReferences = Arrays.asList(userReference1, userReference2, userReference3);
Session session = Session.getInstance(new Properties());
MimeMessageFactory factory = new MimeMessageFactory() {
@Override
public MimeMessage createMessage(Object source, Map parameters) throws MessagingException {
return new ExtendedMimeMessage();
}
};
Map<String, Object> parameters = new HashMap<>();
parameters.put("parameters", Collections.EMPTY_MAP);
parameters.put("session", session);
DocumentAccessBridge accessBridge = mock(DocumentAccessBridge.class);
when(accessBridge.getProperty(eq(userReference1), any(DocumentReference.class), eq("email"))).thenReturn("john@doe.com");
when(accessBridge.getProperty(eq(userReference2), any(DocumentReference.class), eq("email"))).thenReturn("jane@doe.com");
when(accessBridge.getProperty(eq(userReference3), any(DocumentReference.class), eq("email"))).thenReturn("jannie@doe.com");
ComponentManager componentManager = mock(ComponentManager.class);
when(componentManager.getInstance(eq(DocumentAccessBridge.class))).thenReturn(accessBridge);
UsersMimeMessageIterator iterator = new UsersMimeMessageIterator(userReferences, factory, parameters, componentManager);
assertTrue(iterator.hasNext());
MimeMessage message1 = iterator.next();
assertArrayEquals(message1.getRecipients(Message.RecipientType.TO), InternetAddress.parse("john@doe.com"));
assertTrue(iterator.hasNext());
MimeMessage message2 = iterator.next();
assertArrayEquals(message2.getRecipients(Message.RecipientType.TO), InternetAddress.parse("jane@doe.com"));
assertTrue(iterator.hasNext());
MimeMessage message3 = iterator.next();
assertArrayEquals(message3.getRecipients(Message.RecipientType.TO), InternetAddress.parse("jannie@doe.com"));
assertFalse(iterator.hasNext());
}
use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.
the class MessageStreamTest method setupForPublicMessage.
private Event setupForPublicMessage() throws Exception {
final Event e = setupForNewMessage();
final DocumentAccessBridge mockBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
final EntityReferenceSerializer<String> mockSerializer = getComponentManager().getInstance(EntityReferenceSerializer.TYPE_STRING);
getMockery().checking(new Expectations() {
{
exactly(1).of(mockBridge).getCurrentUserReference();
will(returnValue(MessageStreamTest.this.currentUser));
exactly(1).of(mockSerializer).serialize(MessageStreamTest.this.currentUser);
will(returnValue("wiki:XWiki.JohnDoe"));
}
});
return e;
}
Aggregations