Separate email preparation and email sending into separate classes
- SendMailSSL keeps on handling the technical details for sending mails, while EmailService offers methods to other classes and worries about generating the correct email content
This commit is contained in:
+26
-26
@@ -4,7 +4,7 @@ import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||
import fr.xephi.authme.data.auth.PlayerCache;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.mail.SendMailSSL;
|
||||
import fr.xephi.authme.mail.EmailService;
|
||||
import fr.xephi.authme.message.MessageKey;
|
||||
import fr.xephi.authme.security.PasswordSecurity;
|
||||
import fr.xephi.authme.security.crypts.HashedPassword;
|
||||
@@ -59,7 +59,7 @@ public class RecoverEmailCommandTest {
|
||||
private PlayerCache playerCache;
|
||||
|
||||
@Mock
|
||||
private SendMailSSL sendMailSsl;
|
||||
private EmailService emailService;
|
||||
|
||||
@Mock
|
||||
private RecoveryCodeService recoveryCodeService;
|
||||
@@ -72,7 +72,7 @@ public class RecoverEmailCommandTest {
|
||||
@Test
|
||||
public void shouldHandleMissingMailProperties() {
|
||||
// given
|
||||
given(sendMailSsl.hasAllInformation()).willReturn(false);
|
||||
given(emailService.hasAllInformation()).willReturn(false);
|
||||
Player sender = mock(Player.class);
|
||||
|
||||
// when
|
||||
@@ -89,14 +89,14 @@ public class RecoverEmailCommandTest {
|
||||
String name = "Bobby";
|
||||
Player sender = mock(Player.class);
|
||||
given(sender.getName()).willReturn(name);
|
||||
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
||||
given(emailService.hasAllInformation()).willReturn(true);
|
||||
given(playerCache.isAuthenticated(name)).willReturn(true);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.singletonList("bobby@example.org"));
|
||||
|
||||
// then
|
||||
verify(sendMailSsl).hasAllInformation();
|
||||
verify(emailService).hasAllInformation();
|
||||
verifyZeroInteractions(dataSource);
|
||||
verify(commandService).send(sender, MessageKey.ALREADY_LOGGED_IN_ERROR);
|
||||
}
|
||||
@@ -107,7 +107,7 @@ public class RecoverEmailCommandTest {
|
||||
String name = "Player123";
|
||||
Player sender = mock(Player.class);
|
||||
given(sender.getName()).willReturn(name);
|
||||
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
||||
given(emailService.hasAllInformation()).willReturn(true);
|
||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||
given(dataSource.getAuth(name)).willReturn(null);
|
||||
|
||||
@@ -115,7 +115,7 @@ public class RecoverEmailCommandTest {
|
||||
command.executeCommand(sender, Collections.singletonList("someone@example.com"));
|
||||
|
||||
// then
|
||||
verify(sendMailSsl).hasAllInformation();
|
||||
verify(emailService).hasAllInformation();
|
||||
verify(dataSource).getAuth(name);
|
||||
verifyNoMoreInteractions(dataSource);
|
||||
verify(commandService).send(sender, MessageKey.USAGE_REGISTER);
|
||||
@@ -127,7 +127,7 @@ public class RecoverEmailCommandTest {
|
||||
String name = "Tract0r";
|
||||
Player sender = mock(Player.class);
|
||||
given(sender.getName()).willReturn(name);
|
||||
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
||||
given(emailService.hasAllInformation()).willReturn(true);
|
||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||
given(dataSource.getAuth(name)).willReturn(newAuthWithEmail(DEFAULT_EMAIL));
|
||||
|
||||
@@ -135,7 +135,7 @@ public class RecoverEmailCommandTest {
|
||||
command.executeCommand(sender, Collections.singletonList(DEFAULT_EMAIL));
|
||||
|
||||
// then
|
||||
verify(sendMailSsl).hasAllInformation();
|
||||
verify(emailService).hasAllInformation();
|
||||
verify(dataSource).getAuth(name);
|
||||
verifyNoMoreInteractions(dataSource);
|
||||
verify(commandService).send(sender, MessageKey.INVALID_EMAIL);
|
||||
@@ -147,7 +147,7 @@ public class RecoverEmailCommandTest {
|
||||
String name = "Rapt0r";
|
||||
Player sender = mock(Player.class);
|
||||
given(sender.getName()).willReturn(name);
|
||||
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
||||
given(emailService.hasAllInformation()).willReturn(true);
|
||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||
given(dataSource.getAuth(name)).willReturn(newAuthWithEmail("raptor@example.org"));
|
||||
|
||||
@@ -155,7 +155,7 @@ public class RecoverEmailCommandTest {
|
||||
command.executeCommand(sender, Collections.singletonList("wrong-email@example.com"));
|
||||
|
||||
// then
|
||||
verify(sendMailSsl).hasAllInformation();
|
||||
verify(emailService).hasAllInformation();
|
||||
verify(dataSource).getAuth(name);
|
||||
verifyNoMoreInteractions(dataSource);
|
||||
verify(commandService).send(sender, MessageKey.INVALID_EMAIL);
|
||||
@@ -167,8 +167,8 @@ public class RecoverEmailCommandTest {
|
||||
String name = "Vultur3";
|
||||
Player sender = mock(Player.class);
|
||||
given(sender.getName()).willReturn(name);
|
||||
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
||||
given(sendMailSsl.sendRecoveryCode(anyString(), anyString(), anyString())).willReturn(true);
|
||||
given(emailService.hasAllInformation()).willReturn(true);
|
||||
given(emailService.sendRecoveryCode(anyString(), anyString(), anyString())).willReturn(true);
|
||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||
String email = "v@example.com";
|
||||
given(dataSource.getAuth(name)).willReturn(newAuthWithEmail(email));
|
||||
@@ -180,11 +180,11 @@ public class RecoverEmailCommandTest {
|
||||
command.executeCommand(sender, Collections.singletonList(email.toUpperCase()));
|
||||
|
||||
// then
|
||||
verify(sendMailSsl).hasAllInformation();
|
||||
verify(emailService).hasAllInformation();
|
||||
verify(dataSource).getAuth(name);
|
||||
verify(recoveryCodeService).generateCode(name);
|
||||
verify(commandService).send(sender, MessageKey.RECOVERY_CODE_SENT);
|
||||
verify(sendMailSsl).sendRecoveryCode(name, email, code);
|
||||
verify(emailService).sendRecoveryCode(name, email, code);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -193,7 +193,7 @@ public class RecoverEmailCommandTest {
|
||||
String name = "Vultur3";
|
||||
Player sender = mock(Player.class);
|
||||
given(sender.getName()).willReturn(name);
|
||||
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
||||
given(emailService.hasAllInformation()).willReturn(true);
|
||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||
String email = "vulture@example.com";
|
||||
PlayerAuth auth = newAuthWithEmail(email);
|
||||
@@ -205,10 +205,10 @@ public class RecoverEmailCommandTest {
|
||||
command.executeCommand(sender, Arrays.asList(email, "bogus"));
|
||||
|
||||
// then
|
||||
verify(sendMailSsl).hasAllInformation();
|
||||
verify(emailService).hasAllInformation();
|
||||
verify(dataSource, only()).getAuth(name);
|
||||
verify(commandService).send(sender, MessageKey.INCORRECT_RECOVERY_CODE);
|
||||
verifyNoMoreInteractions(sendMailSsl);
|
||||
verifyNoMoreInteractions(emailService);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -217,8 +217,8 @@ public class RecoverEmailCommandTest {
|
||||
String name = "Vultur3";
|
||||
Player sender = mock(Player.class);
|
||||
given(sender.getName()).willReturn(name);
|
||||
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
||||
given(sendMailSsl.sendPasswordMail(anyString(), anyString(), anyString())).willReturn(true);
|
||||
given(emailService.hasAllInformation()).willReturn(true);
|
||||
given(emailService.sendPasswordMail(anyString(), anyString(), anyString())).willReturn(true);
|
||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||
String email = "vulture@example.com";
|
||||
String code = "A6EF3AC8";
|
||||
@@ -234,7 +234,7 @@ public class RecoverEmailCommandTest {
|
||||
command.executeCommand(sender, Arrays.asList(email, code));
|
||||
|
||||
// then
|
||||
verify(sendMailSsl).hasAllInformation();
|
||||
verify(emailService).hasAllInformation();
|
||||
verify(dataSource).getAuth(name);
|
||||
ArgumentCaptor<String> passwordCaptor = ArgumentCaptor.forClass(String.class);
|
||||
verify(passwordSecurity).computeHash(passwordCaptor.capture(), eq(name));
|
||||
@@ -242,7 +242,7 @@ public class RecoverEmailCommandTest {
|
||||
assertThat(generatedPassword, stringWithLength(20));
|
||||
verify(dataSource).updatePassword(eq(name), any(HashedPassword.class));
|
||||
verify(recoveryCodeService).removeCode(name);
|
||||
verify(sendMailSsl).sendPasswordMail(name, email, generatedPassword);
|
||||
verify(emailService).sendPasswordMail(name, email, generatedPassword);
|
||||
verify(commandService).send(sender, MessageKey.RECOVERY_EMAIL_SENT_MESSAGE);
|
||||
}
|
||||
|
||||
@@ -252,8 +252,8 @@ public class RecoverEmailCommandTest {
|
||||
String name = "sh4rK";
|
||||
Player sender = mock(Player.class);
|
||||
given(sender.getName()).willReturn(name);
|
||||
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
||||
given(sendMailSsl.sendPasswordMail(anyString(), anyString(), anyString())).willReturn(true);
|
||||
given(emailService.hasAllInformation()).willReturn(true);
|
||||
given(emailService.sendPasswordMail(anyString(), anyString(), anyString())).willReturn(true);
|
||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||
String email = "shark@example.org";
|
||||
PlayerAuth auth = newAuthWithEmail(email);
|
||||
@@ -267,14 +267,14 @@ public class RecoverEmailCommandTest {
|
||||
command.executeCommand(sender, Collections.singletonList(email));
|
||||
|
||||
// then
|
||||
verify(sendMailSsl).hasAllInformation();
|
||||
verify(emailService).hasAllInformation();
|
||||
verify(dataSource).getAuth(name);
|
||||
ArgumentCaptor<String> passwordCaptor = ArgumentCaptor.forClass(String.class);
|
||||
verify(passwordSecurity).computeHash(passwordCaptor.capture(), eq(name));
|
||||
String generatedPassword = passwordCaptor.getValue();
|
||||
assertThat(generatedPassword, stringWithLength(20));
|
||||
verify(dataSource).updatePassword(eq(name), any(HashedPassword.class));
|
||||
verify(sendMailSsl).sendPasswordMail(name, email, generatedPassword);
|
||||
verify(emailService).sendPasswordMail(name, email, generatedPassword);
|
||||
verify(commandService).send(sender, MessageKey.RECOVERY_EMAIL_SENT_MESSAGE);
|
||||
}
|
||||
|
||||
|
||||
+15
-15
@@ -1,7 +1,7 @@
|
||||
package fr.xephi.authme.command.executable.register;
|
||||
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.mail.SendMailSSL;
|
||||
import fr.xephi.authme.mail.EmailService;
|
||||
import fr.xephi.authme.message.MessageKey;
|
||||
import fr.xephi.authme.process.Management;
|
||||
import fr.xephi.authme.process.register.RegisterSecondaryArgument;
|
||||
@@ -52,7 +52,7 @@ public class RegisterCommandTest {
|
||||
private Management management;
|
||||
|
||||
@Mock
|
||||
private SendMailSSL sendMailSsl;
|
||||
private EmailService emailService;
|
||||
|
||||
@Mock
|
||||
private ValidationService validationService;
|
||||
@@ -82,7 +82,7 @@ public class RegisterCommandTest {
|
||||
|
||||
// then
|
||||
verify(sender).sendMessage(argThat(containsString("Player only!")));
|
||||
verifyZeroInteractions(management, sendMailSsl);
|
||||
verifyZeroInteractions(management, emailService);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -98,7 +98,7 @@ public class RegisterCommandTest {
|
||||
|
||||
// then
|
||||
verify(management).performRegister(player, executor);
|
||||
verifyZeroInteractions(sendMailSsl);
|
||||
verifyZeroInteractions(emailService);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -111,7 +111,7 @@ public class RegisterCommandTest {
|
||||
|
||||
// then
|
||||
verify(commonService).send(player, MessageKey.USAGE_REGISTER);
|
||||
verifyZeroInteractions(management, sendMailSsl);
|
||||
verifyZeroInteractions(management, emailService);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -126,13 +126,13 @@ public class RegisterCommandTest {
|
||||
|
||||
// then
|
||||
verify(commonService).send(player, MessageKey.USAGE_REGISTER);
|
||||
verifyZeroInteractions(management, sendMailSsl);
|
||||
verifyZeroInteractions(management, emailService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnErrorForMissingEmailConfirmation() {
|
||||
// given
|
||||
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
||||
given(emailService.hasAllInformation()).willReturn(true);
|
||||
given(commonService.getProperty(RegistrationSettings.REGISTRATION_TYPE)).willReturn(RegistrationType.EMAIL);
|
||||
given(commonService.getProperty(RegistrationSettings.REGISTER_SECOND_ARGUMENT)).willReturn(RegisterSecondaryArgument.EMAIL_MANDATORY);
|
||||
given(validationService.validateEmail(anyString())).willReturn(true);
|
||||
@@ -150,7 +150,7 @@ public class RegisterCommandTest {
|
||||
public void shouldThrowErrorForMissingEmailConfiguration() {
|
||||
// given
|
||||
given(commonService.getProperty(RegistrationSettings.REGISTRATION_TYPE)).willReturn(RegistrationType.EMAIL);
|
||||
given(sendMailSsl.hasAllInformation()).willReturn(false);
|
||||
given(emailService.hasAllInformation()).willReturn(false);
|
||||
Player player = mock(Player.class);
|
||||
|
||||
// when
|
||||
@@ -158,7 +158,7 @@ public class RegisterCommandTest {
|
||||
|
||||
// then
|
||||
verify(commonService).send(player, MessageKey.INCOMPLETE_EMAIL_SETTINGS);
|
||||
verify(sendMailSsl).hasAllInformation();
|
||||
verify(emailService).hasAllInformation();
|
||||
verifyZeroInteractions(management);
|
||||
}
|
||||
|
||||
@@ -168,7 +168,7 @@ public class RegisterCommandTest {
|
||||
String playerMail = "player@example.org";
|
||||
given(validationService.validateEmail(playerMail)).willReturn(false);
|
||||
given(commonService.getProperty(RegistrationSettings.REGISTRATION_TYPE)).willReturn(RegistrationType.EMAIL);
|
||||
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
||||
given(emailService.hasAllInformation()).willReturn(true);
|
||||
Player player = mock(Player.class);
|
||||
|
||||
// when
|
||||
@@ -187,7 +187,7 @@ public class RegisterCommandTest {
|
||||
given(validationService.validateEmail(playerMail)).willReturn(true);
|
||||
given(commonService.getProperty(RegistrationSettings.REGISTRATION_TYPE)).willReturn(RegistrationType.EMAIL);
|
||||
given(commonService.getProperty(RegistrationSettings.REGISTER_SECOND_ARGUMENT)).willReturn(RegisterSecondaryArgument.CONFIRMATION);
|
||||
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
||||
given(emailService.hasAllInformation()).willReturn(true);
|
||||
Player player = mock(Player.class);
|
||||
|
||||
// when
|
||||
@@ -195,7 +195,7 @@ public class RegisterCommandTest {
|
||||
|
||||
// then
|
||||
verify(commonService).send(player, MessageKey.USAGE_REGISTER);
|
||||
verify(sendMailSsl).hasAllInformation();
|
||||
verify(emailService).hasAllInformation();
|
||||
verifyZeroInteractions(management);
|
||||
}
|
||||
|
||||
@@ -206,7 +206,7 @@ public class RegisterCommandTest {
|
||||
given(validationService.validateEmail(playerMail)).willReturn(true);
|
||||
given(commonService.getProperty(RegistrationSettings.REGISTRATION_TYPE)).willReturn(RegistrationType.EMAIL);
|
||||
given(commonService.getProperty(RegistrationSettings.REGISTER_SECOND_ARGUMENT)).willReturn(RegisterSecondaryArgument.CONFIRMATION);
|
||||
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
||||
given(emailService.hasAllInformation()).willReturn(true);
|
||||
Player player = mock(Player.class);
|
||||
RegistrationExecutor executor = mock(RegistrationExecutor.class);
|
||||
given(registrationExecutorProvider.getEmailRegisterExecutor(player, playerMail)).willReturn(executor);
|
||||
@@ -216,7 +216,7 @@ public class RegisterCommandTest {
|
||||
|
||||
// then
|
||||
verify(validationService).validateEmail(playerMail);
|
||||
verify(sendMailSsl).hasAllInformation();
|
||||
verify(emailService).hasAllInformation();
|
||||
verify(management).performRegister(player, executor);
|
||||
}
|
||||
|
||||
@@ -232,7 +232,7 @@ public class RegisterCommandTest {
|
||||
|
||||
// then
|
||||
verify(commonService).send(player, MessageKey.PASSWORD_MATCH_ERROR);
|
||||
verifyZeroInteractions(management, sendMailSsl);
|
||||
verifyZeroInteractions(management, emailService);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user