Merge branch 'master' of https://github.com/AuthMe-Team/AuthMeReloaded into 293-translate-help-messages

Conflicts:
	src/main/java/fr/xephi/authme/command/help/HelpProvider.java
This commit is contained in:
ljacqu
2016-10-07 19:11:41 +02:00
208 changed files with 1444 additions and 1400 deletions
@@ -1,212 +0,0 @@
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.Settings;
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 Settings 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.handlePlayerJoin(player);
// then
List<String> playerList = 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.handlePlayerJoin(player);
// then
List<?> playerList = 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;
}
}
@@ -15,7 +15,7 @@ import fr.xephi.authme.process.login.ProcessSyncPlayerLogin;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.task.purge.PurgeService;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.service.BukkitService;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.plugin.PluginDescriptionFile;
@@ -1,6 +1,6 @@
package fr.xephi.authme;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.security.crypts.HashedPassword;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
@@ -4,7 +4,6 @@ import fr.xephi.authme.output.LogLevel;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.StringUtils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
@@ -135,7 +134,7 @@ public class ConsoleLoggerTest {
assertThat(loggedLines.get(1),
containsString("[WARN] Exception occurred: [IllegalStateException]: Test exception message"));
// Check that we have this class' full name somewhere in the file -> stacktrace of Exception e
assertThat(StringUtils.join("", loggedLines), containsString(getClass().getCanonicalName()));
assertThat(String.join("", loggedLines), containsString(getClass().getCanonicalName()));
}
@Test
@@ -1,6 +1,6 @@
package fr.xephi.authme;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.service.BukkitService;
import org.bukkit.entity.Player;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
@@ -2,13 +2,13 @@ package fr.xephi.authme.api;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.hooks.PluginHooks;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.util.ValidationService;
import fr.xephi.authme.service.ValidationService;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Server;
@@ -296,17 +296,12 @@ public class CommandInitializerTest {
* @return List of all bindings that lead to the command
*/
private static List<String> getAbsoluteLabels(CommandDescription command) {
String parentPath = "";
CommandDescription elem = command.getParent();
while (elem != null) {
parentPath = elem.getLabels().get(0) + " " + parentPath;
elem = elem.getParent();
}
parentPath = parentPath.trim();
CommandDescription parent = command.getParent();
String parentPath = (parent == null) ? "" : parent.getLabels().get(0) + " ";
List<String> bindings = new ArrayList<>(command.getLabels().size());
for (String label : command.getLabels()) {
bindings.add(StringUtils.join(" ", parentPath, label));
bindings.add(parentPath + label);
}
return bindings;
}
@@ -1,11 +1,11 @@
package fr.xephi.authme.command;
import com.github.authme.configme.properties.Property;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.message.Messages;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.ValidationService;
import fr.xephi.authme.service.ValidationService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Test;
@@ -3,10 +3,6 @@ package fr.xephi.authme.command;
import fr.xephi.authme.TestHelper;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
@@ -15,42 +11,6 @@ import static org.junit.Assert.assertThat;
*/
public class CommandUtilsTest {
@Test
public void shouldPrintPartsForStringRepresentation() {
// given
Iterable<String> parts = Arrays.asList("some", "parts", "for", "test");
// when
String str = CommandUtils.labelsToString(parts);
// then
assertThat(str, equalTo("some parts for test"));
}
@Test
public void shouldPrintEmptyStringForNoArguments() {
// given
List<String> parts = Collections.emptyList();
// when
String str = CommandUtils.labelsToString(parts);
// then
assertThat(str, equalTo(""));
}
@Test
public void shouldPrintLabels() {
// given
List<String> labels = Arrays.asList("authme", "help", "reload");
// when
String result = CommandUtils.labelsToString(labels);
// then
assertThat(result, equalTo("authme help reload"));
}
@Test
public void shouldReturnCommandPath() {
// given
@@ -1,10 +1,10 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.BukkitService;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -1,16 +1,16 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.ValidationService;
import fr.xephi.authme.util.ValidationService.ValidationResult;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.service.ValidationService.ValidationResult;
import org.bukkit.command.CommandSender;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -3,9 +3,9 @@ package fr.xephi.authme.command.executable.authme;
import ch.jalu.injector.Injector;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.converter.Converter;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.datasource.converter.Converter;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.util.StringUtils;
import org.bukkit.command.CommandSender;
import org.junit.BeforeClass;
@@ -3,7 +3,7 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PlayerPermission;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.service.BukkitService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Test;
@@ -1,9 +1,9 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.message.MessageKey;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -1,7 +1,7 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.service.BukkitService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Test;
@@ -1,9 +1,9 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.message.MessageKey;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -1,7 +1,7 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.task.purge.PurgeService;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.service.BukkitService;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.junit.Test;
@@ -1,9 +1,9 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.message.MessageKey;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -1,16 +1,16 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.limbo.LimboCache;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.ValidationService;
import fr.xephi.authme.util.ValidationService.ValidationResult;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.service.ValidationService.ValidationResult;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.BeforeClass;
@@ -9,7 +9,7 @@ import fr.xephi.authme.datasource.DataSourceType;
import fr.xephi.authme.initialization.Reloadable;
import fr.xephi.authme.initialization.SettingsDependent;
import fr.xephi.authme.output.LogLevel;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.DatabaseSettings;
import fr.xephi.authme.settings.properties.PluginSettings;
@@ -1,11 +1,11 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.BukkitService;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -1,6 +1,6 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.AntiBot;
import fr.xephi.authme.service.AntiBotService;
import fr.xephi.authme.command.CommandMapper;
import fr.xephi.authme.command.FoundCommandResult;
import fr.xephi.authme.command.help.HelpProvider;
@@ -32,7 +32,7 @@ public class SwitchAntiBotCommandTest {
private SwitchAntiBotCommand command;
@Mock
private AntiBot antiBot;
private AntiBotService antiBot;
@Mock
private CommandMapper commandMapper;
@@ -43,7 +43,7 @@ public class SwitchAntiBotCommandTest {
@Test
public void shouldReturnAntiBotState() {
// given
given(antiBot.getAntiBotStatus()).willReturn(AntiBot.AntiBotStatus.ACTIVE);
given(antiBot.getAntiBotStatus()).willReturn(AntiBotService.AntiBotStatus.ACTIVE);
CommandSender sender = mock(CommandSender.class);
// when
@@ -2,9 +2,9 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.service.BukkitService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Test;
@@ -1,9 +1,9 @@
package fr.xephi.authme.command.executable.captcha;
import fr.xephi.authme.cache.CaptchaManager;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.data.CaptchaManager;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.message.MessageKey;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -1,13 +1,13 @@
package fr.xephi.authme.command.executable.changepassword;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.ValidationService;
import fr.xephi.authme.util.ValidationService.ValidationResult;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.service.ValidationService.ValidationResult;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@@ -1,7 +1,7 @@
package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.Management;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
@@ -1,15 +1,15 @@
package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.mail.SendMailSSL;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.service.RecoveryCodeManager;
import fr.xephi.authme.service.RecoveryCodeService;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import org.bukkit.entity.Player;
@@ -63,7 +63,7 @@ public class RecoverEmailCommandTest {
private SendMailSSL sendMailSsl;
@Mock
private RecoveryCodeManager recoveryCodeManager;
private RecoveryCodeService recoveryCodeService;
@BeforeClass
public static void initLogger() {
@@ -177,8 +177,8 @@ public class RecoverEmailCommandTest {
int hoursValid = 12;
given(commandService.getProperty(SecuritySettings.RECOVERY_CODE_HOURS_VALID)).willReturn(hoursValid);
String code = "a94f37";
given(recoveryCodeManager.isRecoveryCodeNeeded()).willReturn(true);
given(recoveryCodeManager.generateCode(name)).willReturn(code);
given(recoveryCodeService.isRecoveryCodeNeeded()).willReturn(true);
given(recoveryCodeService.generateCode(name)).willReturn(code);
// when
command.executeCommand(sender, Collections.singletonList(email.toUpperCase()));
@@ -186,7 +186,7 @@ public class RecoverEmailCommandTest {
// then
verify(sendMailSsl).hasAllInformation();
verify(dataSource).getAuth(name);
verify(recoveryCodeManager).generateCode(name);
verify(recoveryCodeService).generateCode(name);
verify(commandService).send(sender, MessageKey.RECOVERY_CODE_SENT);
verify(sendMailSsl).sendRecoveryCode(name, email, code);
}
@@ -203,8 +203,8 @@ public class RecoverEmailCommandTest {
PlayerAuth auth = newAuthWithEmail(email);
given(dataSource.getAuth(name)).willReturn(auth);
given(commandService.getProperty(EmailSettings.RECOVERY_PASSWORD_LENGTH)).willReturn(20);
given(recoveryCodeManager.isRecoveryCodeNeeded()).willReturn(true);
given(recoveryCodeManager.isCodeValid(name, "bogus")).willReturn(false);
given(recoveryCodeService.isRecoveryCodeNeeded()).willReturn(true);
given(recoveryCodeService.isCodeValid(name, "bogus")).willReturn(false);
// when
command.executeCommand(sender, Arrays.asList(email, "bogus"));
@@ -231,8 +231,8 @@ public class RecoverEmailCommandTest {
given(commandService.getProperty(EmailSettings.RECOVERY_PASSWORD_LENGTH)).willReturn(20);
given(passwordSecurity.computeHash(anyString(), eq(name)))
.willAnswer(invocation -> new HashedPassword((String) invocation.getArguments()[0]));
given(recoveryCodeManager.isRecoveryCodeNeeded()).willReturn(true);
given(recoveryCodeManager.isCodeValid(name, code)).willReturn(true);
given(recoveryCodeService.isRecoveryCodeNeeded()).willReturn(true);
given(recoveryCodeService.isCodeValid(name, code)).willReturn(true);
// when
command.executeCommand(sender, Arrays.asList(email, code));
@@ -245,7 +245,7 @@ public class RecoverEmailCommandTest {
String generatedPassword = passwordCaptor.getValue();
assertThat(generatedPassword, stringWithLength(20));
verify(dataSource).updatePassword(eq(name), any(HashedPassword.class));
verify(recoveryCodeManager).removeCode(name);
verify(recoveryCodeService).removeCode(name);
verify(sendMailSsl).sendPasswordMail(name, email, generatedPassword);
verify(commandService).send(sender, MessageKey.RECOVERY_EMAIL_SENT_MESSAGE);
}
@@ -264,7 +264,7 @@ public class RecoverEmailCommandTest {
given(commandService.getProperty(EmailSettings.RECOVERY_PASSWORD_LENGTH)).willReturn(20);
given(passwordSecurity.computeHash(anyString(), eq(name)))
.willAnswer(invocation -> new HashedPassword((String) invocation.getArguments()[0]));
given(recoveryCodeManager.isRecoveryCodeNeeded()).willReturn(false);
given(recoveryCodeService.isRecoveryCodeNeeded()).willReturn(false);
// when
command.executeCommand(sender, Collections.singletonList(email));
@@ -3,7 +3,7 @@ package fr.xephi.authme.command.executable.register;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.mail.SendMailSSL;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.settings.properties.EmailSettings;
@@ -1,8 +1,8 @@
package fr.xephi.authme.command.executable.unregister;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.Management;
import org.bukkit.entity.Player;
import org.junit.Test;
@@ -1,4 +1,4 @@
package fr.xephi.authme.cache;
package fr.xephi.authme.data;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.settings.Settings;
@@ -1,4 +1,4 @@
package fr.xephi.authme.cache;
package fr.xephi.authme.data;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.settings.Settings;
@@ -1,13 +1,13 @@
package fr.xephi.authme.cache;
package fr.xephi.authme.data;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.cache.TempbanManager.TimedCounter;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.data.TempbanManager.TimedCounter;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.message.Messages;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.service.BukkitService;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -1,15 +1,15 @@
package fr.xephi.authme.cache.backup;
package fr.xephi.authme.data.backup;
import ch.jalu.injector.testing.BeforeInjecting;
import ch.jalu.injector.testing.DelayedInjectionRunner;
import ch.jalu.injector.testing.InjectDelayed;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.cache.limbo.PlayerData;
import fr.xephi.authme.data.limbo.LimboPlayer;
import fr.xephi.authme.initialization.DataFolder;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.settings.SpawnLoader;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.util.FileUtils;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
@@ -32,16 +32,16 @@ import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Test for {@link PlayerDataStorage}.
* Test for {@link LimboPlayerStorage}.
*/
@RunWith(DelayedInjectionRunner.class)
public class PlayerDataStorageTest {
public class LimboPlayerStorageTest {
private static final UUID SAMPLE_UUID = UUID.nameUUIDFromBytes("PlayerDataStorageTest".getBytes());
private static final String SOURCE_FOLDER = TestHelper.PROJECT_ROOT + "cache/backup/";
private static final String SOURCE_FOLDER = TestHelper.PROJECT_ROOT + "data/backup/";
@InjectDelayed
private PlayerDataStorage playerDataStorage;
private LimboPlayerStorage limboPlayerStorage;
@Mock
private SpawnLoader spawnLoader;
@@ -61,11 +61,11 @@ public class PlayerDataStorageTest {
@BeforeInjecting
public void copyTestFiles() throws IOException {
dataFolder = temporaryFolder.newFolder();
File playerFolder = new File(dataFolder, StringUtils.makePath("playerdata", SAMPLE_UUID.toString()));
File playerFolder = new File(dataFolder, FileUtils.makePath("playerdata", SAMPLE_UUID.toString()));
if (!playerFolder.mkdirs()) {
throw new IllegalStateException("Cannot create '" + playerFolder.getAbsolutePath() + "'");
}
Files.copy(TestHelper.getJarPath(StringUtils.makePath(SOURCE_FOLDER, "sample-folder", "data.json")),
Files.copy(TestHelper.getJarPath(FileUtils.makePath(SOURCE_FOLDER, "sample-folder", "data.json")),
new File(playerFolder, "data.json").toPath());
}
@@ -78,7 +78,7 @@ public class PlayerDataStorageTest {
given(bukkitService.getWorld("nether")).willReturn(world);
// when
PlayerData data = playerDataStorage.readData(player);
LimboPlayer data = limboPlayerStorage.readData(player);
// then
assertThat(data, not(nullValue()));
@@ -103,7 +103,7 @@ public class PlayerDataStorageTest {
given(player.getUniqueId()).willReturn(UUID.nameUUIDFromBytes("other-player".getBytes()));
// when
PlayerData data = playerDataStorage.readData(player);
LimboPlayer data = limboPlayerStorage.readData(player);
// then
assertThat(data, nullValue());
@@ -118,8 +118,8 @@ public class PlayerDataStorageTest {
given(player2.getUniqueId()).willReturn(UUID.nameUUIDFromBytes("not-stored".getBytes()));
// when / then
assertThat(playerDataStorage.hasData(player1), equalTo(true));
assertThat(playerDataStorage.hasData(player2), equalTo(false));
assertThat(limboPlayerStorage.hasData(player1), equalTo(true));
assertThat(limboPlayerStorage.hasData(player2), equalTo(false));
}
@Test
@@ -140,10 +140,10 @@ public class PlayerDataStorageTest {
given(spawnLoader.getPlayerLocationOrSpawn(player)).willReturn(location);
// when
playerDataStorage.saveData(player);
limboPlayerStorage.saveData(player);
// then
File playerFile = new File(dataFolder, StringUtils.makePath("playerdata", uuid.toString(), "data.json"));
File playerFile = new File(dataFolder, FileUtils.makePath("playerdata", uuid.toString(), "data.json"));
assertThat(playerFile.exists(), equalTo(true));
// TODO ljacqu 20160711: Check contents of file
}
@@ -1,7 +1,7 @@
package fr.xephi.authme.cache.limbo;
package fr.xephi.authme.data.limbo;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.cache.backup.PlayerDataStorage;
import fr.xephi.authme.data.backup.LimboPlayerStorage;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.SpawnLoader;
@@ -44,7 +44,7 @@ public class LimboCacheTest {
private SpawnLoader spawnLoader;
@Mock
private PlayerDataStorage playerDataStorage;
private LimboPlayerStorage limboPlayerStorage;
@Test
public void shouldAddPlayerData() {
@@ -63,13 +63,13 @@ public class LimboCacheTest {
given(permissionsManager.hasGroupSupport()).willReturn(true);
String group = "test-group";
given(permissionsManager.getPrimaryGroup(player)).willReturn(group);
given(playerDataStorage.hasData(player)).willReturn(false);
given(limboPlayerStorage.hasData(player)).willReturn(false);
// when
limboCache.addPlayerData(player);
// then
PlayerData limboPlayer = limboCache.getPlayerData(name);
LimboPlayer limboPlayer = limboCache.getPlayerData(name);
assertThat(limboPlayer.getLocation(), equalTo(location));
assertThat(limboPlayer.isOperator(), equalTo(true));
assertThat(limboPlayer.getWalkSpeed(), equalTo(walkSpeed));
@@ -84,22 +84,22 @@ public class LimboCacheTest {
String name = "player01";
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
given(playerDataStorage.hasData(player)).willReturn(true);
PlayerData playerData = mock(PlayerData.class);
given(playerDataStorage.readData(player)).willReturn(playerData);
given(limboPlayerStorage.hasData(player)).willReturn(true);
LimboPlayer limboPlayer = mock(LimboPlayer.class);
given(limboPlayerStorage.readData(player)).willReturn(limboPlayer);
float walkSpeed = 2.4f;
given(playerData.getWalkSpeed()).willReturn(walkSpeed);
given(playerData.isCanFly()).willReturn(true);
given(limboPlayer.getWalkSpeed()).willReturn(walkSpeed);
given(limboPlayer.isCanFly()).willReturn(true);
float flySpeed = 1.0f;
given(playerData.getFlySpeed()).willReturn(flySpeed);
given(limboPlayer.getFlySpeed()).willReturn(flySpeed);
String group = "primary-group";
given(playerData.getGroup()).willReturn(group);
given(limboPlayer.getGroup()).willReturn(group);
// when
limboCache.addPlayerData(player);
// then
PlayerData result = limboCache.getPlayerData(name);
LimboPlayer result = limboCache.getPlayerData(name);
assertThat(result.getWalkSpeed(), equalTo(walkSpeed));
assertThat(result.isCanFly(), equalTo(true));
assertThat(result.getFlySpeed(), equalTo(flySpeed));
@@ -112,16 +112,16 @@ public class LimboCacheTest {
String name = "Champ";
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
PlayerData playerData = mock(PlayerData.class);
given(playerData.isOperator()).willReturn(true);
LimboPlayer limboPlayer = mock(LimboPlayer.class);
given(limboPlayer.isOperator()).willReturn(true);
float walkSpeed = 2.4f;
given(playerData.getWalkSpeed()).willReturn(walkSpeed);
given(playerData.isCanFly()).willReturn(true);
given(limboPlayer.getWalkSpeed()).willReturn(walkSpeed);
given(limboPlayer.isCanFly()).willReturn(true);
float flySpeed = 1.0f;
given(playerData.getFlySpeed()).willReturn(flySpeed);
given(limboPlayer.getFlySpeed()).willReturn(flySpeed);
String group = "primary-group";
given(playerData.getGroup()).willReturn(group);
getCache().put(name.toLowerCase(), playerData);
given(limboPlayer.getGroup()).willReturn(group);
getCache().put(name.toLowerCase(), limboPlayer);
given(settings.getProperty(PluginSettings.ENABLE_PERMISSION_CHECK)).willReturn(true);
given(permissionsManager.hasGroupSupport()).willReturn(true);
@@ -134,7 +134,7 @@ public class LimboCacheTest {
verify(player).setAllowFlight(true);
verify(player).setFlySpeed(flySpeed);
verify(permissionsManager).setGroup(player, group);
verify(playerData).clearTasks();
verify(limboPlayer).clearTasks();
}
@Test
@@ -143,14 +143,14 @@ public class LimboCacheTest {
String name = "Champ";
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
PlayerData playerData = mock(PlayerData.class);
given(playerData.isOperator()).willReturn(true);
given(playerData.getWalkSpeed()).willReturn(0f);
given(playerData.isCanFly()).willReturn(true);
given(playerData.getFlySpeed()).willReturn(0f);
LimboPlayer limboPlayer = mock(LimboPlayer.class);
given(limboPlayer.isOperator()).willReturn(true);
given(limboPlayer.getWalkSpeed()).willReturn(0f);
given(limboPlayer.isCanFly()).willReturn(true);
given(limboPlayer.getFlySpeed()).willReturn(0f);
String group = "primary-group";
given(playerData.getGroup()).willReturn(group);
getCache().put(name.toLowerCase(), playerData);
given(limboPlayer.getGroup()).willReturn(group);
getCache().put(name.toLowerCase(), limboPlayer);
given(settings.getProperty(PluginSettings.ENABLE_PERMISSION_CHECK)).willReturn(true);
given(permissionsManager.hasGroupSupport()).willReturn(true);
@@ -180,9 +180,9 @@ public class LimboCacheTest {
@Test
public void shouldRemoveAndClearTasks() {
// given
PlayerData playerData = mock(PlayerData.class);
LimboPlayer limboPlayer = mock(LimboPlayer.class);
String name = "abcdef";
getCache().put(name, playerData);
getCache().put(name, limboPlayer);
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
@@ -191,16 +191,16 @@ public class LimboCacheTest {
// then
assertThat(getCache(), anEmptyMap());
verify(playerData).clearTasks();
verify(limboPlayer).clearTasks();
}
@Test
public void shouldDeleteFromCacheAndStorage() {
// given
PlayerData playerData = mock(PlayerData.class);
LimboPlayer limboPlayer = mock(LimboPlayer.class);
String name = "SomeName";
getCache().put(name.toLowerCase(), playerData);
getCache().put("othername", mock(PlayerData.class));
getCache().put(name.toLowerCase(), limboPlayer);
getCache().put("othername", mock(LimboPlayer.class));
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
@@ -209,22 +209,22 @@ public class LimboCacheTest {
// then
assertThat(getCache(), aMapWithSize(1));
verify(playerData).clearTasks();
verify(playerDataStorage).removeData(player);
verify(limboPlayer).clearTasks();
verify(limboPlayerStorage).removeData(player);
}
@Test
public void shouldReturnIfHasData() {
// given
String name = "tester";
getCache().put(name, mock(PlayerData.class));
getCache().put(name, mock(LimboPlayer.class));
// when / then
assertThat(limboCache.hasPlayerData(name), equalTo(true));
assertThat(limboCache.hasPlayerData("someone_else"), equalTo(false));
}
private Map<String, PlayerData> getCache() {
private Map<String, LimboPlayer> getCache() {
return ReflectionTestUtils.getFieldValue(LimboCache.class, limboCache, "cache");
}
}
@@ -1,6 +1,6 @@
package fr.xephi.authme.datasource;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.security.crypts.HashedPassword;
import org.junit.Test;
@@ -6,7 +6,7 @@ import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.settings.Settings;
@@ -2,7 +2,9 @@ package fr.xephi.authme.datasource;
import com.google.common.io.Files;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.datasource.FlatFile;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -23,7 +25,7 @@ import static org.junit.Assert.assertThat;
/**
* Integration test for the deprecated {@link FlatFile} datasource. The flatfile datasource is no longer used.
* Essentially, the only time we use it is in {@link fr.xephi.authme.converter.ForceFlatToSqlite},
* Essentially, the only time we use it is in {@link fr.xephi.authme.datasource.converter.ForceFlatToSqlite},
* which requires {@link FlatFile#getAllAuths()}.
*/
public class FlatFileIntegrationTest {
@@ -4,6 +4,9 @@ import com.github.authme.configme.properties.Property;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.datasource.AbstractDataSourceIntegrationTest;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.datasource.MySQL;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.DatabaseSettings;
import org.junit.After;
@@ -1,6 +1,9 @@
package fr.xephi.authme.datasource;
import com.zaxxer.hikari.HikariDataSource;
import fr.xephi.authme.datasource.AbstractResourceClosingTest;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.datasource.MySQL;
import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.settings.Settings;
@@ -2,7 +2,10 @@ package fr.xephi.authme.datasource;
import com.github.authme.configme.properties.Property;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.AbstractDataSourceIntegrationTest;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.datasource.SQLite;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.DatabaseSettings;
import org.junit.After;
@@ -1,5 +1,8 @@
package fr.xephi.authme.datasource;
import fr.xephi.authme.datasource.AbstractResourceClosingTest;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.datasource.SQLite;
import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.settings.Settings;
@@ -1,7 +1,7 @@
package fr.xephi.authme.converter;
package fr.xephi.authme.datasource.converter;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.datasource.DataSourceType;
import org.bukkit.command.CommandSender;
@@ -1,9 +1,9 @@
package fr.xephi.authme.converter;
package fr.xephi.authme.datasource.converter;
import ch.jalu.injector.testing.DelayedInjectionRunner;
import ch.jalu.injector.testing.InjectDelayed;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.initialization.DataFolder;
import fr.xephi.authme.settings.Settings;
@@ -45,7 +45,7 @@ public class CrazyLoginConverterTest {
private Settings settings;
@DataFolder
private File dataFolder = TestHelper.getJarFile(TestHelper.PROJECT_ROOT + "converter/");
private File dataFolder = TestHelper.getJarFile(TestHelper.PROJECT_ROOT + "/datasource/converter/");
@BeforeClass
public static void initializeLogger() {
@@ -1,8 +1,8 @@
package fr.xephi.authme.converter;
package fr.xephi.authme.datasource.converter;
import com.google.common.io.Files;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.datasource.DataSourceType;
import fr.xephi.authme.datasource.FlatFile;
@@ -1,4 +1,4 @@
package fr.xephi.authme.util;
package fr.xephi.authme.geoip;
import com.maxmind.geoip.Country;
import com.maxmind.geoip.LookupService;
@@ -22,12 +22,12 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
/**
* Test for {@link GeoLiteAPI}.
* Test for {@link GeoIpManager}.
*/
@RunWith(MockitoJUnitRunner.class)
public class GeoLiteAPITest {
public class GeoIpManagerTest {
private GeoLiteAPI geoLiteApi;
private GeoIpManager geoIpManager;
private File dataFolder;
@Mock
private LookupService lookupService;
@@ -38,7 +38,7 @@ public class GeoLiteAPITest {
@Before
public void initializeGeoLiteApi() throws IOException {
dataFolder = temporaryFolder.newFolder();
geoLiteApi = new GeoLiteAPI(dataFolder, lookupService);
geoIpManager = new GeoIpManager(dataFolder, lookupService);
}
@Test
@@ -51,7 +51,7 @@ public class GeoLiteAPITest {
given(lookupService.getCountry(ip)).willReturn(country);
// when
String result = geoLiteApi.getCountryCode(ip);
String result = geoIpManager.getCountryCode(ip);
// then
assertThat(result, equalTo(countryCode));
@@ -64,7 +64,7 @@ public class GeoLiteAPITest {
String ip = "127.0.0.1";
// when
String result = geoLiteApi.getCountryCode(ip);
String result = geoIpManager.getCountryCode(ip);
// then
assertThat(result, equalTo("--"));
@@ -81,7 +81,7 @@ public class GeoLiteAPITest {
given(lookupService.getCountry(ip)).willReturn(country);
// when
String result = geoLiteApi.getCountryName(ip);
String result = geoIpManager.getCountryName(ip);
// then
assertThat(result, equalTo(countryName));
@@ -94,7 +94,7 @@ public class GeoLiteAPITest {
String ip = "127.0.0.1";
// when
String result = geoLiteApi.getCountryName(ip);
String result = geoIpManager.getCountryName(ip);
// then
assertThat(result, equalTo("N/A"));
@@ -3,12 +3,12 @@ package fr.xephi.authme.listener;
import ch.jalu.injector.testing.BeforeInjecting;
import ch.jalu.injector.testing.DelayedInjectionRunner;
import ch.jalu.injector.testing.InjectDelayed;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.hooks.PluginHooks;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.util.ValidationService;
import fr.xephi.authme.service.ValidationService;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
@@ -1,20 +1,19 @@
package fr.xephi.authme.listener;
import fr.xephi.authme.AntiBot;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.message.Messages;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PlayerStatePermission;
import fr.xephi.authme.service.AntiBotService;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.ProtectionSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.ValidationService;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.ValidationService;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerLoginEvent;
@@ -62,7 +61,7 @@ public class OnJoinVerifierTest {
@Mock
private PermissionsManager permissionsManager;
@Mock
private AntiBot antiBot;
private AntiBotService antiBotService;
@Mock
private ValidationService validationService;
@Mock
@@ -377,48 +376,53 @@ public class OnJoinVerifierTest {
}
@Test
public void shouldCheckAntiBot() throws FailedVerificationException {
public void shouldAllowUser() throws FailedVerificationException {
// given
String name = "user123";
boolean hasAuth = false;
given(antiBot.getAntiBotStatus()).willReturn(AntiBot.AntiBotStatus.LISTENING);
Player player = newPlayerWithName("Bobby");
boolean isAuthAvailable = false;
given(permissionsManager.hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT)).willReturn(false);
given(antiBotService.shouldKick(isAuthAvailable)).willReturn(false);
// when
onJoinVerifier.checkAntibot(name, hasAuth);
onJoinVerifier.checkAntibot(player, isAuthAvailable);
// then
verify(antiBot).getAntiBotStatus();
verify(permissionsManager).hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT);
verify(antiBotService).shouldKick(isAuthAvailable);
}
@Test
public void shouldAllowUserWithAuth() throws FailedVerificationException {
public void shouldAllowUserWithBypassPermission() throws FailedVerificationException {
// given
String name = "Bobby";
boolean hasAuth = true;
given(antiBot.getAntiBotStatus()).willReturn(AntiBot.AntiBotStatus.ACTIVE);
Player player = newPlayerWithName("Steward");
boolean isAuthAvailable = false;
given(permissionsManager.hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT)).willReturn(true);
given(antiBotService.shouldKick(isAuthAvailable)).willReturn(true);
// when
onJoinVerifier.checkAntibot(name, hasAuth);
onJoinVerifier.checkAntibot(player, isAuthAvailable);
// then
verify(antiBot).getAntiBotStatus();
verify(permissionsManager).hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT);
}
@Test
public void shouldThrowForActiveAntiBot() {
public void shouldKickUserForFailedAntibotCheck() throws FailedVerificationException {
// given
String name = "Bobby";
boolean hasAuth = false;
given(antiBot.getAntiBotStatus()).willReturn(AntiBot.AntiBotStatus.ACTIVE);
Player player = newPlayerWithName("D3");
boolean isAuthAvailable = false;
given(permissionsManager.hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT)).willReturn(false);
given(antiBotService.shouldKick(isAuthAvailable)).willReturn(true);
// when / then
try {
onJoinVerifier.checkAntibot(name, hasAuth);
onJoinVerifier.checkAntibot(player, isAuthAvailable);
fail("Expected exception to be thrown");
} catch (FailedVerificationException e) {
assertThat(e, exceptionWithData(MessageKey.KICK_ANTIBOT));
verify(antiBot).addPlayerKick(name);
verify(permissionsManager).hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT);
verify(antiBotService).shouldKick(isAuthAvailable);
}
}
/**
@@ -489,7 +493,7 @@ public class OnJoinVerifierTest {
return player;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings("unchecked")
private void returnOnlineListFromBukkitServer(Collection<Player> onlineList) {
// Note ljacqu 20160529: The compiler gets lost in generics because Collection<? extends Player> is returned
// from getOnlinePlayers(). We need to uncheck onlineList to a simple Collection or it will refuse to compile.
@@ -511,7 +515,7 @@ public class OnJoinVerifierTest {
@Override
public void describeTo(Description description) {
description.appendValue("VerificationFailedException: reason=" + messageKey + ";args="
+ (args == null ? "null" : StringUtils.join(", ", args)));
+ (args == null ? "null" : String.join(", ", args)));
}
};
}
@@ -1,18 +1,18 @@
package fr.xephi.authme.listener;
import fr.xephi.authme.AntiBot;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.service.AntiBotService;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.message.Messages;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.SpawnLoader;
import fr.xephi.authme.settings.properties.HooksSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.TeleportationService;
import fr.xephi.authme.util.ValidationService;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.TeleportationService;
import fr.xephi.authme.service.ValidationService;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
@@ -80,7 +80,7 @@ public class PlayerListenerTest {
@Mock
private DataSource dataSource;
@Mock
private AntiBot antiBot;
private AntiBotService antiBotService;
@Mock
private Management management;
@Mock
@@ -112,7 +112,7 @@ public class PlayerListenerTest {
// then
assertThat(event.isCancelled(), equalTo(true));
verifyZeroInteractions(player, management, antiBot);
verifyZeroInteractions(player, management, antiBotService);
}
@Test
@@ -122,14 +122,14 @@ public class PlayerListenerTest {
String name = "Bobby";
Player player = mockPlayerWithName(name);
PlayerKickEvent event = new PlayerKickEvent(player, "You logged in from another location", "");
given(antiBot.wasPlayerKicked(name)).willReturn(false);
given(antiBotService.wasPlayerKicked(name)).willReturn(false);
// when
listener.onPlayerKick(event);
// then
assertThat(event.isCancelled(), equalTo(false));
verify(antiBot).wasPlayerKicked(name);
verify(antiBotService).wasPlayerKicked(name);
verify(management).performQuit(player);
}
@@ -140,14 +140,14 @@ public class PlayerListenerTest {
String name = "Bobby";
Player player = mockPlayerWithName(name);
PlayerKickEvent event = new PlayerKickEvent(player, "No longer desired here!", "");
given(antiBot.wasPlayerKicked(name)).willReturn(true);
given(antiBotService.wasPlayerKicked(name)).willReturn(true);
// when
listener.onPlayerKick(event);
// then
assertThat(event.isCancelled(), equalTo(false));
verify(antiBot).wasPlayerKicked(name);
verify(antiBotService).wasPlayerKicked(name);
verifyZeroInteractions(management);
}
@@ -560,11 +560,11 @@ public class PlayerListenerTest {
verify(onJoinVerifier).refusePlayerForFullServer(event);
verify(onJoinVerifier).checkSingleSession(name);
verify(onJoinVerifier).checkIsValidName(name);
verify(onJoinVerifier).checkAntibot(name, true);
verify(onJoinVerifier).checkAntibot(player, true);
verify(onJoinVerifier).checkKickNonRegistered(true);
verify(onJoinVerifier).checkNameCasing(player, auth);
verify(onJoinVerifier).checkPlayerCountry(true, ip);
verify(antiBot).handlePlayerJoin(player);
verify(antiBotService).handlePlayerJoin();
verify(teleportationService).teleportOnJoin(player);
verifyNoModifyingCalls(event);
}
@@ -1,4 +1,4 @@
package fr.xephi.authme.output;
package fr.xephi.authme.message;
import fr.xephi.authme.util.StringUtils;
import org.junit.Test;
@@ -1,4 +1,4 @@
package fr.xephi.authme.output;
package fr.xephi.authme.message;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.util.StringUtils;
@@ -32,7 +32,7 @@ public class MessagesFileConsistencyTest {
if (!errors.isEmpty()) {
fail("Validation errors in " + MESSAGES_FILE + ":\n- "
+ StringUtils.join("\n- ", errors));
+ String.join("\n- ", errors));
}
}
@@ -55,7 +55,7 @@ public class MessagesFileConsistencyTest {
if (!missingTags.isEmpty()) {
String pluralS = missingTags.size() > 1 ? "s" : "";
errors.add(String.format("Message with key '%s' missing tag%s: %s", key, pluralS,
StringUtils.join(", ", missingTags)));
String.join(", ", missingTags)));
}
}
}
@@ -1,4 +1,4 @@
package fr.xephi.authme.output;
package fr.xephi.authme.message;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.util.StringUtils;
@@ -48,7 +48,7 @@ public class MessagesFileYamlCheckerTest {
// then
if (!errors.isEmpty()) {
fail("Errors during verification of message files:\n-" + StringUtils.join("\n-", errors));
fail("Errors during verification of message files:\n-" + String.join("\n-", errors));
}
}
@@ -1,4 +1,4 @@
package fr.xephi.authme.output;
package fr.xephi.authme.message;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.TestHelper;
@@ -33,8 +33,8 @@ import static org.mockito.Mockito.verify;
*/
public class MessagesIntegrationTest {
private static final String YML_TEST_FILE = TestHelper.PROJECT_ROOT + "output/messages_test.yml";
private static final String YML_DEFAULT_TEST_FILE = TestHelper.PROJECT_ROOT + "output/messages_default.yml";
private static final String YML_TEST_FILE = TestHelper.PROJECT_ROOT + "message/messages_test.yml";
private static final String YML_DEFAULT_TEST_FILE = TestHelper.PROJECT_ROOT + "message/messages_default.yml";
private Messages messages;
@BeforeClass
@@ -256,7 +256,7 @@ public class MessagesIntegrationTest {
assumeThat(messages.retrieveSingle(key), equalTo("§cWrong password!"));
Settings settings = mock(Settings.class);
given(settings.getMessagesFile()).willReturn(TestHelper.getJarFile(
TestHelper.PROJECT_ROOT + "output/messages_test2.yml"));
TestHelper.PROJECT_ROOT + "message/messages_test2.yml"));
// when
messages.reload(settings);
@@ -2,7 +2,6 @@ package fr.xephi.authme.permission;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import fr.xephi.authme.util.StringUtils;
import org.bukkit.configuration.MemorySection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
@@ -65,7 +64,7 @@ public class PermissionConsistencyTest {
// then
if (!errors.isEmpty()) {
fail("Found consistency issues!\n" + StringUtils.join("\n", errors));
fail("Found consistency issues!\n" + String.join("\n", errors));
}
}
@@ -90,7 +89,7 @@ public class PermissionConsistencyTest {
// then
if (!errors.isEmpty()) {
fail("Found consistency issues!\n" + StringUtils.join("\n", errors));
fail("Found consistency issues!\n" + String.join("\n", errors));
}
}
@@ -172,7 +171,7 @@ public class PermissionConsistencyTest {
}
if (!badChildren.isEmpty()) {
errorList.add("Permission '" + definition.node + "' has children that are not logically below it: "
+ StringUtils.join(", ", badChildren));
+ String.join(", ", badChildren));
}
}
@@ -1,7 +1,7 @@
package fr.xephi.authme.process;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.message.Messages;
import fr.xephi.authme.permission.AuthGroupHandler;
import fr.xephi.authme.permission.AuthGroupType;
import fr.xephi.authme.permission.PermissionNode;
@@ -9,7 +9,7 @@ import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PlayerPermission;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.ValidationService;
import fr.xephi.authme.service.ValidationService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Test;
@@ -1,10 +1,10 @@
package fr.xephi.authme.process.email;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import org.bukkit.entity.Player;
@@ -1,9 +1,9 @@
package fr.xephi.authme.process.email;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import org.bukkit.entity.Player;
@@ -1,11 +1,11 @@
package fr.xephi.authme.process.login;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
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.AuthMeAsyncPreLoginEvent;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PlayerStatePermission;
import fr.xephi.authme.process.ProcessService;
@@ -13,8 +13,8 @@ import fr.xephi.authme.settings.properties.DatabaseSettings;
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.task.PlayerDataTaskManager;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.task.LimboPlayerTaskManager;
import fr.xephi.authme.service.BukkitService;
import org.bukkit.entity.Player;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -58,7 +58,7 @@ public class AsynchronousLoginTest {
@Mock
private ProcessService processService;
@Mock
private PlayerDataTaskManager playerDataTaskManager;
private LimboPlayerTaskManager limboPlayerTaskManager;
@Mock
private BukkitService bukkitService;
@Mock
@@ -1,11 +1,11 @@
package fr.xephi.authme.process.unregister;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.data.limbo.LimboCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.permission.AuthGroupHandler;
import fr.xephi.authme.permission.AuthGroupType;
import fr.xephi.authme.process.ProcessService;
@@ -13,9 +13,9 @@ import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.task.PlayerDataTaskManager;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.TeleportationService;
import fr.xephi.authme.task.LimboPlayerTaskManager;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.TeleportationService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.BeforeClass;
@@ -55,7 +55,7 @@ public class AsynchronousUnregisterTest {
@Mock
private LimboCache limboCache;
@Mock
private PlayerDataTaskManager playerDataTaskManager;
private LimboPlayerTaskManager limboPlayerTaskManager;
@Mock
private TeleportationService teleportationService;
@Mock
@@ -85,7 +85,7 @@ public class AsynchronousUnregisterTest {
// then
verify(service).send(player, MessageKey.WRONG_PASSWORD);
verify(passwordSecurity).comparePassword(userPassword, password, name);
verifyZeroInteractions(dataSource, playerDataTaskManager, limboCache, authGroupHandler, teleportationService);
verifyZeroInteractions(dataSource, limboPlayerTaskManager, limboCache, authGroupHandler, teleportationService);
verify(player, only()).getName();
}
@@ -175,7 +175,7 @@ public class AsynchronousUnregisterTest {
verify(dataSource).removeAuth(name);
verify(playerCache).removePlayer(name);
verify(authGroupHandler).setGroup(player, AuthGroupType.UNREGISTERED);
verifyZeroInteractions(teleportationService, playerDataTaskManager);
verifyZeroInteractions(teleportationService, limboPlayerTaskManager);
verify(bukkitService, never()).runTask(any(Runnable.class));
}
@@ -1,5 +1,6 @@
package fr.xephi.authme.security;
import fr.xephi.authme.util.RandomStringUtils;
import org.junit.Test;
import java.util.regex.Pattern;
@@ -8,9 +9,9 @@ import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
/**
* Test for {@link RandomString}.
* Test for {@link RandomStringUtils}.
*/
public class RandomStringTest {
public class RandomStringUtilsTest {
@Test
public void shouldGenerateRandomStrings() {
@@ -20,7 +21,7 @@ public class RandomStringTest {
// when / then
for (int length : lengths) {
String result = RandomString.generate(length);
String result = RandomStringUtils.generate(length);
assertThat("Result '" + result + "' should have length " + length,
result.length(), equalTo(length));
assertThat("Result '" + result + "' should only have characters a-z, 0-9",
@@ -36,7 +37,7 @@ public class RandomStringTest {
// when / then
for (int length : lengths) {
String result = RandomString.generateHex(length);
String result = RandomStringUtils.generateHex(length);
assertThat("Result '" + result + "' should have length " + length,
result.length(), equalTo(length));
assertThat("Result '" + result + "' should only have characters a-f, 0-9",
@@ -52,7 +53,7 @@ public class RandomStringTest {
// when / then
for (int length : lengths) {
String result = RandomString.generateHex(length);
String result = RandomStringUtils.generateHex(length);
assertThat("Result '" + result + "' should have length " + length,
result.length(), equalTo(length));
assertThat("Result '" + result + "' should only have characters a-z, A-Z, 0-9",
@@ -63,7 +64,7 @@ public class RandomStringTest {
@Test(expected = IllegalArgumentException.class)
public void shouldThrowForInvalidLength() {
// given/when
RandomString.generate(-3);
RandomStringUtils.generate(-3);
// then - throw exception
}
@@ -0,0 +1,228 @@
package fr.xephi.authme.service;
import ch.jalu.injector.testing.BeforeInjecting;
import ch.jalu.injector.testing.DelayedInjectionRunner;
import ch.jalu.injector.testing.InjectDelayed;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.message.Messages;
import fr.xephi.authme.permission.AdminPermission;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.ProtectionSettings;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import java.util.Arrays;
import java.util.List;
import static fr.xephi.authme.TestHelper.runSyncDelayedTaskWithDelay;
import static org.hamcrest.Matchers.equalTo;
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.Mockito.mock;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
/**
* Test for {@link AntiBotService}.
*/
@RunWith(DelayedInjectionRunner.class)
public class AntiBotServiceTest {
@InjectDelayed
private AntiBotService antiBotService;
@Mock
private Settings settings;
@Mock
private Messages messages;
@Mock
private PermissionsManager permissionsManager;
@Mock
private BukkitService bukkitService;
@BeforeInjecting
public void initSettings() {
given(settings.getProperty(ProtectionSettings.ANTIBOT_DURATION)).willReturn(10);
given(settings.getProperty(ProtectionSettings.ANTIBOT_SENSIBILITY)).willReturn(5);
given(settings.getProperty(ProtectionSettings.ENABLE_ANTIBOT)).willReturn(true);
given(settings.getProperty(ProtectionSettings.ANTIBOT_DELAY)).willReturn(8);
}
@Test
public void shouldStartListenerOnStartup() {
// given / when
runSyncDelayedTaskWithDelay(bukkitService);
// then
assertThat(antiBotService.getAntiBotStatus(), equalTo(AntiBotService.AntiBotStatus.LISTENING));
}
@Test
public void shouldNotListenForDisabledSetting() {
// given
reset(bukkitService);
given(settings.getProperty(ProtectionSettings.ENABLE_ANTIBOT)).willReturn(false);
// when
AntiBotService antiBotService = new AntiBotService(settings, messages, permissionsManager, bukkitService);
// then
assertThat(antiBotService.getAntiBotStatus(), equalTo(AntiBotService.AntiBotStatus.DISABLED));
verifyZeroInteractions(bukkitService);
}
@Test
public void shouldActivateAntibot() {
// given - listening antibot
runSyncDelayedTaskWithDelay(bukkitService);
// when
antiBotService.overrideAntiBotStatus(true);
// then
assertThat(antiBotService.getAntiBotStatus(), equalTo(AntiBotService.AntiBotStatus.ACTIVE));
// Check that a task is scheduled to disable again
runSyncDelayedTaskWithDelay(bukkitService);
assertThat(antiBotService.getAntiBotStatus(), equalTo(AntiBotService.AntiBotStatus.LISTENING));
}
@Test
public void shouldNotActivateAntibotForDisabledSetting() {
// given - disabled antibot
reset(bukkitService);
assertThat(antiBotService.getAntiBotStatus(), equalTo(AntiBotService.AntiBotStatus.DISABLED));
given(settings.getProperty(ProtectionSettings.ENABLE_ANTIBOT)).willReturn(false);
// when
antiBotService.overrideAntiBotStatus(true);
// then
assertThat(antiBotService.getAntiBotStatus(), equalTo(AntiBotService.AntiBotStatus.DISABLED));
verifyZeroInteractions(bukkitService);
}
@Test
public void shouldKeepTrackOfKickedPlayers() {
// given
String name = "eratic";
antiBotService.addPlayerKick(name);
// when
boolean result1 = antiBotService.wasPlayerKicked(name);
boolean result2 = antiBotService.wasPlayerKicked("other");
// then
assertThat(result1, equalTo(true));
assertThat(result2, equalTo(false));
}
@Test
public void shouldAcceptPlayerToJoin() {
// given - listening antibot
runSyncDelayedTaskWithDelay(bukkitService);
// when
boolean result = antiBotService.shouldKick(false);
// then
assertThat(result, equalTo(false));
}
@Test
public void shouldRejectPlayerWithoutAuth() {
// given - active antibot
runSyncDelayedTaskWithDelay(bukkitService);
antiBotService.overrideAntiBotStatus(true);
// when
boolean kickWithoutAuth = antiBotService.shouldKick(false);
boolean kickWithAuth = antiBotService.shouldKick(true);
// then
assertThat(kickWithoutAuth, equalTo(true));
assertThat(kickWithAuth, equalTo(false));
}
@Test
public void shouldIncreaseCountAndDecreaseAfterDelay() {
// given - listening antibot
runSyncDelayedTaskWithDelay(bukkitService);
reset(bukkitService);
assertThat(getAntiBotCount(antiBotService), equalTo(0));
// when
antiBotService.handlePlayerJoin();
// then
assertThat(getAntiBotCount(antiBotService), equalTo(1));
runSyncDelayedTaskWithDelay(bukkitService);
assertThat(getAntiBotCount(antiBotService), equalTo(0));
}
@Test
public void shouldActivateAntibotAfterThreshold() {
// given
int sensitivity = 10;
given(settings.getProperty(ProtectionSettings.ANTIBOT_SENSIBILITY)).willReturn(sensitivity);
reset(bukkitService);
AntiBotService antiBotService = new AntiBotService(settings, messages, permissionsManager, bukkitService);
runSyncDelayedTaskWithDelay(bukkitService);
for (int i = 0; i < sensitivity; ++i) {
antiBotService.handlePlayerJoin();
}
assertThat(antiBotService.getAntiBotStatus(), equalTo(AntiBotService.AntiBotStatus.LISTENING));
// when
antiBotService.handlePlayerJoin();
// then
assertThat(antiBotService.getAntiBotStatus(), equalTo(AntiBotService.AntiBotStatus.ACTIVE));
}
@Test
@SuppressWarnings("unchecked")
public void shouldInformPlayersOnActivation() {
// given - listening antibot
runSyncDelayedTaskWithDelay(bukkitService);
List<Player> players = Arrays.asList(mock(Player.class), mock(Player.class));
given(bukkitService.getOnlinePlayers()).willReturn((List) players);
given(permissionsManager.hasPermission(players.get(0), AdminPermission.ANTIBOT_MESSAGES)).willReturn(false);
given(permissionsManager.hasPermission(players.get(1), AdminPermission.ANTIBOT_MESSAGES)).willReturn(true);
// when
antiBotService.overrideAntiBotStatus(true);
// then
verify(permissionsManager).hasPermission(players.get(0), AdminPermission.ANTIBOT_MESSAGES);
verify(permissionsManager).hasPermission(players.get(1), AdminPermission.ANTIBOT_MESSAGES);
verify(messages, only()).send(players.get(1), MessageKey.ANTIBOT_AUTO_ENABLED_MESSAGE);
}
@Test
public void shouldImmediatelyStartAfterFirstStartup() {
// given - listening antibot
runSyncDelayedTaskWithDelay(bukkitService);
given(bukkitService.runTaskLater(any(Runnable.class), anyLong())).willReturn(mock(BukkitTask.class));
antiBotService.overrideAntiBotStatus(true);
// when
antiBotService.reload(settings);
// then
assertThat(antiBotService.getAntiBotStatus(), equalTo(AntiBotService.AntiBotStatus.LISTENING));
}
private static int getAntiBotCount(AntiBotService antiBotService) {
return ReflectionTestUtils.getFieldValue(AntiBotService.class, antiBotService, "antibotPlayers");
}
}
@@ -1,4 +1,4 @@
package fr.xephi.authme.util;
package fr.xephi.authme.service;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ReflectionTestUtils;
@@ -1,7 +1,7 @@
package fr.xephi.authme.util;
package fr.xephi.authme.service;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.security.crypts.HashedPassword;
@@ -4,7 +4,7 @@ import ch.jalu.injector.testing.BeforeInjecting;
import ch.jalu.injector.testing.DelayedInjectionRunner;
import ch.jalu.injector.testing.InjectDelayed;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.service.RecoveryCodeManager.ExpiringEntry;
import fr.xephi.authme.service.RecoveryCodeService.ExpiringEntry;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import org.junit.Test;
@@ -20,13 +20,13 @@ import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
/**
* Test for {@link RecoveryCodeManager}.
* Test for {@link RecoveryCodeService}.
*/
@RunWith(DelayedInjectionRunner.class)
public class RecoveryCodeManagerTest {
public class RecoveryCodeServiceTest {
@InjectDelayed
private RecoveryCodeManager recoveryCodeManager;
private RecoveryCodeService recoveryCodeService;
@Mock
private Settings settings;
@@ -39,16 +39,16 @@ public class RecoveryCodeManagerTest {
@Test
public void shouldBeDisabledForNonPositiveLength() {
assertThat(recoveryCodeManager.isRecoveryCodeNeeded(), equalTo(true));
assertThat(recoveryCodeService.isRecoveryCodeNeeded(), equalTo(true));
// given
given(settings.getProperty(SecuritySettings.RECOVERY_CODE_LENGTH)).willReturn(0);
// when
recoveryCodeManager.reload(settings);
recoveryCodeService.reload(settings);
// then
assertThat(recoveryCodeManager.isRecoveryCodeNeeded(), equalTo(false));
assertThat(recoveryCodeService.isRecoveryCodeNeeded(), equalTo(false));
}
@Test
@@ -57,7 +57,7 @@ public class RecoveryCodeManagerTest {
String name = "Bobbers";
// when
recoveryCodeManager.generateCode(name);
recoveryCodeService.generateCode(name);
// then
ExpiringEntry entry = getCodeMap().get(name);
@@ -72,7 +72,7 @@ public class RecoveryCodeManagerTest {
setCodeInMap(player, code, System.currentTimeMillis() - 500);
// when
boolean result = recoveryCodeManager.isCodeValid(player, code);
boolean result = recoveryCodeService.isCodeValid(player, code);
// then
assertThat(result, equalTo(false));
@@ -82,10 +82,10 @@ public class RecoveryCodeManagerTest {
public void shouldRecognizeCorrectCode() {
// given
String player = "dragon";
String code = recoveryCodeManager.generateCode(player);
String code = recoveryCodeService.generateCode(player);
// when
boolean result = recoveryCodeManager.isCodeValid(player, code);
boolean result = recoveryCodeService.isCodeValid(player, code);
// then
assertThat(result, equalTo(true));
@@ -95,19 +95,19 @@ public class RecoveryCodeManagerTest {
public void shouldRemoveCode() {
// given
String player = "Tester";
String code = recoveryCodeManager.generateCode(player);
String code = recoveryCodeService.generateCode(player);
// when
recoveryCodeManager.removeCode(player);
recoveryCodeService.removeCode(player);
// then
assertThat(recoveryCodeManager.isCodeValid(player, code), equalTo(false));
assertThat(recoveryCodeService.isCodeValid(player, code), equalTo(false));
assertThat(getCodeMap().get(player), nullValue());
}
private Map<String, ExpiringEntry> getCodeMap() {
return ReflectionTestUtils.getFieldValue(RecoveryCodeManager.class, recoveryCodeManager, "recoveryCodes");
return ReflectionTestUtils.getFieldValue(RecoveryCodeService.class, recoveryCodeService, "recoveryCodes");
}
private void setCodeInMap(String player, String code, long expiration) {
@@ -1,8 +1,8 @@
package fr.xephi.authme.util;
package fr.xephi.authme.service;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.cache.limbo.PlayerData;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.data.limbo.LimboPlayer;
import fr.xephi.authme.events.FirstSpawnTeleportEvent;
import fr.xephi.authme.events.SpawnTeleportEvent;
import fr.xephi.authme.settings.Settings;
@@ -235,7 +235,7 @@ public class TeleportationServiceTest {
given(settings.getProperty(RestrictionSettings.NO_TELEPORT)).willReturn(true);
Player player = mock(Player.class);
PlayerAuth auth = mock(PlayerAuth.class);
PlayerData limbo = mock(PlayerData.class);
LimboPlayer limbo = mock(LimboPlayer.class);
// when
teleportationService.teleportOnLogin(player, auth, limbo);
@@ -253,7 +253,7 @@ public class TeleportationServiceTest {
Location spawn = mockLocation();
given(spawnLoader.getSpawnLocation(player)).willReturn(spawn);
PlayerAuth auth = mock(PlayerAuth.class);
PlayerData limbo = mock(PlayerData.class);
LimboPlayer limbo = mock(LimboPlayer.class);
Location limboLocation = mockLocation();
given(limboLocation.getWorld().getName()).willReturn("forced1");
given(limbo.getLocation()).willReturn(limboLocation);
@@ -277,7 +277,7 @@ public class TeleportationServiceTest {
Location spawn = mockLocation();
given(spawnLoader.getSpawnLocation(player)).willReturn(spawn);
PlayerAuth auth = mock(PlayerAuth.class);
PlayerData limbo = mock(PlayerData.class);
LimboPlayer limbo = mock(LimboPlayer.class);
Location limboLocation = mockLocation();
given(limboLocation.getWorld().getName()).willReturn("Forced1"); // different case
given(limbo.getLocation()).willReturn(limboLocation);
@@ -304,7 +304,7 @@ public class TeleportationServiceTest {
Player player = mock(Player.class);
given(player.isOnline()).willReturn(true);
PlayerData limbo = mock(PlayerData.class);
LimboPlayer limbo = mock(LimboPlayer.class);
Location limboLocation = mockLocation();
given(limbo.getLocation()).willReturn(limboLocation);
@@ -333,7 +333,7 @@ public class TeleportationServiceTest {
given(player.isOnline()).willReturn(true);
World world = mock(World.class);
given(player.getWorld()).willReturn(world);
PlayerData limbo = mock(PlayerData.class);
LimboPlayer limbo = mock(LimboPlayer.class);
Location limboLocation = mockLocation();
given(limbo.getLocation()).willReturn(limboLocation);
@@ -361,7 +361,7 @@ public class TeleportationServiceTest {
given(player.isOnline()).willReturn(true);
World world = mock(World.class);
given(player.getWorld()).willReturn(world);
PlayerData limbo = mock(PlayerData.class);
LimboPlayer limbo = mock(LimboPlayer.class);
Location location = mockLocation();
given(limbo.getLocation()).willReturn(location);
@@ -386,7 +386,7 @@ public class TeleportationServiceTest {
given(player.isOnline()).willReturn(true);
World world = mock(World.class);
given(player.getWorld()).willReturn(world);
PlayerData limbo = mock(PlayerData.class);
LimboPlayer limbo = mock(LimboPlayer.class);
Location location = mockLocation();
given(limbo.getLocation()).willReturn(location);
@@ -407,7 +407,7 @@ public class TeleportationServiceTest {
PlayerAuth auth = PlayerAuth.builder().name("bobby").build();
Player player = mock(Player.class);
PlayerData limbo = mock(PlayerData.class);
LimboPlayer limbo = mock(LimboPlayer.class);
// when
teleportationService.teleportOnLogin(player, auth, limbo);
@@ -1,11 +1,12 @@
package fr.xephi.authme.util;
package fr.xephi.authme.service;
import ch.jalu.injector.testing.BeforeInjecting;
import ch.jalu.injector.testing.DelayedInjectionRunner;
import ch.jalu.injector.testing.InjectDelayed;
import com.google.common.base.Strings;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.geoip.GeoIpManager;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PlayerStatePermission;
import fr.xephi.authme.settings.Settings;
@@ -13,7 +14,7 @@ import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.settings.properties.ProtectionSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.ValidationService.ValidationResult;
import fr.xephi.authme.service.ValidationService.ValidationResult;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -44,7 +45,7 @@ public class ValidationServiceTest {
@Mock
private PermissionsManager permissionsManager;
@Mock
private GeoLiteAPI geoLiteApi;
private GeoIpManager geoIpManager;
@BeforeInjecting
public void createService() {
@@ -265,7 +266,7 @@ public class ValidationServiceTest {
// then
assertThat(result, equalTo(true));
verifyZeroInteractions(geoLiteApi);
verifyZeroInteractions(geoIpManager);
}
@Test
@@ -274,14 +275,14 @@ public class ValidationServiceTest {
given(settings.getProperty(ProtectionSettings.COUNTRIES_WHITELIST)).willReturn(asList("ch", "it"));
given(settings.getProperty(ProtectionSettings.COUNTRIES_BLACKLIST)).willReturn(Collections.<String>emptyList());
String ip = "127.0.0.1";
given(geoLiteApi.getCountryCode(ip)).willReturn("CH");
given(geoIpManager.getCountryCode(ip)).willReturn("CH");
// when
boolean result = validationService.isCountryAdmitted(ip);
// then
assertThat(result, equalTo(true));
verify(geoLiteApi).getCountryCode(ip);
verify(geoIpManager).getCountryCode(ip);
}
@Test
@@ -290,14 +291,14 @@ public class ValidationServiceTest {
given(settings.getProperty(ProtectionSettings.COUNTRIES_WHITELIST)).willReturn(asList("ch", "it"));
given(settings.getProperty(ProtectionSettings.COUNTRIES_BLACKLIST)).willReturn(Collections.<String>emptyList());
String ip = "123.45.67.89";
given(geoLiteApi.getCountryCode(ip)).willReturn("BR");
given(geoIpManager.getCountryCode(ip)).willReturn("BR");
// when
boolean result = validationService.isCountryAdmitted(ip);
// then
assertThat(result, equalTo(false));
verify(geoLiteApi).getCountryCode(ip);
verify(geoIpManager).getCountryCode(ip);
}
@Test
@@ -306,14 +307,14 @@ public class ValidationServiceTest {
given(settings.getProperty(ProtectionSettings.COUNTRIES_WHITELIST)).willReturn(Collections.<String>emptyList());
given(settings.getProperty(ProtectionSettings.COUNTRIES_BLACKLIST)).willReturn(asList("ch", "it"));
String ip = "127.0.0.1";
given(geoLiteApi.getCountryCode(ip)).willReturn("BR");
given(geoIpManager.getCountryCode(ip)).willReturn("BR");
// when
boolean result = validationService.isCountryAdmitted(ip);
// then
assertThat(result, equalTo(true));
verify(geoLiteApi).getCountryCode(ip);
verify(geoIpManager).getCountryCode(ip);
}
@Test
@@ -322,14 +323,14 @@ public class ValidationServiceTest {
given(settings.getProperty(ProtectionSettings.COUNTRIES_WHITELIST)).willReturn(Collections.<String>emptyList());
given(settings.getProperty(ProtectionSettings.COUNTRIES_BLACKLIST)).willReturn(asList("ch", "it"));
String ip = "123.45.67.89";
given(geoLiteApi.getCountryCode(ip)).willReturn("IT");
given(geoIpManager.getCountryCode(ip)).willReturn("IT");
// when
boolean result = validationService.isCountryAdmitted(ip);
// then
assertThat(result, equalTo(false));
verify(geoLiteApi).getCountryCode(ip);
verify(geoIpManager).getCountryCode(ip);
}
private static void assertErrorEquals(ValidationResult validationResult, MessageKey messageKey, String... args) {
@@ -8,7 +8,6 @@ import com.github.authme.configme.resource.PropertyResource;
import com.github.authme.configme.resource.YamlFileResource;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.settings.properties.AuthMeSettingsRetriever;
import fr.xephi.authme.util.StringUtils;
import org.bukkit.configuration.MemorySection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
@@ -54,7 +53,7 @@ public class ConfigFileConsistencyTest {
missingProperties.add(path);
}
}
fail("Found missing properties!\n-" + StringUtils.join("\n-", missingProperties));
fail("Found missing properties!\n-" + String.join("\n-", missingProperties));
}
}
@@ -78,7 +77,7 @@ public class ConfigFileConsistencyTest {
// then
if (!unknownPaths.isEmpty()) {
fail("Found " + unknownPaths.size() + " unknown property paths in the project's config.yml: \n- "
+ StringUtils.join("\n- ", unknownPaths));
+ String.join("\n- ", unknownPaths));
}
}
@@ -22,7 +22,7 @@ import java.util.Collections;
import java.util.List;
import static fr.xephi.authme.settings.properties.PluginSettings.MESSAGES_LANGUAGE;
import static fr.xephi.authme.util.StringUtils.makePath;
import static fr.xephi.authme.util.FileUtils.makePath;
import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.endsWith;
@@ -1,15 +1,15 @@
package fr.xephi.authme.task;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.cache.limbo.PlayerData;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.data.limbo.LimboCache;
import fr.xephi.authme.data.limbo.LimboPlayer;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.message.Messages;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.service.BukkitService;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import org.junit.BeforeClass;
@@ -28,13 +28,13 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
/**
* Test for {@link PlayerDataTaskManager}.
* Test for {@link LimboPlayerTaskManager}.
*/
@RunWith(MockitoJUnitRunner.class)
public class PlayerDataTaskManagerTest {
public class LimboPlayerTaskManagerTest {
@InjectMocks
private PlayerDataTaskManager playerDataTaskManager;
private LimboPlayerTaskManager limboPlayerTaskManager;
@Mock
private Messages messages;
@@ -60,8 +60,8 @@ public class PlayerDataTaskManagerTest {
public void shouldRegisterMessageTask() {
// given
String name = "bobby";
PlayerData playerData = mock(PlayerData.class);
given(limboCache.getPlayerData(name)).willReturn(playerData);
LimboPlayer limboPlayer = mock(LimboPlayer.class);
given(limboCache.getPlayerData(name)).willReturn(limboPlayer);
MessageKey key = MessageKey.REGISTER_EMAIL_MESSAGE;
given(messages.retrieve(key)).willReturn(new String[]{"Please register!"});
BukkitTask bukkiTask = mock(BukkitTask.class);
@@ -70,10 +70,10 @@ public class PlayerDataTaskManagerTest {
given(settings.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(true);
// when
playerDataTaskManager.registerMessageTask(name, false);
limboPlayerTaskManager.registerMessageTask(name, false);
// then
verify(playerData).setMessageTask(bukkiTask);
verify(limboPlayer).setMessageTask(bukkiTask);
verify(messages).retrieve(key);
}
@@ -85,7 +85,7 @@ public class PlayerDataTaskManagerTest {
given(settings.getProperty(RegistrationSettings.MESSAGE_INTERVAL)).willReturn(5);
// when
playerDataTaskManager.registerMessageTask(name, true);
limboPlayerTaskManager.registerMessageTask(name, true);
// then
verify(limboCache).getPlayerData(name);
@@ -97,28 +97,28 @@ public class PlayerDataTaskManagerTest {
public void shouldNotScheduleTaskForZeroAsInterval() {
// given
String name = "Tester1";
PlayerData playerData = mock(PlayerData.class);
given(limboCache.getPlayerData(name)).willReturn(playerData);
LimboPlayer limboPlayer = mock(LimboPlayer.class);
given(limboCache.getPlayerData(name)).willReturn(limboPlayer);
BukkitTask bukkiTask = mock(BukkitTask.class);
given(bukkitService.runTask(any(MessageTask.class))).willReturn(bukkiTask);
given(settings.getProperty(RegistrationSettings.MESSAGE_INTERVAL)).willReturn(0);
// when
playerDataTaskManager.registerMessageTask(name, true);
limboPlayerTaskManager.registerMessageTask(name, true);
// then
verifyZeroInteractions(playerData, bukkitService);
verifyZeroInteractions(limboPlayer, bukkitService);
}
@Test
public void shouldCancelExistingMessageTask() {
// given
PlayerData playerData = mock(PlayerData.class);
LimboPlayer limboPlayer = mock(LimboPlayer.class);
BukkitTask existingMessageTask = mock(BukkitTask.class);
given(playerData.getMessageTask()).willReturn(existingMessageTask);
given(limboPlayer.getMessageTask()).willReturn(existingMessageTask);
String name = "bobby";
given(limboCache.getPlayerData(name)).willReturn(playerData);
given(limboCache.getPlayerData(name)).willReturn(limboPlayer);
given(messages.retrieve(MessageKey.REGISTER_EMAIL_MESSAGE))
.willReturn(new String[]{"Please register", "Use /register"});
@@ -128,10 +128,10 @@ public class PlayerDataTaskManagerTest {
given(settings.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(true);
// when
playerDataTaskManager.registerMessageTask(name, false);
limboPlayerTaskManager.registerMessageTask(name, false);
// then
verify(playerData).setMessageTask(bukkiTask);
verify(limboPlayer).setMessageTask(bukkiTask);
verify(messages).retrieve(MessageKey.REGISTER_EMAIL_MESSAGE);
verify(existingMessageTask).cancel();
}
@@ -142,17 +142,17 @@ public class PlayerDataTaskManagerTest {
String name = "l33tPlayer";
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
PlayerData playerData = mock(PlayerData.class);
given(limboCache.getPlayerData(name)).willReturn(playerData);
LimboPlayer limboPlayer = mock(LimboPlayer.class);
given(limboCache.getPlayerData(name)).willReturn(limboPlayer);
given(settings.getProperty(RestrictionSettings.TIMEOUT)).willReturn(30);
BukkitTask bukkitTask = mock(BukkitTask.class);
given(bukkitService.runTaskLater(any(TimeoutTask.class), anyLong())).willReturn(bukkitTask);
// when
playerDataTaskManager.registerTimeoutTask(player);
limboPlayerTaskManager.registerTimeoutTask(player);
// then
verify(playerData).setTimeoutTask(bukkitTask);
verify(limboPlayer).setTimeoutTask(bukkitTask);
verify(bukkitService).runTaskLater(any(TimeoutTask.class), eq(600L)); // 30 * TICKS_PER_SECOND
verify(messages).retrieveSingle(MessageKey.LOGIN_TIMEOUT_ERROR);
}
@@ -167,7 +167,7 @@ public class PlayerDataTaskManagerTest {
given(settings.getProperty(RestrictionSettings.TIMEOUT)).willReturn(27);
// when
playerDataTaskManager.registerTimeoutTask(player);
limboPlayerTaskManager.registerTimeoutTask(player);
// then
verifyZeroInteractions(bukkitService, messages);
@@ -179,15 +179,15 @@ public class PlayerDataTaskManagerTest {
String name = "snail";
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
PlayerData playerData = mock(PlayerData.class);
given(limboCache.getPlayerData(name)).willReturn(playerData);
LimboPlayer limboPlayer = mock(LimboPlayer.class);
given(limboCache.getPlayerData(name)).willReturn(limboPlayer);
given(settings.getProperty(RestrictionSettings.TIMEOUT)).willReturn(0);
// when
playerDataTaskManager.registerTimeoutTask(player);
limboPlayerTaskManager.registerTimeoutTask(player);
// then
verifyZeroInteractions(playerData, bukkitService);
verifyZeroInteractions(limboPlayer, bukkitService);
}
@Test
@@ -196,20 +196,20 @@ public class PlayerDataTaskManagerTest {
String name = "l33tPlayer";
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
PlayerData playerData = mock(PlayerData.class);
LimboPlayer limboPlayer = mock(LimboPlayer.class);
BukkitTask existingTask = mock(BukkitTask.class);
given(playerData.getTimeoutTask()).willReturn(existingTask);
given(limboCache.getPlayerData(name)).willReturn(playerData);
given(limboPlayer.getTimeoutTask()).willReturn(existingTask);
given(limboCache.getPlayerData(name)).willReturn(limboPlayer);
given(settings.getProperty(RestrictionSettings.TIMEOUT)).willReturn(18);
BukkitTask bukkitTask = mock(BukkitTask.class);
given(bukkitService.runTaskLater(any(TimeoutTask.class), anyLong())).willReturn(bukkitTask);
// when
playerDataTaskManager.registerTimeoutTask(player);
limboPlayerTaskManager.registerTimeoutTask(player);
// then
verify(existingTask).cancel();
verify(playerData).setTimeoutTask(bukkitTask);
verify(limboPlayer).setTimeoutTask(bukkitTask);
verify(bukkitService).runTaskLater(any(TimeoutTask.class), eq(360L)); // 18 * TICKS_PER_SECOND
verify(messages).retrieveSingle(MessageKey.LOGIN_TIMEOUT_ERROR);
}
@@ -6,7 +6,7 @@ import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.PurgeSettings;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.service.BukkitService;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@@ -119,6 +119,15 @@ public class FileUtilsTest {
// Nothing happens
}
@Test
public void shouldConstructPath() {
// given/when
String result = FileUtils.makePath("path", "to", "test-file.txt");
// then
assertThat(result, equalTo("path" + File.separator + "to" + File.separator + "test-file.txt"));
}
private static void createFiles(File... files) throws IOException {
for (File file : files) {
boolean result = file.getParentFile().mkdirs() & file.createNewFile();
@@ -0,0 +1,69 @@
package fr.xephi.authme.util;
import fr.xephi.authme.TestHelper;
import org.bukkit.entity.Player;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.UUID;
import java.util.regex.Pattern;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
/**
* Test for {@link PlayerUtils}.
*/
public class PlayerUtilsTest {
@BeforeClass
public static void setAuthmeInstance() {
TestHelper.setupLogger();
}
@Test
public void shouldGetPlayerIp() {
// given
Player player = mock(Player.class);
String ip = "124.86.248.62";
TestHelper.mockPlayerIp(player, ip);
// when
String result = PlayerUtils.getPlayerIp(player);
// then
assertThat(result, equalTo(ip));
}
@Test
public void shouldGetUuid() {
// given
UUID uuid = UUID.randomUUID();
Player player = mock(Player.class);
given(player.getUniqueId()).willReturn(uuid);
// when
String result = PlayerUtils.getUUIDorName(player);
// then
assertThat(result, equalTo(uuid.toString()));
}
@Test
public void shouldFallbackToName() {
// given
Player player = mock(Player.class);
doThrow(NoSuchMethodError.class).when(player).getUniqueId();
String name = "Bobby12";
given(player.getName()).willReturn(name);
// when
String result = PlayerUtils.getUUIDorName(player);
// then
assertThat(result, equalTo(name));
}
}
@@ -3,10 +3,7 @@ package fr.xephi.authme.util;
import fr.xephi.authme.TestHelper;
import org.junit.Test;
import java.io.File;
import java.net.MalformedURLException;
import java.util.Arrays;
import java.util.List;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
@@ -65,51 +62,6 @@ public class StringUtilsTest {
assertFalse(StringUtils.isEmpty(" test"));
}
@Test
public void shouldJoinStrings() {
// given
List<String> elements = Arrays.asList("test", "for", null, "join", "StringUtils");
// when
String result = StringUtils.join(", ", elements);
// then
assertThat(result, equalTo("test, for, join, StringUtils"));
}
@Test
public void shouldJoinStringArray() {
// given
String[] elements = {"A", "test", "sentence", "for", "the join", null, "method"};
// when
String result = StringUtils.join("_", elements);
// then
assertThat(result, equalTo("A_test_sentence_for_the join_method"));
}
@Test
public void shouldNotHaveDelimiter() {
// given
List<String> elements = Arrays.asList(" ", null, "\t", "hello", null);
// when
String result = StringUtils.join("-", elements);
// then
assertThat(result, equalTo("hello"));
}
@Test
public void shouldJoinWithNullDelimiter() {
// given/when
String result = StringUtils.join(null, "A", "Few", "Words", "\n", "To", "Join");
// then
assertThat(result, equalTo("AFewWordsToJoin"));
}
@Test
public void shouldFormatException() {
// given
@@ -138,15 +90,6 @@ public class StringUtilsTest {
assertThat(StringUtils.getDifference("test", "something"), greaterThan(0.88));
}
@Test
public void shouldConstructPath() {
// given/when
String result = StringUtils.makePath("path", "to", "test-file.txt");
// then
assertThat(result, equalTo("path" + File.separator + "to" + File.separator + "test-file.txt"));
}
@Test
public void shouldHaveHiddenConstructor() {
TestHelper.validateHasOnlyPrivateEmptyConstructor(StringUtils.class);
@@ -1,18 +1,13 @@
package fr.xephi.authme.util;
import fr.xephi.authme.TestHelper;
import org.bukkit.entity.Player;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.UUID;
import java.util.regex.Pattern;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
/**
* Test for {@link Utils}.
@@ -48,49 +43,6 @@ public class UtilsTest {
assertThat(result.toString(), equalTo(".*?"));
}
@Test
public void shouldGetPlayerIp() {
// given
Player player = mock(Player.class);
String ip = "124.86.248.62";
TestHelper.mockPlayerIp(player, ip);
// when
String result = Utils.getPlayerIp(player);
// then
assertThat(result, equalTo(ip));
}
@Test
public void shouldGetUuid() {
// given
UUID uuid = UUID.randomUUID();
Player player = mock(Player.class);
given(player.getUniqueId()).willReturn(uuid);
// when
String result = Utils.getUUIDorName(player);
// then
assertThat(result, equalTo(uuid.toString()));
}
@Test
public void shouldFallbackToName() {
// given
Player player = mock(Player.class);
doThrow(NoSuchMethodError.class).when(player).getUniqueId();
String name = "Bobby12";
given(player.getName()).willReturn(name);
// when
String result = Utils.getUUIDorName(player);
// then
assertThat(result, equalTo(name));
}
@Test
public void shouldHavePrivateConstructorOnly() {
// given / when / then