diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index 29d7091c9..69112ea20 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -952,4 +952,9 @@ public class AuthMe extends JavaPlugin { public static int getVersionCode() { return PLUGIN_VERSION_CODE; } + + /** Returns the management instance. */ + public Management getManagement() { + return management; + } } diff --git a/src/main/java/fr/xephi/authme/command/executable/login/LoginCommand.java b/src/main/java/fr/xephi/authme/command/executable/login/LoginCommand.java index e9b80cdfc..2479a1058 100644 --- a/src/main/java/fr/xephi/authme/command/executable/login/LoginCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/login/LoginCommand.java @@ -24,7 +24,7 @@ public class LoginCommand extends ExecutableCommand { final String playerPass = commandArguments.get(0); // Log the player in - plugin.management.performLogin(player, playerPass, false); + plugin.getManagement().performLogin(player, playerPass, false); return true; } } diff --git a/src/test/java/fr/xephi/authme/AuthMeMockUtil.java b/src/test/java/fr/xephi/authme/AuthMeMockUtil.java new file mode 100644 index 000000000..6f4ca7a46 --- /dev/null +++ b/src/test/java/fr/xephi/authme/AuthMeMockUtil.java @@ -0,0 +1,30 @@ +package fr.xephi.authme; + +import org.mockito.Mockito; + +import java.lang.reflect.Field; + +/** + * Creates a mock implementation of AuthMe for testing purposes. + */ +public final class AuthMeMockUtil { + + private AuthMeMockUtil() { + // Util class + } + + /** + * Set the AuthMe plugin instance to a mock object. Use {@link AuthMe#getInstance()} to retrieve the mock. + */ + public static void initialize() { + AuthMe mock = Mockito.mock(AuthMe.class); + + try { + Field instance = AuthMe.class.getDeclaredField("plugin"); + instance.setAccessible(true); + instance.set(null, mock); + } catch (NoSuchFieldException | IllegalAccessException e) { + throw new RuntimeException("Could not initialize AuthMe mock", e); + } + } +} diff --git a/src/test/java/fr/xephi/authme/command/executable/login/LoginCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/login/LoginCommandTest.java new file mode 100644 index 000000000..b8c3c2f5e --- /dev/null +++ b/src/test/java/fr/xephi/authme/command/executable/login/LoginCommandTest.java @@ -0,0 +1,77 @@ +package fr.xephi.authme.command.executable.login; + +import fr.xephi.authme.AuthMe; +import fr.xephi.authme.AuthMeMockUtil; +import fr.xephi.authme.command.CommandParts; +import fr.xephi.authme.process.Management; +import fr.xephi.authme.settings.Settings; +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 static org.mockito.Matchers.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; + +/** + * Test for {@link LoginCommand}. + */ +public class LoginCommandTest { + + private static Management managementMock; + + @Before + public void initializeAuthMeMock() { + AuthMeMockUtil.initialize(); + AuthMe pluginMock = AuthMe.getInstance(); + + Settings.captchaLength = 10; + managementMock = mock(Management.class); + Mockito.when(pluginMock.getManagement()).thenReturn(managementMock); + } + + @Test + public void shouldStopIfSenderIsNotAPlayer() { + // given + CommandSender sender = mock(BlockCommandSender.class); + LoginCommand command = new LoginCommand(); + + // when + command.executeCommand(sender, new CommandParts(), new CommandParts()); + + // then + Mockito.verify(managementMock, never()).performLogin(any(Player.class), anyString(), anyBoolean()); + } + + @Test + public void shouldCallManagementForPlayerCaller() { + // given + Player sender = mock(Player.class); + LoginCommand command = new LoginCommand(); + + // when + command.executeCommand(sender, new CommandParts(), new CommandParts("password")); + + // then + Mockito.verify(managementMock).performLogin(eq(sender), eq("password"), eq(false)); + } + + @Test + public void shouldHandleMissingPassword() { + // given + Player sender = mock(Player.class); + LoginCommand command = new LoginCommand(); + + // when + command.executeCommand(sender, new CommandParts(), new CommandParts()); + + // then + // TODO ljacqu 20151121: May make sense to handle null password in LoginCommand instead of forwarding the call + String password = null; + Mockito.verify(managementMock).performLogin(eq(sender), eq(password), eq(false)); + } +}