Skip to content

Commit b2a9547

Browse files
committed
Send welcome email after activation
1 parent f6e46f7 commit b2a9547

8 files changed

Lines changed: 61 additions & 2 deletions

File tree

src/main/java/com/faforever/api/config/FafApiProperties.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ public static class Registration {
177177
private String activationUrlFormat;
178178
private String subject;
179179
private String activationMailTemplatePath;
180+
private String welcomeSubject;
180181
private String welcomeMailTemplatePath;
181182
}
182183

src/main/java/com/faforever/api/email/EmailService.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ public void sendActivationMail(String username, String email, String activationU
4646
);
4747
}
4848

49+
public void sendWelcomeToFafMail(String username, String email) throws IOException {
50+
final var mailBody = mailBodyBuilder.buildWelcomeToFafBody(username);
51+
52+
emailSender.sendMail(
53+
properties.getMail().getFromEmailAddress(),
54+
properties.getMail().getFromEmailName(),
55+
email,
56+
properties.getRegistration().getWelcomeSubject(),
57+
mailBody
58+
);
59+
}
60+
4961
public void sendPasswordResetMail(String username, String email, String passwordResetUrl) throws IOException {
5062
final var mailBody = mailBodyBuilder.buildPasswordResetBody(username, passwordResetUrl);
5163

src/main/java/com/faforever/api/email/MailBodyBuilder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ public String buildAccountActivationBody(String username, String activationUrl)
135135
));
136136
}
137137

138+
public String buildWelcomeToFafBody(String username) throws IOException {
139+
return populate(Template.WELCOME_TO_FAF, Map.of(
140+
"username", username
141+
));
142+
}
143+
138144
public String buildPasswordResetBody(String username, String passwordResetUrl) throws IOException {
139145
return populate(Template.PASSWORD_RESET, Map.of(
140146
"username", username,

src/main/java/com/faforever/api/user/UserService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.stereotype.Service;
2727
import org.springframework.transaction.annotation.Transactional;
2828

29+
import java.io.IOException;
2930
import java.nio.charset.StandardCharsets;
3031
import java.time.Duration;
3132
import java.util.ArrayList;
@@ -212,6 +213,13 @@ void activate(String registrationToken, String password, String ipAddress) {
212213

213214
broadcastUserChange(user);
214215
userActivationCounter.increment();
216+
217+
try {
218+
emailService.sendWelcomeToFafMail(username, email);
219+
} catch (IOException e) {
220+
log.error("Sending welcome email to {} failed, activation finished anyway.", username, e);
221+
// The welcome email is not critical, thus we swallow the exception if it fails
222+
}
215223
}
216224

217225
void changePassword(String currentPassword, String newPassword, User user) {

src/main/resources/config/application.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ faf-api:
6969
activation-url-format: ${ACTIVATION_URL_FORMAT:https://www.${FAF_DOMAIN}/account/activate?username=%s&token=%s}
7070
subject: ${REGISTRATION_EMAIL_SUBJECT:FAF user registration}
7171
activation-mail-template-path: ${ACCOUNT_ACTIVATION_MAIL_TEMPLATE_PATH:/config/mail/account-activation.html}
72+
welcome-subject: ${WELCOME_MAIL_SUBJECT:Welcome to FAF}
7273
welcome-mail-template-path: ${WELCOME_MAIL_TEMPLATE_PATH:/config/mail/welcome-to-faf.html}
7374
replay:
7475
download-url-format: ${REPLAY_DOWNLOAD_URL_FORMAT:https://replays.${FAF_DOMAIN}/%s}

src/test/java/com/faforever/api/email/EmailServiceTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ public void sendActivationMail() throws Exception{
8282
verify(emailSender).sendMail(FROM_EMAIL, FROM_NAME, EMAIL, SUBJECT, HTML_BODY);
8383
}
8484

85+
@Test
86+
public void sendWelcomeToFafMail() throws Exception{
87+
Registration registration = properties.getRegistration();
88+
registration.setWelcomeSubject(SUBJECT);
89+
90+
when(mailBodyBuilder.buildWelcomeToFafBody(USERNAME)).thenReturn(HTML_BODY);
91+
92+
instance.sendWelcomeToFafMail(USERNAME, EMAIL);
93+
94+
verify(emailSender).sendMail(FROM_EMAIL, FROM_NAME, EMAIL, SUBJECT, HTML_BODY);
95+
}
96+
8597
@Test
8698
public void sendPasswordResetMail() throws Exception {
8799
PasswordReset passwordReset = properties.getPasswordReset();

src/test/java/com/faforever/api/email/MailBodyBuilderTest.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,23 @@ public void initSucceeds() {
6868
}
6969

7070
@Test
71-
public void populateFailsOnMissingVariables() throws Exception {
71+
public void buildAccountActivationBodyFailsOnMissingVariables() throws Exception {
7272
writeTemplateFile("I forgot the variables {{and put in wrong ones}}");
7373

7474
var result = assertThrows(IllegalStateException.class, () ->instance.buildAccountActivationBody("junit", "someActionUrl"));
7575

7676
assertThat(result.getMessage(), startsWith("Template file for ACCOUNT_ACTIVATION is missing variables:"));
7777
}
7878

79+
@Test
80+
public void buildWelcomeToFafBodyFailsOnMissingVariables() throws Exception {
81+
writeTemplateFile("I forgot the variables {{and put in wrong ones}}");
82+
83+
var result = assertThrows(IllegalStateException.class, () ->instance.buildWelcomeToFafBody("junit"));
84+
85+
assertThat(result.getMessage(), startsWith("Template file for WELCOME_TO_FAF is missing variables:"));
86+
}
87+
7988
@Test
8089
public void buildAccountActivationBodySucceeds() throws Exception {
8190
writeTemplateFile("{{username}} {{activationUrl}}");
@@ -85,6 +94,15 @@ public void buildAccountActivationBodySucceeds() throws Exception {
8594
assertThat(result, is("junit someActionUrl"));
8695
}
8796

97+
@Test
98+
public void buildWelcomToFafBodySucceeds() throws Exception {
99+
writeTemplateFile("{{username}}");
100+
101+
var result = instance.buildWelcomeToFafBody("junit");
102+
103+
assertThat(result, is("junit"));
104+
}
105+
88106
@Test
89107
public void buildPasswordResetBodySucceeds() throws Exception {
90108
writeTemplateFile("{{username}} {{passwordResetUrl}}");

src/test/java/com/faforever/api/user/UserServiceTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public void registerUsernameReserved() {
180180
}
181181

182182
@Test
183-
public void activate() {
183+
public void activate() throws Exception{
184184
final String TEST_IP_ADDRESS = IP_ADDRESS;
185185

186186
when(fafTokenService.resolveToken(FafTokenType.REGISTRATION, TOKEN_VALUE)).thenReturn(Map.of(
@@ -203,6 +203,7 @@ public void activate() {
203203
assertThat(user.getRecentIpAddress(), is(TEST_IP_ADDRESS));
204204

205205
verify(eventPublisher).publishEvent(any(UserUpdatedEvent.class));
206+
verify(emailService).sendWelcomeToFafMail(TEST_USERNAME, TEST_CURRENT_EMAIL);
206207
}
207208

208209
@Test

0 commit comments

Comments
 (0)