This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
EmailStrategyTest.java
385 lines (316 loc) · 13.8 KB
/
EmailStrategyTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
* Copyright 2014, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.zanata.email;
import static javax.mail.Message.RecipientType.TO;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.Locale;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.zanata.common.ProjectType;
import org.zanata.i18n.Messages;
import org.zanata.i18n.MessagesFactory;
import org.zanata.webtrans.shared.model.ProjectIterationId;
/**
* @author Sean Flanigan <a href="mailto:sflaniga@redhat.com">sflaniga@redhat.com</a>
*/
public class EmailStrategyTest {
// use this if you want to see the real messages on stderr
private static final boolean DEBUG = false;
// context values needed for most/all templates:
Messages msgs = DEBUG ? new Messages() : new Messages() {
@Override
public String get(Object key) {
return "MSG:key=" + key;
}
@Override
public String format(String key, Object... args) {
return get(key) + ",args={" + Joiner.on(',').join(args) + "}";
}
};
String fromAddress = "zanata@example.com";
String fromName = msgs.get("jsf.Zanata");
String toName = "User Name[测试]";
String toAddress = "username@example.com";
String serverPath = "https://zanata.example.com";
InternetAddress toAddr;
InternetAddress[] toAddresses;
MessagesFactory msgsFactory = new MessagesFactory() {
@Override
public Messages getMessages(Locale locale) {
return msgs;
}
};
Session session = Session.getDefaultInstance(new Properties());
EmailBuilder.Context context = new EmailBuilder.Context() {
@Override
String getFromAddress() {
return fromAddress;
}
@Override
String getServerPath() {
return serverPath;
}
};
EmailBuilder builder = new EmailBuilder(session, context, msgsFactory);
MimeMessage message;
// context values needed for some templates:
String key = "123456";
String fromLoginName = "LOGIN_NAME[测试]";
String replyEmail = "REPLY_EMAIL[测试]";
String userSubject = "USER_SUBJECT[测试]";
String localeId = "LOCALE_ID";
String localeNativeName = "LOCALE_NAME[测试]";
String htmlMessage = "some <b>HTML</b>";
public EmailStrategyTest() throws UnsupportedEncodingException {
toAddr = new InternetAddress(toAddress, toName);
toAddresses = new InternetAddress[] { toAddr };
}
@BeforeMethod(alwaysRun = true)
private void beforeMethod() {
message = new MimeMessage(session);
}
private String extractHtmlPart(MimeMessage message)
throws IOException, MessagingException {
if (DEBUG) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
message.writeTo(os);
System.err.println(os.toString("UTF-8"));
}
Multipart multipart = (Multipart) message.getContent();
// one for html, one for text
assertThat(multipart.getCount()).isEqualTo(2);
// Text should appear first (because HTML is the preferred format)
BodyPart textPart = multipart.getBodyPart(0);
assertThat(textPart.getDataHandler().getContentType()).isEqualTo(
"text/plain; charset=UTF-8");
BodyPart htmlPart = multipart.getBodyPart(1);
assertThat(htmlPart.getDataHandler().getContentType()).isEqualTo(
"text/html; charset=UTF-8");
String htmlContent = (String) htmlPart.getContent();
return htmlContent;
}
private void checkFromAndTo(MimeMessage message) throws MessagingException {
assertThat(message.getFrom()).extracting("address").contains(
fromAddress);
assertThat(message.getFrom()).extracting("personal").contains(
fromName);
assertThat(message.getRecipients(TO)).extracting("address").contains(
toAddress);
assertThat(message.getRecipients(TO)).extracting("personal").contains(
toName);
}
private void checkGenericTemplate(String html) {
// a message from the generic email template:
assertThat(html).contains(msgs.get(
"jsf.email.GeneratedFromZanataServerAt"));
}
@Test
public void activation() throws Exception {
EmailStrategy strategy =
new ActivationEmailStrategy(key);
builder.buildMessage(message, strategy, toAddresses,
Lists.newArrayList("activation test"));
checkFromAndTo(message);
assertThat(message.getSubject()).isEqualTo(msgs.get(
"jsf.email.activation.Subject"));
String html = extractHtmlPart(message);
checkGenericTemplate(html);
assertThat(html).contains(msgs.get(
"jsf.email.activation.ClickLinkToActivateAccount"));
assertThat(html).contains(
serverPath + "/account/activate/123456");
}
@Test
public void contactAdmin() throws Exception {
EmailStrategy strategy =
new ContactAdminEmailStrategy(
fromLoginName, fromName, replyEmail, userSubject,
htmlMessage);
builder.buildMessage(message, strategy, toAddresses, Lists.newArrayList("contactAdmin test"));
checkFromAndTo(message);
assertThat(message.getSubject()).isEqualTo(msgs.format(
"jsf.email.admin.SubjectPrefix", fromLoginName) +
" " + userSubject);
String html = extractHtmlPart(message);
checkGenericTemplate(html);
assertThat(html).contains(msgs.format(
"jsf.email.admin.UserMessageIntro", fromName, fromLoginName));
assertThat(html).contains(
htmlMessage);
}
@Test
public void contactLanguageCoordinator() throws Exception {
EmailStrategy strategy =
new ContactLanguageCoordinatorEmailStrategy(
fromLoginName, fromName, replyEmail, userSubject,
localeId, localeNativeName, htmlMessage);
builder.buildMessage(message, strategy, toAddresses,
Lists.newArrayList("contactLanguageCoordinator test"));
checkFromAndTo(message);
assertThat(message.getSubject()).isEqualTo(msgs.format(
"jsf.email.coordinator.SubjectPrefix", localeId, fromLoginName) +
" " + userSubject);
String html = extractHtmlPart(message);
checkGenericTemplate(html);
assertThat(html).contains(msgs.format(
"jsf.email.coordinator.UserMessageIntro",
fromName, fromLoginName, localeId, localeNativeName));
assertThat(html).contains(
htmlMessage);
assertThat(html).contains(
serverPath + "/language/view/" + localeId);
}
@Test
public void emailValidation() throws Exception {
EmailStrategy strategy =
new EmailValidationEmailStrategy(key);
builder.buildMessage(message, strategy, toAddresses,
Lists.newArrayList("emailValidation test"));
checkFromAndTo(message);
assertThat(message.getSubject()).isEqualTo(msgs.get(
"jsf.email.accountchange.Subject"));
String html = extractHtmlPart(message);
checkGenericTemplate(html);
// a message from the template:
assertThat(html).contains(msgs.get(
"jsf.email.accountchange.ConfirmationLink"));
assertThat(html).contains(
serverPath + "/account/validate_email/123456");
}
@Test
public void passwordReset() throws Exception {
EmailStrategy strategy =
new PasswordResetEmailStrategy(key);
builder.buildMessage(message, strategy, toAddresses,
Lists.newArrayList("passwordReset test"));
checkFromAndTo(message);
assertThat(message.getSubject()).isEqualTo(msgs.get(
"jsf.email.passwordreset.Subject"));
String html = extractHtmlPart(message);
checkGenericTemplate(html);
assertThat(html).contains(msgs.get(
"jsf.email.passwordreset.FollowLinkToResetPassword"));
assertThat(html).contains(
serverPath + "/account/password_reset/123456");
}
@Test
public void requestRoleLanguage() throws Exception {
EmailStrategy strategy =
new RequestRoleLanguageEmailStrategy(
fromLoginName, fromName, replyEmail,
localeId, localeNativeName, htmlMessage,
true, true, true);
builder.buildMessage(message, strategy, toAddresses,
Lists.newArrayList("requestRoleLanguage test"));
checkFromAndTo(message);
assertThat(message.getSubject()).isEqualTo(
msgs.format("jsf.email.rolerequest.Subject", fromLoginName, localeId));
String html = extractHtmlPart(message);
checkGenericTemplate(html);
assertThat(html).contains(msgs.format(
"jsf.email.rolerequest.UserRequestingRole",
fromName, fromLoginName, localeId, localeNativeName));
assertThat(html).contains(
htmlMessage);
assertThat(html).contains(
serverPath + "/language/view/" + localeId);
}
@Test
public void requestToJoinLanguage() throws Exception {
EmailStrategy strategy =
new RequestToJoinLanguageEmailStrategy(
fromLoginName, fromName, replyEmail,
localeId, localeNativeName, htmlMessage,
true, true, true);
builder.buildMessage(message, strategy, toAddresses,
Lists.newArrayList("requestToJoinLanguage test"));
checkFromAndTo(message);
assertThat(message.getSubject()).isEqualTo(msgs.format(
"jsf.email.joinrequest.Subject", fromLoginName, localeId));
String html = extractHtmlPart(message);
checkGenericTemplate(html);
assertThat(html).contains(msgs.format(
"jsf.email.joinrequest.UserRequestingToJoin",
fromName, fromLoginName, localeId, localeNativeName));
assertThat(html).contains(
htmlMessage);
assertThat(html).contains(
serverPath + "/language/view/" + localeId);
}
@Test
public void requestToJoinVersionGroup() throws Exception {
String versionGroupName = "GROUP_NAME[测试]";
String versionGroupSlug = "GROUP_SLUG";
Collection<ProjectIterationId> projectIterIds = Lists.newArrayList(
new ProjectIterationId("PROJECT_SLUG", "ITERATION_SLUG",
ProjectType.File)
);
EmailStrategy strategy =
new RequestToJoinVersionGroupEmailStrategy(
fromLoginName, fromName, replyEmail,
versionGroupName, versionGroupSlug,
projectIterIds, htmlMessage);
builder.buildMessage(message, strategy, toAddresses,
Lists.newArrayList("requestToJoinVersionGroup test"));
checkFromAndTo(message);
assertThat(message.getSubject()).isEqualTo(msgs.format(
"jsf.email.JoinGroupRequest.Subject", versionGroupName));
String html = extractHtmlPart(message);
checkGenericTemplate(html);
assertThat(html).contains(msgs.format(
"jsf.email.joingrouprequest.RequestingToJoinGroup",
fromName, fromLoginName, versionGroupName));
assertThat(html).contains(
htmlMessage);
assertThat(html).contains(
serverPath + "/version-group/view/" + versionGroupSlug);
}
@Test
public void usernameChanged() throws Exception {
String newUsername = "NEW_USERNAME[测试]";
EmailStrategy strategy =
new UsernameChangedEmailStrategy(newUsername, true);
builder.buildMessage(message, strategy, toAddresses,
Lists.newArrayList("usernameChanged test"));
checkFromAndTo(message);
assertThat(message.getSubject()).isEqualTo(msgs.get(
"jsf.email.usernamechange.Subject"));
String html = extractHtmlPart(message);
checkGenericTemplate(html);
assertThat(html).contains(msgs.format(
"jsf.email.usernamechange.YourNewUsername", newUsername));
assertThat(html).contains(
serverPath + "/account/password_reset_request");
}
}