#306 Create PlayerCommand abstract type

- Create PlayerCommand to handle player-only command checks centrally and consistently
- Adjust tests
This commit is contained in:
ljacqu 2015-12-26 19:47:47 +01:00
parent 74ceb66430
commit 27fee3ca64
25 changed files with 250 additions and 393 deletions

View File

@ -47,6 +47,10 @@ public class CommandService {
messages.send(sender, messageKey); messages.send(sender, messageKey);
} }
public void send(CommandSender sender, MessageKey messageKey, String... replacements) {
messages.send(sender, messageKey, replacements);
}
/** /**
* Map command parts to a command description. * Map command parts to a command description.
* *

View File

@ -0,0 +1,46 @@
package fr.xephi.authme.command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.List;
/**
* Common base type for player-only commands, handling the verification that the command sender is indeed a player.
*/
public abstract class PlayerCommand implements ExecutableCommand {
@Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
if (sender instanceof Player) {
runCommand((Player) sender, arguments, commandService);
} else {
String alternative = getAlternativeCommand();
if (alternative != null) {
sender.sendMessage("Player only! Please use " + alternative + " instead.");
} else {
sender.sendMessage("This command is only for players.");
}
}
}
/**
* Run the command with the given player and arguments.
*
* @param player The player who initiated the command
* @param arguments The arguments supplied with the command
* @param commandService The command service
*/
protected abstract void runCommand(Player player, List<String> arguments, CommandService commandService);
/**
* Return an alternative command (textual representation) that is not restricted to players only.
* Example: "authme register &lt;playerName> &lt;password>"
*
* @return Alternative command not only for players, or null if not applicable
*/
protected String getAlternativeCommand() {
return null;
}
}

View File

@ -2,25 +2,22 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.settings.Spawn; import fr.xephi.authme.settings.Spawn;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.List; import java.util.List;
public class FirstSpawnCommand implements ExecutableCommand { public class FirstSpawnCommand extends PlayerCommand {
@Override @Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) { public void runCommand(Player player, List<String> arguments, CommandService commandService) {
// Make sure the command executor is a player // Make sure the command executor is a player
try { try {
if (sender instanceof Player) { if (Spawn.getInstance().getFirstSpawn() != null) {
if (Spawn.getInstance().getFirstSpawn() != null) player.teleport(Spawn.getInstance().getFirstSpawn());
((Player) sender).teleport(Spawn.getInstance().getFirstSpawn());
else sender.sendMessage("[AuthMe] First spawn has failed, please try to define the first spawn");
} else { } else {
sender.sendMessage("[AuthMe] Please use that command in game"); player.sendMessage("[AuthMe] First spawn has failed, please try to define the first spawn");
} }
} catch (NullPointerException ex) { } catch (NullPointerException ex) {
// TODO ljacqu 20151119: Catching NullPointerException is never a good idea. Find what can cause one instead // TODO ljacqu 20151119: Catching NullPointerException is never a good idea. Find what can cause one instead

View File

@ -2,24 +2,21 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.settings.Spawn; import fr.xephi.authme.settings.Spawn;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.List; import java.util.List;
public class SetFirstSpawnCommand implements ExecutableCommand { public class SetFirstSpawnCommand extends PlayerCommand {
@Override @Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) { public void runCommand(Player player, List<String> arguments, CommandService commandService) {
try { try {
if (sender instanceof Player) { if (Spawn.getInstance().setFirstSpawn(player.getLocation())) {
if (Spawn.getInstance().setFirstSpawn(((Player) sender).getLocation())) player.sendMessage("[AuthMe] Correctly defined new first spawn point");
sender.sendMessage("[AuthMe] Correctly defined new first spawn point");
else sender.sendMessage("[AuthMe] SetFirstSpawn has failed, please retry");
} else { } else {
sender.sendMessage("[AuthMe] Please use that command in game"); player.sendMessage("[AuthMe] SetFirstSpawn has failed, please retry");
} }
} catch (NullPointerException ex) { } catch (NullPointerException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());

View File

@ -2,27 +2,21 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.settings.Spawn; import fr.xephi.authme.settings.Spawn;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.List; import java.util.List;
public class SetSpawnCommand implements ExecutableCommand { public class SetSpawnCommand extends PlayerCommand {
@Override @Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) { public void runCommand(Player player, List<String> arguments, CommandService commandService) {
// Make sure the command executor is a player
try { try {
if (sender instanceof Player) { if (Spawn.getInstance().setSpawn(player.getLocation())) {
if (Spawn.getInstance().setSpawn(((Player) sender).getLocation())) { player.sendMessage("[AuthMe] Correctly defined new spawn point");
sender.sendMessage("[AuthMe] Correctly defined new spawn point");
} else {
sender.sendMessage("[AuthMe] SetSpawn has failed, please retry");
}
} else { } else {
sender.sendMessage("[AuthMe] Please use that command in game"); player.sendMessage("[AuthMe] SetSpawn has failed, please retry");
} }
} catch (NullPointerException ex) { } catch (NullPointerException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());

View File

@ -2,25 +2,21 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.settings.Spawn; import fr.xephi.authme.settings.Spawn;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.List; import java.util.List;
public class SpawnCommand implements ExecutableCommand { public class SpawnCommand extends PlayerCommand {
@Override @Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) { public void runCommand(Player player, List<String> arguments, CommandService commandService) {
// Make sure the command executor is a player
try { try {
if (sender instanceof Player) { if (Spawn.getInstance().getSpawn() != null) {
if (Spawn.getInstance().getSpawn() != null) player.teleport(Spawn.getInstance().getSpawn());
((Player) sender).teleport(Spawn.getInstance().getSpawn());
else sender.sendMessage("[AuthMe] Spawn has failed, please try to define the spawn");
} else { } else {
sender.sendMessage("[AuthMe] Please use the command in game"); player.sendMessage("[AuthMe] Spawn has failed, please try to define the spawn");
} }
} catch (NullPointerException ex) { } catch (NullPointerException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());

View File

@ -3,54 +3,36 @@ package fr.xephi.authme.command.executable.captcha;
import fr.xephi.authme.AuthMe; import fr.xephi.authme.AuthMe;
import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.security.RandomString; import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.Wrapper;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.List; import java.util.List;
public class CaptchaCommand implements ExecutableCommand { public class CaptchaCommand extends PlayerCommand {
@Override @Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) { public void runCommand(Player player, List<String> arguments, CommandService commandService) {
// Make sure the current command executor is a player
if (!(sender instanceof Player)) {
return;
}
// Get the player instance and name
final Player player = (Player) sender;
final String playerNameLowerCase = player.getName().toLowerCase(); final String playerNameLowerCase = player.getName().toLowerCase();
final String captcha = arguments.get(0);
// Get the parameter values final AuthMe plugin = AuthMe.getInstance();
String captcha = arguments.get(0);
// AuthMe plugin instance
final Wrapper wrapper = Wrapper.getInstance();
final AuthMe plugin = wrapper.getAuthMe();
// Messages instance
final Messages m = wrapper.getMessages();
// Command logic // Command logic
if (PlayerCache.getInstance().isAuthenticated(playerNameLowerCase)) { if (PlayerCache.getInstance().isAuthenticated(playerNameLowerCase)) {
m.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR); commandService.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
return; return;
} }
if (!Settings.useCaptcha) { if (!Settings.useCaptcha) {
m.send(player, MessageKey.USAGE_LOGIN); commandService.send(player, MessageKey.USAGE_LOGIN);
return; return;
} }
if (!plugin.cap.containsKey(playerNameLowerCase)) { if (!plugin.cap.containsKey(playerNameLowerCase)) {
m.send(player, MessageKey.USAGE_LOGIN); commandService.send(player, MessageKey.USAGE_LOGIN);
return; return;
} }
@ -58,7 +40,7 @@ public class CaptchaCommand implements ExecutableCommand {
plugin.cap.remove(playerNameLowerCase); plugin.cap.remove(playerNameLowerCase);
String randStr = new RandomString(Settings.captchaLength).nextString(); String randStr = new RandomString(Settings.captchaLength).nextString();
plugin.cap.put(playerNameLowerCase, randStr); plugin.cap.put(playerNameLowerCase, randStr);
m.send(player, MessageKey.CAPTCHA_WRONG_ERROR, plugin.cap.get(playerNameLowerCase)); commandService.send(player, MessageKey.CAPTCHA_WRONG_ERROR, plugin.cap.get(playerNameLowerCase));
return; return;
} }
@ -66,7 +48,7 @@ public class CaptchaCommand implements ExecutableCommand {
plugin.cap.remove(playerNameLowerCase); plugin.cap.remove(playerNameLowerCase);
// Show a status message // Show a status message
m.send(player, MessageKey.CAPTCHA_SUCCESS); commandService.send(player, MessageKey.CAPTCHA_SUCCESS);
m.send(player, MessageKey.LOGIN_MESSAGE); commandService.send(player, MessageKey.LOGIN_MESSAGE);
} }
} }

View File

@ -3,13 +3,11 @@ package fr.xephi.authme.command.executable.changepassword;
import fr.xephi.authme.AuthMe; import fr.xephi.authme.AuthMe;
import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.task.ChangePasswordTask; import fr.xephi.authme.task.ChangePasswordTask;
import fr.xephi.authme.util.Wrapper; import fr.xephi.authme.util.Wrapper;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.List; import java.util.List;
@ -17,58 +15,47 @@ import java.util.List;
/** /**
* The command for a player to change his password with. * The command for a player to change his password with.
*/ */
public class ChangePasswordCommand implements ExecutableCommand { public class ChangePasswordCommand extends PlayerCommand {
@Override @Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) { public void runCommand(Player player, List<String> arguments, CommandService commandService) {
// Make sure the current command executor is a player
if (!(sender instanceof Player)) {
return;
}
final Wrapper wrapper = Wrapper.getInstance();
final Messages m = wrapper.getMessages();
// Get the passwords
String oldPassword = arguments.get(0); String oldPassword = arguments.get(0);
String newPassword = arguments.get(1); String newPassword = arguments.get(1);
// Get the player instance and make sure he's authenticated
Player player = (Player) sender;
String name = player.getName().toLowerCase(); String name = player.getName().toLowerCase();
Wrapper wrapper = Wrapper.getInstance();
final PlayerCache playerCache = wrapper.getPlayerCache(); final PlayerCache playerCache = wrapper.getPlayerCache();
if (!playerCache.isAuthenticated(name)) { if (!playerCache.isAuthenticated(name)) {
m.send(player, MessageKey.NOT_LOGGED_IN); commandService.send(player, MessageKey.NOT_LOGGED_IN);
return; return;
} }
// Make sure the password is allowed // Make sure the password is allowed
String playerPassLowerCase = newPassword.toLowerCase(); String playerPassLowerCase = newPassword.toLowerCase();
// TODO #308: Remove SQL keywords check
if (playerPassLowerCase.contains("delete") || playerPassLowerCase.contains("where") if (playerPassLowerCase.contains("delete") || playerPassLowerCase.contains("where")
|| playerPassLowerCase.contains("insert") || playerPassLowerCase.contains("modify") || playerPassLowerCase.contains("insert") || playerPassLowerCase.contains("modify")
|| playerPassLowerCase.contains("from") || playerPassLowerCase.contains("select") || playerPassLowerCase.contains("from") || playerPassLowerCase.contains("select")
|| playerPassLowerCase.contains(";") || playerPassLowerCase.contains("null") || playerPassLowerCase.contains(";") || playerPassLowerCase.contains("null")
|| !playerPassLowerCase.matches(Settings.getPassRegex)) { || !playerPassLowerCase.matches(Settings.getPassRegex)) {
m.send(player, MessageKey.PASSWORD_MATCH_ERROR); commandService.send(player, MessageKey.PASSWORD_MATCH_ERROR);
return; return;
} }
if (playerPassLowerCase.equalsIgnoreCase(name)) { if (playerPassLowerCase.equalsIgnoreCase(name)) {
m.send(player, MessageKey.PASSWORD_IS_USERNAME_ERROR); commandService.send(player, MessageKey.PASSWORD_IS_USERNAME_ERROR);
return; return;
} }
if (playerPassLowerCase.length() < Settings.getPasswordMinLen if (playerPassLowerCase.length() < Settings.getPasswordMinLen
|| playerPassLowerCase.length() > Settings.passwordMaxLength) { || playerPassLowerCase.length() > Settings.passwordMaxLength) {
m.send(player, MessageKey.INVALID_PASSWORD_LENGTH); commandService.send(player, MessageKey.INVALID_PASSWORD_LENGTH);
return; return;
} }
if (!Settings.unsafePasswords.isEmpty() && Settings.unsafePasswords.contains(playerPassLowerCase)) { if (!Settings.unsafePasswords.isEmpty() && Settings.unsafePasswords.contains(playerPassLowerCase)) {
m.send(player, MessageKey.PASSWORD_UNSAFE_ERROR); commandService.send(player, MessageKey.PASSWORD_UNSAFE_ERROR);
return; return;
} }
// Set the password AuthMe plugin = AuthMe.getInstance();
final AuthMe plugin = wrapper.getAuthMe(); commandService.runTaskAsynchronously(new ChangePasswordTask(plugin, player, oldPassword, newPassword));
wrapper.getScheduler().runTaskAsynchronously(plugin,
new ChangePasswordTask(plugin, player, oldPassword, newPassword));
} }
} }

View File

@ -14,8 +14,6 @@ import fr.xephi.authme.converter.SqliteToSql;
import fr.xephi.authme.converter.vAuthConverter; import fr.xephi.authme.converter.vAuthConverter;
import fr.xephi.authme.converter.xAuthConverter; import fr.xephi.authme.converter.xAuthConverter;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import java.util.List; import java.util.List;
@ -27,16 +25,13 @@ public class ConverterCommand implements ExecutableCommand {
// AuthMe plugin instance // AuthMe plugin instance
final AuthMe plugin = AuthMe.getInstance(); final AuthMe plugin = AuthMe.getInstance();
// Messages instance
final Messages m = plugin.getMessages();
// Get the conversion job // Get the conversion job
String job = arguments.get(0); String job = arguments.get(0);
// Determine the job type // Determine the job type
ConvertType jobType = ConvertType.fromName(job); ConvertType jobType = ConvertType.fromName(job);
if (jobType == null) { if (jobType == null) {
m.send(sender, MessageKey.ERROR); commandService.send(sender, MessageKey.ERROR);
return; return;
} }
@ -75,7 +70,7 @@ public class ConverterCommand implements ExecutableCommand {
} }
// Run the convert job // Run the convert job
Bukkit.getScheduler().runTaskAsynchronously(plugin, converter); commandService.runTaskAsynchronously(converter);
// Show a status message // Show a status message
sender.sendMessage("[AuthMe] Successfully converted from " + jobType.getName()); sender.sendMessage("[AuthMe] Successfully converted from " + jobType.getName());

View File

@ -1,30 +1,18 @@
package fr.xephi.authme.command.executable.email; package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.util.Wrapper;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.List; import java.util.List;
public class AddEmailCommand implements ExecutableCommand { public class AddEmailCommand extends PlayerCommand {
@Override @Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) { public void runCommand(Player player, List<String> arguments, CommandService commandService) {
// Make sure the current command executor is a player
if (!(sender instanceof Player)) {
return;
}
// Get the parameter values
String playerMail = arguments.get(0); String playerMail = arguments.get(0);
String playerMailVerify = arguments.get(1); String playerMailVerify = arguments.get(1);
// Get the player and perform email addition commandService.getManagement().performAddEmail(player, playerMail, playerMailVerify);
final AuthMe plugin = Wrapper.getInstance().getAuthMe();
final Player player = (Player) sender;
plugin.getManagement().performAddEmail(player, playerMail, playerMailVerify);
} }
} }

View File

@ -1,32 +1,21 @@
package fr.xephi.authme.command.executable.email; package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.util.Wrapper;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.List; import java.util.List;
/** /**
* Change email command.
*/ */
public class ChangeEmailCommand implements ExecutableCommand { public class ChangeEmailCommand extends PlayerCommand {
@Override @Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) { public void runCommand(Player player, List<String> arguments, CommandService commandService) {
// Make sure the current command executor is a player
if (!(sender instanceof Player)) {
return;
}
// Get the parameter values
String playerMailOld = arguments.get(0); String playerMailOld = arguments.get(0);
String playerMailNew = arguments.get(1); String playerMailNew = arguments.get(1);
// Get the player instance and execute action commandService.getManagement().performChangeEmail(player, playerMailOld, playerMailNew);
final AuthMe plugin = Wrapper.getInstance().getAuthMe();
final Player player = (Player) sender;
plugin.getManagement().performChangeEmail(player, playerMailOld, playerMailNew);
} }
} }

View File

@ -5,47 +5,36 @@ import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.security.PasswordSecurity; import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.RandomString; import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.Wrapper; import fr.xephi.authme.util.StringUtils;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.List; import java.util.List;
public class RecoverEmailCommand implements ExecutableCommand { public class RecoverEmailCommand extends PlayerCommand {
@Override @Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) { public void runCommand(Player player, List<String> arguments, CommandService commandService) {
// Make sure the current command executor is a player
if (!(sender instanceof Player)) {
return;
}
// Get the parameter values
String playerMail = arguments.get(0); String playerMail = arguments.get(0);
// Get the player instance and name
final Player player = (Player) sender;
final String playerName = player.getName(); final String playerName = player.getName();
// Command logic // Command logic
final Wrapper wrapper = Wrapper.getInstance(); final AuthMe plugin = AuthMe.getInstance();
final AuthMe plugin = wrapper.getAuthMe();
final Messages m = wrapper.getMessages();
if (plugin.mail == null) { if (plugin.mail == null) {
m.send(player, MessageKey.ERROR); commandService.send(player, MessageKey.ERROR);
return; return;
} }
if (plugin.database.isAuthAvailable(playerName)) { DataSource dataSource = commandService.getDataSource();
if (dataSource.isAuthAvailable(playerName)) {
if (PlayerCache.getInstance().isAuthenticated(playerName)) { if (PlayerCache.getInstance().isAuthenticated(playerName)) {
m.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR); commandService.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
return; return;
} }
try { try {
@ -55,33 +44,32 @@ public class RecoverEmailCommand implements ExecutableCommand {
PlayerAuth auth; PlayerAuth auth;
if (PlayerCache.getInstance().isAuthenticated(playerName)) { if (PlayerCache.getInstance().isAuthenticated(playerName)) {
auth = PlayerCache.getInstance().getAuth(playerName); auth = PlayerCache.getInstance().getAuth(playerName);
} else if (plugin.database.isAuthAvailable(playerName)) { } else if (dataSource.isAuthAvailable(playerName)) {
auth = plugin.database.getAuth(playerName); auth = dataSource.getAuth(playerName);
} else { } else {
m.send(player, MessageKey.UNKNOWN_USER); commandService.send(player, MessageKey.UNKNOWN_USER);
return; return;
} }
if (Settings.getmailAccount.equals("") || Settings.getmailAccount.isEmpty()) { if (Settings.getmailAccount.equals("") || Settings.getmailAccount.isEmpty()) {
m.send(player, MessageKey.ERROR); commandService.send(player, MessageKey.ERROR);
return; return;
} }
if (!playerMail.equalsIgnoreCase(auth.getEmail()) || playerMail.equalsIgnoreCase("your@email.com") if (!playerMail.equalsIgnoreCase(auth.getEmail()) || playerMail.equalsIgnoreCase("your@email.com")
|| auth.getEmail().equalsIgnoreCase("your@email.com")) { || auth.getEmail().equalsIgnoreCase("your@email.com")) {
m.send(player, MessageKey.INVALID_EMAIL); commandService.send(player, MessageKey.INVALID_EMAIL);
return; return;
} }
auth.setHash(hashNew); auth.setHash(hashNew);
plugin.database.updatePassword(auth); dataSource.updatePassword(auth);
plugin.mail.main(auth, thePass); plugin.mail.main(auth, thePass);
m.send(player, MessageKey.RECOVERY_EMAIL_SENT_MESSAGE); commandService.send(player, MessageKey.RECOVERY_EMAIL_SENT_MESSAGE);
} catch (NoSuchAlgorithmException | NoClassDefFoundError ex) { } catch (NoSuchAlgorithmException | NoClassDefFoundError ex) {
ex.printStackTrace(); ConsoleLogger.showError(StringUtils.formatException(ex));
ConsoleLogger.showError(ex.getMessage()); commandService.send(player, MessageKey.ERROR);
m.send(sender, MessageKey.ERROR);
} }
} else { } else {
m.send(player, MessageKey.REGISTER_EMAIL_MESSAGE); commandService.send(player, MessageKey.REGISTER_EMAIL_MESSAGE);
} }
} }
} }

View File

@ -1,29 +1,19 @@
package fr.xephi.authme.command.executable.login; package fr.xephi.authme.command.executable.login;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.util.Wrapper;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.List; import java.util.List;
public class LoginCommand implements ExecutableCommand { /**
* Login command.
*/
public class LoginCommand extends PlayerCommand {
@Override @Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) { public void runCommand(Player player, List<String> arguments, CommandService commandService) {
// Make sure the current command executor is a player
if (!(sender instanceof Player)) {
return;
}
// Get the necessary objects
final AuthMe plugin = Wrapper.getInstance().getAuthMe();
final Player player = (Player) sender;
final String password = arguments.get(0); final String password = arguments.get(0);
commandService.getManagement().performLogin(player, password, false);
// Log the player in
plugin.getManagement().performLogin(player, password, false);
} }
} }

View File

@ -1,30 +1,18 @@
package fr.xephi.authme.command.executable.logout; package fr.xephi.authme.command.executable.logout;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.util.Wrapper;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.List; import java.util.List;
/** /**
* Logout command.
*/ */
public class LogoutCommand implements ExecutableCommand { public class LogoutCommand extends PlayerCommand {
@Override @Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) { public void runCommand(Player player, List<String> arguments, CommandService commandService) {
// Make sure the current command executor is a player commandService.getManagement().performLogout(player);
if (!(sender instanceof Player)) {
return;
}
// Get the player instance
final AuthMe plugin = Wrapper.getInstance().getAuthMe();
final Player player = (Player) sender;
// Logout the player
plugin.getManagement().performLogout(player);
} }
} }

View File

@ -1,61 +1,48 @@
package fr.xephi.authme.command.executable.register; package fr.xephi.authme.command.executable.register;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.process.Management; import fr.xephi.authme.process.Management;
import fr.xephi.authme.security.RandomString; import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.Wrapper;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.List; import java.util.List;
public class RegisterCommand implements ExecutableCommand { public class RegisterCommand extends PlayerCommand {
@Override @Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) { public void runCommand(Player player, List<String> arguments, CommandService commandService) {
// Make sure the sender is a player if (arguments.isEmpty() || Settings.enablePasswordConfirmation && arguments.size() < 2) {
if (!(sender instanceof Player)) { commandService.send(player, MessageKey.USAGE_REGISTER);
sender.sendMessage("Player Only! Use 'authme register <playername> <password>' instead");
return; return;
} }
final Wrapper wrapper = Wrapper.getInstance(); final Management management = commandService.getManagement();
final AuthMe plugin = wrapper.getAuthMe();
final Messages m = wrapper.getMessages();
// Make sure the command arguments are valid
final Player player = (Player) sender;
if (arguments.isEmpty() || (Settings.enablePasswordConfirmation && arguments.size() < 2)) {
m.send(player, MessageKey.USAGE_REGISTER);
return;
}
final Management management = plugin.getManagement();
if (Settings.emailRegistration && !Settings.getmailAccount.isEmpty()) { if (Settings.emailRegistration && !Settings.getmailAccount.isEmpty()) {
if (Settings.doubleEmailCheck && arguments.size() < 2 || !arguments.get(0).equals(arguments.get(1))) { if (Settings.doubleEmailCheck && arguments.size() < 2 || !arguments.get(0).equals(arguments.get(1))) {
m.send(player, MessageKey.USAGE_REGISTER); commandService.send(player, MessageKey.USAGE_REGISTER);
return; return;
} }
final String email = arguments.get(0); final String email = arguments.get(0);
if (!Settings.isEmailCorrect(email)) { if (!Settings.isEmailCorrect(email)) {
m.send(player, MessageKey.INVALID_EMAIL); commandService.send(player, MessageKey.INVALID_EMAIL);
return; return;
} }
final String thePass = new RandomString(Settings.getRecoveryPassLength).nextString(); final String thePass = RandomString.generate(Settings.getRecoveryPassLength);
management.performRegister(player, thePass, email); management.performRegister(player, thePass, email);
return; return;
} }
if (arguments.size() > 1 && Settings.enablePasswordConfirmation) { if (arguments.size() > 1 && Settings.enablePasswordConfirmation && !arguments.get(0).equals(arguments.get(1))) {
if (!arguments.get(0).equals(arguments.get(1))) { commandService.send(player, MessageKey.PASSWORD_MATCH_ERROR);
m.send(player, MessageKey.PASSWORD_MATCH_ERROR); return;
return;
}
} }
management.performRegister(player, arguments.get(0), ""); management.performRegister(player, arguments.get(0), "");
} }
@Override
public String getAlternativeCommand() {
return "authme register <playername> <password>";
}
} }

View File

@ -1,42 +1,27 @@
package fr.xephi.authme.command.executable.unregister; package fr.xephi.authme.command.executable.unregister;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.List; import java.util.List;
public class UnregisterCommand implements ExecutableCommand { public class UnregisterCommand extends PlayerCommand {
@Override @Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) { public void runCommand(Player player, List<String> arguments, CommandService commandService) {
// Make sure the current command executor is a player
if (!(sender instanceof Player)) {
return;
}
final AuthMe plugin = AuthMe.getInstance();
final Messages m = plugin.getMessages();
// Get the password
String playerPass = arguments.get(0); String playerPass = arguments.get(0);
// Get the player instance and name
final Player player = (Player) sender;
final String playerNameLowerCase = player.getName().toLowerCase(); final String playerNameLowerCase = player.getName().toLowerCase();
// Make sure the player is authenticated // Make sure the player is authenticated
if (!PlayerCache.getInstance().isAuthenticated(playerNameLowerCase)) { if (!PlayerCache.getInstance().isAuthenticated(playerNameLowerCase)) {
m.send(player, MessageKey.NOT_LOGGED_IN); commandService.send(player, MessageKey.NOT_LOGGED_IN);
return; return;
} }
// Unregister the player // Unregister the player
plugin.getManagement().performUnregister(player, playerPass, false); commandService.getManagement().performUnregister(player, playerPass, false);
} }
} }

View File

@ -3,29 +3,16 @@ package fr.xephi.authme.converter;
import fr.xephi.authme.AuthMe; import fr.xephi.authme.AuthMe;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
/**
*/
public class xAuthConverter implements Converter { public class xAuthConverter implements Converter {
public final AuthMe plugin; public final AuthMe plugin;
public final CommandSender sender; public final CommandSender sender;
/**
* Constructor for xAuthConverter.
*
* @param plugin AuthMe
* @param sender CommandSender
*/
public xAuthConverter(AuthMe plugin, CommandSender sender) { public xAuthConverter(AuthMe plugin, CommandSender sender) {
this.plugin = plugin; this.plugin = plugin;
this.sender = sender; this.sender = sender;
} }
/**
* Method run.
*
* @see java.lang.Runnable#run()
*/
@Override @Override
public void run() { public void run() {
try { try {

View File

@ -16,8 +16,6 @@ import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
/**
*/
public class xAuthToFlat { public class xAuthToFlat {
public final AuthMe instance; public final AuthMe instance;
@ -36,11 +34,6 @@ public class xAuthToFlat {
this.sender = sender; this.sender = sender;
} }
/**
* Method convert.
*
* @return boolean
*/
public boolean convert() { public boolean convert() {
if (instance.getServer().getPluginManager().getPlugin("xAuth") == null) { if (instance.getServer().getPluginManager().getPlugin("xAuth") == null) {
sender.sendMessage("[AuthMe] xAuth plugin not found"); sender.sendMessage("[AuthMe] xAuth plugin not found");
@ -71,13 +64,6 @@ public class xAuthToFlat {
return true; return true;
} }
/**
* Method getIdPlayer.
*
* @param id int
*
* @return String
*/
public String getIdPlayer(int id) { public String getIdPlayer(int id) {
String realPass = ""; String realPass = "";
Connection conn = xAuth.getPlugin().getDatabaseController().getConnection(); Connection conn = xAuth.getPlugin().getDatabaseController().getConnection();
@ -100,11 +86,6 @@ public class xAuthToFlat {
return realPass; return realPass;
} }
/**
* Method getXAuthPlayers.
*
* @return List<Integer>
*/
public List<Integer> getXAuthPlayers() { public List<Integer> getXAuthPlayers() {
List<Integer> xP = new ArrayList<>(); List<Integer> xP = new ArrayList<>();
Connection conn = xAuth.getPlugin().getDatabaseController().getConnection(); Connection conn = xAuth.getPlugin().getDatabaseController().getConnection();
@ -126,13 +107,6 @@ public class xAuthToFlat {
return xP; return xP;
} }
/**
* Method getPassword.
*
* @param accountId int
*
* @return String
*/
public String getPassword(int accountId) { public String getPassword(int accountId) {
String realPass = ""; String realPass = "";
Connection conn = xAuth.getPlugin().getDatabaseController().getConnection(); Connection conn = xAuth.getPlugin().getDatabaseController().getConnection();

View File

@ -15,8 +15,6 @@ import com.google.common.io.ByteStreams;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
/**
*/
public class ChangePasswordTask implements Runnable { public class ChangePasswordTask implements Runnable {
private final AuthMe plugin; private final AuthMe plugin;
@ -24,14 +22,6 @@ public class ChangePasswordTask implements Runnable {
private final String oldPassword; private final String oldPassword;
private final String newPassword; private final String newPassword;
/**
* Constructor for ChangePasswordTask.
*
* @param plugin AuthMe
* @param player Player
* @param oldPassword String
* @param newPassword String
*/
public ChangePasswordTask(AuthMe plugin, Player player, String oldPassword, String newPassword) { public ChangePasswordTask(AuthMe plugin, Player player, String oldPassword, String newPassword) {
this.plugin = plugin; this.plugin = plugin;
this.player = player; this.player = player;

View File

@ -1,11 +1,9 @@
package fr.xephi.authme.command.executable.changepassword; package fr.xephi.authme.command.executable.changepassword;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ReflectionTestUtils; import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.task.ChangePasswordTask; import fr.xephi.authme.task.ChangePasswordTask;
import fr.xephi.authme.util.WrapperMock; import fr.xephi.authme.util.WrapperMock;
@ -37,14 +35,14 @@ import static org.mockito.Mockito.when;
public class ChangePasswordCommandTest { public class ChangePasswordCommandTest {
private WrapperMock wrapperMock; private WrapperMock wrapperMock;
private Messages messagesMock;
private PlayerCache cacheMock; private PlayerCache cacheMock;
private CommandService commandService;
@Before @Before
public void setUpMocks() { public void setUpMocks() {
wrapperMock = WrapperMock.createInstance(); wrapperMock = WrapperMock.createInstance();
messagesMock = wrapperMock.getMessages();
cacheMock = wrapperMock.getPlayerCache(); cacheMock = wrapperMock.getPlayerCache();
commandService = mock(CommandService.class);
// Only allow passwords with alphanumerical characters for the test // Only allow passwords with alphanumerical characters for the test
Settings.getPassRegex = "[a-zA-Z0-9]+"; Settings.getPassRegex = "[a-zA-Z0-9]+";
@ -60,7 +58,7 @@ public class ChangePasswordCommandTest {
ChangePasswordCommand command = new ChangePasswordCommand(); ChangePasswordCommand command = new ChangePasswordCommand();
// when // when
command.executeCommand(sender, new ArrayList<String>(), mock(CommandService.class)); command.executeCommand(sender, new ArrayList<String>(), commandService);
// then // then
assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false)); assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false));
@ -73,10 +71,10 @@ public class ChangePasswordCommandTest {
ChangePasswordCommand command = new ChangePasswordCommand(); ChangePasswordCommand command = new ChangePasswordCommand();
// when // when
command.executeCommand(sender, Arrays.asList("pass", "pass"), mock(CommandService.class)); command.executeCommand(sender, Arrays.asList("pass", "pass"), commandService);
// then // then
verify(messagesMock).send(sender, MessageKey.NOT_LOGGED_IN); verify(commandService).send(sender, MessageKey.NOT_LOGGED_IN);
assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false)); assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false));
} }
@ -87,10 +85,10 @@ public class ChangePasswordCommandTest {
ChangePasswordCommand command = new ChangePasswordCommand(); ChangePasswordCommand command = new ChangePasswordCommand();
// when // when
command.executeCommand(sender, Arrays.asList("old123", "!pass"), mock(CommandService.class)); command.executeCommand(sender, Arrays.asList("old123", "!pass"), commandService);
// then // then
verify(messagesMock).send(sender, MessageKey.PASSWORD_MATCH_ERROR); verify(commandService).send(sender, MessageKey.PASSWORD_MATCH_ERROR);
assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false)); assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false));
} }
@ -102,10 +100,10 @@ public class ChangePasswordCommandTest {
ChangePasswordCommand command = new ChangePasswordCommand(); ChangePasswordCommand command = new ChangePasswordCommand();
// when // when
command.executeCommand(sender, Arrays.asList("old_", "Tester"), mock(CommandService.class)); command.executeCommand(sender, Arrays.asList("old_", "Tester"), commandService);
// then // then
verify(messagesMock).send(sender, MessageKey.PASSWORD_IS_USERNAME_ERROR); verify(commandService).send(sender, MessageKey.PASSWORD_IS_USERNAME_ERROR);
assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false)); assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false));
} }
@ -117,10 +115,10 @@ public class ChangePasswordCommandTest {
Settings.passwordMaxLength = 3; Settings.passwordMaxLength = 3;
// when // when
command.executeCommand(sender, Arrays.asList("12", "test"), mock(CommandService.class)); command.executeCommand(sender, Arrays.asList("12", "test"), commandService);
// then // then
verify(messagesMock).send(sender, MessageKey.INVALID_PASSWORD_LENGTH); verify(commandService).send(sender, MessageKey.INVALID_PASSWORD_LENGTH);
assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false)); assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false));
} }
@ -132,10 +130,10 @@ public class ChangePasswordCommandTest {
Settings.getPasswordMinLen = 7; Settings.getPasswordMinLen = 7;
// when // when
command.executeCommand(sender, Arrays.asList("oldverylongpassword", "tester"), mock(CommandService.class)); command.executeCommand(sender, Arrays.asList("oldverylongpassword", "tester"), commandService);
// then // then
verify(messagesMock).send(sender, MessageKey.INVALID_PASSWORD_LENGTH); verify(commandService).send(sender, MessageKey.INVALID_PASSWORD_LENGTH);
assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false)); assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false));
} }
@ -147,10 +145,10 @@ public class ChangePasswordCommandTest {
Settings.unsafePasswords = asList("test", "abc123"); Settings.unsafePasswords = asList("test", "abc123");
// when // when
command.executeCommand(sender, Arrays.asList("oldpw", "abc123"), mock(CommandService.class)); command.executeCommand(sender, Arrays.asList("oldpw", "abc123"), commandService);
// then // then
verify(messagesMock).send(sender, MessageKey.PASSWORD_UNSAFE_ERROR); verify(commandService).send(sender, MessageKey.PASSWORD_UNSAFE_ERROR);
assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false)); assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false));
} }
@ -161,12 +159,12 @@ public class ChangePasswordCommandTest {
ChangePasswordCommand command = new ChangePasswordCommand(); ChangePasswordCommand command = new ChangePasswordCommand();
// when // when
command.executeCommand(sender, Arrays.asList("abc123", "abc123"), mock(CommandService.class)); command.executeCommand(sender, Arrays.asList("abc123", "abc123"), commandService);
// then // then
verify(messagesMock, never()).send(eq(sender), any(MessageKey.class)); verify(commandService, never()).send(eq(sender), any(MessageKey.class));
ArgumentCaptor<ChangePasswordTask> taskCaptor = ArgumentCaptor.forClass(ChangePasswordTask.class); ArgumentCaptor<ChangePasswordTask> taskCaptor = ArgumentCaptor.forClass(ChangePasswordTask.class);
verify(wrapperMock.getScheduler()).runTaskAsynchronously(any(AuthMe.class), taskCaptor.capture()); verify(commandService).runTaskAsynchronously(taskCaptor.capture());
ChangePasswordTask task = taskCaptor.getValue(); ChangePasswordTask task = taskCaptor.getValue();
assertThat((String) ReflectionTestUtils.getFieldValue(ChangePasswordTask.class, task, "newPassword"), assertThat((String) ReflectionTestUtils.getFieldValue(ChangePasswordTask.class, task, "newPassword"),
equalTo("abc123")); equalTo("abc123"));

View File

@ -1,9 +1,7 @@
package fr.xephi.authme.command.executable.email; package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.process.Management; import fr.xephi.authme.process.Management;
import fr.xephi.authme.util.WrapperMock;
import org.bukkit.command.BlockCommandSender; import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -14,25 +12,21 @@ import org.mockito.Mockito;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/** /**
* Test for {@link AddEmailCommand}. * Test for {@link AddEmailCommand}.
*/ */
public class AddEmailCommandTest { public class AddEmailCommandTest {
private AuthMe authMeMock; private CommandService commandService;
private Management managementMock;
@Before @Before
public void setUpMocks() { public void setUpMocks() {
WrapperMock wrapper = WrapperMock.createInstance(); commandService = mock(CommandService.class);
authMeMock = wrapper.getAuthMe();
managementMock = Mockito.mock(Management.class);
when(authMeMock.getManagement()).thenReturn(managementMock);
} }
@Test @Test
@ -42,10 +36,10 @@ public class AddEmailCommandTest {
AddEmailCommand command = new AddEmailCommand(); AddEmailCommand command = new AddEmailCommand();
// when // when
command.executeCommand(sender, new ArrayList<String>(), mock(CommandService.class)); command.executeCommand(sender, new ArrayList<String>(), commandService);
// then // then
verify(authMeMock, never()).getManagement(); verify(commandService, never()).getManagement();
} }
@Test @Test
@ -53,13 +47,14 @@ public class AddEmailCommandTest {
// given // given
Player sender = Mockito.mock(Player.class); Player sender = Mockito.mock(Player.class);
AddEmailCommand command = new AddEmailCommand(); AddEmailCommand command = new AddEmailCommand();
Management management = mock(Management.class);
given(commandService.getManagement()).willReturn(management);
// when // when
command.executeCommand(sender, Arrays.asList("mail@example", "other_example"), mock(CommandService.class)); command.executeCommand(sender, Arrays.asList("mail@example", "other_example"), commandService);
// then // then
verify(authMeMock).getManagement(); verify(management).performAddEmail(sender, "mail@example", "other_example");
verify(managementMock).performAddEmail(sender, "mail@example", "other_example");
} }
} }

View File

@ -1,38 +1,31 @@
package fr.xephi.authme.command.executable.email; package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.process.Management; import fr.xephi.authme.process.Management;
import fr.xephi.authme.util.WrapperMock;
import org.bukkit.command.BlockCommandSender; import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/** /**
* Test for {@link ChangeEmailCommand}. * Test for {@link ChangeEmailCommand}.
*/ */
public class ChangeEmailCommandTest { public class ChangeEmailCommandTest {
private AuthMe authMeMock; private CommandService commandService;
private Management managementMock;
@Before @Before
public void setUpMocks() { public void setUpMocks() {
WrapperMock wrapper = WrapperMock.createInstance(); commandService = mock(CommandService.class);
authMeMock = wrapper.getAuthMe();
managementMock = mock(Management.class);
when(authMeMock.getManagement()).thenReturn(managementMock);
} }
@Test @Test
@ -42,10 +35,10 @@ public class ChangeEmailCommandTest {
ChangeEmailCommand command = new ChangeEmailCommand(); ChangeEmailCommand command = new ChangeEmailCommand();
// when // when
command.executeCommand(sender, new ArrayList<String>(), mock(CommandService.class)); command.executeCommand(sender, new ArrayList<String>(), commandService);
// then // then
verify(authMeMock, never()).getManagement(); verify(commandService, never()).getManagement();
} }
@Test @Test
@ -53,14 +46,14 @@ public class ChangeEmailCommandTest {
// given // given
Player sender = mock(Player.class); Player sender = mock(Player.class);
ChangeEmailCommand command = new ChangeEmailCommand(); ChangeEmailCommand command = new ChangeEmailCommand();
Management management = mock(Management.class);
given(commandService.getManagement()).willReturn(management);
// when // when
command.executeCommand(sender, Arrays.asList("new.mail@example.org", "old_mail@example.org"), command.executeCommand(sender, Arrays.asList("new.mail@example.org", "old_mail@example.org"), commandService);
mock(CommandService.class));
// then // then
verify(authMeMock).getManagement(); verify(management).performChangeEmail(sender, "new.mail@example.org", "old_mail@example.org");
verify(managementMock).performChangeEmail(sender, "new.mail@example.org", "old_mail@example.org");
} }
} }

View File

@ -9,29 +9,32 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito; import org.mockito.Mockito;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import static org.mockito.Matchers.*; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
/** /**
* Test for {@link LoginCommand}. * Test for {@link LoginCommand}.
*/ */
public class LoginCommandTest { public class LoginCommandTest {
private static Management managementMock; private CommandService commandService;
@Before @Before
public void initializeAuthMeMock() { public void initializeAuthMeMock() {
WrapperMock wrapper = WrapperMock.createInstance(); WrapperMock.createInstance();
Settings.captchaLength = 10; Settings.captchaLength = 10;
managementMock = mock(Management.class); commandService = mock(CommandService.class);
Mockito.when(wrapper.getAuthMe().getManagement()).thenReturn(managementMock);
} }
@Test @Test
@ -41,10 +44,13 @@ public class LoginCommandTest {
LoginCommand command = new LoginCommand(); LoginCommand command = new LoginCommand();
// when // when
command.executeCommand(sender, new ArrayList<String>(), mock(CommandService.class)); command.executeCommand(sender, new ArrayList<String>(), commandService);
// then // then
Mockito.verify(managementMock, never()).performLogin(any(Player.class), anyString(), anyBoolean()); Mockito.verify(commandService, never()).getManagement();
ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class);
verify(sender).sendMessage(messageCaptor.capture());
assertThat(messageCaptor.getValue(), containsString("only for players"));
} }
@Test @Test
@ -52,12 +58,14 @@ public class LoginCommandTest {
// given // given
Player sender = mock(Player.class); Player sender = mock(Player.class);
LoginCommand command = new LoginCommand(); LoginCommand command = new LoginCommand();
Management management = mock(Management.class);
given(commandService.getManagement()).willReturn(management);
// when // when
command.executeCommand(sender, Collections.singletonList("password"), mock(CommandService.class)); command.executeCommand(sender, Collections.singletonList("password"), commandService);
// then // then
Mockito.verify(managementMock).performLogin(eq(sender), eq("password"), eq(false)); Mockito.verify(management).performLogin(eq(sender), eq("password"), eq(false));
} }
} }

View File

@ -1,6 +1,5 @@
package fr.xephi.authme.command.executable.logout; package fr.xephi.authme.command.executable.logout;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.process.Management; import fr.xephi.authme.process.Management;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
@ -10,30 +9,30 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito; import org.mockito.ArgumentCaptor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import static org.mockito.Matchers.any; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
/** /**
* Test for {@link LogoutCommand}. * Test for {@link LogoutCommand}.
*/ */
public class LogoutCommandTest { public class LogoutCommandTest {
private static Management managementMock; private CommandService commandService;
@Before @Before
public void initializeAuthMeMock() { public void initializeAuthMeMock() {
WrapperMock wrapper = WrapperMock.createInstance(); WrapperMock.createInstance();
AuthMe pluginMock = wrapper.getAuthMe();
Settings.captchaLength = 10; Settings.captchaLength = 10;
managementMock = mock(Management.class); commandService = mock(CommandService.class);
Mockito.when(pluginMock.getManagement()).thenReturn(managementMock);
} }
@Test @Test
@ -43,10 +42,13 @@ public class LogoutCommandTest {
LogoutCommand command = new LogoutCommand(); LogoutCommand command = new LogoutCommand();
// when // when
command.executeCommand(sender, new ArrayList<String>(), mock(CommandService.class)); command.executeCommand(sender, new ArrayList<String>(), commandService);
// then // then
Mockito.verify(managementMock, never()).performLogout(any(Player.class)); verify(commandService, never()).getManagement();
ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class);
verify(sender).sendMessage(messageCaptor.capture());
assertThat(messageCaptor.getValue(), containsString("only for players"));
} }
@Test @Test
@ -54,12 +56,14 @@ public class LogoutCommandTest {
// given // given
Player sender = mock(Player.class); Player sender = mock(Player.class);
LogoutCommand command = new LogoutCommand(); LogoutCommand command = new LogoutCommand();
Management management = mock(Management.class);
given(commandService.getManagement()).willReturn(management);
// when // when
command.executeCommand(sender, Collections.singletonList("password"), mock(CommandService.class)); command.executeCommand(sender, Collections.singletonList("password"), commandService);
// then // then
Mockito.verify(managementMock).performLogout(sender); verify(management).performLogout(sender);
} }
} }

View File

@ -1,9 +1,8 @@
package fr.xephi.authme.command.executable.register; package fr.xephi.authme.command.executable.register;
import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages; import fr.xephi.authme.process.Management;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.WrapperMock; import fr.xephi.authme.util.WrapperMock;
import org.bukkit.command.BlockCommandSender; import org.bukkit.command.BlockCommandSender;
@ -12,15 +11,13 @@ import org.bukkit.entity.Player;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any; import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@ -30,17 +27,13 @@ import static org.mockito.Mockito.verify;
*/ */
public class RegisterCommandTest { public class RegisterCommandTest {
private static Management managementMock; private CommandService commandService;
private static Messages messagesMock;
@Before @Before
public void initializeAuthMeMock() { public void initializeAuthMeMock() {
WrapperMock wrapper = WrapperMock.createInstance(); WrapperMock.createInstance();
messagesMock = wrapper.getMessages();
Settings.captchaLength = 10; Settings.captchaLength = 10;
managementMock = mock(Management.class); commandService = mock(CommandService.class);
Mockito.when(wrapper.getAuthMe().getManagement()).thenReturn(managementMock);
} }
@Test @Test
@ -48,15 +41,15 @@ public class RegisterCommandTest {
// given // given
CommandSender sender = mock(BlockCommandSender.class); CommandSender sender = mock(BlockCommandSender.class);
RegisterCommand command = new RegisterCommand(); RegisterCommand command = new RegisterCommand();
ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class);
// when // when
command.executeCommand(sender, new ArrayList<String>(), mock(CommandService.class)); command.executeCommand(sender, new ArrayList<String>(), commandService);
// then // then
verify(commandService, never()).getManagement();
ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class);
verify(sender).sendMessage(messageCaptor.capture()); verify(sender).sendMessage(messageCaptor.capture());
assertThat(messageCaptor.getValue().contains("Player Only!"), equalTo(true)); assertThat(messageCaptor.getValue(), containsString("Player only!"));
verify(managementMock, never()).performRegister(any(Player.class), anyString(), anyString());
} }
@Test @Test
@ -66,11 +59,11 @@ public class RegisterCommandTest {
RegisterCommand command = new RegisterCommand(); RegisterCommand command = new RegisterCommand();
// when // when
command.executeCommand(sender, new ArrayList<String>(), mock(CommandService.class)); command.executeCommand(sender, new ArrayList<String>(), commandService);
// then // then
verify(messagesMock).send(sender, MessageKey.USAGE_REGISTER); verify(commandService).send(sender, MessageKey.USAGE_REGISTER);
verify(managementMock, never()).performRegister(any(Player.class), anyString(), anyString()); verify(commandService, never()).getManagement();
} }
@Test @Test
@ -78,12 +71,14 @@ public class RegisterCommandTest {
// given // given
Player sender = mock(Player.class); Player sender = mock(Player.class);
RegisterCommand command = new RegisterCommand(); RegisterCommand command = new RegisterCommand();
Management management = mock(Management.class);
given(commandService.getManagement()).willReturn(management);
// when // when
command.executeCommand(sender, Collections.singletonList("password"), mock(CommandService.class)); command.executeCommand(sender, Collections.singletonList("password"), commandService);
// then // then
verify(managementMock).performRegister(sender, "password", ""); verify(management).performRegister(sender, "password", "");
} }
} }