#306 Create PlayerCommand abstract type
- Create PlayerCommand to handle player-only command checks centrally and consistently - Adjust tests
This commit is contained in:
parent
74ceb66430
commit
27fee3ca64
@ -47,6 +47,10 @@ public class CommandService {
|
||||
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.
|
||||
*
|
||||
|
||||
46
src/main/java/fr/xephi/authme/command/PlayerCommand.java
Normal file
46
src/main/java/fr/xephi/authme/command/PlayerCommand.java
Normal 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 <playerName> <password>"
|
||||
*
|
||||
* @return Alternative command not only for players, or null if not applicable
|
||||
*/
|
||||
protected String getAlternativeCommand() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@ -2,25 +2,22 @@ package fr.xephi.authme.command.executable.authme;
|
||||
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
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 org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class FirstSpawnCommand implements ExecutableCommand {
|
||||
public class FirstSpawnCommand extends PlayerCommand {
|
||||
|
||||
@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 {
|
||||
if (sender instanceof Player) {
|
||||
if (Spawn.getInstance().getFirstSpawn() != null)
|
||||
((Player) sender).teleport(Spawn.getInstance().getFirstSpawn());
|
||||
else sender.sendMessage("[AuthMe] First spawn has failed, please try to define the first spawn");
|
||||
if (Spawn.getInstance().getFirstSpawn() != null) {
|
||||
player.teleport(Spawn.getInstance().getFirstSpawn());
|
||||
} 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) {
|
||||
// TODO ljacqu 20151119: Catching NullPointerException is never a good idea. Find what can cause one instead
|
||||
|
||||
@ -2,24 +2,21 @@ package fr.xephi.authme.command.executable.authme;
|
||||
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
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 org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class SetFirstSpawnCommand implements ExecutableCommand {
|
||||
public class SetFirstSpawnCommand extends PlayerCommand {
|
||||
|
||||
@Override
|
||||
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
|
||||
public void runCommand(Player player, List<String> arguments, CommandService commandService) {
|
||||
try {
|
||||
if (sender instanceof Player) {
|
||||
if (Spawn.getInstance().setFirstSpawn(((Player) sender).getLocation()))
|
||||
sender.sendMessage("[AuthMe] Correctly defined new first spawn point");
|
||||
else sender.sendMessage("[AuthMe] SetFirstSpawn has failed, please retry");
|
||||
if (Spawn.getInstance().setFirstSpawn(player.getLocation())) {
|
||||
player.sendMessage("[AuthMe] Correctly defined new first spawn point");
|
||||
} else {
|
||||
sender.sendMessage("[AuthMe] Please use that command in game");
|
||||
player.sendMessage("[AuthMe] SetFirstSpawn has failed, please retry");
|
||||
}
|
||||
} catch (NullPointerException ex) {
|
||||
ConsoleLogger.showError(ex.getMessage());
|
||||
|
||||
@ -2,27 +2,21 @@ package fr.xephi.authme.command.executable.authme;
|
||||
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
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 org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class SetSpawnCommand implements ExecutableCommand {
|
||||
public class SetSpawnCommand extends PlayerCommand {
|
||||
|
||||
@Override
|
||||
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
|
||||
// Make sure the command executor is a player
|
||||
public void runCommand(Player player, List<String> arguments, CommandService commandService) {
|
||||
try {
|
||||
if (sender instanceof Player) {
|
||||
if (Spawn.getInstance().setSpawn(((Player) sender).getLocation())) {
|
||||
sender.sendMessage("[AuthMe] Correctly defined new spawn point");
|
||||
} else {
|
||||
sender.sendMessage("[AuthMe] SetSpawn has failed, please retry");
|
||||
}
|
||||
if (Spawn.getInstance().setSpawn(player.getLocation())) {
|
||||
player.sendMessage("[AuthMe] Correctly defined new spawn point");
|
||||
} else {
|
||||
sender.sendMessage("[AuthMe] Please use that command in game");
|
||||
player.sendMessage("[AuthMe] SetSpawn has failed, please retry");
|
||||
}
|
||||
} catch (NullPointerException ex) {
|
||||
ConsoleLogger.showError(ex.getMessage());
|
||||
|
||||
@ -2,25 +2,21 @@ package fr.xephi.authme.command.executable.authme;
|
||||
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
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 org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class SpawnCommand implements ExecutableCommand {
|
||||
public class SpawnCommand extends PlayerCommand {
|
||||
|
||||
@Override
|
||||
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
|
||||
// Make sure the command executor is a player
|
||||
public void runCommand(Player player, List<String> arguments, CommandService commandService) {
|
||||
try {
|
||||
if (sender instanceof Player) {
|
||||
if (Spawn.getInstance().getSpawn() != null)
|
||||
((Player) sender).teleport(Spawn.getInstance().getSpawn());
|
||||
else sender.sendMessage("[AuthMe] Spawn has failed, please try to define the spawn");
|
||||
if (Spawn.getInstance().getSpawn() != null) {
|
||||
player.teleport(Spawn.getInstance().getSpawn());
|
||||
} 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) {
|
||||
ConsoleLogger.showError(ex.getMessage());
|
||||
|
||||
@ -3,54 +3,36 @@ package fr.xephi.authme.command.executable.captcha;
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.cache.auth.PlayerCache;
|
||||
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.Messages;
|
||||
import fr.xephi.authme.security.RandomString;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.util.Wrapper;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CaptchaCommand implements ExecutableCommand {
|
||||
public class CaptchaCommand extends PlayerCommand {
|
||||
|
||||
@Override
|
||||
public void executeCommand(CommandSender sender, 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;
|
||||
public void runCommand(Player player, List<String> arguments, CommandService commandService) {
|
||||
final String playerNameLowerCase = player.getName().toLowerCase();
|
||||
|
||||
// Get the parameter values
|
||||
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();
|
||||
final String captcha = arguments.get(0);
|
||||
final AuthMe plugin = AuthMe.getInstance();
|
||||
|
||||
// Command logic
|
||||
if (PlayerCache.getInstance().isAuthenticated(playerNameLowerCase)) {
|
||||
m.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
|
||||
commandService.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Settings.useCaptcha) {
|
||||
m.send(player, MessageKey.USAGE_LOGIN);
|
||||
commandService.send(player, MessageKey.USAGE_LOGIN);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (!plugin.cap.containsKey(playerNameLowerCase)) {
|
||||
m.send(player, MessageKey.USAGE_LOGIN);
|
||||
commandService.send(player, MessageKey.USAGE_LOGIN);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -58,7 +40,7 @@ public class CaptchaCommand implements ExecutableCommand {
|
||||
plugin.cap.remove(playerNameLowerCase);
|
||||
String randStr = new RandomString(Settings.captchaLength).nextString();
|
||||
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;
|
||||
}
|
||||
|
||||
@ -66,7 +48,7 @@ public class CaptchaCommand implements ExecutableCommand {
|
||||
plugin.cap.remove(playerNameLowerCase);
|
||||
|
||||
// Show a status message
|
||||
m.send(player, MessageKey.CAPTCHA_SUCCESS);
|
||||
m.send(player, MessageKey.LOGIN_MESSAGE);
|
||||
commandService.send(player, MessageKey.CAPTCHA_SUCCESS);
|
||||
commandService.send(player, MessageKey.LOGIN_MESSAGE);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,13 +3,11 @@ package fr.xephi.authme.command.executable.changepassword;
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.cache.auth.PlayerCache;
|
||||
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.Messages;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.task.ChangePasswordTask;
|
||||
import fr.xephi.authme.util.Wrapper;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.List;
|
||||
@ -17,58 +15,47 @@ import java.util.List;
|
||||
/**
|
||||
* The command for a player to change his password with.
|
||||
*/
|
||||
public class ChangePasswordCommand implements ExecutableCommand {
|
||||
public class ChangePasswordCommand extends PlayerCommand {
|
||||
|
||||
@Override
|
||||
public void executeCommand(CommandSender sender, 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
|
||||
public void runCommand(Player player, List<String> arguments, CommandService commandService) {
|
||||
String oldPassword = arguments.get(0);
|
||||
String newPassword = arguments.get(1);
|
||||
|
||||
// Get the player instance and make sure he's authenticated
|
||||
Player player = (Player) sender;
|
||||
String name = player.getName().toLowerCase();
|
||||
Wrapper wrapper = Wrapper.getInstance();
|
||||
final PlayerCache playerCache = wrapper.getPlayerCache();
|
||||
if (!playerCache.isAuthenticated(name)) {
|
||||
m.send(player, MessageKey.NOT_LOGGED_IN);
|
||||
commandService.send(player, MessageKey.NOT_LOGGED_IN);
|
||||
return;
|
||||
}
|
||||
|
||||
// Make sure the password is allowed
|
||||
String playerPassLowerCase = newPassword.toLowerCase();
|
||||
// TODO #308: Remove SQL keywords check
|
||||
if (playerPassLowerCase.contains("delete") || playerPassLowerCase.contains("where")
|
||||
|| playerPassLowerCase.contains("insert") || playerPassLowerCase.contains("modify")
|
||||
|| playerPassLowerCase.contains("from") || playerPassLowerCase.contains("select")
|
||||
|| playerPassLowerCase.contains(";") || playerPassLowerCase.contains("null")
|
||||
|| !playerPassLowerCase.matches(Settings.getPassRegex)) {
|
||||
m.send(player, MessageKey.PASSWORD_MATCH_ERROR);
|
||||
commandService.send(player, MessageKey.PASSWORD_MATCH_ERROR);
|
||||
return;
|
||||
}
|
||||
if (playerPassLowerCase.equalsIgnoreCase(name)) {
|
||||
m.send(player, MessageKey.PASSWORD_IS_USERNAME_ERROR);
|
||||
commandService.send(player, MessageKey.PASSWORD_IS_USERNAME_ERROR);
|
||||
return;
|
||||
}
|
||||
if (playerPassLowerCase.length() < Settings.getPasswordMinLen
|
||||
|| playerPassLowerCase.length() > Settings.passwordMaxLength) {
|
||||
m.send(player, MessageKey.INVALID_PASSWORD_LENGTH);
|
||||
commandService.send(player, MessageKey.INVALID_PASSWORD_LENGTH);
|
||||
return;
|
||||
}
|
||||
if (!Settings.unsafePasswords.isEmpty() && Settings.unsafePasswords.contains(playerPassLowerCase)) {
|
||||
m.send(player, MessageKey.PASSWORD_UNSAFE_ERROR);
|
||||
commandService.send(player, MessageKey.PASSWORD_UNSAFE_ERROR);
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the password
|
||||
final AuthMe plugin = wrapper.getAuthMe();
|
||||
wrapper.getScheduler().runTaskAsynchronously(plugin,
|
||||
new ChangePasswordTask(plugin, player, oldPassword, newPassword));
|
||||
AuthMe plugin = AuthMe.getInstance();
|
||||
commandService.runTaskAsynchronously(new ChangePasswordTask(plugin, player, oldPassword, newPassword));
|
||||
}
|
||||
}
|
||||
|
||||
@ -14,8 +14,6 @@ import fr.xephi.authme.converter.SqliteToSql;
|
||||
import fr.xephi.authme.converter.vAuthConverter;
|
||||
import fr.xephi.authme.converter.xAuthConverter;
|
||||
import fr.xephi.authme.output.MessageKey;
|
||||
import fr.xephi.authme.output.Messages;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.List;
|
||||
@ -27,16 +25,13 @@ public class ConverterCommand implements ExecutableCommand {
|
||||
// AuthMe plugin instance
|
||||
final AuthMe plugin = AuthMe.getInstance();
|
||||
|
||||
// Messages instance
|
||||
final Messages m = plugin.getMessages();
|
||||
|
||||
// Get the conversion job
|
||||
String job = arguments.get(0);
|
||||
|
||||
// Determine the job type
|
||||
ConvertType jobType = ConvertType.fromName(job);
|
||||
if (jobType == null) {
|
||||
m.send(sender, MessageKey.ERROR);
|
||||
commandService.send(sender, MessageKey.ERROR);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -75,7 +70,7 @@ public class ConverterCommand implements ExecutableCommand {
|
||||
}
|
||||
|
||||
// Run the convert job
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, converter);
|
||||
commandService.runTaskAsynchronously(converter);
|
||||
|
||||
// Show a status message
|
||||
sender.sendMessage("[AuthMe] Successfully converted from " + jobType.getName());
|
||||
|
||||
@ -1,30 +1,18 @@
|
||||
package fr.xephi.authme.command.executable.email;
|
||||
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.command.CommandService;
|
||||
import fr.xephi.authme.command.ExecutableCommand;
|
||||
import fr.xephi.authme.util.Wrapper;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import fr.xephi.authme.command.PlayerCommand;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class AddEmailCommand implements ExecutableCommand {
|
||||
public class AddEmailCommand extends PlayerCommand {
|
||||
|
||||
@Override
|
||||
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
|
||||
// Make sure the current command executor is a player
|
||||
if (!(sender instanceof Player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the parameter values
|
||||
public void runCommand(Player player, List<String> arguments, CommandService commandService) {
|
||||
String playerMail = arguments.get(0);
|
||||
String playerMailVerify = arguments.get(1);
|
||||
|
||||
// Get the player and perform email addition
|
||||
final AuthMe plugin = Wrapper.getInstance().getAuthMe();
|
||||
final Player player = (Player) sender;
|
||||
plugin.getManagement().performAddEmail(player, playerMail, playerMailVerify);
|
||||
commandService.getManagement().performAddEmail(player, playerMail, playerMailVerify);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,32 +1,21 @@
|
||||
package fr.xephi.authme.command.executable.email;
|
||||
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.command.CommandService;
|
||||
import fr.xephi.authme.command.ExecutableCommand;
|
||||
import fr.xephi.authme.util.Wrapper;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import fr.xephi.authme.command.PlayerCommand;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Change email command.
|
||||
*/
|
||||
public class ChangeEmailCommand implements ExecutableCommand {
|
||||
public class ChangeEmailCommand extends PlayerCommand {
|
||||
|
||||
@Override
|
||||
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
|
||||
// Make sure the current command executor is a player
|
||||
if (!(sender instanceof Player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the parameter values
|
||||
public void runCommand(Player player, List<String> arguments, CommandService commandService) {
|
||||
String playerMailOld = arguments.get(0);
|
||||
String playerMailNew = arguments.get(1);
|
||||
|
||||
// Get the player instance and execute action
|
||||
final AuthMe plugin = Wrapper.getInstance().getAuthMe();
|
||||
final Player player = (Player) sender;
|
||||
plugin.getManagement().performChangeEmail(player, playerMailOld, playerMailNew);
|
||||
commandService.getManagement().performChangeEmail(player, playerMailOld, playerMailNew);
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,47 +5,36 @@ 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.command.PlayerCommand;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.output.MessageKey;
|
||||
import fr.xephi.authme.output.Messages;
|
||||
import fr.xephi.authme.security.PasswordSecurity;
|
||||
import fr.xephi.authme.security.RandomString;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.util.Wrapper;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import fr.xephi.authme.util.StringUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.List;
|
||||
|
||||
public class RecoverEmailCommand implements ExecutableCommand {
|
||||
public class RecoverEmailCommand extends PlayerCommand {
|
||||
|
||||
@Override
|
||||
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
|
||||
// Make sure the current command executor is a player
|
||||
if (!(sender instanceof Player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the parameter values
|
||||
public void runCommand(Player player, List<String> arguments, CommandService commandService) {
|
||||
String playerMail = arguments.get(0);
|
||||
|
||||
// Get the player instance and name
|
||||
final Player player = (Player) sender;
|
||||
final String playerName = player.getName();
|
||||
|
||||
// Command logic
|
||||
final Wrapper wrapper = Wrapper.getInstance();
|
||||
final AuthMe plugin = wrapper.getAuthMe();
|
||||
final Messages m = wrapper.getMessages();
|
||||
final AuthMe plugin = AuthMe.getInstance();
|
||||
|
||||
if (plugin.mail == null) {
|
||||
m.send(player, MessageKey.ERROR);
|
||||
commandService.send(player, MessageKey.ERROR);
|
||||
return;
|
||||
}
|
||||
if (plugin.database.isAuthAvailable(playerName)) {
|
||||
DataSource dataSource = commandService.getDataSource();
|
||||
if (dataSource.isAuthAvailable(playerName)) {
|
||||
if (PlayerCache.getInstance().isAuthenticated(playerName)) {
|
||||
m.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
|
||||
commandService.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
@ -55,33 +44,32 @@ public class RecoverEmailCommand implements ExecutableCommand {
|
||||
PlayerAuth auth;
|
||||
if (PlayerCache.getInstance().isAuthenticated(playerName)) {
|
||||
auth = PlayerCache.getInstance().getAuth(playerName);
|
||||
} else if (plugin.database.isAuthAvailable(playerName)) {
|
||||
auth = plugin.database.getAuth(playerName);
|
||||
} else if (dataSource.isAuthAvailable(playerName)) {
|
||||
auth = dataSource.getAuth(playerName);
|
||||
} else {
|
||||
m.send(player, MessageKey.UNKNOWN_USER);
|
||||
commandService.send(player, MessageKey.UNKNOWN_USER);
|
||||
return;
|
||||
}
|
||||
if (Settings.getmailAccount.equals("") || Settings.getmailAccount.isEmpty()) {
|
||||
m.send(player, MessageKey.ERROR);
|
||||
commandService.send(player, MessageKey.ERROR);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!playerMail.equalsIgnoreCase(auth.getEmail()) || playerMail.equalsIgnoreCase("your@email.com")
|
||||
|| auth.getEmail().equalsIgnoreCase("your@email.com")) {
|
||||
m.send(player, MessageKey.INVALID_EMAIL);
|
||||
commandService.send(player, MessageKey.INVALID_EMAIL);
|
||||
return;
|
||||
}
|
||||
auth.setHash(hashNew);
|
||||
plugin.database.updatePassword(auth);
|
||||
dataSource.updatePassword(auth);
|
||||
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) {
|
||||
ex.printStackTrace();
|
||||
ConsoleLogger.showError(ex.getMessage());
|
||||
m.send(sender, MessageKey.ERROR);
|
||||
ConsoleLogger.showError(StringUtils.formatException(ex));
|
||||
commandService.send(player, MessageKey.ERROR);
|
||||
}
|
||||
} else {
|
||||
m.send(player, MessageKey.REGISTER_EMAIL_MESSAGE);
|
||||
commandService.send(player, MessageKey.REGISTER_EMAIL_MESSAGE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,29 +1,19 @@
|
||||
package fr.xephi.authme.command.executable.login;
|
||||
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.command.CommandService;
|
||||
import fr.xephi.authme.command.ExecutableCommand;
|
||||
import fr.xephi.authme.util.Wrapper;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import fr.xephi.authme.command.PlayerCommand;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class LoginCommand implements ExecutableCommand {
|
||||
/**
|
||||
* Login command.
|
||||
*/
|
||||
public class LoginCommand extends PlayerCommand {
|
||||
|
||||
@Override
|
||||
public void executeCommand(CommandSender sender, 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;
|
||||
public void runCommand(Player player, List<String> arguments, CommandService commandService) {
|
||||
final String password = arguments.get(0);
|
||||
|
||||
// Log the player in
|
||||
plugin.getManagement().performLogin(player, password, false);
|
||||
commandService.getManagement().performLogin(player, password, false);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,30 +1,18 @@
|
||||
package fr.xephi.authme.command.executable.logout;
|
||||
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.command.CommandService;
|
||||
import fr.xephi.authme.command.ExecutableCommand;
|
||||
import fr.xephi.authme.util.Wrapper;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import fr.xephi.authme.command.PlayerCommand;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Logout command.
|
||||
*/
|
||||
public class LogoutCommand implements ExecutableCommand {
|
||||
public class LogoutCommand extends PlayerCommand {
|
||||
|
||||
@Override
|
||||
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
|
||||
// Make sure the current command executor is a 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);
|
||||
public void runCommand(Player player, List<String> arguments, CommandService commandService) {
|
||||
commandService.getManagement().performLogout(player);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,61 +1,48 @@
|
||||
package fr.xephi.authme.command.executable.register;
|
||||
|
||||
import fr.xephi.authme.AuthMe;
|
||||
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.Messages;
|
||||
import fr.xephi.authme.process.Management;
|
||||
import fr.xephi.authme.security.RandomString;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.util.Wrapper;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class RegisterCommand implements ExecutableCommand {
|
||||
public class RegisterCommand extends PlayerCommand {
|
||||
|
||||
@Override
|
||||
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
|
||||
// Make sure the sender is a player
|
||||
if (!(sender instanceof Player)) {
|
||||
sender.sendMessage("Player Only! Use 'authme register <playername> <password>' instead");
|
||||
public void runCommand(Player player, List<String> arguments, CommandService commandService) {
|
||||
if (arguments.isEmpty() || Settings.enablePasswordConfirmation && arguments.size() < 2) {
|
||||
commandService.send(player, MessageKey.USAGE_REGISTER);
|
||||
return;
|
||||
}
|
||||
|
||||
final Wrapper wrapper = Wrapper.getInstance();
|
||||
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();
|
||||
final Management management = commandService.getManagement();
|
||||
if (Settings.emailRegistration && !Settings.getmailAccount.isEmpty()) {
|
||||
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;
|
||||
}
|
||||
final String email = arguments.get(0);
|
||||
if (!Settings.isEmailCorrect(email)) {
|
||||
m.send(player, MessageKey.INVALID_EMAIL);
|
||||
commandService.send(player, MessageKey.INVALID_EMAIL);
|
||||
return;
|
||||
}
|
||||
final String thePass = new RandomString(Settings.getRecoveryPassLength).nextString();
|
||||
final String thePass = RandomString.generate(Settings.getRecoveryPassLength);
|
||||
management.performRegister(player, thePass, email);
|
||||
return;
|
||||
}
|
||||
if (arguments.size() > 1 && Settings.enablePasswordConfirmation) {
|
||||
if (!arguments.get(0).equals(arguments.get(1))) {
|
||||
m.send(player, MessageKey.PASSWORD_MATCH_ERROR);
|
||||
return;
|
||||
}
|
||||
if (arguments.size() > 1 && Settings.enablePasswordConfirmation && !arguments.get(0).equals(arguments.get(1))) {
|
||||
commandService.send(player, MessageKey.PASSWORD_MATCH_ERROR);
|
||||
return;
|
||||
}
|
||||
management.performRegister(player, arguments.get(0), "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAlternativeCommand() {
|
||||
return "authme register <playername> <password>";
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,42 +1,27 @@
|
||||
package fr.xephi.authme.command.executable.unregister;
|
||||
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.cache.auth.PlayerCache;
|
||||
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.Messages;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class UnregisterCommand implements ExecutableCommand {
|
||||
public class UnregisterCommand extends PlayerCommand {
|
||||
|
||||
@Override
|
||||
public void executeCommand(CommandSender sender, 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
|
||||
public void runCommand(Player player, List<String> arguments, CommandService commandService) {
|
||||
String playerPass = arguments.get(0);
|
||||
|
||||
// Get the player instance and name
|
||||
final Player player = (Player) sender;
|
||||
final String playerNameLowerCase = player.getName().toLowerCase();
|
||||
|
||||
// Make sure the player is authenticated
|
||||
if (!PlayerCache.getInstance().isAuthenticated(playerNameLowerCase)) {
|
||||
m.send(player, MessageKey.NOT_LOGGED_IN);
|
||||
commandService.send(player, MessageKey.NOT_LOGGED_IN);
|
||||
return;
|
||||
}
|
||||
|
||||
// Unregister the player
|
||||
plugin.getManagement().performUnregister(player, playerPass, false);
|
||||
commandService.getManagement().performUnregister(player, playerPass, false);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,29 +3,16 @@ package fr.xephi.authme.converter;
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
/**
|
||||
*/
|
||||
public class xAuthConverter implements Converter {
|
||||
|
||||
public final AuthMe plugin;
|
||||
public final CommandSender sender;
|
||||
|
||||
/**
|
||||
* Constructor for xAuthConverter.
|
||||
*
|
||||
* @param plugin AuthMe
|
||||
* @param sender CommandSender
|
||||
*/
|
||||
public xAuthConverter(AuthMe plugin, CommandSender sender) {
|
||||
this.plugin = plugin;
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method run.
|
||||
*
|
||||
* @see java.lang.Runnable#run()
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
|
||||
@ -16,8 +16,6 @@ import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*/
|
||||
public class xAuthToFlat {
|
||||
|
||||
public final AuthMe instance;
|
||||
@ -36,11 +34,6 @@ public class xAuthToFlat {
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method convert.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean convert() {
|
||||
if (instance.getServer().getPluginManager().getPlugin("xAuth") == null) {
|
||||
sender.sendMessage("[AuthMe] xAuth plugin not found");
|
||||
@ -71,13 +64,6 @@ public class xAuthToFlat {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method getIdPlayer.
|
||||
*
|
||||
* @param id int
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public String getIdPlayer(int id) {
|
||||
String realPass = "";
|
||||
Connection conn = xAuth.getPlugin().getDatabaseController().getConnection();
|
||||
@ -100,11 +86,6 @@ public class xAuthToFlat {
|
||||
return realPass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method getXAuthPlayers.
|
||||
*
|
||||
* @return List<Integer>
|
||||
*/
|
||||
public List<Integer> getXAuthPlayers() {
|
||||
List<Integer> xP = new ArrayList<>();
|
||||
Connection conn = xAuth.getPlugin().getDatabaseController().getConnection();
|
||||
@ -126,13 +107,6 @@ public class xAuthToFlat {
|
||||
return xP;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method getPassword.
|
||||
*
|
||||
* @param accountId int
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public String getPassword(int accountId) {
|
||||
String realPass = "";
|
||||
Connection conn = xAuth.getPlugin().getDatabaseController().getConnection();
|
||||
|
||||
@ -15,8 +15,6 @@ import com.google.common.io.ByteStreams;
|
||||
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
/**
|
||||
*/
|
||||
public class ChangePasswordTask implements Runnable {
|
||||
|
||||
private final AuthMe plugin;
|
||||
@ -24,14 +22,6 @@ public class ChangePasswordTask implements Runnable {
|
||||
private final String oldPassword;
|
||||
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) {
|
||||
this.plugin = plugin;
|
||||
this.player = player;
|
||||
|
||||
@ -1,11 +1,9 @@
|
||||
package fr.xephi.authme.command.executable.changepassword;
|
||||
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import fr.xephi.authme.cache.auth.PlayerCache;
|
||||
import fr.xephi.authme.command.CommandService;
|
||||
import fr.xephi.authme.output.MessageKey;
|
||||
import fr.xephi.authme.output.Messages;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.task.ChangePasswordTask;
|
||||
import fr.xephi.authme.util.WrapperMock;
|
||||
@ -37,14 +35,14 @@ import static org.mockito.Mockito.when;
|
||||
public class ChangePasswordCommandTest {
|
||||
|
||||
private WrapperMock wrapperMock;
|
||||
private Messages messagesMock;
|
||||
private PlayerCache cacheMock;
|
||||
private CommandService commandService;
|
||||
|
||||
@Before
|
||||
public void setUpMocks() {
|
||||
wrapperMock = WrapperMock.createInstance();
|
||||
messagesMock = wrapperMock.getMessages();
|
||||
cacheMock = wrapperMock.getPlayerCache();
|
||||
commandService = mock(CommandService.class);
|
||||
|
||||
// Only allow passwords with alphanumerical characters for the test
|
||||
Settings.getPassRegex = "[a-zA-Z0-9]+";
|
||||
@ -60,7 +58,7 @@ public class ChangePasswordCommandTest {
|
||||
ChangePasswordCommand command = new ChangePasswordCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, new ArrayList<String>(), mock(CommandService.class));
|
||||
command.executeCommand(sender, new ArrayList<String>(), commandService);
|
||||
|
||||
// then
|
||||
assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false));
|
||||
@ -73,10 +71,10 @@ public class ChangePasswordCommandTest {
|
||||
ChangePasswordCommand command = new ChangePasswordCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Arrays.asList("pass", "pass"), mock(CommandService.class));
|
||||
command.executeCommand(sender, Arrays.asList("pass", "pass"), commandService);
|
||||
|
||||
// then
|
||||
verify(messagesMock).send(sender, MessageKey.NOT_LOGGED_IN);
|
||||
verify(commandService).send(sender, MessageKey.NOT_LOGGED_IN);
|
||||
assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false));
|
||||
}
|
||||
|
||||
@ -87,10 +85,10 @@ public class ChangePasswordCommandTest {
|
||||
ChangePasswordCommand command = new ChangePasswordCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Arrays.asList("old123", "!pass"), mock(CommandService.class));
|
||||
command.executeCommand(sender, Arrays.asList("old123", "!pass"), commandService);
|
||||
|
||||
// then
|
||||
verify(messagesMock).send(sender, MessageKey.PASSWORD_MATCH_ERROR);
|
||||
verify(commandService).send(sender, MessageKey.PASSWORD_MATCH_ERROR);
|
||||
assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false));
|
||||
}
|
||||
|
||||
@ -102,10 +100,10 @@ public class ChangePasswordCommandTest {
|
||||
ChangePasswordCommand command = new ChangePasswordCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Arrays.asList("old_", "Tester"), mock(CommandService.class));
|
||||
command.executeCommand(sender, Arrays.asList("old_", "Tester"), commandService);
|
||||
|
||||
// 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));
|
||||
}
|
||||
|
||||
@ -117,10 +115,10 @@ public class ChangePasswordCommandTest {
|
||||
Settings.passwordMaxLength = 3;
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Arrays.asList("12", "test"), mock(CommandService.class));
|
||||
command.executeCommand(sender, Arrays.asList("12", "test"), commandService);
|
||||
|
||||
// then
|
||||
verify(messagesMock).send(sender, MessageKey.INVALID_PASSWORD_LENGTH);
|
||||
verify(commandService).send(sender, MessageKey.INVALID_PASSWORD_LENGTH);
|
||||
assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false));
|
||||
}
|
||||
|
||||
@ -132,10 +130,10 @@ public class ChangePasswordCommandTest {
|
||||
Settings.getPasswordMinLen = 7;
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Arrays.asList("oldverylongpassword", "tester"), mock(CommandService.class));
|
||||
command.executeCommand(sender, Arrays.asList("oldverylongpassword", "tester"), commandService);
|
||||
|
||||
// then
|
||||
verify(messagesMock).send(sender, MessageKey.INVALID_PASSWORD_LENGTH);
|
||||
verify(commandService).send(sender, MessageKey.INVALID_PASSWORD_LENGTH);
|
||||
assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false));
|
||||
}
|
||||
|
||||
@ -147,10 +145,10 @@ public class ChangePasswordCommandTest {
|
||||
Settings.unsafePasswords = asList("test", "abc123");
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Arrays.asList("oldpw", "abc123"), mock(CommandService.class));
|
||||
command.executeCommand(sender, Arrays.asList("oldpw", "abc123"), commandService);
|
||||
|
||||
// then
|
||||
verify(messagesMock).send(sender, MessageKey.PASSWORD_UNSAFE_ERROR);
|
||||
verify(commandService).send(sender, MessageKey.PASSWORD_UNSAFE_ERROR);
|
||||
assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false));
|
||||
}
|
||||
|
||||
@ -161,12 +159,12 @@ public class ChangePasswordCommandTest {
|
||||
ChangePasswordCommand command = new ChangePasswordCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Arrays.asList("abc123", "abc123"), mock(CommandService.class));
|
||||
command.executeCommand(sender, Arrays.asList("abc123", "abc123"), commandService);
|
||||
|
||||
// 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);
|
||||
verify(wrapperMock.getScheduler()).runTaskAsynchronously(any(AuthMe.class), taskCaptor.capture());
|
||||
verify(commandService).runTaskAsynchronously(taskCaptor.capture());
|
||||
ChangePasswordTask task = taskCaptor.getValue();
|
||||
assertThat((String) ReflectionTestUtils.getFieldValue(ChangePasswordTask.class, task, "newPassword"),
|
||||
equalTo("abc123"));
|
||||
|
||||
@ -1,9 +1,7 @@
|
||||
package fr.xephi.authme.command.executable.email;
|
||||
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.command.CommandService;
|
||||
import fr.xephi.authme.process.Management;
|
||||
import fr.xephi.authme.util.WrapperMock;
|
||||
import org.bukkit.command.BlockCommandSender;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
@ -14,25 +12,21 @@ import org.mockito.Mockito;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Test for {@link AddEmailCommand}.
|
||||
*/
|
||||
public class AddEmailCommandTest {
|
||||
|
||||
private AuthMe authMeMock;
|
||||
private Management managementMock;
|
||||
private CommandService commandService;
|
||||
|
||||
@Before
|
||||
public void setUpMocks() {
|
||||
WrapperMock wrapper = WrapperMock.createInstance();
|
||||
authMeMock = wrapper.getAuthMe();
|
||||
managementMock = Mockito.mock(Management.class);
|
||||
when(authMeMock.getManagement()).thenReturn(managementMock);
|
||||
commandService = mock(CommandService.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -42,10 +36,10 @@ public class AddEmailCommandTest {
|
||||
AddEmailCommand command = new AddEmailCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, new ArrayList<String>(), mock(CommandService.class));
|
||||
command.executeCommand(sender, new ArrayList<String>(), commandService);
|
||||
|
||||
// then
|
||||
verify(authMeMock, never()).getManagement();
|
||||
verify(commandService, never()).getManagement();
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -53,13 +47,14 @@ public class AddEmailCommandTest {
|
||||
// given
|
||||
Player sender = Mockito.mock(Player.class);
|
||||
AddEmailCommand command = new AddEmailCommand();
|
||||
Management management = mock(Management.class);
|
||||
given(commandService.getManagement()).willReturn(management);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Arrays.asList("mail@example", "other_example"), mock(CommandService.class));
|
||||
command.executeCommand(sender, Arrays.asList("mail@example", "other_example"), commandService);
|
||||
|
||||
// then
|
||||
verify(authMeMock).getManagement();
|
||||
verify(managementMock).performAddEmail(sender, "mail@example", "other_example");
|
||||
verify(management).performAddEmail(sender, "mail@example", "other_example");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,38 +1,31 @@
|
||||
package fr.xephi.authme.command.executable.email;
|
||||
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.command.CommandService;
|
||||
import fr.xephi.authme.process.Management;
|
||||
import fr.xephi.authme.util.WrapperMock;
|
||||
import org.bukkit.command.BlockCommandSender;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Test for {@link ChangeEmailCommand}.
|
||||
*/
|
||||
public class ChangeEmailCommandTest {
|
||||
|
||||
private AuthMe authMeMock;
|
||||
private Management managementMock;
|
||||
private CommandService commandService;
|
||||
|
||||
@Before
|
||||
public void setUpMocks() {
|
||||
WrapperMock wrapper = WrapperMock.createInstance();
|
||||
authMeMock = wrapper.getAuthMe();
|
||||
managementMock = mock(Management.class);
|
||||
when(authMeMock.getManagement()).thenReturn(managementMock);
|
||||
commandService = mock(CommandService.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -42,10 +35,10 @@ public class ChangeEmailCommandTest {
|
||||
ChangeEmailCommand command = new ChangeEmailCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, new ArrayList<String>(), mock(CommandService.class));
|
||||
command.executeCommand(sender, new ArrayList<String>(), commandService);
|
||||
|
||||
// then
|
||||
verify(authMeMock, never()).getManagement();
|
||||
verify(commandService, never()).getManagement();
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -53,14 +46,14 @@ public class ChangeEmailCommandTest {
|
||||
// given
|
||||
Player sender = mock(Player.class);
|
||||
ChangeEmailCommand command = new ChangeEmailCommand();
|
||||
Management management = mock(Management.class);
|
||||
given(commandService.getManagement()).willReturn(management);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Arrays.asList("new.mail@example.org", "old_mail@example.org"),
|
||||
mock(CommandService.class));
|
||||
command.executeCommand(sender, Arrays.asList("new.mail@example.org", "old_mail@example.org"), commandService);
|
||||
|
||||
// then
|
||||
verify(authMeMock).getManagement();
|
||||
verify(managementMock).performChangeEmail(sender, "new.mail@example.org", "old_mail@example.org");
|
||||
verify(management).performChangeEmail(sender, "new.mail@example.org", "old_mail@example.org");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -9,29 +9,32 @@ import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.ArrayList;
|
||||
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.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Test for {@link LoginCommand}.
|
||||
*/
|
||||
public class LoginCommandTest {
|
||||
|
||||
private static Management managementMock;
|
||||
private CommandService commandService;
|
||||
|
||||
@Before
|
||||
public void initializeAuthMeMock() {
|
||||
WrapperMock wrapper = WrapperMock.createInstance();
|
||||
|
||||
WrapperMock.createInstance();
|
||||
Settings.captchaLength = 10;
|
||||
managementMock = mock(Management.class);
|
||||
Mockito.when(wrapper.getAuthMe().getManagement()).thenReturn(managementMock);
|
||||
commandService = mock(CommandService.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -41,10 +44,13 @@ public class LoginCommandTest {
|
||||
LoginCommand command = new LoginCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, new ArrayList<String>(), mock(CommandService.class));
|
||||
command.executeCommand(sender, new ArrayList<String>(), commandService);
|
||||
|
||||
// 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
|
||||
@ -52,12 +58,14 @@ public class LoginCommandTest {
|
||||
// given
|
||||
Player sender = mock(Player.class);
|
||||
LoginCommand command = new LoginCommand();
|
||||
Management management = mock(Management.class);
|
||||
given(commandService.getManagement()).willReturn(management);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.singletonList("password"), mock(CommandService.class));
|
||||
command.executeCommand(sender, Collections.singletonList("password"), commandService);
|
||||
|
||||
// then
|
||||
Mockito.verify(managementMock).performLogin(eq(sender), eq("password"), eq(false));
|
||||
Mockito.verify(management).performLogin(eq(sender), eq("password"), eq(false));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
package fr.xephi.authme.command.executable.logout;
|
||||
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.command.CommandService;
|
||||
import fr.xephi.authme.process.Management;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
@ -10,30 +9,30 @@ import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
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.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Test for {@link LogoutCommand}.
|
||||
*/
|
||||
public class LogoutCommandTest {
|
||||
|
||||
private static Management managementMock;
|
||||
private CommandService commandService;
|
||||
|
||||
@Before
|
||||
public void initializeAuthMeMock() {
|
||||
WrapperMock wrapper = WrapperMock.createInstance();
|
||||
AuthMe pluginMock = wrapper.getAuthMe();
|
||||
|
||||
WrapperMock.createInstance();
|
||||
Settings.captchaLength = 10;
|
||||
managementMock = mock(Management.class);
|
||||
Mockito.when(pluginMock.getManagement()).thenReturn(managementMock);
|
||||
commandService = mock(CommandService.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -43,10 +42,13 @@ public class LogoutCommandTest {
|
||||
LogoutCommand command = new LogoutCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, new ArrayList<String>(), mock(CommandService.class));
|
||||
command.executeCommand(sender, new ArrayList<String>(), commandService);
|
||||
|
||||
// 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
|
||||
@ -54,12 +56,14 @@ public class LogoutCommandTest {
|
||||
// given
|
||||
Player sender = mock(Player.class);
|
||||
LogoutCommand command = new LogoutCommand();
|
||||
Management management = mock(Management.class);
|
||||
given(commandService.getManagement()).willReturn(management);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.singletonList("password"), mock(CommandService.class));
|
||||
command.executeCommand(sender, Collections.singletonList("password"), commandService);
|
||||
|
||||
// then
|
||||
Mockito.verify(managementMock).performLogout(sender);
|
||||
verify(management).performLogout(sender);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,9 +1,8 @@
|
||||
package fr.xephi.authme.command.executable.register;
|
||||
|
||||
import fr.xephi.authme.command.CommandService;
|
||||
import fr.xephi.authme.process.Management;
|
||||
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.util.WrapperMock;
|
||||
import org.bukkit.command.BlockCommandSender;
|
||||
@ -12,15 +11,13 @@ import org.bukkit.entity.Player;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.ArrayList;
|
||||
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.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@ -30,17 +27,13 @@ import static org.mockito.Mockito.verify;
|
||||
*/
|
||||
public class RegisterCommandTest {
|
||||
|
||||
private static Management managementMock;
|
||||
private static Messages messagesMock;
|
||||
private CommandService commandService;
|
||||
|
||||
@Before
|
||||
public void initializeAuthMeMock() {
|
||||
WrapperMock wrapper = WrapperMock.createInstance();
|
||||
messagesMock = wrapper.getMessages();
|
||||
|
||||
WrapperMock.createInstance();
|
||||
Settings.captchaLength = 10;
|
||||
managementMock = mock(Management.class);
|
||||
Mockito.when(wrapper.getAuthMe().getManagement()).thenReturn(managementMock);
|
||||
commandService = mock(CommandService.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -48,15 +41,15 @@ public class RegisterCommandTest {
|
||||
// given
|
||||
CommandSender sender = mock(BlockCommandSender.class);
|
||||
RegisterCommand command = new RegisterCommand();
|
||||
ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, new ArrayList<String>(), mock(CommandService.class));
|
||||
command.executeCommand(sender, new ArrayList<String>(), commandService);
|
||||
|
||||
// then
|
||||
verify(commandService, never()).getManagement();
|
||||
ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class);
|
||||
verify(sender).sendMessage(messageCaptor.capture());
|
||||
assertThat(messageCaptor.getValue().contains("Player Only!"), equalTo(true));
|
||||
verify(managementMock, never()).performRegister(any(Player.class), anyString(), anyString());
|
||||
assertThat(messageCaptor.getValue(), containsString("Player only!"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -66,11 +59,11 @@ public class RegisterCommandTest {
|
||||
RegisterCommand command = new RegisterCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, new ArrayList<String>(), mock(CommandService.class));
|
||||
command.executeCommand(sender, new ArrayList<String>(), commandService);
|
||||
|
||||
// then
|
||||
verify(messagesMock).send(sender, MessageKey.USAGE_REGISTER);
|
||||
verify(managementMock, never()).performRegister(any(Player.class), anyString(), anyString());
|
||||
verify(commandService).send(sender, MessageKey.USAGE_REGISTER);
|
||||
verify(commandService, never()).getManagement();
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -78,12 +71,14 @@ public class RegisterCommandTest {
|
||||
// given
|
||||
Player sender = mock(Player.class);
|
||||
RegisterCommand command = new RegisterCommand();
|
||||
Management management = mock(Management.class);
|
||||
given(commandService.getManagement()).willReturn(management);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.singletonList("password"), mock(CommandService.class));
|
||||
command.executeCommand(sender, Collections.singletonList("password"), commandService);
|
||||
|
||||
// then
|
||||
verify(managementMock).performRegister(sender, "password", "");
|
||||
verify(management).performRegister(sender, "password", "");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user