1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.mail.integration

File AuthenticatingIntegrationTest.java

 

Code metrics

0
49
5
1
230
154
5
0.1
9.8
5
1

Classes

Class Line # Actions
AuthenticatingIntegrationTest 102 49 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.mail.integration;
21   
22    import java.security.Security;
23    import java.util.Arrays;
24    import java.util.Collections;
25    import java.util.Properties;
26   
27    import javax.inject.Provider;
28    import javax.mail.BodyPart;
29    import javax.mail.Message.RecipientType;
30    import javax.mail.Multipart;
31    import javax.mail.Session;
32    import javax.mail.internet.InternetAddress;
33    import javax.mail.internet.MimeMessage;
34    import javax.mail.internet.MimeMultipart;
35   
36    import org.junit.After;
37    import org.junit.Before;
38    import org.junit.Rule;
39    import org.junit.Test;
40    import org.mockito.Mockito;
41    import org.xwiki.bridge.event.ApplicationReadyEvent;
42    import org.xwiki.component.phase.Disposable;
43    import org.xwiki.component.util.DefaultParameterizedType;
44    import org.xwiki.context.Execution;
45    import org.xwiki.context.ExecutionContext;
46    import org.xwiki.context.ExecutionContextManager;
47    import org.xwiki.environment.internal.EnvironmentConfiguration;
48    import org.xwiki.environment.internal.StandardEnvironment;
49    import org.xwiki.mail.MailSender;
50    import org.xwiki.mail.MailSenderConfiguration;
51    import org.xwiki.mail.MimeBodyPartFactory;
52    import org.xwiki.mail.XWikiAuthenticator;
53    import org.xwiki.mail.internal.DefaultMailSender;
54    import org.xwiki.mail.internal.FileSystemMailContentStore;
55    import org.xwiki.mail.internal.MemoryMailListener;
56    import org.xwiki.mail.internal.configuration.DefaultMailSenderConfiguration;
57    import org.xwiki.mail.internal.factory.attachment.AttachmentMimeBodyPartFactory;
58    import org.xwiki.mail.internal.factory.text.TextMimeBodyPartFactory;
59    import org.xwiki.mail.internal.thread.MailSenderInitializerListener;
60    import org.xwiki.mail.internal.thread.PrepareMailQueueManager;
61    import org.xwiki.mail.internal.thread.PrepareMailRunnable;
62    import org.xwiki.mail.internal.thread.SendMailQueueManager;
63    import org.xwiki.mail.internal.thread.SendMailRunnable;
64    import org.xwiki.mail.internal.thread.context.Copier;
65    import org.xwiki.model.ModelContext;
66    import org.xwiki.model.reference.WikiReference;
67    import org.xwiki.observation.EventListener;
68    import org.xwiki.test.annotation.BeforeComponent;
69    import org.xwiki.test.annotation.ComponentList;
70    import org.xwiki.test.mockito.MockitoComponentManagerRule;
71   
72    import com.icegreen.greenmail.junit.GreenMailRule;
73    import com.icegreen.greenmail.util.DummySSLSocketFactory;
74    import com.icegreen.greenmail.util.ServerSetupTest;
75    import com.xpn.xwiki.XWikiContext;
76   
77    import static org.junit.Assert.assertEquals;
78    import static org.mockito.Mockito.when;
79   
80    /**
81    * Integration tests to prove that mail sending is working fully end to end with the Java API when using an
82    * authenticating SMTP server that requires SSL.
83    *
84    * @version $Id: d8efc06663927055b60bbe4b340e5d500643ebce $
85    * @since 6.4M1
86    */
87    // @formatter:off
88    @ComponentList({
89    MailSenderInitializerListener.class,
90    TextMimeBodyPartFactory.class,
91    AttachmentMimeBodyPartFactory.class,
92    StandardEnvironment.class,
93    DefaultMailSender.class,
94    MemoryMailListener.class,
95    SendMailRunnable.class,
96    PrepareMailRunnable.class,
97    PrepareMailQueueManager.class,
98    SendMailQueueManager.class,
99    FileSystemMailContentStore.class
100    })
101    // @formatter:on
 
102    public class AuthenticatingIntegrationTest
103    {
104    // Required by GreenMail.
 
105  1 toggle static {
106  1 Security.setProperty("ssl.SocketFactory.provider", DummySSLSocketFactory.class.getName());
107    }
108   
109    @Rule
110    public GreenMailRule mail = new GreenMailRule(ServerSetupTest.SMTPS);
111   
112    @Rule
113    public MockitoComponentManagerRule componentManager = new MockitoComponentManagerRule();
114   
115    private MailSenderConfiguration configuration;
116   
117    private MimeBodyPartFactory<String> defaultBodyPartFactory;
118   
119    private MailSender sender;
120   
 
121  1 toggle @BeforeComponent
122    public void registerConfiguration() throws Exception
123    {
124  1 Properties properties = new Properties();
125  1 properties.setProperty("mail.smtp.starttls.enable", "true");
126   
127    // Required by GreenMail. When using XWiki with Gmail for example this is not required.
128  1 properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
129   
130  1 this.configuration =
131    new TestMailSenderConfiguration(this.mail.getSmtps().getPort(), "peter", "password", properties);
132  1 this.componentManager.registerComponent(MailSenderConfiguration.class, this.configuration);
133   
134    // Set the current wiki in the Context
135  1 ModelContext modelContext = this.componentManager.registerMockComponent(ModelContext.class);
136  1 when(modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("wiki"));
137   
138  1 Provider<XWikiContext> xwikiContextProvider =
139    this.componentManager.registerMockComponent(XWikiContext.TYPE_PROVIDER);
140  1 when(xwikiContextProvider.get()).thenReturn(Mockito.mock(XWikiContext.class));
141   
142  1 this.componentManager.registerMockComponent(ExecutionContextManager.class);
143  1 this.componentManager.registerMockComponent(Execution.class);
144   
145  1 this.componentManager.registerMockComponent(new DefaultParameterizedType(null, Copier.class,
146    ExecutionContext.class));
147   
148  1 EnvironmentConfiguration environmentConfiguration =
149    this.componentManager.registerMockComponent(EnvironmentConfiguration.class);
150  1 when(environmentConfiguration.getPermanentDirectoryPath()).thenReturn(System.getProperty("java.io.tmpdir"));
151    }
152   
 
153  1 toggle @Before
154    public void initialize() throws Exception
155    {
156    // Create a user in the SMTP server.
157  1 this.mail.setUser("peter@doe.com", "peter", "password");
158   
159  1 this.defaultBodyPartFactory =
160    this.componentManager.getInstance(new DefaultParameterizedType(null, MimeBodyPartFactory.class,
161    String.class));
162  1 this.sender = this.componentManager.getInstance(MailSender.class);
163   
164    // Simulate receiving the Application Ready Event to start the mail threads
165  1 MailSenderInitializerListener listener =
166    this.componentManager.getInstance(EventListener.class, MailSenderInitializerListener.LISTENER_NAME);
167  1 listener.onEvent(new ApplicationReadyEvent(), null, null);
168    }
169   
 
170  1 toggle @After
171    public void cleanUp() throws Exception
172    {
173    // Make sure we stop the Mail Sender thread after each test (since it's started automatically when looking
174    // up the MailSender component.
175  1 Disposable listener =
176    this.componentManager.getInstance(EventListener.class, MailSenderInitializerListener.LISTENER_NAME);
177  1 listener.dispose();
178    }
179   
 
180  1 toggle @Test
181    public void sendTextMail() throws Exception
182    {
183    // Set the EC
184  1 Execution execution = this.componentManager.getInstance(Execution.class);
185  1 ExecutionContext executionContext = new ExecutionContext();
186  1 XWikiContext xContext = new XWikiContext();
187  1 xContext.setWikiId("wiki");
188  1 executionContext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, xContext);
189  1 when(execution.getContext()).thenReturn(executionContext);
190   
191  1 Copier<ExecutionContext> executionContextCloner =
192    this.componentManager.getInstance(new DefaultParameterizedType(null, Copier.class, ExecutionContext.class));
193    // Just return the same execution context
194  1 when(executionContextCloner.copy(executionContext)).thenReturn(executionContext);
195   
196    // Step 1: Create a JavaMail Session
197  1 Properties properties = this.configuration.getAllProperties();
198  1 assertEquals("true", properties.getProperty(DefaultMailSenderConfiguration.JAVAMAIL_SMTP_AUTH));
199  1 Session session = Session.getInstance(properties, new XWikiAuthenticator(this.configuration));
200   
201    // Step 2: Create the Message to send
202  1 MimeMessage message = new MimeMessage(session);
203  1 message.setSubject("subject");
204  1 message.setRecipient(RecipientType.TO, new InternetAddress("john@doe.com"));
205   
206    // Step 3: Add the Message Body
207  1 Multipart multipart = new MimeMultipart("mixed");
208    // Add text in the body
209  1 multipart.addBodyPart(this.defaultBodyPartFactory.create("some text here",
210    Collections.<String, Object>singletonMap("mimetype", "text/plain")));
211  1 message.setContent(multipart);
212   
213    // Step 4: Send the mail
214  1 this.sender.sendAsynchronously(Arrays.asList(message), session, null);
215   
216    // Verify that the mail has been received (wait maximum 10 seconds).
217  1 this.mail.waitForIncomingEmail(10000L, 1);
218  1 MimeMessage[] messages = this.mail.getReceivedMessages();
219   
220  1 assertEquals(1, messages.length);
221  1 assertEquals("subject", messages[0].getHeader("Subject", null));
222  1 assertEquals("john@doe.com", messages[0].getHeader("To", null));
223   
224  1 assertEquals(1, ((MimeMultipart) messages[0].getContent()).getCount());
225   
226  1 BodyPart textBodyPart = ((MimeMultipart) messages[0].getContent()).getBodyPart(0);
227  1 assertEquals("text/plain", textBodyPart.getHeader("Content-Type")[0]);
228  1 assertEquals("some text here", textBodyPart.getContent());
229    }
230    }