Skip to content

Commit

Permalink
Send welcome email after activation
Browse files Browse the repository at this point in the history
  • Loading branch information
Brutus5000 committed Feb 18, 2022
1 parent f6e46f7 commit b2a9547
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ public static class Registration {
private String activationUrlFormat;
private String subject;
private String activationMailTemplatePath;
private String welcomeSubject;
private String welcomeMailTemplatePath;
}

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/faforever/api/email/EmailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ public void sendActivationMail(String username, String email, String activationU
);
}

public void sendWelcomeToFafMail(String username, String email) throws IOException {
final var mailBody = mailBodyBuilder.buildWelcomeToFafBody(username);

emailSender.sendMail(
properties.getMail().getFromEmailAddress(),
properties.getMail().getFromEmailName(),
email,
properties.getRegistration().getWelcomeSubject(),
mailBody
);
}

public void sendPasswordResetMail(String username, String email, String passwordResetUrl) throws IOException {
final var mailBody = mailBodyBuilder.buildPasswordResetBody(username, passwordResetUrl);

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/faforever/api/email/MailBodyBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ public String buildAccountActivationBody(String username, String activationUrl)
));
}

public String buildWelcomeToFafBody(String username) throws IOException {
return populate(Template.WELCOME_TO_FAF, Map.of(
"username", username
));
}

public String buildPasswordResetBody(String username, String passwordResetUrl) throws IOException {
return populate(Template.PASSWORD_RESET, Map.of(
"username", username,
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/faforever/api/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.ArrayList;
Expand Down Expand Up @@ -212,6 +213,13 @@ void activate(String registrationToken, String password, String ipAddress) {

broadcastUserChange(user);
userActivationCounter.increment();

try {
emailService.sendWelcomeToFafMail(username, email);
} catch (IOException e) {
log.error("Sending welcome email to {} failed, activation finished anyway.", username, e);
// The welcome email is not critical, thus we swallow the exception if it fails
}
}

void changePassword(String currentPassword, String newPassword, User user) {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/config/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ faf-api:
activation-url-format: ${ACTIVATION_URL_FORMAT:https://www.${FAF_DOMAIN}/account/activate?username=%s&token=%s}
subject: ${REGISTRATION_EMAIL_SUBJECT:FAF user registration}
activation-mail-template-path: ${ACCOUNT_ACTIVATION_MAIL_TEMPLATE_PATH:/config/mail/account-activation.html}
welcome-subject: ${WELCOME_MAIL_SUBJECT:Welcome to FAF}
welcome-mail-template-path: ${WELCOME_MAIL_TEMPLATE_PATH:/config/mail/welcome-to-faf.html}
replay:
download-url-format: ${REPLAY_DOWNLOAD_URL_FORMAT:https://replays.${FAF_DOMAIN}/%s}
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/com/faforever/api/email/EmailServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ public void sendActivationMail() throws Exception{
verify(emailSender).sendMail(FROM_EMAIL, FROM_NAME, EMAIL, SUBJECT, HTML_BODY);
}

@Test
public void sendWelcomeToFafMail() throws Exception{
Registration registration = properties.getRegistration();
registration.setWelcomeSubject(SUBJECT);

when(mailBodyBuilder.buildWelcomeToFafBody(USERNAME)).thenReturn(HTML_BODY);

instance.sendWelcomeToFafMail(USERNAME, EMAIL);

verify(emailSender).sendMail(FROM_EMAIL, FROM_NAME, EMAIL, SUBJECT, HTML_BODY);
}

@Test
public void sendPasswordResetMail() throws Exception {
PasswordReset passwordReset = properties.getPasswordReset();
Expand Down
20 changes: 19 additions & 1 deletion src/test/java/com/faforever/api/email/MailBodyBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,23 @@ public void initSucceeds() {
}

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

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

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

@Test
public void buildWelcomeToFafBodyFailsOnMissingVariables() throws Exception {
writeTemplateFile("I forgot the variables {{and put in wrong ones}}");

var result = assertThrows(IllegalStateException.class, () ->instance.buildWelcomeToFafBody("junit"));

assertThat(result.getMessage(), startsWith("Template file for WELCOME_TO_FAF is missing variables:"));
}

@Test
public void buildAccountActivationBodySucceeds() throws Exception {
writeTemplateFile("{{username}} {{activationUrl}}");
Expand All @@ -85,6 +94,15 @@ public void buildAccountActivationBodySucceeds() throws Exception {
assertThat(result, is("junit someActionUrl"));
}

@Test
public void buildWelcomToFafBodySucceeds() throws Exception {
writeTemplateFile("{{username}}");

var result = instance.buildWelcomeToFafBody("junit");

assertThat(result, is("junit"));
}

@Test
public void buildPasswordResetBodySucceeds() throws Exception {
writeTemplateFile("{{username}} {{passwordResetUrl}}");
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/faforever/api/user/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public void registerUsernameReserved() {
}

@Test
public void activate() {
public void activate() throws Exception{
final String TEST_IP_ADDRESS = IP_ADDRESS;

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

verify(eventPublisher).publishEvent(any(UserUpdatedEvent.class));
verify(emailService).sendWelcomeToFafMail(TEST_USERNAME, TEST_CURRENT_EMAIL);
}

@Test
Expand Down

0 comments on commit b2a9547

Please sign in to comment.