mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-11-09 20:21:02 +01:00
#960 Send message to player if email could not be sent
This commit is contained in:
parent
7054c087f1
commit
5a4e827893
@ -84,19 +84,22 @@ public class RecoverEmailCommand extends PlayerCommand {
|
|||||||
|
|
||||||
private void createAndSendRecoveryCode(Player player, String email) {
|
private void createAndSendRecoveryCode(Player player, String email) {
|
||||||
String recoveryCode = recoveryCodeService.generateCode(player.getName());
|
String recoveryCode = recoveryCodeService.generateCode(player.getName());
|
||||||
sendMailSsl.sendRecoveryCode(player.getName(), email, recoveryCode);
|
boolean couldSendMail = sendMailSsl.sendRecoveryCode(player.getName(), email, recoveryCode);
|
||||||
|
if (couldSendMail) {
|
||||||
commonService.send(player, MessageKey.RECOVERY_CODE_SENT);
|
commonService.send(player, MessageKey.RECOVERY_CODE_SENT);
|
||||||
|
} else {
|
||||||
|
commonService.send(player, MessageKey.EMAIL_SEND_FAILURE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processRecoveryCode(Player player, String code, String email) {
|
private void processRecoveryCode(Player player, String code, String email) {
|
||||||
final String name = player.getName();
|
final String name = player.getName();
|
||||||
if (!recoveryCodeService.isCodeValid(name, code)) {
|
if (recoveryCodeService.isCodeValid(name, code)) {
|
||||||
commonService.send(player, MessageKey.INCORRECT_RECOVERY_CODE);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
generateAndSendNewPassword(player, email);
|
generateAndSendNewPassword(player, email);
|
||||||
recoveryCodeService.removeCode(name);
|
recoveryCodeService.removeCode(name);
|
||||||
|
} else {
|
||||||
|
commonService.send(player, MessageKey.INCORRECT_RECOVERY_CODE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void generateAndSendNewPassword(Player player, String email) {
|
private void generateAndSendNewPassword(Player player, String email) {
|
||||||
@ -105,7 +108,11 @@ public class RecoverEmailCommand extends PlayerCommand {
|
|||||||
HashedPassword hashNew = passwordSecurity.computeHash(thePass, name);
|
HashedPassword hashNew = passwordSecurity.computeHash(thePass, name);
|
||||||
|
|
||||||
dataSource.updatePassword(name, hashNew);
|
dataSource.updatePassword(name, hashNew);
|
||||||
sendMailSsl.sendPasswordMail(name, email, thePass);
|
boolean couldSendMail = sendMailSsl.sendPasswordMail(name, email, thePass);
|
||||||
|
if (couldSendMail) {
|
||||||
commonService.send(player, MessageKey.RECOVERY_EMAIL_SENT_MESSAGE);
|
commonService.send(player, MessageKey.RECOVERY_EMAIL_SENT_MESSAGE);
|
||||||
|
} else {
|
||||||
|
commonService.send(player, MessageKey.EMAIL_SEND_FAILURE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package fr.xephi.authme.mail;
|
package fr.xephi.authme.mail;
|
||||||
|
|
||||||
import fr.xephi.authme.AuthMe;
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
import fr.xephi.authme.ConsoleLogger;
|
import fr.xephi.authme.ConsoleLogger;
|
||||||
import fr.xephi.authme.service.BukkitService;
|
import fr.xephi.authme.initialization.DataFolder;
|
||||||
import fr.xephi.authme.settings.Settings;
|
import fr.xephi.authme.settings.Settings;
|
||||||
import fr.xephi.authme.settings.properties.EmailSettings;
|
import fr.xephi.authme.settings.properties.EmailSettings;
|
||||||
import fr.xephi.authme.settings.properties.SecuritySettings;
|
import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||||
@ -11,6 +11,7 @@ import fr.xephi.authme.util.StringUtils;
|
|||||||
import org.apache.commons.mail.EmailConstants;
|
import org.apache.commons.mail.EmailConstants;
|
||||||
import org.apache.commons.mail.EmailException;
|
import org.apache.commons.mail.EmailException;
|
||||||
import org.apache.commons.mail.HtmlEmail;
|
import org.apache.commons.mail.HtmlEmail;
|
||||||
|
import org.bukkit.Server;
|
||||||
|
|
||||||
import javax.activation.CommandMap;
|
import javax.activation.CommandMap;
|
||||||
import javax.activation.DataSource;
|
import javax.activation.DataSource;
|
||||||
@ -29,18 +30,19 @@ import static fr.xephi.authme.settings.properties.EmailSettings.MAIL_PASSWORD;
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Xephi59
|
* Sends emails to players on behalf of the server.
|
||||||
*/
|
*/
|
||||||
public class SendMailSSL {
|
public class SendMailSSL {
|
||||||
|
|
||||||
@Inject
|
private final File dataFolder;
|
||||||
private AuthMe plugin;
|
private final String serverName;
|
||||||
@Inject
|
private final Settings settings;
|
||||||
private Settings settings;
|
|
||||||
@Inject
|
|
||||||
private BukkitService bukkitService;
|
|
||||||
|
|
||||||
SendMailSSL() {
|
@Inject
|
||||||
|
SendMailSSL(@DataFolder File dataFolder, Server server, Settings settings) {
|
||||||
|
this.dataFolder = dataFolder;
|
||||||
|
this.serverName = server.getServerName();
|
||||||
|
this.settings = settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -59,62 +61,57 @@ public class SendMailSSL {
|
|||||||
* @param name the name of the player
|
* @param name the name of the player
|
||||||
* @param mailAddress the player's email
|
* @param mailAddress the player's email
|
||||||
* @param newPass the new password
|
* @param newPass the new password
|
||||||
|
* @return true if email could be sent, false otherwise
|
||||||
*/
|
*/
|
||||||
public void sendPasswordMail(String name, String mailAddress, String newPass) {
|
public boolean sendPasswordMail(String name, String mailAddress, String newPass) {
|
||||||
if (!hasAllInformation()) {
|
if (!hasAllInformation()) {
|
||||||
ConsoleLogger.warning("Cannot perform email registration: not all email settings are complete");
|
ConsoleLogger.warning("Cannot perform email registration: not all email settings are complete");
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
final String mailText = replaceTagsForPasswordMail(settings.getPasswordEmailMessage(), name, newPass);
|
|
||||||
bukkitService.runTaskAsynchronously(new Runnable() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
HtmlEmail email;
|
HtmlEmail email;
|
||||||
try {
|
try {
|
||||||
email = initializeMail(mailAddress);
|
email = initializeMail(mailAddress);
|
||||||
} catch (EmailException e) {
|
} catch (EmailException e) {
|
||||||
ConsoleLogger.logException("Failed to create email with the given settings:", e);
|
ConsoleLogger.logException("Failed to create email with the given settings:", e);
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
String content = mailText;
|
String mailText = replaceTagsForPasswordMail(settings.getPasswordEmailMessage(), name, newPass);
|
||||||
// Generate an image?
|
// Generate an image?
|
||||||
File file = null;
|
File file = null;
|
||||||
if (settings.getProperty(EmailSettings.PASSWORD_AS_IMAGE)) {
|
if (settings.getProperty(EmailSettings.PASSWORD_AS_IMAGE)) {
|
||||||
try {
|
try {
|
||||||
file = generateImage(name, plugin, newPass);
|
file = generateImage(name, newPass);
|
||||||
content = embedImageIntoEmailContent(file, email, content);
|
mailText = embedImageIntoEmailContent(file, email, mailText);
|
||||||
} catch (IOException | EmailException e) {
|
} catch (IOException | EmailException e) {
|
||||||
ConsoleLogger.logException(
|
ConsoleLogger.logException(
|
||||||
"Unable to send new password as image for email " + mailAddress + ":", e);
|
"Unable to send new password as image for email " + mailAddress + ":", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sendEmail(content, email);
|
boolean couldSendEmail = sendEmail(mailText, email);
|
||||||
FileUtils.delete(file);
|
FileUtils.delete(file);
|
||||||
}
|
return couldSendEmail;
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendRecoveryCode(String name, String email, String code) {
|
public boolean sendRecoveryCode(String name, String email, String code) {
|
||||||
String message = replaceTagsForRecoveryCodeMail(settings.getRecoveryCodeEmailMessage(),
|
|
||||||
name, code, settings.getProperty(SecuritySettings.RECOVERY_CODE_HOURS_VALID));
|
|
||||||
|
|
||||||
HtmlEmail htmlEmail;
|
HtmlEmail htmlEmail;
|
||||||
try {
|
try {
|
||||||
htmlEmail = initializeMail(email);
|
htmlEmail = initializeMail(email);
|
||||||
} catch (EmailException e) {
|
} catch (EmailException e) {
|
||||||
ConsoleLogger.logException("Failed to create email for recovery code:", e);
|
ConsoleLogger.logException("Failed to create email for recovery code:", e);
|
||||||
return;
|
return false;
|
||||||
}
|
|
||||||
sendEmail(message, htmlEmail);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static File generateImage(String name, AuthMe plugin, String newPass) throws IOException {
|
String message = replaceTagsForRecoveryCodeMail(settings.getRecoveryCodeEmailMessage(),
|
||||||
|
name, code, settings.getProperty(SecuritySettings.RECOVERY_CODE_HOURS_VALID));
|
||||||
|
return sendEmail(message, htmlEmail);
|
||||||
|
}
|
||||||
|
|
||||||
|
private File generateImage(String name, String newPass) throws IOException {
|
||||||
ImageGenerator gen = new ImageGenerator(newPass);
|
ImageGenerator gen = new ImageGenerator(newPass);
|
||||||
File file = new File(plugin.getDataFolder(), name + "_new_pass.jpg");
|
File file = new File(dataFolder, name + "_new_pass.jpg");
|
||||||
ImageIO.write(gen.generateImage(), "jpg", file);
|
ImageIO.write(gen.generateImage(), "jpg", file);
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
@ -126,7 +123,8 @@ public class SendMailSSL {
|
|||||||
return content.replace("<image />", "<img src=\"cid:" + tag + "\">");
|
return content.replace("<image />", "<img src=\"cid:" + tag + "\">");
|
||||||
}
|
}
|
||||||
|
|
||||||
private HtmlEmail initializeMail(String emailAddress) throws EmailException {
|
@VisibleForTesting
|
||||||
|
HtmlEmail initializeMail(String emailAddress) throws EmailException {
|
||||||
String senderMail = settings.getProperty(EmailSettings.MAIL_ACCOUNT);
|
String senderMail = settings.getProperty(EmailSettings.MAIL_ACCOUNT);
|
||||||
String senderName = StringUtils.isEmpty(settings.getProperty(EmailSettings.MAIL_SENDER_NAME))
|
String senderName = StringUtils.isEmpty(settings.getProperty(EmailSettings.MAIL_SENDER_NAME))
|
||||||
? senderMail
|
? senderMail
|
||||||
@ -147,7 +145,8 @@ public class SendMailSSL {
|
|||||||
return email;
|
return email;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean sendEmail(String content, HtmlEmail email) {
|
@VisibleForTesting
|
||||||
|
boolean sendEmail(String content, HtmlEmail email) {
|
||||||
Thread.currentThread().setContextClassLoader(SendMailSSL.class.getClassLoader());
|
Thread.currentThread().setContextClassLoader(SendMailSSL.class.getClassLoader());
|
||||||
// Issue #999: Prevent UnsupportedDataTypeException: no object DCH for MIME type multipart/alternative
|
// Issue #999: Prevent UnsupportedDataTypeException: no object DCH for MIME type multipart/alternative
|
||||||
// cf. http://stackoverflow.com/questions/21856211/unsupporteddatatypeexception-no-object-dch-for-mime-type
|
// cf. http://stackoverflow.com/questions/21856211/unsupporteddatatypeexception-no-object-dch-for-mime-type
|
||||||
@ -177,14 +176,14 @@ public class SendMailSSL {
|
|||||||
private String replaceTagsForPasswordMail(String mailText, String name, String newPass) {
|
private String replaceTagsForPasswordMail(String mailText, String name, String newPass) {
|
||||||
return mailText
|
return mailText
|
||||||
.replace("<playername />", name)
|
.replace("<playername />", name)
|
||||||
.replace("<servername />", plugin.getServer().getServerName())
|
.replace("<servername />", serverName)
|
||||||
.replace("<generatedpass />", newPass);
|
.replace("<generatedpass />", newPass);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String replaceTagsForRecoveryCodeMail(String mailText, String name, String code, int hoursValid) {
|
private String replaceTagsForRecoveryCodeMail(String mailText, String name, String code, int hoursValid) {
|
||||||
return mailText
|
return mailText
|
||||||
.replace("<playername />", name)
|
.replace("<playername />", name)
|
||||||
.replace("<servername />", plugin.getServer().getServerName())
|
.replace("<servername />", serverName)
|
||||||
.replace("<recoverycode />", code)
|
.replace("<recoverycode />", code)
|
||||||
.replace("<hoursvalid />", String.valueOf(hoursValid));
|
.replace("<hoursvalid />", String.valueOf(hoursValid));
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,7 @@ public class MessageFileHandler {
|
|||||||
|
|
||||||
if (message == null) {
|
if (message == null) {
|
||||||
ConsoleLogger.warning("Error getting message with key '" + key + "'. "
|
ConsoleLogger.warning("Error getting message with key '" + key + "'. "
|
||||||
+ "Please verify your config file at '" + filename + "'");
|
+ "Please update your config file '" + filename + "' (or run /authme messages)");
|
||||||
return getDefault(key);
|
return getDefault(key);
|
||||||
}
|
}
|
||||||
return message;
|
return message;
|
||||||
|
@ -153,6 +153,8 @@ public enum MessageKey {
|
|||||||
|
|
||||||
INCOMPLETE_EMAIL_SETTINGS("incomplete_email_settings"),
|
INCOMPLETE_EMAIL_SETTINGS("incomplete_email_settings"),
|
||||||
|
|
||||||
|
EMAIL_SEND_FAILURE("email_send_failure"),
|
||||||
|
|
||||||
RECOVERY_CODE_SENT("recovery_code_sent"),
|
RECOVERY_CODE_SENT("recovery_code_sent"),
|
||||||
|
|
||||||
INCORRECT_RECOVERY_CODE("recovery_code_incorrect");
|
INCORRECT_RECOVERY_CODE("recovery_code_incorrect");
|
||||||
|
@ -148,8 +148,12 @@ public class AsyncRegister implements AsynchronousProcess {
|
|||||||
}
|
}
|
||||||
database.updateEmail(auth);
|
database.updateEmail(auth);
|
||||||
database.updateSession(auth);
|
database.updateSession(auth);
|
||||||
sendMailSsl.sendPasswordMail(name, email, password);
|
boolean couldSendMail = sendMailSsl.sendPasswordMail(name, email, password);
|
||||||
|
if (couldSendMail) {
|
||||||
syncProcessManager.processSyncEmailRegister(player);
|
syncProcessManager.processSyncEmailRegister(player);
|
||||||
|
} else {
|
||||||
|
service.send(player, MessageKey.EMAIL_SEND_FAILURE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void passwordRegister(final Player player, String password, boolean autoLogin) {
|
private void passwordRegister(final Player player, String password, boolean autoLogin) {
|
||||||
|
@ -72,5 +72,6 @@ accounts_owned_self: 'You own %count accounts:'
|
|||||||
accounts_owned_other: 'The player %name has %count accounts:'
|
accounts_owned_other: 'The player %name has %count accounts:'
|
||||||
kicked_admin_registered: 'An admin just registered you; please log in again'
|
kicked_admin_registered: 'An admin just registered you; please log in again'
|
||||||
incomplete_email_settings: 'Error: not all required settings are set for sending emails. Please contact an admin.'
|
incomplete_email_settings: 'Error: not all required settings are set for sending emails. Please contact an admin.'
|
||||||
|
email_send_failure: 'The email could not be sent. Please contact an administrator.'
|
||||||
recovery_code_sent: 'A recovery code to reset your password has been sent to your email.'
|
recovery_code_sent: 'A recovery code to reset your password has been sent to your email.'
|
||||||
recovery_code_incorrect: 'The recovery code is not correct! Use /email recovery [email] to generate a new one'
|
recovery_code_incorrect: 'The recovery code is not correct! Use /email recovery [email] to generate a new one'
|
||||||
|
@ -38,8 +38,8 @@ name_len: '&4Numele tau este prea scurt pentru a te inregistra!'
|
|||||||
regex: '&4Numele tau contine caractere ilegale. Caractere permise: REG_EX'
|
regex: '&4Numele tau contine caractere ilegale. Caractere permise: REG_EX'
|
||||||
add_email: '&3Te rugam adaugati email-ul la contul tau folosind comanda "/email add <email> <email>"'
|
add_email: '&3Te rugam adaugati email-ul la contul tau folosind comanda "/email add <email> <email>"'
|
||||||
recovery_email: '&3Ti-ai uitat parola? Te rugam foloseste comanda "/email recovery <email>"'
|
recovery_email: '&3Ti-ai uitat parola? Te rugam foloseste comanda "/email recovery <email>"'
|
||||||
usage_captcha: '&3Pentru a te autentifica trebuie sa folosesti codul de la captcha, te rugam foloseste comanda "/captcha <cod>"'
|
usage_captcha: '&3Pentru a te autentifica trebuie sa folosesti codul de la captcha, te rugam foloseste comanda "/captcha <theCaptcha>"'
|
||||||
wrong_captcha: '&cCod-ul captcha este gresit, te rugam foloseste comanda "/captcha <cod>"!'
|
wrong_captcha: '&cCod-ul captcha este gresit, te rugam foloseste comanda "/captcha THE_CAPTCHA"!'
|
||||||
valid_captcha: '&2Cod-ul captcha a fost scris corect!'
|
valid_captcha: '&2Cod-ul captcha a fost scris corect!'
|
||||||
kick_forvip: '&3Un V.I.P a intrat pe server cand era plin!'
|
kick_forvip: '&3Un V.I.P a intrat pe server cand era plin!'
|
||||||
kick_fullserver: '&4Server-ul este plin, te rugam incearca din nou mai tarziu!'
|
kick_fullserver: '&4Server-ul este plin, te rugam incearca din nou mai tarziu!'
|
||||||
|
@ -168,6 +168,7 @@ public class RecoverEmailCommandTest {
|
|||||||
Player sender = mock(Player.class);
|
Player sender = mock(Player.class);
|
||||||
given(sender.getName()).willReturn(name);
|
given(sender.getName()).willReturn(name);
|
||||||
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
||||||
|
given(sendMailSsl.sendRecoveryCode(anyString(), anyString(), anyString())).willReturn(true);
|
||||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||||
String email = "v@example.com";
|
String email = "v@example.com";
|
||||||
given(dataSource.getAuth(name)).willReturn(newAuthWithEmail(email));
|
given(dataSource.getAuth(name)).willReturn(newAuthWithEmail(email));
|
||||||
@ -217,6 +218,7 @@ public class RecoverEmailCommandTest {
|
|||||||
Player sender = mock(Player.class);
|
Player sender = mock(Player.class);
|
||||||
given(sender.getName()).willReturn(name);
|
given(sender.getName()).willReturn(name);
|
||||||
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
||||||
|
given(sendMailSsl.sendPasswordMail(anyString(), anyString(), anyString())).willReturn(true);
|
||||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||||
String email = "vulture@example.com";
|
String email = "vulture@example.com";
|
||||||
String code = "A6EF3AC8";
|
String code = "A6EF3AC8";
|
||||||
@ -251,6 +253,7 @@ public class RecoverEmailCommandTest {
|
|||||||
Player sender = mock(Player.class);
|
Player sender = mock(Player.class);
|
||||||
given(sender.getName()).willReturn(name);
|
given(sender.getName()).willReturn(name);
|
||||||
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
||||||
|
given(sendMailSsl.sendPasswordMail(anyString(), anyString(), anyString())).willReturn(true);
|
||||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||||
String email = "shark@example.org";
|
String email = "shark@example.org";
|
||||||
PlayerAuth auth = newAuthWithEmail(email);
|
PlayerAuth auth = newAuthWithEmail(email);
|
||||||
|
248
src/test/java/fr/xephi/authme/mail/SendMailSSLTest.java
Normal file
248
src/test/java/fr/xephi/authme/mail/SendMailSSLTest.java
Normal file
@ -0,0 +1,248 @@
|
|||||||
|
package fr.xephi.authme.mail;
|
||||||
|
|
||||||
|
import ch.jalu.injector.testing.BeforeInjecting;
|
||||||
|
import ch.jalu.injector.testing.DelayedInjectionRunner;
|
||||||
|
import ch.jalu.injector.testing.InjectDelayed;
|
||||||
|
import fr.xephi.authme.TestHelper;
|
||||||
|
import fr.xephi.authme.initialization.DataFolder;
|
||||||
|
import fr.xephi.authme.settings.Settings;
|
||||||
|
import fr.xephi.authme.settings.properties.EmailSettings;
|
||||||
|
import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||||
|
import org.apache.commons.mail.EmailException;
|
||||||
|
import org.apache.commons.mail.HtmlEmail;
|
||||||
|
import org.bukkit.Server;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Rule;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.TemporaryFolder;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
import static org.hamcrest.Matchers.hasSize;
|
||||||
|
import static org.hamcrest.Matchers.not;
|
||||||
|
import static org.hamcrest.Matchers.nullValue;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.BDDMockito.given;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.doThrow;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link SendMailSSL}.
|
||||||
|
*/
|
||||||
|
@RunWith(DelayedInjectionRunner.class)
|
||||||
|
public class SendMailSSLTest {
|
||||||
|
|
||||||
|
@InjectDelayed
|
||||||
|
private SendMailSSL sendMailSSL;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private Settings settings;
|
||||||
|
@Mock
|
||||||
|
private Server server;
|
||||||
|
@DataFolder
|
||||||
|
private File dataFolder;
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void initLogger() {
|
||||||
|
TestHelper.setupLogger();
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeInjecting
|
||||||
|
public void initFields() throws IOException {
|
||||||
|
dataFolder = temporaryFolder.newFolder();
|
||||||
|
given(server.getServerName()).willReturn("serverName");
|
||||||
|
given(settings.getProperty(EmailSettings.MAIL_ACCOUNT)).willReturn("mail@example.org");
|
||||||
|
given(settings.getProperty(EmailSettings.MAIL_PASSWORD)).willReturn("pass1234");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldHaveAllInformation() {
|
||||||
|
// given / when / then
|
||||||
|
assertThat(sendMailSSL.hasAllInformation(), equalTo(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldSendPasswordMail() throws EmailException {
|
||||||
|
// given
|
||||||
|
given(settings.getPasswordEmailMessage())
|
||||||
|
.willReturn("Hi <playername />, your new password for <servername /> is <generatedpass />");
|
||||||
|
given(settings.getProperty(EmailSettings.PASSWORD_AS_IMAGE)).willReturn(false);
|
||||||
|
SendMailSSL sendMailSpy = spy(sendMailSSL);
|
||||||
|
HtmlEmail email = mock(HtmlEmail.class);
|
||||||
|
doReturn(email).when(sendMailSpy).initializeMail(anyString());
|
||||||
|
doReturn(true).when(sendMailSpy).sendEmail(anyString(), any(HtmlEmail.class));
|
||||||
|
|
||||||
|
// when
|
||||||
|
boolean result = sendMailSpy.sendPasswordMail("Player", "user@example.com", "new_password");
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(result, equalTo(true));
|
||||||
|
verify(sendMailSpy).initializeMail("user@example.com");
|
||||||
|
ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class);
|
||||||
|
verify(sendMailSpy).sendEmail(messageCaptor.capture(), eq(email));
|
||||||
|
assertThat(messageCaptor.getValue(),
|
||||||
|
equalTo("Hi Player, your new password for serverName is new_password"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldHandleMailCreationError() throws EmailException {
|
||||||
|
// given
|
||||||
|
SendMailSSL sendMailSpy = spy(sendMailSSL);
|
||||||
|
doThrow(EmailException.class).when(sendMailSpy).initializeMail(anyString());
|
||||||
|
|
||||||
|
// when
|
||||||
|
boolean result = sendMailSpy.sendPasswordMail("Player", "user@example.com", "new_password");
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(result, equalTo(false));
|
||||||
|
verify(sendMailSpy).initializeMail("user@example.com");
|
||||||
|
verify(sendMailSpy, never()).sendEmail(anyString(), any(HtmlEmail.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldHandleMailSendingFailure() throws EmailException {
|
||||||
|
// given
|
||||||
|
given(settings.getPasswordEmailMessage()).willReturn("Hi <playername />, your new pass is <generatedpass />");
|
||||||
|
given(settings.getProperty(EmailSettings.PASSWORD_AS_IMAGE)).willReturn(false);
|
||||||
|
SendMailSSL sendMailSpy = spy(sendMailSSL);
|
||||||
|
HtmlEmail email = mock(HtmlEmail.class);
|
||||||
|
doReturn(email).when(sendMailSpy).initializeMail(anyString());
|
||||||
|
doReturn(false).when(sendMailSpy).sendEmail(anyString(), any(HtmlEmail.class));
|
||||||
|
|
||||||
|
// when
|
||||||
|
boolean result = sendMailSpy.sendPasswordMail("bobby", "user@example.com", "myPassw0rd");
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(result, equalTo(false));
|
||||||
|
verify(sendMailSpy).initializeMail("user@example.com");
|
||||||
|
ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class);
|
||||||
|
verify(sendMailSpy).sendEmail(messageCaptor.capture(), eq(email));
|
||||||
|
assertThat(messageCaptor.getValue(), equalTo("Hi bobby, your new pass is myPassw0rd"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldSendRecoveryCode() throws EmailException {
|
||||||
|
// given
|
||||||
|
given(settings.getProperty(SecuritySettings.RECOVERY_CODE_HOURS_VALID)).willReturn(7);
|
||||||
|
given(settings.getRecoveryCodeEmailMessage())
|
||||||
|
.willReturn("Hi <playername />, your code on <servername /> is <recoverycode /> (valid <hoursvalid /> hours)");
|
||||||
|
SendMailSSL sendMailSpy = spy(sendMailSSL);
|
||||||
|
HtmlEmail email = mock(HtmlEmail.class);
|
||||||
|
doReturn(email).when(sendMailSpy).initializeMail(anyString());
|
||||||
|
doReturn(true).when(sendMailSpy).sendEmail(anyString(), any(HtmlEmail.class));
|
||||||
|
|
||||||
|
// when
|
||||||
|
boolean result = sendMailSpy.sendRecoveryCode("Timmy", "tim@example.com", "12C56A");
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(result, equalTo(true));
|
||||||
|
verify(sendMailSpy).initializeMail("tim@example.com");
|
||||||
|
ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class);
|
||||||
|
verify(sendMailSpy).sendEmail(messageCaptor.capture(), eq(email));
|
||||||
|
assertThat(messageCaptor.getValue(), equalTo("Hi Timmy, your code on serverName is 12C56A (valid 7 hours)"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldHandleMailCreationErrorForRecoveryCode() throws EmailException {
|
||||||
|
// given
|
||||||
|
SendMailSSL sendMailSpy = spy(sendMailSSL);
|
||||||
|
doThrow(EmailException.class).when(sendMailSpy).initializeMail(anyString());
|
||||||
|
|
||||||
|
// when
|
||||||
|
boolean result = sendMailSpy.sendRecoveryCode("Player", "player@example.org", "ABC1234");
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(result, equalTo(false));
|
||||||
|
verify(sendMailSpy).initializeMail("player@example.org");
|
||||||
|
verify(sendMailSpy, never()).sendEmail(anyString(), any(HtmlEmail.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldHandleFailureToSendRecoveryCode() throws EmailException {
|
||||||
|
// given
|
||||||
|
given(settings.getProperty(SecuritySettings.RECOVERY_CODE_HOURS_VALID)).willReturn(7);
|
||||||
|
given(settings.getRecoveryCodeEmailMessage()).willReturn("Hi <playername />, your code is <recoverycode />");
|
||||||
|
SendMailSSL sendMailSpy = spy(sendMailSSL);
|
||||||
|
HtmlEmail email = mock(HtmlEmail.class);
|
||||||
|
doReturn(email).when(sendMailSpy).initializeMail(anyString());
|
||||||
|
doReturn(false).when(sendMailSpy).sendEmail(anyString(), any(HtmlEmail.class));
|
||||||
|
|
||||||
|
// when
|
||||||
|
boolean result = sendMailSpy.sendRecoveryCode("John", "user@example.com", "1DEF77");
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(result, equalTo(false));
|
||||||
|
verify(sendMailSpy).initializeMail("user@example.com");
|
||||||
|
ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class);
|
||||||
|
verify(sendMailSpy).sendEmail(messageCaptor.capture(), eq(email));
|
||||||
|
assertThat(messageCaptor.getValue(), equalTo("Hi John, your code is 1DEF77"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldCreateEmailObject() throws EmailException {
|
||||||
|
// given
|
||||||
|
given(settings.getProperty(EmailSettings.SMTP_PORT)).willReturn(465);
|
||||||
|
String smtpHost = "mail.example.com";
|
||||||
|
given(settings.getProperty(EmailSettings.SMTP_HOST)).willReturn(smtpHost);
|
||||||
|
String senderMail = "sender@example.org";
|
||||||
|
given(settings.getProperty(EmailSettings.MAIL_ACCOUNT)).willReturn(senderMail);
|
||||||
|
String senderName = "Server administration";
|
||||||
|
given(settings.getProperty(EmailSettings.MAIL_SENDER_NAME)).willReturn(senderName);
|
||||||
|
|
||||||
|
// when
|
||||||
|
HtmlEmail email = sendMailSSL.initializeMail("recipient@example.com");
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(email, not(nullValue()));
|
||||||
|
assertThat(email.getToAddresses(), hasSize(1));
|
||||||
|
assertThat(email.getToAddresses().get(0).getAddress(), equalTo("recipient@example.com"));
|
||||||
|
assertThat(email.getFromAddress().getAddress(), equalTo(senderMail));
|
||||||
|
assertThat(email.getFromAddress().getPersonal(), equalTo(senderName));
|
||||||
|
assertThat(email.getHostName(), equalTo(smtpHost));
|
||||||
|
assertThat(email.getSmtpPort(), equalTo("465"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldCreateEmailObjectWithOAuth2() throws EmailException {
|
||||||
|
// given
|
||||||
|
given(settings.getProperty(EmailSettings.SMTP_PORT)).willReturn(587);
|
||||||
|
given(settings.getProperty(EmailSettings.OAUTH2_TOKEN)).willReturn("oAuth2 token");
|
||||||
|
String smtpHost = "mail.example.com";
|
||||||
|
given(settings.getProperty(EmailSettings.SMTP_HOST)).willReturn(smtpHost);
|
||||||
|
String senderMail = "sender@example.org";
|
||||||
|
given(settings.getProperty(EmailSettings.MAIL_ACCOUNT)).willReturn(senderMail);
|
||||||
|
|
||||||
|
// when
|
||||||
|
HtmlEmail email = sendMailSSL.initializeMail("recipient@example.com");
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(email, not(nullValue()));
|
||||||
|
assertThat(email.getToAddresses(), hasSize(1));
|
||||||
|
assertThat(email.getToAddresses().get(0).getAddress(), equalTo("recipient@example.com"));
|
||||||
|
assertThat(email.getFromAddress().getAddress(), equalTo(senderMail));
|
||||||
|
assertThat(email.getHostName(), equalTo(smtpHost));
|
||||||
|
assertThat(email.getSmtpPort(), equalTo("587"));
|
||||||
|
|
||||||
|
Properties mailProperties = email.getMailSession().getProperties();
|
||||||
|
assertThat(mailProperties.getProperty("mail.smtp.auth.mechanisms"), equalTo("XOAUTH2"));
|
||||||
|
assertThat(mailProperties.getProperty("mail.smtp.auth.plain.disable"), equalTo("true"));
|
||||||
|
assertThat(mailProperties.getProperty(OAuth2SaslClientFactory.OAUTH_TOKEN_PROP), equalTo("oAuth2 token"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user