Merge branch 'master' of https://github.com/AuthMe/AuthMeReloaded into 1141-optional-additional-2fa-auth
# Conflicts: # src/main/java/fr/xephi/authme/permission/PlayerPermission.java # src/main/java/fr/xephi/authme/service/BukkitService.java
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
package fr.xephi.authme.data;
|
||||
|
||||
import fr.xephi.authme.permission.PermissionsManager;
|
||||
import fr.xephi.authme.permission.PlayerPermission;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.ProtectionSettings;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Test for {@link QuickCommandsProtectionManager}.
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class QuickCommandsProtectionManagerTest {
|
||||
|
||||
@Mock
|
||||
private Settings settings;
|
||||
|
||||
@Mock
|
||||
private PermissionsManager permissionsManager;
|
||||
|
||||
@Test
|
||||
public void shouldAllowCommand() {
|
||||
// given
|
||||
String playername = "PlayerName";
|
||||
Player player = mockPlayerWithName(playername);
|
||||
given(settings.getProperty(ProtectionSettings.QUICK_COMMANDS_DENIED_BEFORE_MILLISECONDS)).willReturn(0);
|
||||
given(permissionsManager.hasPermission(player, PlayerPermission.QUICK_COMMANDS_PROTECTION)).willReturn(true);
|
||||
|
||||
String name = "TestName";
|
||||
|
||||
QuickCommandsProtectionManager qcpm = createQuickCommandsProtectioneManager();
|
||||
qcpm.processJoin(player);
|
||||
|
||||
// when
|
||||
boolean test1 = qcpm.isAllowed(name);
|
||||
boolean test2 = qcpm.isAllowed(playername);
|
||||
|
||||
// then
|
||||
assertThat(test1, equalTo(true));
|
||||
assertThat(test2, equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDenyCommand() {
|
||||
// given
|
||||
String name = "TestName1";
|
||||
Player player = mockPlayerWithName(name);
|
||||
given(settings.getProperty(ProtectionSettings.QUICK_COMMANDS_DENIED_BEFORE_MILLISECONDS)).willReturn(5000);
|
||||
|
||||
QuickCommandsProtectionManager qcpm = createQuickCommandsProtectioneManager();
|
||||
given(permissionsManager.hasPermission(player, PlayerPermission.QUICK_COMMANDS_PROTECTION)).willReturn(true);
|
||||
qcpm.processJoin(player);
|
||||
|
||||
// when
|
||||
boolean test = qcpm.isAllowed(name);
|
||||
|
||||
// then
|
||||
assertThat(test, equalTo(false));
|
||||
}
|
||||
|
||||
private QuickCommandsProtectionManager createQuickCommandsProtectioneManager() {
|
||||
return new QuickCommandsProtectionManager(settings, permissionsManager);
|
||||
}
|
||||
|
||||
private static Player mockPlayerWithName(String name) {
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn(name);
|
||||
return player;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package fr.xephi.authme.listener;
|
||||
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.data.QuickCommandsProtectionManager;
|
||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.message.MessageKey;
|
||||
@@ -110,6 +111,8 @@ public class PlayerListenerTest {
|
||||
private ValidationService validationService;
|
||||
@Mock
|
||||
private JoinMessageService joinMessageService;
|
||||
@Mock
|
||||
private QuickCommandsProtectionManager quickCommandsProtectionManager;
|
||||
|
||||
/**
|
||||
* #831: If a player is kicked because of "logged in from another location", the kick
|
||||
@@ -219,6 +222,7 @@ public class PlayerListenerTest {
|
||||
// PlayerCommandPreprocessEvent#getPlayer is final, so create a spy instead of a mock
|
||||
PlayerCommandPreprocessEvent event = spy(new PlayerCommandPreprocessEvent(player, "/hub"));
|
||||
given(listenerService.shouldCancelEvent(player)).willReturn(false);
|
||||
given(quickCommandsProtectionManager.isAllowed(player.getName())).willReturn(true);
|
||||
|
||||
// when
|
||||
listener.onPlayerCommandPreprocess(event);
|
||||
@@ -238,6 +242,7 @@ public class PlayerListenerTest {
|
||||
Player player = playerWithMockedServer();
|
||||
PlayerCommandPreprocessEvent event = spy(new PlayerCommandPreprocessEvent(player, "/hub"));
|
||||
given(listenerService.shouldCancelEvent(player)).willReturn(true);
|
||||
given(quickCommandsProtectionManager.isAllowed(player.getName())).willReturn(true);
|
||||
|
||||
// when
|
||||
listener.onPlayerCommandPreprocess(event);
|
||||
@@ -248,6 +253,23 @@ public class PlayerListenerTest {
|
||||
verify(messages).send(player, MessageKey.DENIED_COMMAND);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCancelFastCommandEvent() {
|
||||
// given
|
||||
given(settings.getProperty(HooksSettings.USE_ESSENTIALS_MOTD)).willReturn(false);
|
||||
given(settings.getProperty(RestrictionSettings.ALLOW_COMMANDS)).willReturn(Arrays.asList("/spawn", "/help"));
|
||||
Player player = playerWithMockedServer();
|
||||
PlayerCommandPreprocessEvent event = spy(new PlayerCommandPreprocessEvent(player, "/hub"));
|
||||
given(quickCommandsProtectionManager.isAllowed(player.getName())).willReturn(false);
|
||||
|
||||
// when
|
||||
listener.onPlayerCommandPreprocess(event);
|
||||
|
||||
// then
|
||||
verify(event).setCancelled(true);
|
||||
verify(player).kickPlayer(messages.retrieveSingle(player, MessageKey.QUICK_COMMAND_PROTECTION_KICK));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldAllowChat() {
|
||||
// given
|
||||
|
||||
@@ -1,18 +1,21 @@
|
||||
package fr.xephi.authme.settings;
|
||||
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.security.HashAlgorithm;
|
||||
import fr.xephi.authme.service.BukkitService;
|
||||
import fr.xephi.authme.settings.properties.EmailSettings;
|
||||
import fr.xephi.authme.settings.properties.HooksSettings;
|
||||
import fr.xephi.authme.settings.properties.PluginSettings;
|
||||
import fr.xephi.authme.settings.properties.RestrictionSettings;
|
||||
import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
@@ -27,12 +30,18 @@ import static org.mockito.internal.verification.VerificationModeFactory.times;
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class SettingsWarnerTest {
|
||||
|
||||
@InjectMocks
|
||||
private SettingsWarner settingsWarner;
|
||||
|
||||
@Mock
|
||||
private Settings settings;
|
||||
|
||||
@Mock
|
||||
private AuthMe authMe;
|
||||
|
||||
@Mock
|
||||
private BukkitService bukkitService;
|
||||
|
||||
@Test
|
||||
public void shouldLogWarnings() {
|
||||
// given
|
||||
@@ -43,12 +52,14 @@ public class SettingsWarnerTest {
|
||||
given(settings.getProperty(PluginSettings.SESSIONS_ENABLED)).willReturn(true);
|
||||
given(settings.getProperty(PluginSettings.SESSIONS_TIMEOUT)).willReturn(-5);
|
||||
given(settings.getProperty(SecuritySettings.PASSWORD_HASH)).willReturn(HashAlgorithm.BCRYPT);
|
||||
given(settings.getProperty(HooksSettings.BUNGEECORD)).willReturn(false);
|
||||
given(bukkitService.isBungeeCordConfiguredForSpigot()).willReturn(Optional.of(true));
|
||||
|
||||
// when
|
||||
createSettingsWarner().logWarningsForMisconfigurations();
|
||||
settingsWarner.logWarningsForMisconfigurations();
|
||||
|
||||
// then
|
||||
verify(logger, times(3)).warning(anyString());
|
||||
verify(logger, times(4)).warning(anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -59,18 +70,12 @@ public class SettingsWarnerTest {
|
||||
given(settings.getProperty(EmailSettings.PORT25_USE_TLS)).willReturn(false);
|
||||
given(settings.getProperty(PluginSettings.SESSIONS_ENABLED)).willReturn(false);
|
||||
given(settings.getProperty(SecuritySettings.PASSWORD_HASH)).willReturn(HashAlgorithm.MD5);
|
||||
given(bukkitService.isBungeeCordConfiguredForSpigot()).willReturn(Optional.empty());
|
||||
|
||||
// when
|
||||
createSettingsWarner().logWarningsForMisconfigurations();
|
||||
settingsWarner.logWarningsForMisconfigurations();
|
||||
|
||||
// then
|
||||
verifyZeroInteractions(logger);
|
||||
}
|
||||
|
||||
private SettingsWarner createSettingsWarner() {
|
||||
SettingsWarner warner = new SettingsWarner();
|
||||
ReflectionTestUtils.setField(warner, "settings", settings);
|
||||
ReflectionTestUtils.setField(warner, "authMe", authMe);
|
||||
return warner;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user