* #1037 Improve architecture of registration methods - Introduce parameters classes for each registration method - RegistrationMethod has constants with the required parameters class -> no longer need to have a RegistrationExecutorProvider class that needs to be injected everywhere, let AsyncRegister be the only one to worry about which class will perform the registration - Fix inheritance of password registration types - previously two-factor auth registration inherited from password registration directly * Create matcher which checks for equality via reflections - Allow to perform equality check on objects with default equals() method
This commit is contained in:
@@ -3,9 +3,13 @@ package fr.xephi.authme.process.register;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.data.auth.PlayerCache;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.initialization.factory.SingletonStore;
|
||||
import fr.xephi.authme.message.MessageKey;
|
||||
import fr.xephi.authme.permission.PermissionsManager;
|
||||
import fr.xephi.authme.process.register.executors.PasswordRegisterParams;
|
||||
import fr.xephi.authme.process.register.executors.RegistrationExecutor;
|
||||
import fr.xephi.authme.process.register.executors.RegistrationMethod;
|
||||
import fr.xephi.authme.process.register.executors.TwoFactorRegisterParams;
|
||||
import fr.xephi.authme.service.CommonService;
|
||||
import fr.xephi.authme.settings.properties.RegistrationSettings;
|
||||
import fr.xephi.authme.settings.properties.RestrictionSettings;
|
||||
@@ -16,6 +20,7 @@ import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.only;
|
||||
@@ -39,6 +44,8 @@ public class AsyncRegisterTest {
|
||||
private CommonService commonService;
|
||||
@Mock
|
||||
private DataSource dataSource;
|
||||
@Mock
|
||||
private SingletonStore<RegistrationExecutor> registrationExecutorStore;
|
||||
|
||||
@Test
|
||||
public void shouldDetectAlreadyLoggedInPlayer() {
|
||||
@@ -47,9 +54,10 @@ public class AsyncRegisterTest {
|
||||
Player player = mockPlayerWithName(name);
|
||||
given(playerCache.isAuthenticated(name)).willReturn(true);
|
||||
RegistrationExecutor executor = mock(RegistrationExecutor.class);
|
||||
singletonStoreWillReturn(registrationExecutorStore, executor);
|
||||
|
||||
// when
|
||||
asyncRegister.register(player, executor);
|
||||
asyncRegister.register(RegistrationMethod.PASSWORD_REGISTRATION, PasswordRegisterParams.of(player, "abc", null));
|
||||
|
||||
// then
|
||||
verify(commonService).send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
|
||||
@@ -64,9 +72,10 @@ public class AsyncRegisterTest {
|
||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||
given(commonService.getProperty(RegistrationSettings.IS_ENABLED)).willReturn(false);
|
||||
RegistrationExecutor executor = mock(RegistrationExecutor.class);
|
||||
singletonStoreWillReturn(registrationExecutorStore, executor);
|
||||
|
||||
// when
|
||||
asyncRegister.register(player, executor);
|
||||
asyncRegister.register(RegistrationMethod.TWO_FACTOR_REGISTRATION, TwoFactorRegisterParams.of(player));
|
||||
|
||||
// then
|
||||
verify(commonService).send(player, MessageKey.REGISTRATION_DISABLED);
|
||||
@@ -82,9 +91,10 @@ public class AsyncRegisterTest {
|
||||
given(commonService.getProperty(RegistrationSettings.IS_ENABLED)).willReturn(true);
|
||||
given(dataSource.isAuthAvailable(name)).willReturn(true);
|
||||
RegistrationExecutor executor = mock(RegistrationExecutor.class);
|
||||
singletonStoreWillReturn(registrationExecutorStore, executor);
|
||||
|
||||
// when
|
||||
asyncRegister.register(player, executor);
|
||||
asyncRegister.register(RegistrationMethod.TWO_FACTOR_REGISTRATION, TwoFactorRegisterParams.of(player));
|
||||
|
||||
// then
|
||||
verify(commonService).send(player, MessageKey.NAME_ALREADY_REGISTERED);
|
||||
@@ -93,6 +103,7 @@ public class AsyncRegisterTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void shouldStopForFailedExecutorCheck() {
|
||||
// given
|
||||
String name = "edbert";
|
||||
@@ -103,14 +114,16 @@ public class AsyncRegisterTest {
|
||||
given(commonService.getProperty(RestrictionSettings.MAX_REGISTRATION_PER_IP)).willReturn(0);
|
||||
given(dataSource.isAuthAvailable(name)).willReturn(false);
|
||||
RegistrationExecutor executor = mock(RegistrationExecutor.class);
|
||||
given(executor.isRegistrationAdmitted()).willReturn(false);
|
||||
TwoFactorRegisterParams params = TwoFactorRegisterParams.of(player);
|
||||
given(executor.isRegistrationAdmitted(params)).willReturn(false);
|
||||
singletonStoreWillReturn(registrationExecutorStore, executor);
|
||||
|
||||
// when
|
||||
asyncRegister.register(player, executor);
|
||||
asyncRegister.register(RegistrationMethod.TWO_FACTOR_REGISTRATION, params);
|
||||
|
||||
// then
|
||||
verify(dataSource, only()).isAuthAvailable(name);
|
||||
verify(executor, only()).isRegistrationAdmitted();
|
||||
verify(executor, only()).isRegistrationAdmitted(params);
|
||||
}
|
||||
|
||||
private static Player mockPlayerWithName(String name) {
|
||||
@@ -118,4 +131,10 @@ public class AsyncRegisterTest {
|
||||
given(player.getName()).willReturn(name);
|
||||
return player;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static void singletonStoreWillReturn(SingletonStore<RegistrationExecutor> store,
|
||||
RegistrationExecutor mock) {
|
||||
given(store.getSingleton(any(Class.class))).willReturn(mock);
|
||||
}
|
||||
}
|
||||
|
||||
+16
-19
@@ -1,6 +1,5 @@
|
||||
package fr.xephi.authme.process.register.executors;
|
||||
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
@@ -34,13 +33,13 @@ import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
|
||||
/**
|
||||
* Test for {@link EmailRegisterExecutorProvider}.
|
||||
* Test for {@link EmailRegisterExecutor}.
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class EmailRegisterExecutorProviderTest {
|
||||
|
||||
@InjectMocks
|
||||
private EmailRegisterExecutorProvider emailRegisterExecutorProvider;
|
||||
private EmailRegisterExecutor executor;
|
||||
|
||||
@Mock
|
||||
private PermissionsManager permissionsManager;
|
||||
@@ -62,10 +61,10 @@ public class EmailRegisterExecutorProviderTest {
|
||||
String email = "test@example.com";
|
||||
given(dataSource.countAuthsByEmail(email)).willReturn(4);
|
||||
Player player = mock(Player.class);
|
||||
RegistrationExecutor executor = emailRegisterExecutorProvider.new EmailRegisterExecutor(player, email);
|
||||
EmailRegisterParams params = EmailRegisterParams.of(player, email);
|
||||
|
||||
// when
|
||||
boolean result = executor.isRegistrationAdmitted();
|
||||
boolean result = executor.isRegistrationAdmitted(params);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(false));
|
||||
@@ -80,10 +79,10 @@ public class EmailRegisterExecutorProviderTest {
|
||||
given(commonService.getProperty(EmailSettings.MAX_REG_PER_EMAIL)).willReturn(3);
|
||||
Player player = mock(Player.class);
|
||||
given(permissionsManager.hasPermission(player, PlayerStatePermission.ALLOW_MULTIPLE_ACCOUNTS)).willReturn(true);
|
||||
RegistrationExecutor executor = emailRegisterExecutorProvider.new EmailRegisterExecutor(player, "test@example.com");
|
||||
EmailRegisterParams params = EmailRegisterParams.of(player, "test@example.com");
|
||||
|
||||
// when
|
||||
boolean result = executor.isRegistrationAdmitted();
|
||||
boolean result = executor.isRegistrationAdmitted(params);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
@@ -97,10 +96,10 @@ public class EmailRegisterExecutorProviderTest {
|
||||
String email = "test@example.com";
|
||||
given(dataSource.countAuthsByEmail(email)).willReturn(0);
|
||||
Player player = mock(Player.class);
|
||||
RegistrationExecutor executor = emailRegisterExecutorProvider.new EmailRegisterExecutor(player, "test@example.com");
|
||||
EmailRegisterParams params = EmailRegisterParams.of(player, "test@example.com");
|
||||
|
||||
// when
|
||||
boolean result = executor.isRegistrationAdmitted();
|
||||
boolean result = executor.isRegistrationAdmitted(params);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
@@ -120,10 +119,10 @@ public class EmailRegisterExecutorProviderTest {
|
||||
World world = mock(World.class);
|
||||
given(world.getName()).willReturn("someWorld");
|
||||
given(player.getLocation()).willReturn(new Location(world, 48, 96, 144));
|
||||
RegistrationExecutor executor = emailRegisterExecutorProvider.new EmailRegisterExecutor(player, "test@example.com");
|
||||
EmailRegisterParams params = EmailRegisterParams.of(player, "test@example.com");
|
||||
|
||||
// when
|
||||
PlayerAuth auth = executor.buildPlayerAuth();
|
||||
PlayerAuth auth = executor.buildPlayerAuth(params);
|
||||
|
||||
// then
|
||||
assertThat(auth, hasAuthBasicData("veronica", "Veronica", "test@example.com", "123.45.67.89"));
|
||||
@@ -132,18 +131,17 @@ public class EmailRegisterExecutorProviderTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void shouldPerformActionAfterDataSourceSave() {
|
||||
// given
|
||||
given(emailService.sendPasswordMail(anyString(), anyString(), anyString())).willReturn(true);
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn("Laleh");
|
||||
RegistrationExecutor executor = emailRegisterExecutorProvider.new EmailRegisterExecutor(player, "test@example.com");
|
||||
EmailRegisterParams params = EmailRegisterParams.of(player, "test@example.com");
|
||||
String password = "A892C#@";
|
||||
ReflectionTestUtils.setField((Class) executor.getClass(), executor, "password", password);
|
||||
params.setPassword(password);
|
||||
|
||||
// when
|
||||
executor.executePostPersistAction();
|
||||
executor.executePostPersistAction(params);
|
||||
|
||||
// then
|
||||
verify(emailService).sendPasswordMail("Laleh", "test@example.com", password);
|
||||
@@ -151,18 +149,17 @@ public class EmailRegisterExecutorProviderTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void shouldHandleEmailSendingFailure() {
|
||||
// given
|
||||
given(emailService.sendPasswordMail(anyString(), anyString(), anyString())).willReturn(false);
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn("Laleh");
|
||||
RegistrationExecutor executor = emailRegisterExecutorProvider.new EmailRegisterExecutor(player, "test@example.com");
|
||||
EmailRegisterParams params = EmailRegisterParams.of(player, "test@example.com");
|
||||
String password = "A892C#@";
|
||||
ReflectionTestUtils.setField((Class) executor.getClass(), executor, "password", password);
|
||||
params.setPassword(password);
|
||||
|
||||
// when
|
||||
executor.executePostPersistAction();
|
||||
executor.executePostPersistAction(params);
|
||||
|
||||
// then
|
||||
verify(emailService).sendPasswordMail("Laleh", "test@example.com", password);
|
||||
|
||||
+13
-13
@@ -34,13 +34,13 @@ import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
|
||||
/**
|
||||
* Test for {@link PasswordRegisterExecutorProvider}.
|
||||
* Test for {@link PasswordRegisterExecutor}.
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class PasswordRegisterExecutorProviderTest {
|
||||
public class PasswordRegisterExecutorTest {
|
||||
|
||||
@InjectMocks
|
||||
private PasswordRegisterExecutorProvider passwordRegisterExecutorProvider;
|
||||
private PasswordRegisterExecutor executor;
|
||||
|
||||
@Mock
|
||||
private ValidationService validationService;
|
||||
@@ -62,10 +62,10 @@ public class PasswordRegisterExecutorProviderTest {
|
||||
String name = "player040";
|
||||
given(validationService.validatePassword(password, name)).willReturn(new ValidationResult());
|
||||
Player player = mockPlayerWithName(name);
|
||||
RegistrationExecutor executor = passwordRegisterExecutorProvider.new PasswordRegisterExecutor(player, password, null);
|
||||
PasswordRegisterParams params = PasswordRegisterParams.of(player, password, null);
|
||||
|
||||
// when
|
||||
boolean result = executor.isRegistrationAdmitted();
|
||||
boolean result = executor.isRegistrationAdmitted(params);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
@@ -80,10 +80,10 @@ public class PasswordRegisterExecutorProviderTest {
|
||||
given(validationService.validatePassword(password, name)).willReturn(
|
||||
new ValidationResult(MessageKey.PASSWORD_CHARACTERS_ERROR, "[a-z]"));
|
||||
Player player = mockPlayerWithName(name);
|
||||
RegistrationExecutor executor = passwordRegisterExecutorProvider.new PasswordRegisterExecutor(player, password, null);
|
||||
PasswordRegisterParams params = PasswordRegisterParams.of(player, password, null);
|
||||
|
||||
// when
|
||||
boolean result = executor.isRegistrationAdmitted();
|
||||
boolean result = executor.isRegistrationAdmitted(params);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(false));
|
||||
@@ -101,10 +101,10 @@ public class PasswordRegisterExecutorProviderTest {
|
||||
World world = mock(World.class);
|
||||
given(world.getName()).willReturn("someWorld");
|
||||
given(player.getLocation()).willReturn(new Location(world, 48, 96, 144));
|
||||
RegistrationExecutor executor = passwordRegisterExecutorProvider.new PasswordRegisterExecutor(player, "pass", "mail@example.org");
|
||||
PasswordRegisterParams params = PasswordRegisterParams.of(player, "pass", "mail@example.org");
|
||||
|
||||
// when
|
||||
PlayerAuth auth = executor.buildPlayerAuth();
|
||||
PlayerAuth auth = executor.buildPlayerAuth(params);
|
||||
|
||||
// then
|
||||
assertThat(auth, hasAuthBasicData("s1m0n", "S1m0N", "mail@example.org", "123.45.67.89"));
|
||||
@@ -118,10 +118,10 @@ public class PasswordRegisterExecutorProviderTest {
|
||||
given(commonService.getProperty(RegistrationSettings.FORCE_LOGIN_AFTER_REGISTER)).willReturn(false);
|
||||
given(commonService.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(false);
|
||||
Player player = mock(Player.class);
|
||||
RegistrationExecutor executor = passwordRegisterExecutorProvider.new PasswordRegisterExecutor(player, "pass", "mail@example.org");
|
||||
PasswordRegisterParams params = PasswordRegisterParams.of(player, "pass", "mail@example.org");
|
||||
|
||||
// when
|
||||
executor.executePostPersistAction();
|
||||
executor.executePostPersistAction(params);
|
||||
|
||||
// then
|
||||
TestHelper.runSyncDelayedTaskWithDelay(bukkitService);
|
||||
@@ -134,10 +134,10 @@ public class PasswordRegisterExecutorProviderTest {
|
||||
// given
|
||||
given(commonService.getProperty(RegistrationSettings.FORCE_LOGIN_AFTER_REGISTER)).willReturn(true);
|
||||
Player player = mock(Player.class);
|
||||
RegistrationExecutor executor = passwordRegisterExecutorProvider.new PasswordRegisterExecutor(player, "pass", "mail@example.org");
|
||||
PasswordRegisterParams params = PasswordRegisterParams.of(player, "pass", "mail@example.org");
|
||||
|
||||
// when
|
||||
executor.executePostPersistAction();
|
||||
executor.executePostPersistAction(params);
|
||||
|
||||
// then
|
||||
verifyZeroInteractions(bukkitService, asynchronousLogin);
|
||||
Reference in New Issue
Block a user