Merge pull request #1517 from AuthMe/642-kick-on-fast-commands
#642 - Quick Command Protection
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
|
||||
|
||||
Reference in New Issue
Block a user