Search in sources :

Example 36 with DocumentAccessBridge

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());
    }
}
Also used : VelocityEngine(org.xwiki.velocity.VelocityEngine) MessagingException(javax.mail.MessagingException) VelocityManager(org.xwiki.velocity.VelocityManager) VelocityContext(org.apache.velocity.VelocityContext) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) Test(org.junit.Test)

Example 37 with DocumentAccessBridge

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());
    }
}
Also used : MessagingException(javax.mail.MessagingException) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 38 with DocumentAccessBridge

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);
}
Also used : HashMap(java.util.HashMap) MessagingException(javax.mail.MessagingException) MimeBodyPartFactory(org.xwiki.mail.MimeBodyPartFactory) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachment(com.xpn.xwiki.api.Attachment) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) MessagingException(javax.mail.MessagingException) Test(org.junit.Test)

Example 39 with DocumentAccessBridge

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());
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) HashMap(java.util.HashMap) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) Properties(java.util.Properties) MimeMessage(javax.mail.internet.MimeMessage) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MimeMessageFactory(org.xwiki.mail.MimeMessageFactory) ComponentManager(org.xwiki.component.manager.ComponentManager) HashMap(java.util.HashMap) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference) Session(javax.mail.Session) Test(org.junit.Test)

Example 40 with DocumentAccessBridge

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;
}
Also used : Expectations(org.jmock.Expectations) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) Event(org.xwiki.eventstream.Event) DefaultEvent(org.xwiki.eventstream.internal.DefaultEvent)

Aggregations

DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)56 DocumentReference (org.xwiki.model.reference.DocumentReference)39 Test (org.junit.Test)33 Expectations (org.jmock.Expectations)17 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)15 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)11 Before (org.junit.Before)10 XDOM (org.xwiki.rendering.block.XDOM)10 Execution (org.xwiki.context.Execution)7 SpaceReference (org.xwiki.model.reference.SpaceReference)7 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)7 EntityReferenceResolver (org.xwiki.model.reference.EntityReferenceResolver)6 MacroBlock (org.xwiki.rendering.block.MacroBlock)6 DocumentResourceReference (org.xwiki.rendering.listener.reference.DocumentResourceReference)6 HashMap (java.util.HashMap)5 VelocityContext (org.apache.velocity.VelocityContext)5 Event (org.xwiki.eventstream.Event)5 DefaultEvent (org.xwiki.eventstream.internal.DefaultEvent)5 VelocityEngine (org.xwiki.velocity.VelocityEngine)5 VelocityManager (org.xwiki.velocity.VelocityManager)5