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:
@@ -4,22 +4,17 @@ 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;
|
||||
|
||||
@@ -28,16 +23,7 @@ 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}.
|
||||
@@ -50,10 +36,6 @@ public class SendMailSSLTest {
|
||||
|
||||
@Mock
|
||||
private Settings settings;
|
||||
@Mock
|
||||
private Server server;
|
||||
@DataFolder
|
||||
private File dataFolder;
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
@@ -65,8 +47,6 @@ public class SendMailSSLTest {
|
||||
|
||||
@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");
|
||||
}
|
||||
@@ -77,123 +57,6 @@ public class SendMailSSLTest {
|
||||
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
|
||||
@@ -270,5 +133,4 @@ public class SendMailSSLTest {
|
||||
assertThat(mailProperties.getProperty("mail.smtp.auth.plain.disable"), equalTo("true"));
|
||||
assertThat(mailProperties.getProperty(OAuth2SaslClientFactory.OAUTH_TOKEN_PROP), equalTo("oAuth2 token"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user