diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index c1d4a4d69..9fb59f289 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -73,7 +73,7 @@ public class AuthMe extends JavaPlugin { private static Server server; private static Wrapper wrapper = Wrapper.getInstance(); - public Management management; + private Management management; public NewAPI api; public SendMailSSL mail; public DataManager dataManager; diff --git a/src/main/java/fr/xephi/authme/api/API.java b/src/main/java/fr/xephi/authme/api/API.java index e849e7950..1f1ac69d8 100644 --- a/src/main/java/fr/xephi/authme/api/API.java +++ b/src/main/java/fr/xephi/authme/api/API.java @@ -170,7 +170,7 @@ public class API { */ @Deprecated public static void forceLogin(Player player) { - instance.management.performLogin(player, "dontneed", true); + instance.getManagement().performLogin(player, "dontneed", true); } /** diff --git a/src/main/java/fr/xephi/authme/api/NewAPI.java b/src/main/java/fr/xephi/authme/api/NewAPI.java index cfe4508f0..908f75df0 100644 --- a/src/main/java/fr/xephi/authme/api/NewAPI.java +++ b/src/main/java/fr/xephi/authme/api/NewAPI.java @@ -170,7 +170,7 @@ public class NewAPI { * @param player * player */ public void forceLogin(Player player) { - plugin.management.performLogin(player, "dontneed", true); + plugin.getManagement().performLogin(player, "dontneed", true); } /** @@ -179,7 +179,7 @@ public class NewAPI { * @param player * player */ public void forceLogout(Player player) { - plugin.management.performLogout(player); + plugin.getManagement().performLogout(player); } /** @@ -189,7 +189,7 @@ public class NewAPI { * @param password String */ public void forceRegister(Player player, String password) { - plugin.management.performRegister(player, password, null); + plugin.getManagement().performRegister(player, password, null); } /** @@ -198,6 +198,6 @@ public class NewAPI { * @param player * player */ public void forceUnregister(Player player) { - plugin.management.performUnregister(player, "", true); + plugin.getManagement().performUnregister(player, "", true); } } diff --git a/src/main/java/fr/xephi/authme/command/executable/authme/ForceLoginCommand.java b/src/main/java/fr/xephi/authme/command/executable/authme/ForceLoginCommand.java index b5fa337eb..f6f3efef5 100644 --- a/src/main/java/fr/xephi/authme/command/executable/authme/ForceLoginCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/authme/ForceLoginCommand.java @@ -34,7 +34,7 @@ public class ForceLoginCommand extends ExecutableCommand { sender.sendMessage("You cannot force login for the player " + playerName + "!"); return true; } - plugin.management.performLogin(player, "dontneed", true); + plugin.getManagement().performLogin(player, "dontneed", true); sender.sendMessage("Force Login for " + playerName + " performed!"); } catch (Exception e) { sender.sendMessage("An error occurred while trying to get that player!"); diff --git a/src/main/java/fr/xephi/authme/command/executable/captcha/CaptchaCommand.java b/src/main/java/fr/xephi/authme/command/executable/captcha/CaptchaCommand.java index 4fa2bbef9..696773537 100644 --- a/src/main/java/fr/xephi/authme/command/executable/captcha/CaptchaCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/captcha/CaptchaCommand.java @@ -8,6 +8,7 @@ import fr.xephi.authme.security.RandomString; import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.Messages; import fr.xephi.authme.settings.Settings; +import fr.xephi.authme.util.Wrapper; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -30,10 +31,11 @@ public class CaptchaCommand extends ExecutableCommand { String captcha = commandArguments.get(0); // AuthMe plugin instance - final AuthMe plugin = AuthMe.getInstance(); + final Wrapper wrapper = Wrapper.getInstance(); + final AuthMe plugin = wrapper.getAuthMe(); // Messages instance - final Messages m = plugin.getMessages(); + final Messages m = wrapper.getMessages(); // Command logic if (PlayerCache.getInstance().isAuthenticated(playerNameLowerCase)) { diff --git a/src/main/java/fr/xephi/authme/command/executable/unregister/UnregisterCommand.java b/src/main/java/fr/xephi/authme/command/executable/unregister/UnregisterCommand.java index 22ef35661..8ff40ed3d 100644 --- a/src/main/java/fr/xephi/authme/command/executable/unregister/UnregisterCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/unregister/UnregisterCommand.java @@ -49,7 +49,7 @@ public class UnregisterCommand extends ExecutableCommand { } // Unregister the player - plugin.management.performUnregister(player, playerPass, false); + plugin.getManagement().performUnregister(player, playerPass, false); return true; } } diff --git a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java index 773ed8d5d..52fc51ab9 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java @@ -333,7 +333,7 @@ public class AuthMePlayerListener implements Listener { event.setQuitMessage(null); } - plugin.management.performQuit(player, false); + plugin.getManagement().performQuit(player, false); } @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR) @@ -349,7 +349,7 @@ public class AuthMePlayerListener implements Listener { } Player player = event.getPlayer(); - plugin.management.performQuit(player, true); + plugin.getManagement().performQuit(player, true); } @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) 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 3160ddcc4..724c334fc 100644 --- a/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java +++ b/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java @@ -238,7 +238,7 @@ public class AsynchronousJoin { PlayerCache.getInstance().removePlayer(name); if (auth != null && auth.getIp().equals(ip)) { m.send(player, MessageKey.SESSION_RECONNECTION); - plugin.management.performLogin(player, "dontneed", true); + plugin.getManagement().performLogin(player, "dontneed", true); return; } else if (Settings.sessionExpireOnIpChange) { m.send(player, MessageKey.SESSION_EXPIRED); diff --git a/src/test/java/fr/xephi/authme/command/executable/captcha/CaptchaCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/captcha/CaptchaCommandTest.java new file mode 100644 index 000000000..36169db05 --- /dev/null +++ b/src/test/java/fr/xephi/authme/command/executable/captcha/CaptchaCommandTest.java @@ -0,0 +1,71 @@ +package fr.xephi.authme.command.executable.captcha; + +import fr.xephi.authme.AuthMe; +import fr.xephi.authme.command.CommandParts; +import fr.xephi.authme.command.ExecutableCommand; +import fr.xephi.authme.output.MessageKey; +import fr.xephi.authme.output.Messages; +import fr.xephi.authme.settings.Settings; +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.Ignore; +import org.junit.Test; +import org.mockito.Mockito; + +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Test for {@link CaptchaCommand}. + */ +public class CaptchaCommandTest { + + private WrapperMock wrapperMock; + + @Before + public void setUpWrapperMock() { + wrapperMock = WrapperMock.createInstance(); + Settings.useCaptcha = true; + } + + @Test + public void shouldRejectNonPlayerSender() { + // given + CommandSender sender = Mockito.mock(BlockCommandSender.class); + ExecutableCommand command = new CaptchaCommand(); + + // when + boolean result = command.executeCommand(sender, new CommandParts(), new CommandParts()); + + // then + assertThat(result, equalTo(true)); + assertThat(wrapperMock.wasMockCalled(AuthMe.class), equalTo(false)); + assertThat(wrapperMock.wasMockCalled(Messages.class), equalTo(false)); + } + + @Test + @Ignore + public void shouldRejectIfCaptchaIsNotUsed() { + // given + Player player = mockPlayerWithName("testplayer"); + ExecutableCommand command = new CaptchaCommand(); + + // when + boolean result = command.executeCommand(player, new CommandParts(), new CommandParts()); + + // then + assertThat(result, equalTo(true)); + verify(wrapperMock.getMessages()).send(player, MessageKey.USAGE_LOGIN); + } + + private static Player mockPlayerWithName(String name) { + Player player = Mockito.mock(Player.class); + when(player.getName()).thenReturn(name); + return player; + } +}