Test AntiBot + SwitchAntiBotCommand
This commit is contained in:
@@ -0,0 +1,212 @@
|
||||
package fr.xephi.authme;
|
||||
|
||||
import fr.xephi.authme.output.MessageKey;
|
||||
import fr.xephi.authme.output.Messages;
|
||||
import fr.xephi.authme.permission.PermissionsManager;
|
||||
import fr.xephi.authme.permission.PlayerStatePermission;
|
||||
import fr.xephi.authme.settings.NewSetting;
|
||||
import fr.xephi.authme.settings.properties.ProtectionSettings;
|
||||
import fr.xephi.authme.util.BukkitService;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static fr.xephi.authme.util.BukkitService.TICKS_PER_MINUTE;
|
||||
import static fr.xephi.authme.util.BukkitService.TICKS_PER_SECOND;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.empty;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyLong;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.reset;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Test for {@link AntiBot}.
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class AntiBotTest {
|
||||
|
||||
@Mock
|
||||
private NewSetting settings;
|
||||
@Mock
|
||||
private Messages messages;
|
||||
@Mock
|
||||
private PermissionsManager permissionsManager;
|
||||
@Mock
|
||||
private BukkitService bukkitService;
|
||||
|
||||
@Before
|
||||
public void setDefaultSettingValues() {
|
||||
given(settings.getProperty(ProtectionSettings.ENABLE_ANTIBOT)).willReturn(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldKeepAntiBotDisabled() {
|
||||
// given / when
|
||||
given(settings.getProperty(ProtectionSettings.ENABLE_ANTIBOT)).willReturn(false);
|
||||
AntiBot antiBot = new AntiBot(settings, messages, permissionsManager, bukkitService);
|
||||
|
||||
// then
|
||||
verify(bukkitService, never()).scheduleSyncDelayedTask(any(Runnable.class), anyLong());
|
||||
assertThat(antiBot.getAntiBotStatus(), equalTo(AntiBot.AntiBotStatus.DISABLED));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldTransitionToListening() {
|
||||
// given / when
|
||||
AntiBot antiBot = new AntiBot(settings, messages, permissionsManager, bukkitService);
|
||||
TestHelper.runSyncDelayedTaskWithDelay(bukkitService);
|
||||
|
||||
// then
|
||||
assertThat(antiBot.getAntiBotStatus(), equalTo(AntiBot.AntiBotStatus.LISTENING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSetStatusToActive() {
|
||||
// given
|
||||
AntiBot antiBot = createListeningAntiBot();
|
||||
|
||||
// when
|
||||
antiBot.overrideAntiBotStatus(true);
|
||||
|
||||
// then
|
||||
assertThat(antiBot.getAntiBotStatus(), equalTo(AntiBot.AntiBotStatus.ACTIVE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSetStatusToListening() {
|
||||
// given
|
||||
AntiBot antiBot = createListeningAntiBot();
|
||||
|
||||
// when
|
||||
antiBot.overrideAntiBotStatus(false);
|
||||
|
||||
// then
|
||||
assertThat(antiBot.getAntiBotStatus(), equalTo(AntiBot.AntiBotStatus.LISTENING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRemainDisabled() {
|
||||
// given
|
||||
given(settings.getProperty(ProtectionSettings.ENABLE_ANTIBOT)).willReturn(false);
|
||||
AntiBot antiBot = new AntiBot(settings, messages, permissionsManager, bukkitService);
|
||||
|
||||
// when
|
||||
antiBot.overrideAntiBotStatus(true);
|
||||
|
||||
// then
|
||||
assertThat(antiBot.getAntiBotStatus(), equalTo(AntiBot.AntiBotStatus.DISABLED));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldActivateAntiBot() {
|
||||
// given
|
||||
given(messages.retrieve(MessageKey.ANTIBOT_AUTO_ENABLED_MESSAGE))
|
||||
.willReturn(new String[]{"Test line #1", "Test line #2"});
|
||||
int duration = 300;
|
||||
given(settings.getProperty(ProtectionSettings.ANTIBOT_DURATION)).willReturn(duration);
|
||||
AntiBot antiBot = createListeningAntiBot();
|
||||
|
||||
// when
|
||||
antiBot.activateAntiBot();
|
||||
|
||||
// then
|
||||
assertThat(antiBot.getAntiBotStatus(), equalTo(AntiBot.AntiBotStatus.ACTIVE));
|
||||
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
|
||||
verify(bukkitService, times(2)).broadcastMessage(captor.capture());
|
||||
assertThat(captor.getAllValues(), contains("Test line #1", "Test line #2"));
|
||||
long expectedTicks = duration * TICKS_PER_MINUTE;
|
||||
verify(bukkitService).scheduleSyncDelayedTask(any(Runnable.class), eq(expectedTicks));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDisableAntiBotAfterSetDuration() {
|
||||
// given
|
||||
given(messages.retrieve(MessageKey.ANTIBOT_AUTO_ENABLED_MESSAGE)).willReturn(new String[0]);
|
||||
given(messages.retrieve(MessageKey.ANTIBOT_AUTO_DISABLED_MESSAGE))
|
||||
.willReturn(new String[]{"Disabled...", "Placeholder: %m."});
|
||||
given(settings.getProperty(ProtectionSettings.ANTIBOT_DURATION)).willReturn(4);
|
||||
AntiBot antiBot = createListeningAntiBot();
|
||||
|
||||
// when
|
||||
antiBot.activateAntiBot();
|
||||
TestHelper.runSyncDelayedTaskWithDelay(bukkitService);
|
||||
|
||||
// then
|
||||
assertThat(antiBot.getAntiBotStatus(), equalTo(AntiBot.AntiBotStatus.LISTENING));
|
||||
verify(bukkitService).scheduleSyncDelayedTask(any(Runnable.class), eq((long) 4800));
|
||||
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
|
||||
verify(bukkitService, times(2)).broadcastMessage(captor.capture());
|
||||
assertThat(captor.getAllValues(), contains("Disabled...", "Placeholder: 4."));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCheckPlayerAndRemoveHimLater() {
|
||||
// given
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn("Plaer");
|
||||
given(permissionsManager.hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT)).willReturn(false);
|
||||
given(settings.getProperty(ProtectionSettings.ANTIBOT_SENSIBILITY)).willReturn(10);
|
||||
AntiBot antiBot = createListeningAntiBot();
|
||||
|
||||
// when
|
||||
antiBot.checkAntiBot(player);
|
||||
|
||||
// then
|
||||
List<String> playerList = (List<String>) ReflectionTestUtils
|
||||
.getFieldValue(AntiBot.class, antiBot, "antibotPlayers");
|
||||
assertThat(playerList, hasSize(1));
|
||||
verify(bukkitService).scheduleSyncDelayedTask(any(Runnable.class), eq((long) 15 * TICKS_PER_SECOND));
|
||||
|
||||
// Follow-up: Check that player will be removed from list again by running the Runnable
|
||||
// given (2)
|
||||
// Add another player to the list
|
||||
playerList.add("other_player");
|
||||
|
||||
// when (2)
|
||||
TestHelper.runSyncDelayedTaskWithDelay(bukkitService);
|
||||
|
||||
// then (2)
|
||||
assertThat(playerList, contains("other_player"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotUpdateListForPlayerWithByPassPermission() {
|
||||
// given
|
||||
Player player = mock(Player.class);
|
||||
given(permissionsManager.hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT)).willReturn(true);
|
||||
given(settings.getProperty(ProtectionSettings.ANTIBOT_SENSIBILITY)).willReturn(3);
|
||||
AntiBot antiBot = createListeningAntiBot();
|
||||
|
||||
// when
|
||||
antiBot.checkAntiBot(player);
|
||||
|
||||
// then
|
||||
List<?> playerList = (List) ReflectionTestUtils.getFieldValue(AntiBot.class, antiBot, "antibotPlayers");
|
||||
assertThat(playerList, empty());
|
||||
verify(bukkitService, never()).scheduleSyncDelayedTask(any(Runnable.class), anyLong());
|
||||
}
|
||||
|
||||
private AntiBot createListeningAntiBot() {
|
||||
AntiBot antiBot = new AntiBot(settings, messages, permissionsManager, bukkitService);
|
||||
TestHelper.runSyncDelayedTaskWithDelay(bukkitService);
|
||||
// Make BukkitService forget about all interactions up to here
|
||||
reset(bukkitService);
|
||||
return antiBot;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package fr.xephi.authme;
|
||||
|
||||
import fr.xephi.authme.command.CommandService;
|
||||
import fr.xephi.authme.util.BukkitService;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.io.File;
|
||||
@@ -8,6 +9,7 @@ import java.net.URL;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static org.mockito.Matchers.anyLong;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
@@ -66,4 +68,32 @@ public final class TestHelper {
|
||||
runnable.run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a {@link Runnable} passed to a mock's {@link BukkitService#scheduleSyncDelayedTask(Runnable)} method.
|
||||
* Note that calling this method expects that there be a runnable sent to the method and will fail
|
||||
* otherwise.
|
||||
*
|
||||
* @param service The mock service
|
||||
*/
|
||||
public static void runSyncDelayedTask(BukkitService service) {
|
||||
ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class);
|
||||
verify(service).scheduleSyncDelayedTask(captor.capture());
|
||||
Runnable runnable = captor.getValue();
|
||||
runnable.run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a {@link Runnable} passed to a mock's {@link BukkitService#scheduleSyncDelayedTask(Runnable, long)}
|
||||
* method. Note that calling this method expects that there be a runnable sent to the method and will fail
|
||||
* otherwise.
|
||||
*
|
||||
* @param service The mock service
|
||||
*/
|
||||
public static void runSyncDelayedTaskWithDelay(BukkitService service) {
|
||||
ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class);
|
||||
verify(service).scheduleSyncDelayedTask(captor.capture(), anyLong());
|
||||
Runnable runnable = captor.getValue();
|
||||
runnable.run();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
package fr.xephi.authme.command.executable.authme;
|
||||
|
||||
import fr.xephi.authme.AntiBot;
|
||||
import fr.xephi.authme.command.CommandService;
|
||||
import fr.xephi.authme.command.ExecutableCommand;
|
||||
import fr.xephi.authme.command.FoundCommandResult;
|
||||
import fr.xephi.authme.command.help.HelpProvider;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Matchers.anyBoolean;
|
||||
import static org.mockito.Matchers.argThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Test for {@link SwitchAntiBotCommand}.
|
||||
*/
|
||||
public class SwitchAntiBotCommandTest {
|
||||
|
||||
@Test
|
||||
public void shouldReturnAntiBotState() {
|
||||
// given
|
||||
AntiBot antiBot = mock(AntiBot.class);
|
||||
given(antiBot.getAntiBotStatus()).willReturn(AntiBot.AntiBotStatus.ACTIVE);
|
||||
CommandService service = mock(CommandService.class);
|
||||
given(service.getAntiBot()).willReturn(antiBot);
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
ExecutableCommand command = new SwitchAntiBotCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.<String>emptyList(), service);
|
||||
|
||||
// then
|
||||
verify(sender).sendMessage(argThat(containsString("status: ACTIVE")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldActivateAntiBot() {
|
||||
// given
|
||||
AntiBot antiBot = mock(AntiBot.class);
|
||||
CommandService service = mock(CommandService.class);
|
||||
given(service.getAntiBot()).willReturn(antiBot);
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
ExecutableCommand command = new SwitchAntiBotCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.singletonList("on"), service);
|
||||
|
||||
// then
|
||||
verify(antiBot).overrideAntiBotStatus(true);
|
||||
verify(sender).sendMessage(argThat(containsString("enabled")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDeactivateAntiBot() {
|
||||
// given
|
||||
AntiBot antiBot = mock(AntiBot.class);
|
||||
CommandService service = mock(CommandService.class);
|
||||
given(service.getAntiBot()).willReturn(antiBot);
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
ExecutableCommand command = new SwitchAntiBotCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.singletonList("Off"), service);
|
||||
|
||||
// then
|
||||
verify(antiBot).overrideAntiBotStatus(false);
|
||||
verify(sender).sendMessage(argThat(containsString("disabled")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldShowHelpForUnknownState() {
|
||||
// given
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
|
||||
AntiBot antiBot = mock(AntiBot.class);
|
||||
FoundCommandResult foundCommandResult = mock(FoundCommandResult.class);
|
||||
CommandService service = mock(CommandService.class);
|
||||
given(service.getAntiBot()).willReturn(antiBot);
|
||||
given(service.mapPartsToCommand(sender, asList("authme", "antibot"))).willReturn(foundCommandResult);
|
||||
|
||||
ExecutableCommand command = new SwitchAntiBotCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.singletonList("wrong"), service);
|
||||
|
||||
// then
|
||||
verify(antiBot, never()).overrideAntiBotStatus(anyBoolean());
|
||||
verify(sender).sendMessage(argThat(containsString("Invalid")));
|
||||
verify(service).outputHelp(sender, foundCommandResult, HelpProvider.SHOW_ARGUMENTS);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user