#432 Inject in commands: DataSource / AntiBot / PasswordSecurity / PlayerCache

- Inject the services instead of passing them through the command service
This commit is contained in:
ljacqu 2016-05-02 18:52:34 +02:00
parent e6dacd6951
commit 9af596327a
23 changed files with 211 additions and 243 deletions

View File

@ -256,11 +256,11 @@ public class AuthMe extends JavaPlugin {
initializer.register(NewSetting.class, newSettings);
initializer.register(Messages.class, messages);
initializer.register(DataSource.class, database);
initializer.provide(BaseCommands.class, CommandInitializer.buildCommands(initializer));
// Some statically injected things
initializer.register(PlayerCache.class, PlayerCache.getInstance());
initializer.register(LimboCache.class, LimboCache.getInstance());
initializer.provide(BaseCommands.class, CommandInitializer.buildCommands(initializer));
permsMan = initializer.get(PermissionsManager.class);
bukkitService = initializer.get(BukkitService.class);

View File

@ -1,10 +1,7 @@
package fr.xephi.authme.command;
import fr.xephi.authme.AntiBot;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.command.help.HelpProvider;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.hooks.PluginHooks;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
@ -48,8 +45,6 @@ public class CommandService {
@Inject
private SpawnLoader spawnLoader;
@Inject
private AntiBot antiBot;
@Inject
private ValidationService validationService;
@Inject
private BukkitService bukkitService;
@ -95,15 +90,6 @@ public class CommandService {
authMe.getServer().getScheduler().runTaskAsynchronously(authMe, task);
}
/**
* Return the AuthMe data source.
*
* @return The used data source
*/
public DataSource getDataSource() {
return authMe.getDataSource();
}
/**
* Return the AuthMe instance for further manipulation. Use only if other methods from
* the command service cannot be used.
@ -114,15 +100,6 @@ public class CommandService {
return authMe;
}
/**
* Return the PasswordSecurity instance.
*
* @return The password security instance
*/
public PasswordSecurity getPasswordSecurity() {
return passwordSecurity;
}
/**
* Output the help for a given command.
*
@ -185,10 +162,6 @@ public class CommandService {
return settings;
}
public PlayerCache getPlayerCache() {
return PlayerCache.getInstance();
}
public PluginHooks getPluginHooks() {
return pluginHooks;
}
@ -197,10 +170,6 @@ public class CommandService {
return spawnLoader;
}
public AntiBot getAntiBot() {
return antiBot;
}
/**
* Verifies whether a password is valid according to the plugin settings.
*

View File

@ -2,13 +2,16 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
import java.util.List;
/**
@ -16,6 +19,15 @@ import java.util.List;
*/
public class ChangePasswordAdminCommand implements ExecutableCommand {
@Inject
private PasswordSecurity passwordSecurity;
@Inject
private PlayerCache playerCache;
@Inject
private DataSource dataSource;
@Override
public void executeCommand(final CommandSender sender, List<String> arguments,
final CommandService commandService) {
@ -36,10 +48,9 @@ public class ChangePasswordAdminCommand implements ExecutableCommand {
@Override
public void run() {
DataSource dataSource = commandService.getDataSource();
PlayerAuth auth = null;
if (commandService.getPlayerCache().isAuthenticated(playerNameLowerCase)) {
auth = commandService.getPlayerCache().getAuth(playerNameLowerCase);
if (playerCache.isAuthenticated(playerNameLowerCase)) {
auth = playerCache.getAuth(playerNameLowerCase);
} else if (dataSource.isAuthAvailable(playerNameLowerCase)) {
auth = dataSource.getAuth(playerNameLowerCase);
}
@ -48,8 +59,7 @@ public class ChangePasswordAdminCommand implements ExecutableCommand {
return;
}
HashedPassword hashedPassword = commandService.getPasswordSecurity()
.computeHash(playerPass, playerNameLowerCase);
HashedPassword hashedPassword = passwordSecurity.computeHash(playerPass, playerNameLowerCase);
auth.setPassword(hashedPassword);
if (!dataSource.updatePassword(auth)) {

View File

@ -3,9 +3,11 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
import java.util.List;
/**
@ -13,11 +15,14 @@ import java.util.List;
*/
public class GetEmailCommand implements ExecutableCommand {
@Inject
private DataSource dataSource;
@Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
String playerName = arguments.isEmpty() ? sender.getName() : arguments.get(0);
PlayerAuth auth = commandService.getDataSource().getAuth(playerName);
PlayerAuth auth = dataSource.getAuth(playerName);
if (auth == null) {
commandService.send(sender, MessageKey.UNKNOWN_USER);
} else {

View File

@ -3,9 +3,11 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
import java.util.Date;
import java.util.List;
@ -14,12 +16,15 @@ import java.util.List;
*/
public class LastLoginCommand implements ExecutableCommand {
@Inject
private DataSource dataSource;
@Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
// Get the player
String playerName = (arguments.size() >= 1) ? arguments.get(0) : sender.getName();
PlayerAuth auth = commandService.getDataSource().getAuth(playerName);
PlayerAuth auth = dataSource.getAuth(playerName);
if (auth == null) {
commandService.send(sender, MessageKey.USER_NOT_REGISTERED);
return;

View File

@ -3,10 +3,12 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.settings.properties.PurgeSettings;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
@ -16,6 +18,9 @@ import java.util.List;
*/
public class PurgeBannedPlayersCommand implements ExecutableCommand {
@Inject
private DataSource dataSource;
@Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
// AuthMe plugin instance
@ -23,12 +28,12 @@ public class PurgeBannedPlayersCommand implements ExecutableCommand {
// Get the list of banned players
List<String> bannedPlayers = new ArrayList<>();
for (OfflinePlayer offlinePlayer : plugin.getServer().getBannedPlayers()) {
for (OfflinePlayer offlinePlayer : commandService.getBukkitService().getBannedPlayers()) {
bannedPlayers.add(offlinePlayer.getName().toLowerCase());
}
// Purge the banned players
commandService.getDataSource().purgeBanned(bannedPlayers);
dataSource.purgeBanned(bannedPlayers);
if (commandService.getProperty(PurgeSettings.REMOVE_ESSENTIALS_FILES)
&& commandService.getPluginHooks().isEssentialsAvailable())
plugin.dataManager.purgeEssentials(bannedPlayers);

View File

@ -3,10 +3,12 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.settings.properties.PurgeSettings;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
import java.util.Calendar;
import java.util.List;
@ -18,6 +20,9 @@ public class PurgeCommand implements ExecutableCommand {
private static final int MINIMUM_LAST_SEEN_DAYS = 30;
@Inject
private DataSource dataSource;
@Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
// Get the days parameter
@ -45,7 +50,7 @@ public class PurgeCommand implements ExecutableCommand {
long until = calendar.getTimeInMillis();
// Purge the data, get the purged values
List<String> purged = commandService.getDataSource().autoPurgeDatabase(until);
List<String> purged = dataSource.autoPurgeDatabase(until);
// Show a status message
sender.sendMessage(ChatColor.GOLD + "Deleted " + purged.size() + " user accounts");

View File

@ -3,9 +3,11 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
import java.util.List;
/**
@ -13,26 +15,29 @@ import java.util.List;
*/
public class PurgeLastPositionCommand implements ExecutableCommand {
@Inject
private DataSource dataSource;
@Override
public void executeCommand(final CommandSender sender, List<String> arguments, CommandService commandService) {
String playerName = arguments.isEmpty() ? sender.getName() : arguments.get(0);
if ("*".equals(playerName)) {
for (PlayerAuth auth : commandService.getDataSource().getAllAuths()) {
for (PlayerAuth auth : dataSource.getAllAuths()) {
resetLastPosition(auth);
commandService.getDataSource().updateQuitLoc(auth);
dataSource.updateQuitLoc(auth);
}
sender.sendMessage("All players last position locations are now reset");
} else {
// Get the user auth and make sure the user exists
PlayerAuth auth = commandService.getDataSource().getAuth(playerName);
PlayerAuth auth = dataSource.getAuth(playerName);
if (auth == null) {
commandService.send(sender, MessageKey.UNKNOWN_USER);
return;
}
resetLastPosition(auth);
commandService.getDataSource().updateQuitLoc(auth);
dataSource.updateQuitLoc(auth);
sender.sendMessage(playerName + "'s last position location is now reset");
}
}

View File

@ -4,11 +4,14 @@ import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import javax.inject.Inject;
import java.util.List;
/**
@ -16,6 +19,12 @@ import java.util.List;
*/
public class RegisterAdminCommand implements ExecutableCommand {
@Inject
private PasswordSecurity passwordSecurity;
@Inject
private DataSource dataSource;
@Override
public void executeCommand(final CommandSender sender, List<String> arguments,
final CommandService commandService) {
@ -35,23 +44,22 @@ public class RegisterAdminCommand implements ExecutableCommand {
@Override
public void run() {
if (commandService.getDataSource().isAuthAvailable(playerNameLowerCase)) {
if (dataSource.isAuthAvailable(playerNameLowerCase)) {
commandService.send(sender, MessageKey.NAME_ALREADY_REGISTERED);
return;
}
HashedPassword hashedPassword = commandService.getPasswordSecurity()
.computeHash(playerPass, playerNameLowerCase);
HashedPassword hashedPassword = passwordSecurity.computeHash(playerPass, playerNameLowerCase);
PlayerAuth auth = PlayerAuth.builder()
.name(playerNameLowerCase)
.realName(playerName)
.password(hashedPassword)
.build();
if (!commandService.getDataSource().saveAuth(auth)) {
if (!dataSource.saveAuth(auth)) {
commandService.send(sender, MessageKey.ERROR);
return;
}
commandService.getDataSource().setUnlogged(playerNameLowerCase);
dataSource.setUnlogged(playerNameLowerCase);
commandService.send(sender, MessageKey.REGISTER_SUCCESS);
ConsoleLogger.info(sender.getName() + " registered " + playerName);

View File

@ -8,6 +8,7 @@ import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
import java.util.List;
/**
@ -15,6 +16,12 @@ import java.util.List;
*/
public class SetEmailCommand implements ExecutableCommand {
@Inject
private DataSource dataSource;
@Inject
private PlayerCache playerCache;
@Override
public void executeCommand(final CommandSender sender, List<String> arguments,
final CommandService commandService) {
@ -32,7 +39,6 @@ public class SetEmailCommand implements ExecutableCommand {
@Override
public void run() {
// Validate the user
DataSource dataSource = commandService.getDataSource();
PlayerAuth auth = dataSource.getAuth(playerName);
if (auth == null) {
commandService.send(sender, MessageKey.UNKNOWN_USER);
@ -50,8 +56,8 @@ public class SetEmailCommand implements ExecutableCommand {
}
// Update the player cache
if (PlayerCache.getInstance().getAuth(playerName) != null) {
PlayerCache.getInstance().updatePlayer(auth);
if (playerCache.getAuth(playerName) != null) {
playerCache.updatePlayer(auth);
}
// Show a status message

View File

@ -8,6 +8,7 @@ import fr.xephi.authme.command.help.HelpProvider;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
import java.util.Arrays;
import java.util.List;
@ -16,9 +17,11 @@ import java.util.List;
*/
public class SwitchAntiBotCommand implements ExecutableCommand {
@Inject
private AntiBot antiBot;
@Override
public void executeCommand(final CommandSender sender, List<String> arguments, CommandService commandService) {
AntiBot antiBot = commandService.getAntiBot();
if (arguments.isEmpty()) {
sender.sendMessage("[AuthMe] AntiBot status: " + antiBot.getAntiBotStatus().name());
return;

View File

@ -6,6 +6,7 @@ import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
@ -19,6 +20,7 @@ import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitTask;
import javax.inject.Inject;
import java.util.List;
import static fr.xephi.authme.util.BukkitService.TICKS_PER_SECOND;
@ -28,6 +30,12 @@ import static fr.xephi.authme.util.BukkitService.TICKS_PER_SECOND;
*/
public class UnregisterAdminCommand implements ExecutableCommand {
@Inject
private DataSource dataSource;
@Inject
private PlayerCache playerCache;
@Override
public void executeCommand(final CommandSender sender, List<String> arguments, CommandService commandService) {
// Get the player name
@ -35,20 +43,20 @@ public class UnregisterAdminCommand implements ExecutableCommand {
String playerNameLowerCase = playerName.toLowerCase();
// Make sure the user is valid
if (!commandService.getDataSource().isAuthAvailable(playerNameLowerCase)) {
if (!dataSource.isAuthAvailable(playerNameLowerCase)) {
commandService.send(sender, MessageKey.UNKNOWN_USER);
return;
}
// Remove the player
if (!commandService.getDataSource().removeAuth(playerNameLowerCase)) {
if (!dataSource.removeAuth(playerNameLowerCase)) {
commandService.send(sender, MessageKey.ERROR);
return;
}
// Unregister the player
Player target = commandService.getPlayer(playerNameLowerCase);
PlayerCache.getInstance().removePlayer(playerNameLowerCase);
playerCache.removePlayer(playerNameLowerCase);
Utils.setGroup(target, Utils.GroupType.UNREGISTERED);
if (target != null && target.isOnline()) {
if (commandService.getProperty(RegistrationSettings.FORCE)) {

View File

@ -8,6 +8,7 @@ import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.task.ChangePasswordTask;
import org.bukkit.entity.Player;
import javax.inject.Inject;
import java.util.List;
/**
@ -15,13 +16,15 @@ import java.util.List;
*/
public class ChangePasswordCommand extends PlayerCommand {
@Inject
private PlayerCache playerCache;
@Override
public void runCommand(Player player, List<String> arguments, CommandService commandService) {
String oldPassword = arguments.get(0);
String newPassword = arguments.get(1);
String name = player.getName().toLowerCase();
final PlayerCache playerCache = commandService.getPlayerCache();
if (!playerCache.isAuthenticated(name)) {
commandService.send(player, MessageKey.NOT_LOGGED_IN);
return;

View File

@ -8,16 +8,27 @@ import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.util.StringUtils;
import org.bukkit.entity.Player;
import javax.inject.Inject;
import java.util.List;
public class RecoverEmailCommand extends PlayerCommand {
@Inject
private PasswordSecurity passwordSecurity;
@Inject
private DataSource dataSource;
@Inject
private PlayerCache playerCache;
@Override
public void runCommand(Player player, List<String> arguments, CommandService commandService) {
final String playerMail = arguments.get(0);
@ -29,7 +40,6 @@ public class RecoverEmailCommand extends PlayerCommand {
commandService.send(player, MessageKey.ERROR);
return;
}
DataSource dataSource = commandService.getDataSource();
if (dataSource.isAuthAvailable(playerName)) {
if (PlayerCache.getInstance().isAuthenticated(playerName)) {
commandService.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
@ -37,10 +47,10 @@ public class RecoverEmailCommand extends PlayerCommand {
}
String thePass = RandomString.generate(commandService.getProperty(EmailSettings.RECOVERY_PASSWORD_LENGTH));
HashedPassword hashNew = commandService.getPasswordSecurity().computeHash(thePass, playerName);
HashedPassword hashNew = passwordSecurity.computeHash(thePass, playerName);
PlayerAuth auth;
if (PlayerCache.getInstance().isAuthenticated(playerName)) {
auth = PlayerCache.getInstance().getAuth(playerName);
if (playerCache.isAuthenticated(playerName)) {
auth = playerCache.getAuth(playerName);
} else if (dataSource.isAuthAvailable(playerName)) {
auth = dataSource.getAuth(playerName);
} else {

View File

@ -1,9 +1,7 @@
package fr.xephi.authme.command;
import fr.xephi.authme.AntiBot;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.help.HelpProvider;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.hooks.PluginHooks;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
@ -62,8 +60,6 @@ public class CommandServiceTest {
@Mock
private SpawnLoader spawnLoader;
@Mock
private AntiBot antiBot;
@Mock
private ValidationService validationService;
@Mock
private BukkitService bukkitService;
@ -108,28 +104,6 @@ public class CommandServiceTest {
verify(commandMapper).mapPartsToCommand(sender, commandParts);
}
@Test
public void shouldGetDataSource() {
// given
DataSource dataSource = mock(DataSource.class);
given(authMe.getDataSource()).willReturn(dataSource);
// when
DataSource result = commandService.getDataSource();
// then
assertThat(result, equalTo(dataSource));
}
@Test
public void shouldGetPasswordSecurity() {
// given/when
PasswordSecurity passwordSecurity = commandService.getPasswordSecurity();
// then
assertThat(passwordSecurity, equalTo(this.passwordSecurity));
}
@Test
public void shouldOutputHelp() {
// given

View File

@ -4,7 +4,6 @@ import fr.xephi.authme.TestHelper;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.security.PasswordSecurity;
@ -13,6 +12,7 @@ import org.bukkit.command.CommandSender;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
@ -24,6 +24,7 @@ import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
/**
* Test for {@link ChangePasswordAdminCommand}.
@ -31,9 +32,21 @@ import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class)
public class ChangePasswordAdminCommandTest {
@InjectMocks
private ChangePasswordAdminCommand command;
@Mock
private CommandService service;
@Mock
private PasswordSecurity passwordSecurity;
@Mock
private DataSource dataSource;
@Mock
private PlayerCache playerCache;
@BeforeClass
public static void setUpLogger() {
TestHelper.setupLogger();
@ -42,7 +55,6 @@ public class ChangePasswordAdminCommandTest {
@Test
public void shouldRejectInvalidPassword() {
// given
ExecutableCommand command = new ChangePasswordAdminCommand();
CommandSender sender = mock(CommandSender.class);
given(service.validatePassword("Bobby", "bobby")).willReturn(MessageKey.PASSWORD_IS_USERNAME_ERROR);
@ -52,23 +64,16 @@ public class ChangePasswordAdminCommandTest {
// then
verify(service).validatePassword("Bobby", "bobby");
verify(service).send(sender, MessageKey.PASSWORD_IS_USERNAME_ERROR);
verify(service, never()).getDataSource();
verifyZeroInteractions(dataSource);
}
@Test
public void shouldRejectCommandForUnknownUser() {
// given
ExecutableCommand command = new ChangePasswordAdminCommand();
CommandSender sender = mock(CommandSender.class);
String player = "player";
PlayerCache playerCache = mock(PlayerCache.class);
given(playerCache.isAuthenticated(player)).willReturn(false);
given(service.getPlayerCache()).willReturn(playerCache);
DataSource dataSource = mock(DataSource.class);
given(dataSource.getAuth(player)).willReturn(null);
given(service.getDataSource()).willReturn(dataSource);
// when
command.executeCommand(sender, Arrays.asList(player, "password"), service);
@ -82,26 +87,17 @@ public class ChangePasswordAdminCommandTest {
@Test
public void shouldUpdatePasswordOfLoggedInUser() {
// given
ExecutableCommand command = new ChangePasswordAdminCommand();
CommandSender sender = mock(CommandSender.class);
String player = "my_user12";
String password = "passPass";
PlayerAuth auth = mock(PlayerAuth.class);
PlayerCache playerCache = mock(PlayerCache.class);
given(playerCache.isAuthenticated(player)).willReturn(true);
given(playerCache.getAuth(player)).willReturn(auth);
given(service.getPlayerCache()).willReturn(playerCache);
PasswordSecurity passwordSecurity = mock(PasswordSecurity.class);
HashedPassword hashedPassword = mock(HashedPassword.class);
given(passwordSecurity.computeHash(password, player)).willReturn(hashedPassword);
given(service.getPasswordSecurity()).willReturn(passwordSecurity);
DataSource dataSource = mock(DataSource.class);
given(dataSource.updatePassword(auth)).willReturn(true);
given(service.getDataSource()).willReturn(dataSource);
// when
command.executeCommand(sender, Arrays.asList(player, password), service);
@ -118,27 +114,17 @@ public class ChangePasswordAdminCommandTest {
@Test
public void shouldUpdatePasswordOfOfflineUser() {
// given
ExecutableCommand command = new ChangePasswordAdminCommand();
CommandSender sender = mock(CommandSender.class);
String player = "my_user12";
String password = "passPass";
PlayerAuth auth = mock(PlayerAuth.class);
PlayerCache playerCache = mock(PlayerCache.class);
given(playerCache.isAuthenticated(player)).willReturn(false);
given(service.getPlayerCache()).willReturn(playerCache);
DataSource dataSource = mock(DataSource.class);
given(dataSource.isAuthAvailable(player)).willReturn(true);
given(dataSource.getAuth(player)).willReturn(auth);
given(dataSource.updatePassword(auth)).willReturn(true);
given(service.getDataSource()).willReturn(dataSource);
PasswordSecurity passwordSecurity = mock(PasswordSecurity.class);
HashedPassword hashedPassword = mock(HashedPassword.class);
given(passwordSecurity.computeHash(password, player)).willReturn(hashedPassword);
given(service.getPasswordSecurity()).willReturn(passwordSecurity);
// when
command.executeCommand(sender, Arrays.asList(player, password), service);
@ -155,26 +141,16 @@ public class ChangePasswordAdminCommandTest {
@Test
public void shouldReportWhenSaveFailed() {
// given
ExecutableCommand command = new ChangePasswordAdminCommand();
CommandSender sender = mock(CommandSender.class);
String player = "my_user12";
String password = "passPass";
PlayerAuth auth = mock(PlayerAuth.class);
PlayerCache playerCache = mock(PlayerCache.class);
given(playerCache.isAuthenticated(player)).willReturn(true);
given(playerCache.getAuth(player)).willReturn(auth);
given(service.getPlayerCache()).willReturn(playerCache);
PasswordSecurity passwordSecurity = mock(PasswordSecurity.class);
HashedPassword hashedPassword = mock(HashedPassword.class);
given(passwordSecurity.computeHash(password, player)).willReturn(hashedPassword);
given(service.getPasswordSecurity()).willReturn(passwordSecurity);
DataSource dataSource = mock(DataSource.class);
given(dataSource.updatePassword(auth)).willReturn(false);
given(service.getDataSource()).willReturn(dataSource);
// when
command.executeCommand(sender, Arrays.asList(player, password), service);

View File

@ -2,11 +2,14 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.Collections;
@ -19,19 +22,26 @@ import static org.mockito.Mockito.verify;
/**
* Test for {@link GetEmailCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class GetEmailCommandTest {
@InjectMocks
private GetEmailCommand command;
@Mock
private DataSource dataSource;
@Mock
private CommandSender sender;
@Mock
private CommandService service;
@Test
public void shouldReportUnknownUser() {
// given
String user = "myTestUser";
DataSource dataSource = mock(DataSource.class);
given(dataSource.getAuth(user)).willReturn(null);
CommandService service = mock(CommandService.class);
given(service.getDataSource()).willReturn(dataSource);
CommandSender sender = mock(CommandSender.class);
ExecutableCommand command = new GetEmailCommand();
// when
command.executeCommand(sender, Collections.singletonList(user), service);
@ -47,14 +57,7 @@ public class GetEmailCommandTest {
String email = "user.email@example.org";
PlayerAuth auth = mock(PlayerAuth.class);
given(auth.getEmail()).willReturn(email);
DataSource dataSource = mock(DataSource.class);
given(dataSource.getAuth(user)).willReturn(auth);
CommandService service = mock(CommandService.class);
given(service.getDataSource()).willReturn(dataSource);
CommandSender sender = mock(CommandSender.class);
ExecutableCommand command = new GetEmailCommand();
// when
command.executeCommand(sender, Collections.singletonList(user), service);

View File

@ -2,12 +2,15 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.Collections;
import java.util.Date;
@ -23,8 +26,21 @@ import static org.mockito.Mockito.verify;
/**
* Test for {@link LastLoginCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class LastLoginCommandTest {
@InjectMocks
private LastLoginCommand command;
@Mock
private DataSource dataSource;
@Mock
private CommandService service;
@Mock
private CommandSender sender;
private static final long HOUR_IN_MSEC = 3600 * 1000;
private static final long DAY_IN_MSEC = 24 * HOUR_IN_MSEC;
@ -32,15 +48,8 @@ public class LastLoginCommandTest {
public void shouldRejectNonExistentUser() {
// given
String player = "tester";
DataSource dataSource = mock(DataSource.class);
given(dataSource.getAuth(player)).willReturn(null);
CommandService service = mock(CommandService.class);
given(service.getDataSource()).willReturn(dataSource);
CommandSender sender = mock(CommandSender.class);
ExecutableCommand command = new LastLoginCommand();
// when
command.executeCommand(sender, Collections.singletonList(player), service);
@ -58,14 +67,7 @@ public class LastLoginCommandTest {
PlayerAuth auth = mock(PlayerAuth.class);
given(auth.getLastLogin()).willReturn(lastLogin);
given(auth.getIp()).willReturn("123.45.66.77");
DataSource dataSource = mock(DataSource.class);
given(dataSource.getAuth(player)).willReturn(auth);
CommandService service = mock(CommandService.class);
given(service.getDataSource()).willReturn(dataSource);
CommandSender sender = mock(CommandSender.class);
ExecutableCommand command = new LastLoginCommand();
// when
command.executeCommand(sender, Collections.singletonList(player), service);
@ -85,7 +87,6 @@ public class LastLoginCommandTest {
public void shouldDisplayLastLoginOfCommandSender() {
// given
String name = "CommandSender";
CommandSender sender = mock(CommandSender.class);
given(sender.getName()).willReturn(name);
long lastLogin = System.currentTimeMillis() -
@ -93,14 +94,7 @@ public class LastLoginCommandTest {
PlayerAuth auth = mock(PlayerAuth.class);
given(auth.getLastLogin()).willReturn(lastLogin);
given(auth.getIp()).willReturn("123.45.66.77");
DataSource dataSource = mock(DataSource.class);
given(dataSource.getAuth(name)).willReturn(auth);
CommandService service = mock(CommandService.class);
given(service.getDataSource()).willReturn(dataSource);
ExecutableCommand command = new LastLoginCommand();
// when
command.executeCommand(sender, Collections.<String>emptyList(), service);

View File

@ -2,11 +2,14 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.Arrays;
import java.util.Collections;
@ -20,21 +23,27 @@ import static org.mockito.Mockito.verify;
/**
* Test for {@link PurgeLastPositionCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class PurgeLastPositionCommandTest {
@InjectMocks
private PurgeLastPositionCommand command;
@Mock
private DataSource dataSource;
@Mock
private CommandService service;
@Mock
private CommandSender sender;
@Test
public void shouldPurgeLastPosOfUser() {
// given
String player = "_Bobby";
PlayerAuth auth = mock(PlayerAuth.class);
DataSource dataSource = mock(DataSource.class);
given(dataSource.getAuth(player)).willReturn(auth);
CommandService service = mock(CommandService.class);
given(service.getDataSource()).willReturn(dataSource);
CommandSender sender = mock(CommandSender.class);
ExecutableCommand command = new PurgeLastPositionCommand();
// when
command.executeCommand(sender, Collections.singletonList(player), service);
@ -51,14 +60,8 @@ public class PurgeLastPositionCommandTest {
String player = "_Bobby";
CommandSender sender = mock(CommandSender.class);
given(sender.getName()).willReturn(player);
PlayerAuth auth = mock(PlayerAuth.class);
DataSource dataSource = mock(DataSource.class);
given(dataSource.getAuth(player)).willReturn(auth);
CommandService service = mock(CommandService.class);
given(service.getDataSource()).willReturn(dataSource);
ExecutableCommand command = new PurgeLastPositionCommand();
// when
command.executeCommand(sender, Collections.<String>emptyList(), service);
@ -72,12 +75,6 @@ public class PurgeLastPositionCommandTest {
@Test
public void shouldHandleNonExistentUser() {
// given
DataSource dataSource = mock(DataSource.class);
CommandService service = mock(CommandService.class);
given(service.getDataSource()).willReturn(dataSource);
ExecutableCommand command = new PurgeLastPositionCommand();
CommandSender sender = mock(CommandSender.class);
String name = "invalidPlayer";
// when
@ -94,14 +91,7 @@ public class PurgeLastPositionCommandTest {
PlayerAuth auth1 = mock(PlayerAuth.class);
PlayerAuth auth2 = mock(PlayerAuth.class);
PlayerAuth auth3 = mock(PlayerAuth.class);
DataSource dataSource = mock(DataSource.class);
given(dataSource.getAllAuths()).willReturn(Arrays.asList(auth1, auth2, auth3));
CommandService service = mock(CommandService.class);
given(service.getDataSource()).willReturn(dataSource);
ExecutableCommand command = new PurgeLastPositionCommand();
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList("*"), service);

View File

@ -3,7 +3,6 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.security.PasswordSecurity;
@ -14,6 +13,7 @@ import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
@ -35,11 +35,21 @@ import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class)
public class RegisterAdminCommandTest {
@InjectMocks
private RegisterAdminCommand command;
@Mock
private PasswordSecurity passwordSecurity;
@Mock
private CommandSender sender;
@Mock
private CommandService commandService;
@Mock
private DataSource dataSource;
@BeforeClass
public static void setUpLogger() {
TestHelper.setupLogger();
@ -51,7 +61,6 @@ public class RegisterAdminCommandTest {
String user = "tester";
String password = "myPassword";
given(commandService.validatePassword(password, user)).willReturn(MessageKey.INVALID_PASSWORD_LENGTH);
ExecutableCommand command = new RegisterAdminCommand();
// when
command.executeCommand(sender, Arrays.asList(user, password), commandService);
@ -68,10 +77,7 @@ public class RegisterAdminCommandTest {
String user = "my_name55";
String password = "@some-pass@";
given(commandService.validatePassword(password, user)).willReturn(null);
DataSource dataSource = mock(DataSource.class);
given(dataSource.isAuthAvailable(user)).willReturn(true);
given(commandService.getDataSource()).willReturn(dataSource);
ExecutableCommand command = new RegisterAdminCommand();
// when
command.executeCommand(sender, Arrays.asList(user, password), commandService);
@ -89,15 +95,10 @@ public class RegisterAdminCommandTest {
String user = "test-test";
String password = "afdjhfkt";
given(commandService.validatePassword(password, user)).willReturn(null);
DataSource dataSource = mock(DataSource.class);
given(dataSource.isAuthAvailable(user)).willReturn(false);
given(dataSource.saveAuth(any(PlayerAuth.class))).willReturn(false);
given(commandService.getDataSource()).willReturn(dataSource);
PasswordSecurity passwordSecurity = mock(PasswordSecurity.class);
HashedPassword hashedPassword = new HashedPassword("235sdf4w5udsgf");
given(passwordSecurity.computeHash(password, user)).willReturn(hashedPassword);
given(commandService.getPasswordSecurity()).willReturn(passwordSecurity);
ExecutableCommand command = new RegisterAdminCommand();
// when
command.executeCommand(sender, Arrays.asList(user, password), commandService);
@ -117,16 +118,11 @@ public class RegisterAdminCommandTest {
String user = "someone";
String password = "Al1O3P49S5%";
given(commandService.validatePassword(password, user)).willReturn(null);
DataSource dataSource = mock(DataSource.class);
given(dataSource.isAuthAvailable(user)).willReturn(false);
given(dataSource.saveAuth(any(PlayerAuth.class))).willReturn(true);
given(commandService.getDataSource()).willReturn(dataSource);
PasswordSecurity passwordSecurity = mock(PasswordSecurity.class);
HashedPassword hashedPassword = new HashedPassword("$aea2345EW235dfsa@#R%987048");
given(passwordSecurity.computeHash(password, user)).willReturn(hashedPassword);
given(commandService.getPasswordSecurity()).willReturn(passwordSecurity);
given(commandService.getPlayer(user)).willReturn(null);
ExecutableCommand command = new RegisterAdminCommand();
// when
command.executeCommand(sender, Arrays.asList(user, password), commandService);
@ -147,17 +143,12 @@ public class RegisterAdminCommandTest {
String user = "someone";
String password = "Al1O3P49S5%";
given(commandService.validatePassword(password, user)).willReturn(null);
DataSource dataSource = mock(DataSource.class);
given(dataSource.isAuthAvailable(user)).willReturn(false);
given(dataSource.saveAuth(any(PlayerAuth.class))).willReturn(true);
given(commandService.getDataSource()).willReturn(dataSource);
PasswordSecurity passwordSecurity = mock(PasswordSecurity.class);
HashedPassword hashedPassword = new HashedPassword("$aea2345EW235dfsa@#R%987048");
given(passwordSecurity.computeHash(password, user)).willReturn(hashedPassword);
given(commandService.getPasswordSecurity()).willReturn(passwordSecurity);
Player player = mock(Player.class);
given(commandService.getPlayer(user)).willReturn(player);
ExecutableCommand command = new RegisterAdminCommand();
// when
command.executeCommand(sender, Arrays.asList(user, password), commandService);

View File

@ -2,11 +2,14 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.AntiBot;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.command.FoundCommandResult;
import fr.xephi.authme.command.help.HelpProvider;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.Collections;
@ -22,17 +25,23 @@ import static org.mockito.Mockito.verify;
/**
* Test for {@link SwitchAntiBotCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class SwitchAntiBotCommandTest {
@InjectMocks
private SwitchAntiBotCommand command;
@Mock
private AntiBot antiBot;
@Mock
private CommandService service;
@Test
public void shouldReturnAntiBotState() {
// given
AntiBot antiBot = mock(AntiBot.class);
given(antiBot.getAntiBotStatus()).willReturn(AntiBot.AntiBotStatus.ACTIVE);
CommandService service = mock(CommandService.class);
given(service.getAntiBot()).willReturn(antiBot);
CommandSender sender = mock(CommandSender.class);
ExecutableCommand command = new SwitchAntiBotCommand();
// when
command.executeCommand(sender, Collections.<String>emptyList(), service);
@ -44,11 +53,7 @@ public class SwitchAntiBotCommandTest {
@Test
public void shouldActivateAntiBot() {
// given
AntiBot antiBot = mock(AntiBot.class);
CommandService service = mock(CommandService.class);
given(service.getAntiBot()).willReturn(antiBot);
CommandSender sender = mock(CommandSender.class);
ExecutableCommand command = new SwitchAntiBotCommand();
// when
command.executeCommand(sender, Collections.singletonList("on"), service);
@ -61,11 +66,7 @@ public class SwitchAntiBotCommandTest {
@Test
public void shouldDeactivateAntiBot() {
// given
AntiBot antiBot = mock(AntiBot.class);
CommandService service = mock(CommandService.class);
given(service.getAntiBot()).willReturn(antiBot);
CommandSender sender = mock(CommandSender.class);
ExecutableCommand command = new SwitchAntiBotCommand();
// when
command.executeCommand(sender, Collections.singletonList("Off"), service);
@ -79,15 +80,9 @@ public class SwitchAntiBotCommandTest {
public void shouldShowHelpForUnknownState() {
// given
CommandSender sender = mock(CommandSender.class);
AntiBot antiBot = mock(AntiBot.class);
FoundCommandResult foundCommandResult = mock(FoundCommandResult.class);
CommandService service = mock(CommandService.class);
given(service.getAntiBot()).willReturn(antiBot);
given(service.mapPartsToCommand(sender, asList("authme", "antibot"))).willReturn(foundCommandResult);
ExecutableCommand command = new SwitchAntiBotCommand();
// when
command.executeCommand(sender, Collections.singletonList("wrong"), service);

View File

@ -12,7 +12,11 @@ import org.bukkit.command.CommandSender;
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.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.ArrayList;
import java.util.Arrays;
@ -33,26 +37,31 @@ import static org.mockito.Mockito.when;
/**
* Test for {@link ChangePasswordCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class ChangePasswordCommandTest {
@InjectMocks
private ChangePasswordCommand command;
@Mock
private PlayerCache playerCache;
@Mock
private CommandService commandService;
@Before
public void setUpMocks() {
commandService = mock(CommandService.class);
public void setSettings() {
when(commandService.getProperty(SecuritySettings.MIN_PASSWORD_LENGTH)).thenReturn(2);
when(commandService.getProperty(SecuritySettings.MAX_PASSWORD_LENGTH)).thenReturn(50);
// Only allow passwords with alphanumerical characters for the test
when(commandService.getProperty(RestrictionSettings.ALLOWED_PASSWORD_REGEX)).thenReturn("[a-zA-Z0-9]+");
when(commandService.getProperty(SecuritySettings.UNSAFE_PASSWORDS)).thenReturn(Collections.<String> emptyList());
when(commandService.getProperty(SecuritySettings.UNSAFE_PASSWORDS)).thenReturn(Collections.<String>emptyList());
}
@Test
public void shouldRejectNonPlayerSender() {
// given
CommandSender sender = mock(BlockCommandSender.class);
ChangePasswordCommand command = new ChangePasswordCommand();
// when
command.executeCommand(sender, new ArrayList<String>(), commandService);
@ -65,7 +74,6 @@ public class ChangePasswordCommandTest {
public void shouldRejectNotLoggedInPlayer() {
// given
CommandSender sender = initPlayerWithName("name", false);
ChangePasswordCommand command = new ChangePasswordCommand();
// when
command.executeCommand(sender, Arrays.asList("pass", "pass"), commandService);
@ -78,7 +86,6 @@ public class ChangePasswordCommandTest {
public void shouldRejectInvalidPassword() {
// given
CommandSender sender = initPlayerWithName("abc12", true);
ChangePasswordCommand command = new ChangePasswordCommand();
String password = "newPW";
given(commandService.validatePassword(password, "abc12")).willReturn(MessageKey.INVALID_PASSWORD_LENGTH);
@ -94,7 +101,6 @@ public class ChangePasswordCommandTest {
public void shouldForwardTheDataForValidPassword() {
// given
CommandSender sender = initPlayerWithName("parker", true);
ChangePasswordCommand command = new ChangePasswordCommand();
// when
command.executeCommand(sender, Arrays.asList("abc123", "abc123"), commandService);
@ -112,9 +118,7 @@ public class ChangePasswordCommandTest {
private Player initPlayerWithName(String name, boolean loggedIn) {
Player player = mock(Player.class);
when(player.getName()).thenReturn(name);
PlayerCache playerCache = mock(PlayerCache.class);
when(playerCache.isAuthenticated(name)).thenReturn(loggedIn);
when(commandService.getPlayerCache()).thenReturn(playerCache);
return player;
}

View File

@ -1,7 +1,6 @@
package hashmethods;
import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.EncryptionMethod;
import fr.xephi.authme.security.crypts.HexSaltedMethod;
import fr.xephi.authme.security.crypts.description.AsciiRestricted;
@ -55,7 +54,7 @@ public class EncryptionMethodInfoGatherer {
private static MethodDescription createDescription(HashAlgorithm algorithm) {
Class<? extends EncryptionMethod> clazz = algorithm.getClazz();
EncryptionMethod method = PasswordSecurity.initializeEncryptionMethod(algorithm, settings);
EncryptionMethod method = null; // TODO ljacqu PasswordSecurity.initializeEncryptionMethod(algorithm, settings);
if (method == null) {
throw new NullPointerException("Method for '" + algorithm + "' is null");
}