From f5c89e897f894c6accff77439e5eda9d28ac9973 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Tue, 17 May 2016 19:49:06 +0200 Subject: [PATCH] #707 Convert async processes as services (work in progress - rough, untested changes) --- .../fr/xephi/authme/process/Management.java | 91 +++++++++++---- .../fr/xephi/authme/process/NewProcess.java | 7 ++ .../xephi/authme/process/ProcessService.java | 36 ------ .../authme/process/email/AsyncAddEmail.java | 35 +++--- .../process/email/AsyncChangeEmail.java | 41 +++---- .../authme/process/join/AsynchronousJoin.java | 79 +++++++------ .../process/login/AsynchronousLogin.java | 105 +++++++++--------- .../process/logout/AsynchronousLogout.java | 65 +++++------ .../authme/process/quit/AsynchronousQuit.java | 59 +++++----- .../process/register/AsyncRegister.java | 64 +++++------ .../unregister/AsynchronousUnregister.java | 82 ++++++-------- .../authme/process/ProcessServiceTest.java | 27 ----- .../process/email/AsyncAddEmailTest.java | 41 +++---- .../process/email/AsyncChangeEmailTest.java | 39 +++---- 14 files changed, 359 insertions(+), 412 deletions(-) create mode 100644 src/main/java/fr/xephi/authme/process/NewProcess.java diff --git a/src/main/java/fr/xephi/authme/process/Management.java b/src/main/java/fr/xephi/authme/process/Management.java index cb3efa81b..2cd0b3881 100644 --- a/src/main/java/fr/xephi/authme/process/Management.java +++ b/src/main/java/fr/xephi/authme/process/Management.java @@ -1,9 +1,5 @@ package fr.xephi.authme.process; -import fr.xephi.authme.AuthMe; -import fr.xephi.authme.cache.auth.PlayerCache; -import fr.xephi.authme.datasource.DataSource; -import fr.xephi.authme.hooks.PluginHooks; import fr.xephi.authme.process.email.AsyncAddEmail; import fr.xephi.authme.process.email.AsyncChangeEmail; import fr.xephi.authme.process.join.AsynchronousJoin; @@ -12,63 +8,110 @@ import fr.xephi.authme.process.logout.AsynchronousLogout; import fr.xephi.authme.process.quit.AsynchronousQuit; import fr.xephi.authme.process.register.AsyncRegister; import fr.xephi.authme.process.unregister.AsynchronousUnregister; +import fr.xephi.authme.util.BukkitService; import org.bukkit.entity.Player; -import org.bukkit.scheduler.BukkitScheduler; import javax.inject.Inject; -/** - */ + public class Management { @Inject - private AuthMe plugin; + private BukkitService bukkitService; + + // Processes @Inject - private BukkitScheduler sched; + private AsyncAddEmail asyncAddEmail; @Inject - private ProcessService processService; + private AsyncChangeEmail asyncChangeEmail; @Inject - private DataSource dataSource; + private AsynchronousLogout asynchronousLogout; @Inject - private PlayerCache playerCache; + private AsynchronousQuit asynchronousQuit; @Inject - private PluginHooks pluginHooks; + private AsynchronousJoin asynchronousJoin; + @Inject + private AsyncRegister asyncRegister; + @Inject + private AsynchronousLogin asynchronousLogin; + @Inject + private AsynchronousUnregister asynchronousUnregister; Management() { } public void performLogin(final Player player, final String password, final boolean forceLogin) { - runTask(new AsynchronousLogin(player, password, forceLogin, plugin, dataSource, processService)); + runTask(new Runnable() { + @Override + public void run() { + asynchronousLogin.login(player, password, forceLogin); + } + }); } public void performLogout(final Player player) { - runTask(new AsynchronousLogout(player, plugin, dataSource, processService)); + runTask(new Runnable() { + @Override + public void run() { + asynchronousLogout.logout(player); + } + }); } public void performRegister(final Player player, final String password, final String email, final boolean autoLogin) { - runTask(new AsyncRegister(player, password, email, plugin, dataSource, playerCache, processService, autoLogin)); + runTask(new Runnable() { + @Override + public void run() { + asyncRegister.register(player, password, email, autoLogin); + } + }); } - public void performUnregister(final Player player, final String password, final boolean force) { - runTask(new AsynchronousUnregister(player, password, force, plugin, processService)); + public void performUnregister(final Player player, final String password, final boolean isForce) { + runTask(new Runnable() { + @Override + public void run() { + asynchronousUnregister.unregister(player, password, isForce); + } + }); } public void performJoin(final Player player) { - runTask(new AsynchronousJoin(player, plugin, dataSource, playerCache, pluginHooks, processService)); + runTask(new Runnable() { + @Override + public void run() { + asynchronousJoin.processJoin(player); + } + }); } public void performQuit(final Player player, final boolean isKick) { - runTask(new AsynchronousQuit(player, plugin, dataSource, isKick, processService)); + runTask(new Runnable() { + @Override + public void run() { + asynchronousQuit.processQuit(player, isKick); + } + }); } public void performAddEmail(final Player player, final String newEmail) { - runTask(new AsyncAddEmail(player, newEmail, dataSource, playerCache, processService)); + runTask(new Runnable() { + @Override + public void run() { + asyncAddEmail.addEmail(player, newEmail); + } + }); } public void performChangeEmail(final Player player, final String oldEmail, final String newEmail) { - runTask(new AsyncChangeEmail(player, oldEmail, newEmail, dataSource, playerCache, processService)); + runTask(new Runnable() { + @Override + public void run() { + asyncChangeEmail.changeEmail(player, oldEmail, newEmail); + } + }); } - private void runTask(Process process) { - sched.runTaskAsynchronously(plugin, process); + private void runTask(Runnable runnable) { + bukkitService.runTaskAsynchronously(runnable); } } diff --git a/src/main/java/fr/xephi/authme/process/NewProcess.java b/src/main/java/fr/xephi/authme/process/NewProcess.java new file mode 100644 index 000000000..524f2c773 --- /dev/null +++ b/src/main/java/fr/xephi/authme/process/NewProcess.java @@ -0,0 +1,7 @@ +package fr.xephi.authme.process; + +/** + * Marker interface for AuthMe processes. + */ +public interface NewProcess { +} diff --git a/src/main/java/fr/xephi/authme/process/ProcessService.java b/src/main/java/fr/xephi/authme/process/ProcessService.java index a338cad42..e8f0a73ba 100644 --- a/src/main/java/fr/xephi/authme/process/ProcessService.java +++ b/src/main/java/fr/xephi/authme/process/ProcessService.java @@ -1,14 +1,11 @@ package fr.xephi.authme.process; import fr.xephi.authme.AuthMe; -import fr.xephi.authme.datasource.DataSource; -import fr.xephi.authme.hooks.PluginHooks; import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.Messages; import fr.xephi.authme.security.PasswordSecurity; import fr.xephi.authme.security.crypts.HashedPassword; import fr.xephi.authme.settings.NewSetting; -import fr.xephi.authme.settings.SpawnLoader; import fr.xephi.authme.settings.domain.Property; import fr.xephi.authme.util.BukkitService; import fr.xephi.authme.util.ValidationService; @@ -32,14 +29,8 @@ public class ProcessService { @Inject private AuthMe authMe; @Inject - private DataSource dataSource; - @Inject private PasswordSecurity passwordSecurity; @Inject - private PluginHooks pluginHooks; - @Inject - private SpawnLoader spawnLoader; - @Inject private ValidationService validationService; @Inject private BukkitService bukkitService; @@ -165,33 +156,6 @@ public class ProcessService { return passwordSecurity.computeHash(password, username); } - /** - * Return the PluginHooks manager. - * - * @return PluginHooks instance - */ - public PluginHooks getPluginHooks() { - return pluginHooks; - } - - /** - * Return the spawn manager. - * - * @return SpawnLoader instance - */ - public SpawnLoader getSpawnLoader() { - return spawnLoader; - } - - /** - * Return the plugin's datasource. - * - * @return the datasource - */ - public DataSource getDataSource() { - return dataSource; - } - /** * Verifies whether a password is valid according to the plugin settings. * diff --git a/src/main/java/fr/xephi/authme/process/email/AsyncAddEmail.java b/src/main/java/fr/xephi/authme/process/email/AsyncAddEmail.java index 282f8f09e..f4f1ed87f 100644 --- a/src/main/java/fr/xephi/authme/process/email/AsyncAddEmail.java +++ b/src/main/java/fr/xephi/authme/process/email/AsyncAddEmail.java @@ -5,33 +5,30 @@ import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.output.MessageKey; -import fr.xephi.authme.process.Process; +import fr.xephi.authme.process.NewProcess; import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.settings.properties.RegistrationSettings; import org.bukkit.entity.Player; +import javax.inject.Inject; + /** * Async task to add an email to an account. */ -public class AsyncAddEmail implements Process { +public class AsyncAddEmail implements NewProcess { - private final Player player; - private final String email; - private final ProcessService service; - private final DataSource dataSource; - private final PlayerCache playerCache; + @Inject + private ProcessService service; - public AsyncAddEmail(Player player, String email, DataSource dataSource, PlayerCache playerCache, - ProcessService service) { - this.player = player; - this.email = email; - this.dataSource = dataSource; - this.playerCache = playerCache; - this.service = service; - } + @Inject + private DataSource dataSource; - @Override - public void run() { + @Inject + private PlayerCache playerCache; + + AsyncAddEmail() { } + + public void addEmail(Player player, String email) { String playerName = player.getName().toLowerCase(); if (playerCache.isAuthenticated(playerName)) { @@ -55,11 +52,11 @@ public class AsyncAddEmail implements Process { } } } else { - sendUnloggedMessage(dataSource); + sendUnloggedMessage(player); } } - private void sendUnloggedMessage(DataSource dataSource) { + private void sendUnloggedMessage(Player player) { if (dataSource.isAuthAvailable(player.getName())) { service.send(player, MessageKey.LOGIN_MESSAGE); } else if (service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)) { diff --git a/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java b/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java index 7489327be..2f4e90b5b 100644 --- a/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java +++ b/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java @@ -4,35 +4,30 @@ import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.output.MessageKey; -import fr.xephi.authme.process.Process; +import fr.xephi.authme.process.NewProcess; import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.settings.properties.RegistrationSettings; import org.bukkit.entity.Player; +import javax.inject.Inject; + /** * Async task for changing the email. */ -public class AsyncChangeEmail implements Process { +public class AsyncChangeEmail implements NewProcess { - private final Player player; - private final String oldEmail; - private final String newEmail; - private final ProcessService service; - private final PlayerCache playerCache; - private final DataSource dataSource; + @Inject + private ProcessService service; - public AsyncChangeEmail(Player player, String oldEmail, String newEmail, DataSource dataSource, - PlayerCache playerCache, ProcessService service) { - this.player = player; - this.oldEmail = oldEmail; - this.newEmail = newEmail; - this.playerCache = playerCache; - this.dataSource = dataSource; - this.service = service; - } + @Inject + private PlayerCache playerCache; - @Override - public void run() { + @Inject + private DataSource dataSource; + + AsyncChangeEmail() { } + + public void changeEmail(Player player, String oldEmail, String newEmail) { String playerName = player.getName().toLowerCase(); if (playerCache.isAuthenticated(playerName)) { PlayerAuth auth = playerCache.getAuth(playerName); @@ -47,14 +42,14 @@ public class AsyncChangeEmail implements Process { } else if (!service.isEmailFreeForRegistration(newEmail, player)) { service.send(player, MessageKey.EMAIL_ALREADY_USED_ERROR); } else { - saveNewEmail(auth); + saveNewEmail(auth, player, newEmail); } } else { - outputUnloggedMessage(); + outputUnloggedMessage(player); } } - private void saveNewEmail(PlayerAuth auth) { + private void saveNewEmail(PlayerAuth auth, Player player, String newEmail) { auth.setEmail(newEmail); if (dataSource.updateEmail(auth)) { playerCache.updatePlayer(auth); @@ -64,7 +59,7 @@ public class AsyncChangeEmail implements Process { } } - private void outputUnloggedMessage() { + private void outputUnloggedMessage(Player player) { if (dataSource.isAuthAvailable(player.getName())) { service.send(player, MessageKey.LOGIN_MESSAGE); } else if (service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)) { diff --git a/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java b/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java index 3aff89417..f35044758 100644 --- a/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java +++ b/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java @@ -14,9 +14,10 @@ import fr.xephi.authme.hooks.PluginHooks; import fr.xephi.authme.listener.AuthMePlayerListener; import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.permission.PlayerStatePermission; -import fr.xephi.authme.process.Process; +import fr.xephi.authme.process.NewProcess; import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.settings.Settings; +import fr.xephi.authme.settings.SpawnLoader; import fr.xephi.authme.settings.properties.HooksSettings; import fr.xephi.authme.settings.properties.PluginSettings; import fr.xephi.authme.settings.properties.RegistrationSettings; @@ -35,42 +36,48 @@ import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; import org.bukkit.scheduler.BukkitTask; +import javax.inject.Inject; + import static fr.xephi.authme.settings.properties.RestrictionSettings.PROTECT_INVENTORY_BEFORE_LOGIN; /** */ -public class AsynchronousJoin implements Process { +public class AsynchronousJoin implements NewProcess { - private final AuthMe plugin; - private final Player player; - private final DataSource database; - private final String name; - private final ProcessService service; - private final PlayerCache playerCache; - private final PluginHooks pluginHooks; + @Inject + private AuthMe plugin; - private final boolean disableCollisions = MethodUtils + @Inject + private DataSource database; + + @Inject + private ProcessService service; + + @Inject + private PlayerCache playerCache; + + @Inject + private LimboCache limboCache; + + @Inject + private PluginHooks pluginHooks; + + @Inject + private SpawnLoader spawnLoader; + + private static final boolean DISABLE_COLLISIONS = MethodUtils .getAccessibleMethod(LivingEntity.class, "setCollidable", new Class[]{}) != null; - public AsynchronousJoin(Player player, AuthMe plugin, DataSource database, PlayerCache playerCache, - PluginHooks pluginHooks, ProcessService service) { - this.player = player; - this.plugin = plugin; - this.database = database; - this.name = player.getName().toLowerCase(); - this.service = service; - this.playerCache = playerCache; - this.pluginHooks = pluginHooks; - } + AsynchronousJoin() { } - @Override - public void run() { + public void processJoin(final Player player) { if (Utils.isUnrestricted(player)) { return; } + final String name = player.getName().toLowerCase(); if (service.getProperty(HooksSettings.DISABLE_SOCIAL_SPY)) { - service.getPluginHooks().setEssentialsSocialSpyStatus(player, false); + pluginHooks.setEssentialsSocialSpyStatus(player, false); } final String ip = Utils.getPlayerIp(player); @@ -101,10 +108,10 @@ public class AsynchronousJoin implements Process { return; } // Prevent player collisions in 1.9 - if (disableCollisions) { + if (DISABLE_COLLISIONS) { ((LivingEntity) player).setCollidable(false); } - final Location spawnLoc = service.getSpawnLoader().getSpawnLocation(player); + final Location spawnLoc = spawnLoader.getSpawnLocation(player); final boolean isAuthAvailable = database.isAuthAvailable(name); if (isAuthAvailable) { if (!service.getProperty(RestrictionSettings.NO_TELEPORT)) { @@ -123,7 +130,7 @@ public class AsynchronousJoin implements Process { } } placePlayerSafely(player, spawnLoc); - LimboCache.getInstance().updateLimboPlayer(player); + limboCache.updateLimboPlayer(player); // protect inventory if (service.getProperty(PROTECT_INVENTORY_BEFORE_LOGIN) && plugin.inventoryProtector != null) { @@ -144,7 +151,7 @@ public class AsynchronousJoin implements Process { } PlayerAuth auth = database.getAuth(name); database.setUnlogged(name); - PlayerCache.getInstance().removePlayer(name); + playerCache.removePlayer(name); if (auth != null && auth.getIp().equals(ip)) { service.send(player, MessageKey.SESSION_RECONNECTION); plugin.getManagement().performLogin(player, "dontneed", true); @@ -161,12 +168,12 @@ public class AsynchronousJoin implements Process { return; } - if (!Settings.noTeleport && !needFirstSpawn() && Settings.isTeleportToSpawnEnabled + if (!Settings.noTeleport && !needFirstSpawn(player) && Settings.isTeleportToSpawnEnabled || (Settings.isForceSpawnLocOnJoinEnabled && Settings.getForcedWorlds.contains(player.getWorld().getName()))) { service.scheduleSyncDelayedTask(new Runnable() { @Override public void run() { - SpawnTeleportEvent tpEvent = new SpawnTeleportEvent(player, player.getLocation(), spawnLoc, PlayerCache.getInstance().isAuthenticated(name)); + SpawnTeleportEvent tpEvent = new SpawnTeleportEvent(player, player.getLocation(), spawnLoc, playerCache.isAuthenticated(name)); service.callEvent(tpEvent); if (!tpEvent.isCancelled() && player.isOnline() && tpEvent.getTo() != null && tpEvent.getTo().getWorld() != null) { @@ -177,8 +184,8 @@ public class AsynchronousJoin implements Process { } } - if (!LimboCache.getInstance().hasLimboPlayer(name)) { - LimboCache.getInstance().addLimboPlayer(player); + if (!limboCache.hasLimboPlayer(name)) { + limboCache.addLimboPlayer(player); } Utils.setGroup(player, isAuthAvailable ? GroupType.NOTLOGGEDIN : GroupType.UNREGISTERED); @@ -209,7 +216,7 @@ public class AsynchronousJoin implements Process { int msgInterval = service.getProperty(RegistrationSettings.MESSAGE_INTERVAL); if (registrationTimeout > 0) { BukkitTask id = service.runTaskLater(new TimeoutTask(plugin, name, player), registrationTimeout); - LimboPlayer limboPlayer = LimboCache.getInstance().getLimboPlayer(name); + LimboPlayer limboPlayer = limboCache.getLimboPlayer(name); if (limboPlayer != null) { limboPlayer.setTimeoutTask(id); } @@ -223,21 +230,21 @@ public class AsynchronousJoin implements Process { ? MessageKey.REGISTER_EMAIL_MESSAGE : MessageKey.REGISTER_MESSAGE; } - if (msgInterval > 0 && LimboCache.getInstance().getLimboPlayer(name) != null) { + if (msgInterval > 0 && limboCache.getLimboPlayer(name) != null) { BukkitTask msgTask = service.runTask(new MessageTask(service.getBukkitService(), plugin.getMessages(), name, msg, msgInterval)); - LimboPlayer limboPlayer = LimboCache.getInstance().getLimboPlayer(name); + LimboPlayer limboPlayer = limboCache.getLimboPlayer(name); if (limboPlayer != null) { limboPlayer.setMessageTask(msgTask); } } } - private boolean needFirstSpawn() { + private boolean needFirstSpawn(final Player player) { if (player.hasPlayedBefore()) { return false; } - Location firstSpawn = service.getSpawnLoader().getFirstSpawn(); + Location firstSpawn = spawnLoader.getFirstSpawn(); if (firstSpawn == null) { return false; } diff --git a/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java b/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java index da98a05b4..1bbc6bafc 100644 --- a/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java +++ b/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java @@ -10,9 +10,10 @@ import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.events.AuthMeAsyncPreLoginEvent; import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.permission.AdminPermission; +import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.permission.PlayerPermission; import fr.xephi.authme.permission.PlayerStatePermission; -import fr.xephi.authme.process.Process; +import fr.xephi.authme.process.NewProcess; import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.security.RandomString; import fr.xephi.authme.settings.Settings; @@ -22,43 +23,41 @@ import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.settings.properties.SecuritySettings; import fr.xephi.authme.task.MessageTask; -import fr.xephi.authme.util.BukkitService; import fr.xephi.authme.util.StringUtils; import fr.xephi.authme.util.Utils; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitTask; +import javax.inject.Inject; import java.util.List; /** */ -public class AsynchronousLogin implements Process { +public class AsynchronousLogin implements NewProcess { - private final Player player; - private final String name; - private final String realName; - private final String password; - private final boolean forceLogin; - private final AuthMe plugin; - private final DataSource database; - private final String ip; - private final ProcessService service; + @Inject + private AuthMe plugin; - public AsynchronousLogin(Player player, String password, boolean forceLogin, AuthMe plugin, DataSource data, - ProcessService service) { - this.player = player; - this.name = player.getName().toLowerCase(); - this.password = password; - this.realName = player.getName(); - this.forceLogin = forceLogin; - this.plugin = plugin; - this.database = data; - this.ip = Utils.getPlayerIp(player); - this.service = service; - } + @Inject + private DataSource database; - private boolean needsCaptcha() { + @Inject + private ProcessService service; + + @Inject + private PermissionsManager permissionsManager; + + @Inject + private PlayerCache playerCache; + + @Inject + private LimboCache limboCache; + + AsynchronousLogin() { } + + private boolean needsCaptcha(Player player) { + final String name = player.getName().toLowerCase(); if (service.getProperty(SecuritySettings.USE_CAPTCHA)) { if (!plugin.captcha.containsKey(name)) { plugin.captcha.putIfAbsent(name, 1); @@ -82,8 +81,9 @@ public class AsynchronousLogin implements Process { * * @return PlayerAuth */ - private PlayerAuth preAuth() { - if (PlayerCache.getInstance().isAuthenticated(name)) { + private PlayerAuth preAuth(Player player) { + final String name = player.getName().toLowerCase(); + if (playerCache.isAuthenticated(name)) { service.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR); return null; } @@ -92,7 +92,7 @@ public class AsynchronousLogin implements Process { if (pAuth == null) { service.send(player, MessageKey.USER_NOT_REGISTERED); - LimboPlayer limboPlayer = LimboCache.getInstance().getLimboPlayer(name); + LimboPlayer limboPlayer = limboCache.getLimboPlayer(name); if (limboPlayer != null) { limboPlayer.getMessageTask().cancel(); String[] msg = service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION) @@ -110,9 +110,10 @@ public class AsynchronousLogin implements Process { return null; } + final String ip = Utils.getPlayerIp(player); if (Settings.getMaxLoginPerIp > 0 - && !plugin.getPermissionsManager().hasPermission(player, PlayerStatePermission.ALLOW_MULTIPLE_ACCOUNTS) - && !ip.equalsIgnoreCase("127.0.0.1") && !ip.equalsIgnoreCase("localhost")) { + && !permissionsManager.hasPermission(player, PlayerStatePermission.ALLOW_MULTIPLE_ACCOUNTS) + && !"127.0.0.1".equalsIgnoreCase(ip) && !"localhost".equalsIgnoreCase(ip)) { if (plugin.isLoggedIp(name, ip)) { service.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR); return null; @@ -127,13 +128,13 @@ public class AsynchronousLogin implements Process { return pAuth; } - @Override - public void run() { - PlayerAuth pAuth = preAuth(); - if (pAuth == null || needsCaptcha()) { + public void login(final Player player, String password, boolean forceLogin) { + PlayerAuth pAuth = preAuth(player); + if (pAuth == null || needsCaptcha(player)) { return; } + final String ip = Utils.getPlayerIp(player); if ("127.0.0.1".equals(pAuth.getIp()) && !pAuth.getIp().equals(ip)) { pAuth.setIp(ip); database.updateIp(pAuth.getNickname(), ip); @@ -141,12 +142,13 @@ public class AsynchronousLogin implements Process { String email = pAuth.getEmail(); boolean passwordVerified = forceLogin || plugin.getPasswordSecurity() - .comparePassword(password, pAuth.getPassword(), realName); + .comparePassword(password, pAuth.getPassword(), player.getName()); + final String name = player.getName().toLowerCase(); if (passwordVerified && player.isOnline()) { PlayerAuth auth = PlayerAuth.builder() .name(name) - .realName(realName) + .realName(player.getName()) .ip(ip) .email(email) .password(pAuth.getPassword()) @@ -166,7 +168,7 @@ public class AsynchronousLogin implements Process { if (!forceLogin) service.send(player, MessageKey.LOGIN_SUCCESS); - displayOtherAccounts(auth); + displayOtherAccounts(auth, player); if (service.getProperty(EmailSettings.RECALL_PLAYERS) && (StringUtils.isEmpty(email) || "your@email.com".equalsIgnoreCase(email))) { @@ -174,11 +176,11 @@ public class AsynchronousLogin implements Process { } if (!service.getProperty(SecuritySettings.REMOVE_SPAM_FROM_CONSOLE)) { - ConsoleLogger.info(realName + " logged in!"); + ConsoleLogger.info(player.getName() + " logged in!"); } // makes player isLoggedin via API - PlayerCache.getInstance().addPlayer(auth); + playerCache.addPlayer(auth); database.setLogged(name); // As the scheduling executes the Task most likely after the current @@ -197,7 +199,7 @@ public class AsynchronousLogin implements Process { Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, syncPlayerLogin); } else if (player.isOnline()) { if (!service.getProperty(SecuritySettings.REMOVE_SPAM_FROM_CONSOLE)) { - ConsoleLogger.info(realName + " used the wrong password"); + ConsoleLogger.info(player.getName() + " used the wrong password"); } if (service.getProperty(RestrictionSettings.KICK_ON_WRONG_PASSWORD)) { service.scheduleSyncDelayedTask(new Runnable() { @@ -214,29 +216,30 @@ public class AsynchronousLogin implements Process { } } - // TODO: allow translation! - private void displayOtherAccounts(PlayerAuth auth) { + // TODO #423: allow translation! + private void displayOtherAccounts(PlayerAuth auth, Player player) { if (!service.getProperty(RestrictionSettings.DISPLAY_OTHER_ACCOUNTS) || auth == null) { return; } - List auths = this.database.getAllAuthsByIp(auth.getIp()); + List auths = database.getAllAuthsByIp(auth.getIp()); if (auths.size() < 2) { return; } - // TODO: color player names with green if the account is online + // TODO #423: color player names with green if the account is online String message = StringUtils.join(", ", auths) + "."; ConsoleLogger.info("The user " + player.getName() + " has " + auths.size() + " accounts:"); ConsoleLogger.info(message); - for (Player player : service.getOnlinePlayers()) { - if ((player.getName().equalsIgnoreCase(this.player.getName()) && plugin.getPermissionsManager().hasPermission(player, PlayerPermission.SEE_OWN_ACCOUNTS))) { - player.sendMessage("You own " + auths.size() + " accounts:"); - player.sendMessage(message); - } else if (plugin.getPermissionsManager().hasPermission(player, AdminPermission.SEE_OTHER_ACCOUNTS)) { - player.sendMessage("The user " + player.getName() + " has " + auths.size() + " accounts:"); - player.sendMessage(message); + for (Player onlinePlayer : service.getOnlinePlayers()) { + if ((onlinePlayer.getName().equalsIgnoreCase(onlinePlayer.getName()) + && permissionsManager.hasPermission(onlinePlayer, PlayerPermission.SEE_OWN_ACCOUNTS))) { + onlinePlayer.sendMessage("You own " + auths.size() + " accounts:"); + onlinePlayer.sendMessage(message); + } else if (permissionsManager.hasPermission(onlinePlayer, AdminPermission.SEE_OTHER_ACCOUNTS)) { + onlinePlayer.sendMessage("The user " + onlinePlayer.getName() + " has " + auths.size() + " accounts:"); + onlinePlayer.sendMessage(message); } } } diff --git a/src/main/java/fr/xephi/authme/process/logout/AsynchronousLogout.java b/src/main/java/fr/xephi/authme/process/logout/AsynchronousLogout.java index a882ab30b..2ea02d474 100644 --- a/src/main/java/fr/xephi/authme/process/logout/AsynchronousLogout.java +++ b/src/main/java/fr/xephi/authme/process/logout/AsynchronousLogout.java @@ -6,50 +6,37 @@ import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.limbo.LimboCache; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.output.MessageKey; -import fr.xephi.authme.process.Process; +import fr.xephi.authme.process.NewProcess; import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.util.Utils; import fr.xephi.authme.util.Utils.GroupType; import org.bukkit.entity.Player; -/** - */ -public class AsynchronousLogout implements Process { +import javax.inject.Inject; - private final Player player; - private final String name; - private final AuthMe plugin; - private final DataSource database; - private boolean canLogout = true; - private final ProcessService service; +public class AsynchronousLogout implements NewProcess { - /** - * Constructor for AsynchronousLogout. - * - * @param player Player - * @param plugin AuthMe - * @param database DataSource - * @param service The process service - */ - public AsynchronousLogout(Player player, AuthMe plugin, DataSource database, ProcessService service) { - this.player = player; - this.plugin = plugin; - this.database = database; - this.name = player.getName().toLowerCase(); - this.service = service; - } + @Inject + private AuthMe plugin; - private void preLogout() { - if (!PlayerCache.getInstance().isAuthenticated(name)) { + @Inject + private DataSource database; + + @Inject + private ProcessService service; + + @Inject + private PlayerCache playerCache; + + @Inject + private LimboCache limboCache; + + AsynchronousLogout() { } + + public void logout(Player player) { + final String name = player.getName().toLowerCase(); + if (!playerCache.isAuthenticated(name)) { service.send(player, MessageKey.NOT_LOGGED_IN); - canLogout = false; - } - } - - @Override - public void run() { - preLogout(); - if (!canLogout) { return; } final Player p = player; @@ -61,7 +48,7 @@ public class AsynchronousLogout implements Process { auth.setWorld(p.getWorld().getName()); database.updateQuitLoc(auth); - PlayerCache.getInstance().removePlayer(name); + playerCache.removePlayer(name); database.setUnlogged(name); service.scheduleSyncDelayedTask(new Runnable() { @Override @@ -69,10 +56,10 @@ public class AsynchronousLogout implements Process { Utils.teleportToSpawn(p); } }); - if (LimboCache.getInstance().hasLimboPlayer(name)) { - LimboCache.getInstance().deleteLimboPlayer(name); + if (limboCache.hasLimboPlayer(name)) { + limboCache.deleteLimboPlayer(name); } - LimboCache.getInstance().addLimboPlayer(player); + limboCache.addLimboPlayer(player); Utils.setGroup(player, GroupType.NOTLOGGEDIN); service.scheduleSyncDelayedTask(new ProcessSynchronousPlayerLogout(p, plugin, service)); } diff --git a/src/main/java/fr/xephi/authme/process/quit/AsynchronousQuit.java b/src/main/java/fr/xephi/authme/process/quit/AsynchronousQuit.java index c4e1a7f5b..110faa336 100644 --- a/src/main/java/fr/xephi/authme/process/quit/AsynchronousQuit.java +++ b/src/main/java/fr/xephi/authme/process/quit/AsynchronousQuit.java @@ -7,7 +7,7 @@ import fr.xephi.authme.cache.limbo.LimboCache; import fr.xephi.authme.cache.limbo.LimboPlayer; import fr.xephi.authme.datasource.CacheDataSource; import fr.xephi.authme.datasource.DataSource; -import fr.xephi.authme.process.Process; +import fr.xephi.authme.process.NewProcess; import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.properties.RestrictionSettings; @@ -17,35 +17,37 @@ import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitTask; -public class AsynchronousQuit implements Process { +import javax.inject.Inject; - private final AuthMe plugin; - private final DataSource database; - private final Player player; - private final String name; - private boolean isOp = false; - private boolean needToChange = false; - private final boolean isKick; - private final ProcessService service; +public class AsynchronousQuit implements NewProcess { - public AsynchronousQuit(Player p, AuthMe plugin, DataSource database, boolean isKick, ProcessService service) { - this.player = p; - this.plugin = plugin; - this.database = database; - this.name = p.getName().toLowerCase(); - this.isKick = isKick; - this.service = service; - } + @Inject + private AuthMe plugin; - @Override - public void run() { + @Inject + private DataSource database; + + @Inject + private ProcessService service; + + @Inject + private PlayerCache playerCache; + + @Inject + private LimboCache limboCache; + + AsynchronousQuit() { } + + + public void processQuit(Player player, boolean isKick) { if (player == null || Utils.isUnrestricted(player)) { return; } + final String name = player.getName().toLowerCase(); String ip = Utils.getPlayerIp(player); - if (PlayerCache.getInstance().isAuthenticated(name)) { + if (playerCache.isAuthenticated(name)) { if (service.getProperty(RestrictionSettings.SAVE_QUIT_LOCATION)) { Location loc = player.getLocation(); PlayerAuth auth = PlayerAuth.builder() @@ -62,14 +64,17 @@ public class AsynchronousQuit implements Process { database.updateSession(auth); } - LimboPlayer limbo = LimboCache.getInstance().getLimboPlayer(name); + boolean needToChange = false; + boolean isOp = false; + + LimboPlayer limbo = limboCache.getLimboPlayer(name); if (limbo != null) { if (!StringUtils.isEmpty(limbo.getGroup())) { Utils.addNormal(player, limbo.getGroup()); } needToChange = true; isOp = limbo.isOperator(); - LimboCache.getInstance().deleteLimboPlayer(name); + limboCache.deleteLimboPlayer(name); } if (Settings.isSessionsEnabled && !isKick) { if (Settings.getSessionTimeout != 0) { @@ -78,7 +83,7 @@ public class AsynchronousQuit implements Process { @Override public void run() { - postLogout(); + postLogout(name); } }, Settings.getSessionTimeout * 20 * 60); @@ -86,11 +91,11 @@ public class AsynchronousQuit implements Process { plugin.sessions.put(name, task); } else { //plugin is disabled; we cannot schedule more tasks so run it directly here - postLogout(); + postLogout(name); } } } else { - PlayerCache.getInstance().removePlayer(name); + playerCache.removePlayer(name); database.setUnlogged(name); } @@ -103,7 +108,7 @@ public class AsynchronousQuit implements Process { } } - private void postLogout() { + private void postLogout(String name) { PlayerCache.getInstance().removePlayer(name); database.setUnlogged(name); plugin.sessions.remove(name); diff --git a/src/main/java/fr/xephi/authme/process/register/AsyncRegister.java b/src/main/java/fr/xephi/authme/process/register/AsyncRegister.java index 77171cd08..6ab889807 100644 --- a/src/main/java/fr/xephi/authme/process/register/AsyncRegister.java +++ b/src/main/java/fr/xephi/authme/process/register/AsyncRegister.java @@ -6,7 +6,7 @@ import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.permission.PlayerStatePermission; -import fr.xephi.authme.process.Process; +import fr.xephi.authme.process.NewProcess; import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.security.HashAlgorithm; import fr.xephi.authme.security.crypts.HashedPassword; @@ -18,42 +18,30 @@ import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.settings.properties.SecuritySettings; import fr.xephi.authme.util.StringUtils; import fr.xephi.authme.util.Utils; - import org.bukkit.Bukkit; import org.bukkit.entity.Player; +import javax.inject.Inject; import java.util.List; -/** - */ -public class AsyncRegister implements Process { +public class AsyncRegister implements NewProcess { - private final Player player; - private final String name; - private final String password; - private final String ip; - private final String email; - private final AuthMe plugin; - private final DataSource database; - private final PlayerCache playerCache; - private final ProcessService service; - private final boolean autoLogin; + @Inject + private AuthMe plugin; - public AsyncRegister(Player player, String password, String email, AuthMe plugin, DataSource data, - PlayerCache playerCache, ProcessService service, boolean autoLogin) { - this.player = player; - this.password = password; - this.name = player.getName().toLowerCase(); - this.email = email; - this.plugin = plugin; - this.database = data; - this.ip = Utils.getPlayerIp(player); - this.playerCache = playerCache; - this.service = service; - this.autoLogin = autoLogin; - } + @Inject + private DataSource database; - private boolean preRegisterCheck() { + @Inject + private PlayerCache playerCache; + + @Inject + private ProcessService service; + + AsyncRegister() { } + + private boolean preRegisterCheck(Player player, String password) { + final String name = player.getName().toLowerCase(); if (playerCache.isAuthenticated(name)) { service.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR); return false; @@ -78,6 +66,7 @@ public class AsyncRegister implements Process { } final int maxRegPerIp = service.getProperty(RestrictionSettings.MAX_REGISTRATION_PER_IP); + final String ip = Utils.getPlayerIp(player); if (maxRegPerIp > 0 && !"127.0.0.1".equalsIgnoreCase(ip) && !"localhost".equalsIgnoreCase(ip) @@ -92,18 +81,18 @@ public class AsyncRegister implements Process { return true; } - @Override - public void run() { - if (preRegisterCheck()) { + public void register(Player player, String password, String email, boolean autoLogin) { + if (preRegisterCheck(player, password)) { if (!StringUtils.isEmpty(email)) { - emailRegister(); + emailRegister(player, password, email); } else { - passwordRegister(); + passwordRegister(player, password, autoLogin); } } } - private void emailRegister() { + private void emailRegister(Player player, String password, String email) { + final String name = player.getName().toLowerCase(); final int maxRegPerEmail = service.getProperty(EmailSettings.MAX_REG_PER_EMAIL); if (maxRegPerEmail > 0 && !plugin.getPermissionsManager().hasPermission(player, PlayerStatePermission.ALLOW_MULTIPLE_ACCOUNTS)) { @@ -116,6 +105,7 @@ public class AsyncRegister implements Process { } final HashedPassword hashedPassword = service.computeHash(password, name); + final String ip = Utils.getPlayerIp(player); PlayerAuth auth = PlayerAuth.builder() .name(name) .realName(player.getName()) @@ -137,7 +127,9 @@ public class AsyncRegister implements Process { } - private void passwordRegister() { + private void passwordRegister(Player player, String password, boolean autoLogin) { + final String name = player.getName().toLowerCase(); + final String ip = Utils.getPlayerIp(player); final HashedPassword hashedPassword = service.computeHash(password, name); PlayerAuth auth = PlayerAuth.builder() .name(name) diff --git a/src/main/java/fr/xephi/authme/process/unregister/AsynchronousUnregister.java b/src/main/java/fr/xephi/authme/process/unregister/AsynchronousUnregister.java index cd3f41a21..022899637 100644 --- a/src/main/java/fr/xephi/authme/process/unregister/AsynchronousUnregister.java +++ b/src/main/java/fr/xephi/authme/process/unregister/AsynchronousUnregister.java @@ -4,12 +4,13 @@ import fr.xephi.authme.AuthMe; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerCache; -import fr.xephi.authme.cache.backup.JsonCache; import fr.xephi.authme.cache.limbo.LimboCache; import fr.xephi.authme.cache.limbo.LimboPlayer; +import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.output.MessageKey; -import fr.xephi.authme.process.Process; +import fr.xephi.authme.process.NewProcess; import fr.xephi.authme.process.ProcessService; +import fr.xephi.authme.security.PasswordSecurity; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RestrictionSettings; @@ -22,55 +23,50 @@ import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; import org.bukkit.scheduler.BukkitTask; -public class AsynchronousUnregister implements Process { +import javax.inject.Inject; - private final Player player; - private final String name; - private final String password; - private final boolean force; - private final AuthMe plugin; - private final JsonCache playerCache; - private final ProcessService service; +import static fr.xephi.authme.util.BukkitService.TICKS_PER_SECOND; - /** - * Constructor. - * - * @param player The player to perform the action for - * @param password The password - * @param force True to bypass password validation - * @param plugin The plugin instance - * @param service The process service - */ - public AsynchronousUnregister(Player player, String password, boolean force, AuthMe plugin, - ProcessService service) { - this.player = player; - this.name = player.getName().toLowerCase(); - this.password = password; - this.force = force; - this.plugin = plugin; - this.playerCache = new JsonCache(); - this.service = service; - } +public class AsynchronousUnregister implements NewProcess { - @Override - public void run() { - PlayerAuth cachedAuth = PlayerCache.getInstance().getAuth(name); - if (force || plugin.getPasswordSecurity().comparePassword( - password, cachedAuth.getPassword(), player.getName())) { - if (!service.getDataSource().removeAuth(name)) { + @Inject + private AuthMe plugin; + + @Inject + private DataSource dataSource; + + @Inject + private ProcessService service; + + @Inject + private PasswordSecurity passwordSecurity; + + @Inject + private PlayerCache playerCache; + + @Inject + private LimboCache limboCache; + + AsynchronousUnregister() { } + + public void unregister(Player player, String password, boolean force) { + final String name = player.getName().toLowerCase(); + PlayerAuth cachedAuth = playerCache.getAuth(name); + if (force || passwordSecurity.comparePassword(password, cachedAuth.getPassword(), player.getName())) { + if (!dataSource.removeAuth(name)) { service.send(player, MessageKey.ERROR); return; } - int timeOut = service.getProperty(RestrictionSettings.TIMEOUT) * 20; + int timeOut = service.getProperty(RestrictionSettings.TIMEOUT) * TICKS_PER_SECOND; if (Settings.isForcedRegistrationEnabled) { Utils.teleportToSpawn(player); player.saveData(); - PlayerCache.getInstance().removePlayer(player.getName().toLowerCase()); + playerCache.removePlayer(player.getName().toLowerCase()); if (!Settings.getRegisteredGroup.isEmpty()) { Utils.setGroup(player, GroupType.UNREGISTERED); } - LimboCache.getInstance().addLimboPlayer(player); - LimboPlayer limboPlayer = LimboCache.getInstance().getLimboPlayer(name); + limboCache.addLimboPlayer(player); + LimboPlayer limboPlayer = limboCache.getLimboPlayer(name); int interval = service.getProperty(RegistrationSettings.MESSAGE_INTERVAL); if (timeOut != 0) { BukkitTask id = service.runTaskLater(new TimeoutTask(plugin, name, player), timeOut); @@ -85,12 +81,8 @@ public class AsynchronousUnregister implements Process { if (!Settings.unRegisteredGroup.isEmpty()) { Utils.setGroup(player, Utils.GroupType.UNREGISTERED); } - PlayerCache.getInstance().removePlayer(name); - // check if Player cache File Exist and delete it, preventing - // duplication of items - if (playerCache.doesCacheExist(player)) { - playerCache.removeCache(player); - } + playerCache.removePlayer(name); + // Apply blind effect if (service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)) { player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, timeOut, 2)); diff --git a/src/test/java/fr/xephi/authme/process/ProcessServiceTest.java b/src/test/java/fr/xephi/authme/process/ProcessServiceTest.java index f641b1f01..7bedb9142 100644 --- a/src/test/java/fr/xephi/authme/process/ProcessServiceTest.java +++ b/src/test/java/fr/xephi/authme/process/ProcessServiceTest.java @@ -140,33 +140,6 @@ public class ProcessServiceTest { assertThat(result, equalTo(authMe)); } - @Test - public void shouldReturnPluginHooks() { - // given / when - PluginHooks result = processService.getPluginHooks(); - - // then - assertThat(result, equalTo(pluginHooks)); - } - - @Test - public void shouldReturnSpawnLoader() { - // given / when - SpawnLoader result = processService.getSpawnLoader(); - - // then - assertThat(result, equalTo(spawnLoader)); - } - - @Test - public void shouldReturnDatasource() { - // given / when - DataSource result = processService.getDataSource(); - - // then - assertThat(result, equalTo(dataSource)); - } - @Test public void shouldComputeHash() { // given diff --git a/src/test/java/fr/xephi/authme/process/email/AsyncAddEmailTest.java b/src/test/java/fr/xephi/authme/process/email/AsyncAddEmailTest.java index 9f92a5c82..4d013bd3b 100644 --- a/src/test/java/fr/xephi/authme/process/email/AsyncAddEmailTest.java +++ b/src/test/java/fr/xephi/authme/process/email/AsyncAddEmailTest.java @@ -11,6 +11,7 @@ import org.bukkit.entity.Player; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; @@ -26,12 +27,18 @@ import static org.mockito.Mockito.verify; @RunWith(MockitoJUnitRunner.class) public class AsyncAddEmailTest { + @InjectMocks + private AsyncAddEmail asyncAddEmail; + @Mock private Player player; + @Mock private DataSource dataSource; + @Mock private PlayerCache playerCache; + @Mock private ProcessService service; @@ -44,7 +51,6 @@ public class AsyncAddEmailTest { public void shouldAddEmail() { // given String email = "my.mail@example.org"; - AsyncAddEmail process = createProcess(email); given(player.getName()).willReturn("testEr"); given(playerCache.isAuthenticated("tester")).willReturn(true); PlayerAuth auth = mock(PlayerAuth.class); @@ -55,7 +61,7 @@ public class AsyncAddEmailTest { given(service.isEmailFreeForRegistration(email, player)).willReturn(true); // when - process.run(); + asyncAddEmail.addEmail(player, email); // then verify(dataSource).updateEmail(auth); @@ -68,7 +74,6 @@ public class AsyncAddEmailTest { public void shouldReturnErrorWhenMailCannotBeSaved() { // given String email = "my.mail@example.org"; - AsyncAddEmail process = createProcess(email); given(player.getName()).willReturn("testEr"); given(playerCache.isAuthenticated("tester")).willReturn(true); PlayerAuth auth = mock(PlayerAuth.class); @@ -80,7 +85,7 @@ public class AsyncAddEmailTest { given(service.isEmailFreeForRegistration(email, player)).willReturn(true); // when - process.run(); + asyncAddEmail.addEmail(player, email); // then verify(dataSource).updateEmail(auth); @@ -90,7 +95,6 @@ public class AsyncAddEmailTest { @Test public void shouldNotAddMailIfPlayerAlreadyHasEmail() { // given - AsyncAddEmail process = createProcess("some.mail@example.org"); given(player.getName()).willReturn("my_Player"); given(playerCache.isAuthenticated("my_player")).willReturn(true); PlayerAuth auth = mock(PlayerAuth.class); @@ -98,7 +102,7 @@ public class AsyncAddEmailTest { given(playerCache.getAuth("my_player")).willReturn(auth); // when - process.run(); + asyncAddEmail.addEmail(player, "some.mail@example.org"); // then verify(service).send(player, MessageKey.USAGE_CHANGE_EMAIL); @@ -109,7 +113,6 @@ public class AsyncAddEmailTest { public void shouldNotAddMailIfItIsInvalid() { // given String email = "invalid_mail"; - AsyncAddEmail process = createProcess(email); given(player.getName()).willReturn("my_Player"); given(playerCache.isAuthenticated("my_player")).willReturn(true); PlayerAuth auth = mock(PlayerAuth.class); @@ -118,7 +121,7 @@ public class AsyncAddEmailTest { given(service.validateEmail(email)).willReturn(false); // when - process.run(); + asyncAddEmail.addEmail(player, email); // then verify(service).send(player, MessageKey.INVALID_EMAIL); @@ -129,7 +132,6 @@ public class AsyncAddEmailTest { public void shouldNotAddMailIfAlreadyUsed() { // given String email = "player@mail.tld"; - AsyncAddEmail process = createProcess(email); given(player.getName()).willReturn("TestName"); given(playerCache.isAuthenticated("testname")).willReturn(true); PlayerAuth auth = mock(PlayerAuth.class); @@ -139,7 +141,7 @@ public class AsyncAddEmailTest { given(service.isEmailFreeForRegistration(email, player)).willReturn(false); // when - process.run(); + asyncAddEmail.addEmail(player, email); // then verify(service).send(player, MessageKey.EMAIL_ALREADY_USED_ERROR); @@ -149,13 +151,12 @@ public class AsyncAddEmailTest { @Test public void shouldShowLoginMessage() { // given - AsyncAddEmail process = createProcess("test@mail.com"); given(player.getName()).willReturn("Username12"); given(playerCache.isAuthenticated("username12")).willReturn(false); given(dataSource.isAuthAvailable("Username12")).willReturn(true); // when - process.run(); + asyncAddEmail.addEmail(player, "test@mail.com"); // then verify(service).send(player, MessageKey.LOGIN_MESSAGE); @@ -165,14 +166,13 @@ public class AsyncAddEmailTest { @Test public void shouldShowEmailRegisterMessage() { // given - AsyncAddEmail process = createProcess("test@mail.com"); given(player.getName()).willReturn("user"); given(playerCache.isAuthenticated("user")).willReturn(false); given(dataSource.isAuthAvailable("user")).willReturn(false); given(service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(true); // when - process.run(); + asyncAddEmail.addEmail(player, "test@mail.com"); // then verify(service).send(player, MessageKey.REGISTER_EMAIL_MESSAGE); @@ -182,28 +182,17 @@ public class AsyncAddEmailTest { @Test public void shouldShowRegularRegisterMessage() { // given - AsyncAddEmail process = createProcess("test@mail.com"); given(player.getName()).willReturn("user"); given(playerCache.isAuthenticated("user")).willReturn(false); given(dataSource.isAuthAvailable("user")).willReturn(false); given(service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(false); // when - process.run(); + asyncAddEmail.addEmail(player, "test@mail.com"); // then verify(service).send(player, MessageKey.REGISTER_MESSAGE); verify(playerCache, never()).updatePlayer(any(PlayerAuth.class)); } - /** - * Create an instance of {@link AsyncAddEmail} with the class' mocks. - * - * @param email The email to use - * @return The created process - */ - private AsyncAddEmail createProcess(String email) { - return new AsyncAddEmail(player, email, dataSource, playerCache, service); - } - } diff --git a/src/test/java/fr/xephi/authme/process/email/AsyncChangeEmailTest.java b/src/test/java/fr/xephi/authme/process/email/AsyncChangeEmailTest.java index a90ff9b27..7b9dbfdb6 100644 --- a/src/test/java/fr/xephi/authme/process/email/AsyncChangeEmailTest.java +++ b/src/test/java/fr/xephi/authme/process/email/AsyncChangeEmailTest.java @@ -5,11 +5,11 @@ import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.process.ProcessService; -import fr.xephi.authme.settings.properties.EmailSettings; import fr.xephi.authme.settings.properties.RegistrationSettings; import org.bukkit.entity.Player; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; @@ -26,12 +26,18 @@ import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class AsyncChangeEmailTest { + @InjectMocks + private AsyncChangeEmail process; + @Mock private Player player; + @Mock private PlayerCache playerCache; + @Mock private DataSource dataSource; + @Mock private ProcessService service; @@ -39,7 +45,6 @@ public class AsyncChangeEmailTest { public void shouldAddEmail() { // given String newEmail = "new@mail.tld"; - AsyncChangeEmail process = createProcess("old@mail.tld", newEmail); given(player.getName()).willReturn("Bobby"); given(playerCache.isAuthenticated("bobby")).willReturn(true); PlayerAuth auth = authWithMail("old@mail.tld"); @@ -49,7 +54,7 @@ public class AsyncChangeEmailTest { given(service.isEmailFreeForRegistration(newEmail, player)).willReturn(true); // when - process.run(); + process.changeEmail(player, "old@mail.tld", newEmail); // then verify(dataSource).updateEmail(auth); @@ -61,7 +66,6 @@ public class AsyncChangeEmailTest { public void shouldShowErrorIfSaveFails() { // given String newEmail = "new@mail.tld"; - AsyncChangeEmail process = createProcess("old@mail.tld", newEmail); given(player.getName()).willReturn("Bobby"); given(playerCache.isAuthenticated("bobby")).willReturn(true); PlayerAuth auth = authWithMail("old@mail.tld"); @@ -71,7 +75,7 @@ public class AsyncChangeEmailTest { given(service.isEmailFreeForRegistration(newEmail, player)).willReturn(true); // when - process.run(); + process.changeEmail(player, "old@mail.tld", newEmail); // then verify(dataSource).updateEmail(auth); @@ -82,14 +86,13 @@ public class AsyncChangeEmailTest { @Test public void shouldShowAddEmailUsage() { // given - AsyncChangeEmail process = createProcess("old@mail.tld", "new@mail.tld"); given(player.getName()).willReturn("Bobby"); given(playerCache.isAuthenticated("bobby")).willReturn(true); PlayerAuth auth = authWithMail(null); given(playerCache.getAuth("bobby")).willReturn(auth); // when - process.run(); + process.changeEmail(player, "old@mail.tld", "new@mailt.tld"); // then verify(dataSource, never()).updateEmail(any(PlayerAuth.class)); @@ -101,7 +104,6 @@ public class AsyncChangeEmailTest { public void shouldRejectInvalidNewMail() { // given String newEmail = "bogus"; - AsyncChangeEmail process = createProcess("old@mail.tld", newEmail); given(player.getName()).willReturn("Bobby"); given(playerCache.isAuthenticated("bobby")).willReturn(true); PlayerAuth auth = authWithMail("old@mail.tld"); @@ -109,7 +111,7 @@ public class AsyncChangeEmailTest { given(service.validateEmail(newEmail)).willReturn(false); // when - process.run(); + process.changeEmail(player, "old@mail.tld", newEmail); // then verify(dataSource, never()).updateEmail(any(PlayerAuth.class)); @@ -121,7 +123,6 @@ public class AsyncChangeEmailTest { public void shouldRejectInvalidOldEmail() { // given String newEmail = "new@mail.tld"; - AsyncChangeEmail process = createProcess("old@mail.tld", newEmail); given(player.getName()).willReturn("Bobby"); given(playerCache.isAuthenticated("bobby")).willReturn(true); PlayerAuth auth = authWithMail("other@address.email"); @@ -131,7 +132,7 @@ public class AsyncChangeEmailTest { // when - process.run(); + process.changeEmail(player, "old@mail.tld", newEmail); // then verify(dataSource, never()).updateEmail(any(PlayerAuth.class)); @@ -143,7 +144,6 @@ public class AsyncChangeEmailTest { public void shouldRejectAlreadyUsedEmail() { // given String newEmail = "new@example.com"; - AsyncChangeEmail process = createProcess("old@example.com", newEmail); given(player.getName()).willReturn("Username"); given(playerCache.isAuthenticated("username")).willReturn(true); PlayerAuth auth = authWithMail("old@example.com"); @@ -152,7 +152,7 @@ public class AsyncChangeEmailTest { given(service.isEmailFreeForRegistration(newEmail, player)).willReturn(false); // when - process.run(); + process.changeEmail(player, "old@example.com", newEmail); // then verify(dataSource, never()).updateEmail(any(PlayerAuth.class)); @@ -163,13 +163,12 @@ public class AsyncChangeEmailTest { @Test public void shouldSendLoginMessage() { // given - AsyncChangeEmail process = createProcess("old@mail.tld", "new@mail.tld"); given(player.getName()).willReturn("Bobby"); given(playerCache.isAuthenticated("bobby")).willReturn(false); given(dataSource.isAuthAvailable("Bobby")).willReturn(true); // when - process.run(); + process.changeEmail(player, "old@mail.tld", "new@mail.tld"); // then verify(dataSource, never()).updateEmail(any(PlayerAuth.class)); @@ -180,14 +179,13 @@ public class AsyncChangeEmailTest { @Test public void shouldShowEmailRegistrationMessage() { // given - AsyncChangeEmail process = createProcess("old@mail.tld", "new@mail.tld"); given(player.getName()).willReturn("Bobby"); given(playerCache.isAuthenticated("bobby")).willReturn(false); given(dataSource.isAuthAvailable("Bobby")).willReturn(false); given(service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(true); // when - process.run(); + process.changeEmail(player, "old@mail.tld", "new@mail.tld"); // then verify(dataSource, never()).updateEmail(any(PlayerAuth.class)); @@ -198,14 +196,13 @@ public class AsyncChangeEmailTest { @Test public void shouldShowRegistrationMessage() { // given - AsyncChangeEmail process = createProcess("old@mail.tld", "new@mail.tld"); given(player.getName()).willReturn("Bobby"); given(playerCache.isAuthenticated("bobby")).willReturn(false); given(dataSource.isAuthAvailable("Bobby")).willReturn(false); given(service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(false); // when - process.run(); + process.changeEmail(player, "old@mail.tld", "new@mail.tld"); // then verify(dataSource, never()).updateEmail(any(PlayerAuth.class)); @@ -219,8 +216,4 @@ public class AsyncChangeEmailTest { return auth; } - private AsyncChangeEmail createProcess(String oldEmail, String newEmail) { - given(service.getProperty(EmailSettings.MAX_REG_PER_EMAIL)).willReturn(5); - return new AsyncChangeEmail(player, oldEmail, newEmail, dataSource, playerCache, service); - } }