Merge branch 'master' of https://github.com/AuthMe/AuthMeReloaded into 1141-optional-additional-2fa-auth

This commit is contained in:
ljacqu
2018-04-22 07:14:05 +02:00
56 changed files with 605 additions and 123 deletions
@@ -4,7 +4,9 @@ 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.events.EmailChangedEvent;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.service.bungeecord.BungeeSender;
@@ -15,11 +17,13 @@ import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.function.Function;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
/**
@@ -49,6 +53,9 @@ public class AsyncAddEmailTest {
@Mock
private BungeeSender bungeeSender;
@Mock
private BukkitService bukkitService;
@BeforeClass
public static void setUp() {
TestHelper.setupLogger();
@@ -66,6 +73,8 @@ public class AsyncAddEmailTest {
given(dataSource.updateEmail(any(PlayerAuth.class))).willReturn(true);
given(validationService.validateEmail(email)).willReturn(true);
given(validationService.isEmailFreeForRegistration(email, player)).willReturn(true);
EmailChangedEvent event = spy(new EmailChangedEvent(player, null, email, false));
given(bukkitService.createAndCallEvent(any(Function.class))).willReturn(event);
// when
asyncAddEmail.addEmail(player, email);
@@ -89,6 +98,8 @@ public class AsyncAddEmailTest {
given(dataSource.updateEmail(any(PlayerAuth.class))).willReturn(false);
given(validationService.validateEmail(email)).willReturn(true);
given(validationService.isEmailFreeForRegistration(email, player)).willReturn(true);
EmailChangedEvent event = spy(new EmailChangedEvent(player, null, email, false));
given(bukkitService.createAndCallEvent(any(Function.class))).willReturn(event);
// when
asyncAddEmail.addEmail(player, email);
@@ -184,4 +195,27 @@ public class AsyncAddEmailTest {
verify(playerCache, never()).updatePlayer(any(PlayerAuth.class));
}
@Test
public void shouldNotAddOnCancelledEvent() {
// given
String email = "player@mail.tld";
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(validationService.validateEmail(email)).willReturn(true);
given(validationService.isEmailFreeForRegistration(email, player)).willReturn(true);
EmailChangedEvent event = spy(new EmailChangedEvent(player, null, email, false));
event.setCancelled(true);
given(bukkitService.createAndCallEvent(any(Function.class))).willReturn(event);
// when
asyncAddEmail.addEmail(player, email);
// then
verify(service).send(player, MessageKey.EMAIL_ADD_NOT_ALLOWED);
verify(playerCache, never()).updatePlayer(any(PlayerAuth.class));
}
}
@@ -3,7 +3,9 @@ package fr.xephi.authme.process.email;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.events.EmailChangedEvent;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.service.bungeecord.BungeeSender;
@@ -14,10 +16,13 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.function.Function;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -48,6 +53,9 @@ public class AsyncChangeEmailTest {
@Mock
private BungeeSender bungeeSender;
@Mock
private BukkitService bukkitService;
@Test
public void shouldChangeEmail() {
// given
@@ -59,7 +67,9 @@ public class AsyncChangeEmailTest {
given(dataSource.updateEmail(auth)).willReturn(true);
given(validationService.validateEmail(newEmail)).willReturn(true);
given(validationService.isEmailFreeForRegistration(newEmail, player)).willReturn(true);
EmailChangedEvent event = spy(new EmailChangedEvent(player, "old@mail.tld", newEmail, false));
given(bukkitService.createAndCallEvent(any(Function.class))).willReturn(event);
// when
process.changeEmail(player, "old@mail.tld", newEmail);
@@ -81,6 +91,8 @@ public class AsyncChangeEmailTest {
given(dataSource.updateEmail(auth)).willReturn(true);
given(validationService.validateEmail(newEmail)).willReturn(true);
given(validationService.isEmailFreeForRegistration(newEmail, player)).willReturn(true);
EmailChangedEvent event = spy(new EmailChangedEvent(player, oldEmail, newEmail, false));
given(bukkitService.createAndCallEvent(any(Function.class))).willReturn(event);
// when
process.changeEmail(player, "old-mail@example.org", newEmail);
@@ -102,6 +114,8 @@ public class AsyncChangeEmailTest {
given(dataSource.updateEmail(auth)).willReturn(false);
given(validationService.validateEmail(newEmail)).willReturn(true);
given(validationService.isEmailFreeForRegistration(newEmail, player)).willReturn(true);
EmailChangedEvent event = spy(new EmailChangedEvent(player, "old@mail.tld", newEmail, false));
given(bukkitService.createAndCallEvent(any(Function.class))).willReturn(event);
// when
process.changeEmail(player, "old@mail.tld", newEmail);
@@ -219,6 +233,30 @@ public class AsyncChangeEmailTest {
verify(service).send(player, MessageKey.REGISTER_MESSAGE);
}
@Test
public void shouldNotChangeOnCancelledEvent() {
// given
String newEmail = "new@example.com";
String oldEmail = "old@example.com";
given(player.getName()).willReturn("Username");
given(playerCache.isAuthenticated("username")).willReturn(true);
PlayerAuth auth = authWithMail(oldEmail);
given(playerCache.getAuth("username")).willReturn(auth);
given(validationService.validateEmail(newEmail)).willReturn(true);
given(validationService.isEmailFreeForRegistration(newEmail, player)).willReturn(true);
EmailChangedEvent event = spy(new EmailChangedEvent(player, oldEmail, newEmail, false));
event.setCancelled(true);
given(bukkitService.createAndCallEvent(any(Function.class))).willReturn(event);
// when
process.changeEmail(player, oldEmail, newEmail);
// then
verify(dataSource, never()).updateEmail(any(PlayerAuth.class));
verify(playerCache, never()).updatePlayer(any(PlayerAuth.class));
verify(service).send(player, MessageKey.EMAIL_CHANGE_NOT_ALLOWED);
}
private static PlayerAuth authWithMail(String email) {
PlayerAuth auth = mock(PlayerAuth.class);
when(auth.getEmail()).thenReturn(email);
@@ -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);
@@ -216,18 +216,16 @@ public class PurgeTaskTest {
private void setPermissionsBehavior() {
given(permissionsManager.hasPermissionOffline(any(OfflinePlayer.class), eq(BYPASS_NODE)))
.willAnswer(new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock invocationOnMock) throws Throwable {
OfflinePlayer player = invocationOnMock.getArgument(0);
Boolean hasPermission = playerBypassAssignments.get(player);
if (hasPermission == null) {
throw new IllegalStateException("Unexpected check of '" + BYPASS_NODE
+ "' with player = " + player);
}
return hasPermission;
.willAnswer((Answer<Boolean>) invocationOnMock -> {
OfflinePlayer player = invocationOnMock.getArgument(0);
Boolean hasPermission = playerBypassAssignments.get(player);
if (hasPermission == null) {
throw new IllegalStateException("Unexpected check of '" + BYPASS_NODE
+ "' with player = " + player);
}
return hasPermission;
});
given(permissionsManager.loadUserData(any(OfflinePlayer.class))).willReturn(true);
}
private void assertRanPurgeWithPlayers(OfflinePlayer... players) {