#567 Move email validation logic to validation service
This commit is contained in:
@@ -15,9 +15,9 @@ import fr.xephi.authme.util.ValidationService;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
@@ -28,23 +28,38 @@ import static org.mockito.Mockito.verify;
|
||||
/**
|
||||
* Test for {@link ProcessService}.
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ProcessServiceTest {
|
||||
|
||||
private ProcessService processService;
|
||||
private Map<Class<?>, Object> mocks;
|
||||
@Mock
|
||||
private ValidationService validationService;
|
||||
@Mock
|
||||
private NewSetting settings;
|
||||
@Mock
|
||||
private Messages messages;
|
||||
@Mock
|
||||
private IpAddressManager ipAddressManager;
|
||||
@Mock
|
||||
private PasswordSecurity passwordSecurity;
|
||||
@Mock
|
||||
private AuthMe authMe;
|
||||
@Mock
|
||||
private DataSource dataSource;
|
||||
@Mock
|
||||
private SpawnLoader spawnLoader;
|
||||
@Mock
|
||||
private PluginHooks pluginHooks;
|
||||
|
||||
@Before
|
||||
public void setUpService() {
|
||||
mocks = new HashMap<>();
|
||||
processService = new ProcessService(newMock(NewSetting.class), newMock(Messages.class), newMock(AuthMe.class),
|
||||
newMock(DataSource.class), newMock(IpAddressManager.class), newMock(PasswordSecurity.class),
|
||||
newMock(PluginHooks.class), newMock(SpawnLoader.class), newMock(ValidationService.class));
|
||||
processService = new ProcessService(settings, messages, authMe, dataSource, ipAddressManager, passwordSecurity,
|
||||
pluginHooks, spawnLoader, validationService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetProperty() {
|
||||
// given
|
||||
NewSetting settings = getMock(NewSetting.class);
|
||||
given(settings.getProperty(SecuritySettings.CAPTCHA_LENGTH)).willReturn(8);
|
||||
|
||||
// when
|
||||
@@ -58,16 +73,15 @@ public class ProcessServiceTest {
|
||||
@Test
|
||||
public void shouldReturnSettings() {
|
||||
// given/when
|
||||
NewSetting settings = processService.getSettings();
|
||||
NewSetting result = processService.getSettings();
|
||||
|
||||
// then
|
||||
assertThat(settings, equalTo(getMock(NewSetting.class)));
|
||||
assertThat(result, equalTo(settings));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSendMessageToPlayer() {
|
||||
// given
|
||||
Messages messages = getMock(Messages.class);
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
MessageKey key = MessageKey.ACCOUNT_NOT_ACTIVATED;
|
||||
|
||||
@@ -81,7 +95,6 @@ public class ProcessServiceTest {
|
||||
@Test
|
||||
public void shouldSendMessageWithReplacements() {
|
||||
// given
|
||||
Messages messages = getMock(Messages.class);
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
MessageKey key = MessageKey.ACCOUNT_NOT_ACTIVATED;
|
||||
String[] replacements = new String[]{"test", "toast"};
|
||||
@@ -96,7 +109,6 @@ public class ProcessServiceTest {
|
||||
@Test
|
||||
public void shouldRetrieveMessage() {
|
||||
// given
|
||||
Messages messages = getMock(Messages.class);
|
||||
MessageKey key = MessageKey.ACCOUNT_NOT_ACTIVATED;
|
||||
String[] lines = new String[]{"First message line", "second line"};
|
||||
given(messages.retrieve(key)).willReturn(lines);
|
||||
@@ -112,7 +124,6 @@ public class ProcessServiceTest {
|
||||
@Test
|
||||
public void shouldRetrieveSingleMessage() {
|
||||
// given
|
||||
Messages messages = getMock(Messages.class);
|
||||
MessageKey key = MessageKey.ACCOUNT_NOT_ACTIVATED;
|
||||
String text = "Test text";
|
||||
given(messages.retrieveSingle(key)).willReturn(text);
|
||||
@@ -128,52 +139,51 @@ public class ProcessServiceTest {
|
||||
@Test
|
||||
public void shouldReturnAuthMeInstance() {
|
||||
// given / when
|
||||
AuthMe authMe = processService.getAuthMe();
|
||||
AuthMe result = processService.getAuthMe();
|
||||
|
||||
// then
|
||||
assertThat(authMe, equalTo(getMock(AuthMe.class)));
|
||||
assertThat(result, equalTo(authMe));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnPluginHooks() {
|
||||
// given / when
|
||||
PluginHooks pluginHooks = processService.getPluginHooks();
|
||||
PluginHooks result = processService.getPluginHooks();
|
||||
|
||||
// then
|
||||
assertThat(pluginHooks, equalTo(getMock(PluginHooks.class)));
|
||||
assertThat(result, equalTo(pluginHooks));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnIpAddressManager() {
|
||||
// given / when
|
||||
IpAddressManager ipAddressManager = processService.getIpAddressManager();
|
||||
IpAddressManager result = processService.getIpAddressManager();
|
||||
|
||||
// then
|
||||
assertThat(ipAddressManager, equalTo(getMock(IpAddressManager.class)));
|
||||
assertThat(result, equalTo(ipAddressManager));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnSpawnLoader() {
|
||||
// given / when
|
||||
SpawnLoader spawnLoader = processService.getSpawnLoader();
|
||||
SpawnLoader result = processService.getSpawnLoader();
|
||||
|
||||
// then
|
||||
assertThat(spawnLoader, equalTo(getMock(SpawnLoader.class)));
|
||||
assertThat(result, equalTo(spawnLoader));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnDatasource() {
|
||||
// given / when
|
||||
DataSource dataSource = processService.getDataSource();
|
||||
DataSource result = processService.getDataSource();
|
||||
|
||||
// then
|
||||
assertThat(dataSource, equalTo(getMock(DataSource.class)));
|
||||
assertThat(result, equalTo(dataSource));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldComputeHash() {
|
||||
// given
|
||||
PasswordSecurity passwordSecurity = getMock(PasswordSecurity.class);
|
||||
String password = "test123";
|
||||
String username = "Username";
|
||||
HashedPassword hashedPassword = new HashedPassword("hashedResult", "salt12342");
|
||||
@@ -192,7 +202,6 @@ public class ProcessServiceTest {
|
||||
// given
|
||||
String user = "test-user";
|
||||
String password = "passw0rd";
|
||||
ValidationService validationService = getMock(ValidationService.class);
|
||||
given(validationService.validatePassword(password, user)).willReturn(MessageKey.PASSWORD_MATCH_ERROR);
|
||||
|
||||
// when
|
||||
@@ -203,17 +212,33 @@ public class ProcessServiceTest {
|
||||
verify(validationService).validatePassword(password, user);
|
||||
}
|
||||
|
||||
private <T> T newMock(Class<T> clazz) {
|
||||
T mock = mock(clazz);
|
||||
mocks.put(clazz, mock);
|
||||
return mock;
|
||||
@Test
|
||||
public void shouldValidateEmail() {
|
||||
// given
|
||||
String email = "test@example.tld";
|
||||
given(validationService.validateEmail(email)).willReturn(true);
|
||||
|
||||
// when
|
||||
boolean result = processService.validateEmail(email);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
verify(validationService).validateEmail(email);
|
||||
}
|
||||
|
||||
private <T> T getMock(Class<T> clazz) {
|
||||
Object mock = mocks.get(clazz);
|
||||
if (mock == null) {
|
||||
throw new IllegalArgumentException("No mock of type " + clazz);
|
||||
}
|
||||
return clazz.cast(mock);
|
||||
@Test
|
||||
public void shouldCheckIfEmailCanBeUsed() {
|
||||
// given
|
||||
String email = "mail@example.com";
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
given(validationService.isEmailFreeForRegistration(email, sender))
|
||||
.willReturn(true);
|
||||
|
||||
// when
|
||||
boolean result = processService.isEmailFreeForRegistration(email, sender);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
verify(validationService).isEmailFreeForRegistration(email, sender);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,8 +6,6 @@ import fr.xephi.authme.cache.auth.PlayerCache;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.output.MessageKey;
|
||||
import fr.xephi.authme.process.ProcessService;
|
||||
import fr.xephi.authme.settings.NewSetting;
|
||||
import fr.xephi.authme.settings.properties.EmailSettings;
|
||||
import fr.xephi.authme.settings.properties.RegistrationSettings;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.BeforeClass;
|
||||
@@ -21,7 +19,6 @@ import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Test for {@link AsyncAddEmail}.
|
||||
@@ -46,14 +43,16 @@ public class AsyncAddEmailTest {
|
||||
@Test
|
||||
public void shouldAddEmail() {
|
||||
// given
|
||||
AsyncAddEmail process = createProcess("my.mail@example.org");
|
||||
String email = "my.mail@example.org";
|
||||
AsyncAddEmail process = createProcess(email);
|
||||
given(player.getName()).willReturn("testEr");
|
||||
given(playerCache.isAuthenticated("tester")).willReturn(true);
|
||||
PlayerAuth auth = mock(PlayerAuth.class);
|
||||
given(auth.getEmail()).willReturn(null);
|
||||
given(playerCache.getAuth("tester")).willReturn(auth);
|
||||
given(dataSource.countAuthsByEmail("my.mail@example.org")).willReturn(1);
|
||||
given(dataSource.updateEmail(any(PlayerAuth.class))).willReturn(true);
|
||||
given(service.validateEmail(email)).willReturn(true);
|
||||
given(service.isEmailFreeForRegistration(email, player)).willReturn(true);
|
||||
|
||||
// when
|
||||
process.run();
|
||||
@@ -61,21 +60,24 @@ public class AsyncAddEmailTest {
|
||||
// then
|
||||
verify(dataSource).updateEmail(auth);
|
||||
verify(service).send(player, MessageKey.EMAIL_ADDED_SUCCESS);
|
||||
verify(auth).setEmail("my.mail@example.org");
|
||||
verify(auth).setEmail(email);
|
||||
verify(playerCache).updatePlayer(auth);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnErrorWhenMailCannotBeSaved() {
|
||||
// given
|
||||
AsyncAddEmail process = createProcess("my.mail@example.org");
|
||||
String email = "my.mail@example.org";
|
||||
AsyncAddEmail process = createProcess(email);
|
||||
given(player.getName()).willReturn("testEr");
|
||||
given(playerCache.isAuthenticated("tester")).willReturn(true);
|
||||
PlayerAuth auth = mock(PlayerAuth.class);
|
||||
given(auth.getEmail()).willReturn(null);
|
||||
given(playerCache.getAuth("tester")).willReturn(auth);
|
||||
given(dataSource.countAuthsByEmail("my.mail@example.org")).willReturn(0);
|
||||
given(dataSource.countAuthsByEmail(email)).willReturn(0);
|
||||
given(dataSource.updateEmail(any(PlayerAuth.class))).willReturn(false);
|
||||
given(service.validateEmail(email)).willReturn(true);
|
||||
given(service.isEmailFreeForRegistration(email, player)).willReturn(true);
|
||||
|
||||
// when
|
||||
process.run();
|
||||
@@ -94,7 +96,6 @@ public class AsyncAddEmailTest {
|
||||
PlayerAuth auth = mock(PlayerAuth.class);
|
||||
given(auth.getEmail()).willReturn("another@mail.tld");
|
||||
given(playerCache.getAuth("my_player")).willReturn(auth);
|
||||
given(dataSource.countAuthsByEmail("some.mail@example.org")).willReturn(0);
|
||||
|
||||
// when
|
||||
process.run();
|
||||
@@ -107,13 +108,14 @@ public class AsyncAddEmailTest {
|
||||
@Test
|
||||
public void shouldNotAddMailIfItIsInvalid() {
|
||||
// given
|
||||
AsyncAddEmail process = createProcess("invalid_mail");
|
||||
String email = "invalid_mail";
|
||||
AsyncAddEmail process = createProcess(email);
|
||||
given(player.getName()).willReturn("my_Player");
|
||||
given(playerCache.isAuthenticated("my_player")).willReturn(true);
|
||||
PlayerAuth auth = mock(PlayerAuth.class);
|
||||
given(auth.getEmail()).willReturn(null);
|
||||
given(playerCache.getAuth("my_player")).willReturn(auth);
|
||||
given(dataSource.countAuthsByEmail("invalid_mail")).willReturn(0);
|
||||
given(service.validateEmail(email)).willReturn(false);
|
||||
|
||||
// when
|
||||
process.run();
|
||||
@@ -126,13 +128,15 @@ public class AsyncAddEmailTest {
|
||||
@Test
|
||||
public void shouldNotAddMailIfAlreadyUsed() {
|
||||
// given
|
||||
AsyncAddEmail process = createProcess("player@mail.tld");
|
||||
String email = "player@mail.tld";
|
||||
AsyncAddEmail process = createProcess(email);
|
||||
given(player.getName()).willReturn("TestName");
|
||||
given(playerCache.isAuthenticated("testname")).willReturn(true);
|
||||
PlayerAuth auth = mock(PlayerAuth.class);
|
||||
given(auth.getEmail()).willReturn(null);
|
||||
given(playerCache.getAuth("testname")).willReturn(auth);
|
||||
given(dataSource.countAuthsByEmail("player@mail.tld")).willReturn(2);
|
||||
given(service.validateEmail(email)).willReturn(true);
|
||||
given(service.isEmailFreeForRegistration(email, player)).willReturn(false);
|
||||
|
||||
// when
|
||||
process.run();
|
||||
@@ -193,15 +197,12 @@ public class AsyncAddEmailTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance of {@link AsyncAddEmail} and save the mocks to this class' fields.
|
||||
* Create an instance of {@link AsyncAddEmail} with the class' mocks.
|
||||
*
|
||||
* @param email The email to use
|
||||
* @return The created process
|
||||
*/
|
||||
private AsyncAddEmail createProcess(String email) {
|
||||
NewSetting settings = mock(NewSetting.class);
|
||||
when(service.getProperty(EmailSettings.MAX_REG_PER_EMAIL)).thenReturn(2);
|
||||
when(service.getSettings()).thenReturn(settings);
|
||||
return new AsyncAddEmail(player, email, dataSource, playerCache, service);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ import fr.xephi.authme.cache.auth.PlayerCache;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.output.MessageKey;
|
||||
import fr.xephi.authme.process.ProcessService;
|
||||
import fr.xephi.authme.settings.NewSetting;
|
||||
import fr.xephi.authme.settings.properties.EmailSettings;
|
||||
import fr.xephi.authme.settings.properties.RegistrationSettings;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -35,18 +34,19 @@ public class AsyncChangeEmailTest {
|
||||
private DataSource dataSource;
|
||||
@Mock
|
||||
private ProcessService service;
|
||||
@Mock
|
||||
private NewSetting settings;
|
||||
|
||||
@Test
|
||||
public void shouldAddEmail() {
|
||||
// given
|
||||
AsyncChangeEmail process = createProcess("old@mail.tld", "new@mail.tld");
|
||||
String newEmail = "new@mail.tld";
|
||||
AsyncChangeEmail process = createProcess("old@mail.tld", newEmail);
|
||||
given(player.getName()).willReturn("Bobby");
|
||||
given(playerCache.isAuthenticated("bobby")).willReturn(true);
|
||||
PlayerAuth auth = authWithMail("old@mail.tld");
|
||||
given(playerCache.getAuth("bobby")).willReturn(auth);
|
||||
given(dataSource.updateEmail(auth)).willReturn(true);
|
||||
given(service.validateEmail(newEmail)).willReturn(true);
|
||||
given(service.isEmailFreeForRegistration(newEmail, player)).willReturn(true);
|
||||
|
||||
// when
|
||||
process.run();
|
||||
@@ -60,12 +60,15 @@ public class AsyncChangeEmailTest {
|
||||
@Test
|
||||
public void shouldShowErrorIfSaveFails() {
|
||||
// given
|
||||
AsyncChangeEmail process = createProcess("old@mail.tld", "new@mail.tld");
|
||||
String newEmail = "new@mail.tld";
|
||||
AsyncChangeEmail process = createProcess("old@mail.tld", newEmail);
|
||||
given(player.getName()).willReturn("Bobby");
|
||||
given(playerCache.isAuthenticated("bobby")).willReturn(true);
|
||||
PlayerAuth auth = authWithMail("old@mail.tld");
|
||||
given(playerCache.getAuth("bobby")).willReturn(auth);
|
||||
given(dataSource.updateEmail(auth)).willReturn(false);
|
||||
given(service.validateEmail(newEmail)).willReturn(true);
|
||||
given(service.isEmailFreeForRegistration(newEmail, player)).willReturn(true);
|
||||
|
||||
// when
|
||||
process.run();
|
||||
@@ -97,11 +100,13 @@ public class AsyncChangeEmailTest {
|
||||
@Test
|
||||
public void shouldRejectInvalidNewMail() {
|
||||
// given
|
||||
AsyncChangeEmail process = createProcess("old@mail.tld", "bogus");
|
||||
String newEmail = "bogus";
|
||||
AsyncChangeEmail process = createProcess("old@mail.tld", newEmail);
|
||||
given(player.getName()).willReturn("Bobby");
|
||||
given(playerCache.isAuthenticated("bobby")).willReturn(true);
|
||||
PlayerAuth auth = authWithMail("old@mail.tld");
|
||||
given(playerCache.getAuth("bobby")).willReturn(auth);
|
||||
given(service.validateEmail(newEmail)).willReturn(false);
|
||||
|
||||
// when
|
||||
process.run();
|
||||
@@ -115,11 +120,15 @@ public class AsyncChangeEmailTest {
|
||||
@Test
|
||||
public void shouldRejectInvalidOldEmail() {
|
||||
// given
|
||||
AsyncChangeEmail process = createProcess("old@mail.tld", "new@mail.tld");
|
||||
String newEmail = "new@mail.tld";
|
||||
AsyncChangeEmail process = createProcess("old@mail.tld", newEmail);
|
||||
given(player.getName()).willReturn("Bobby");
|
||||
given(playerCache.isAuthenticated("bobby")).willReturn(true);
|
||||
PlayerAuth auth = authWithMail("other@address.email");
|
||||
given(playerCache.getAuth("bobby")).willReturn(auth);
|
||||
given(service.validateEmail(newEmail)).willReturn(true);
|
||||
given(service.isEmailFreeForRegistration(newEmail, player)).willReturn(true);
|
||||
|
||||
|
||||
// when
|
||||
process.run();
|
||||
@@ -133,12 +142,14 @@ public class AsyncChangeEmailTest {
|
||||
@Test
|
||||
public void shouldRejectAlreadyUsedEmail() {
|
||||
// given
|
||||
AsyncChangeEmail process = createProcess("old@example.com", "new@example.com");
|
||||
String newEmail = "new@example.com";
|
||||
AsyncChangeEmail process = createProcess("old@example.com", newEmail);
|
||||
given(player.getName()).willReturn("Username");
|
||||
given(playerCache.isAuthenticated("username")).willReturn(true);
|
||||
PlayerAuth auth = authWithMail("old@example.com");
|
||||
given(playerCache.getAuth("username")).willReturn(auth);
|
||||
given(dataSource.countAuthsByEmail("new@example.com")).willReturn(5);
|
||||
given(service.validateEmail(newEmail)).willReturn(true);
|
||||
given(service.isEmailFreeForRegistration(newEmail, player)).willReturn(false);
|
||||
|
||||
// when
|
||||
process.run();
|
||||
@@ -210,7 +221,6 @@ public class AsyncChangeEmailTest {
|
||||
|
||||
private AsyncChangeEmail createProcess(String oldEmail, String newEmail) {
|
||||
given(service.getProperty(EmailSettings.MAX_REG_PER_EMAIL)).willReturn(5);
|
||||
given(service.getSettings()).willReturn(settings);
|
||||
return new AsyncChangeEmail(player, oldEmail, newEmail, dataSource, playerCache, service);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user