#1555 Add RegisterEvent and AuthMeAsyncPreRegisterEvent (#1559)

* #1555 Add RegisterEvent and AuthMeAsyncPreRegisterEvent

* Add missing javadoc
This commit is contained in:
Gabriele C
2018-04-21 13:02:14 +02:00
committed by GitHub
parent 5194f76f39
commit baec034909
7 changed files with 191 additions and 11 deletions
@@ -156,12 +156,9 @@ public class AsynchronousLoginTest {
given(commonService.getProperty(DatabaseSettings.MYSQL_COL_GROUP)).willReturn("");
given(commonService.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(true);
doReturn(false).when(asynchronousLogin).hasReachedMaxLoggedInPlayersForIp(any(Player.class), anyString());
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
((AuthMeAsyncPreLoginEvent) invocation.getArgument(0)).setCanLogin(false);
return null;
}
doAnswer((Answer<Void>) invocation -> {
((AuthMeAsyncPreLoginEvent) invocation.getArgument(0)).setCanLogin(false);
return null;
}).when(bukkitService).callEvent(any(AuthMeAsyncPreLoginEvent.class));
// when
@@ -4,12 +4,15 @@ import ch.jalu.injector.factory.SingletonStore;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.events.AuthMeAsyncPreRegisterEvent;
import fr.xephi.authme.message.MessageKey;
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.BukkitService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import org.bukkit.entity.Player;
@@ -18,9 +21,11 @@ import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.verify;
@@ -40,6 +45,8 @@ public class AsyncRegisterTest {
@Mock
private CommonService commonService;
@Mock
private BukkitService bukkitService;
@Mock
private DataSource dataSource;
@Mock
private SingletonStore<RegistrationExecutor> registrationExecutorStore;
@@ -102,6 +109,32 @@ public class AsyncRegisterTest {
@Test
@SuppressWarnings("unchecked")
public void shouldStopForFailedExecutorCheck() {
// given
String name = "edbert";
Player player = mockPlayerWithName(name);
TestHelper.mockPlayerIp(player, "33.44.55.66");
given(playerCache.isAuthenticated(name)).willReturn(false);
given(commonService.getProperty(RegistrationSettings.IS_ENABLED)).willReturn(true);
given(dataSource.isAuthAvailable(name)).willReturn(false);
given(commonService.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(true);
RegistrationExecutor executor = mock(RegistrationExecutor.class);
TwoFactorRegisterParams params = TwoFactorRegisterParams.of(player);
singletonStoreWillReturn(registrationExecutorStore, executor);
doAnswer((Answer<Void>) invocation -> {
((AuthMeAsyncPreRegisterEvent) invocation.getArgument(0)).setCanRegister(false);
return null;
}).when(bukkitService).callEvent(any(AuthMeAsyncPreRegisterEvent.class));
// when
asyncRegister.register(RegistrationMethod.TWO_FACTOR_REGISTRATION, params);
// then
verify(dataSource, only()).isAuthAvailable(name);
}
@Test
@SuppressWarnings("unchecked")
public void shouldStopForCancelledEvent() {
// given
String name = "edbert";
Player player = mockPlayerWithName(name);
@@ -110,6 +143,7 @@ public class AsyncRegisterTest {
given(commonService.getProperty(RegistrationSettings.IS_ENABLED)).willReturn(true);
given(commonService.getProperty(RestrictionSettings.MAX_REGISTRATION_PER_IP)).willReturn(0);
given(dataSource.isAuthAvailable(name)).willReturn(false);
given(commonService.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(true);
RegistrationExecutor executor = mock(RegistrationExecutor.class);
TwoFactorRegisterParams params = TwoFactorRegisterParams.of(player);
given(executor.isRegistrationAdmitted(params)).willReturn(false);