diff --git a/src/main/java/fr/xephi/authme/command/executable/logout/LogoutCommand.java b/src/main/java/fr/xephi/authme/command/executable/logout/LogoutCommand.java index e2e97806e..60c14a268 100644 --- a/src/main/java/fr/xephi/authme/command/executable/logout/LogoutCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/logout/LogoutCommand.java @@ -23,7 +23,7 @@ public class LogoutCommand extends ExecutableCommand { final Player player = (Player) sender; // Logout the player - plugin.management.performLogout(player); + plugin.getManagement().performLogout(player); return true; } } diff --git a/src/main/java/fr/xephi/authme/command/executable/register/RegisterCommand.java b/src/main/java/fr/xephi/authme/command/executable/register/RegisterCommand.java index 3c7f07ee4..6e89fc62f 100644 --- a/src/main/java/fr/xephi/authme/command/executable/register/RegisterCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/register/RegisterCommand.java @@ -1,5 +1,6 @@ package fr.xephi.authme.command.executable.register; +import fr.xephi.authme.process.Management; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -31,7 +32,7 @@ public class RegisterCommand extends ExecutableCommand { return true; } - final AuthMe plugin = AuthMe.getInstance(); + final Management management = AuthMe.getInstance().getManagement(); if (Settings.emailRegistration && !Settings.getmailAccount.isEmpty()) { if (Settings.doubleEmailCheck) { if (commandArguments.getCount() < 2 || !commandArguments.get(0).equals(commandArguments.get(1))) { @@ -46,7 +47,7 @@ public class RegisterCommand extends ExecutableCommand { } RandomString rand = new RandomString(Settings.getRecoveryPassLength); final String thePass = rand.nextString(); - plugin.management.performRegister(player, thePass, email); + management.performRegister(player, thePass, email); return true; } if (commandArguments.getCount() > 1 && Settings.getEnablePasswordVerifier) @@ -54,7 +55,7 @@ public class RegisterCommand extends ExecutableCommand { m.send(player, "password_error"); return true; } - plugin.management.performRegister(player, commandArguments.get(0), ""); + management.performRegister(player, commandArguments.get(0), ""); return true; } } diff --git a/src/test/java/fr/xephi/authme/AuthMeMockUtil.java b/src/test/java/fr/xephi/authme/AuthMeMockUtil.java index 6f4ca7a46..905c46303 100644 --- a/src/test/java/fr/xephi/authme/AuthMeMockUtil.java +++ b/src/test/java/fr/xephi/authme/AuthMeMockUtil.java @@ -1,5 +1,6 @@ package fr.xephi.authme; +import fr.xephi.authme.settings.Messages; import org.mockito.Mockito; import java.lang.reflect.Field; @@ -14,17 +15,35 @@ public final class AuthMeMockUtil { } /** - * Set the AuthMe plugin instance to a mock object. Use {@link AuthMe#getInstance()} to retrieve the mock. + * Sets the AuthMe plugin instance to a mock object. Use {@link AuthMe#getInstance()} to retrieve the mock. */ - public static void initialize() { + public static void mockAuthMeInstance() { AuthMe mock = Mockito.mock(AuthMe.class); + mockSingletonForClass(AuthMe.class, "plugin", mock); + } + /** + * Creates a mock Messages object for the instance returned from {@link Messages#getInstance()}. + */ + public static void mockMessagesInstance() { + Messages mock = Mockito.mock(Messages.class); + mockSingletonForClass(Messages.class, "singleton", mock); + } + + /** + * Sets a field of a class to the given mock. + * + * @param clazz the class to modify + * @param fieldName the field name + * @param mock the mock to set for the given field + */ + private static void mockSingletonForClass(Class clazz, String fieldName, Object mock) { try { - Field instance = AuthMe.class.getDeclaredField("plugin"); + Field instance = clazz.getDeclaredField(fieldName); instance.setAccessible(true); instance.set(null, mock); } catch (NoSuchFieldException | IllegalAccessException e) { - throw new RuntimeException("Could not initialize AuthMe mock", e); + throw new RuntimeException("Could not set mock instance for class " + clazz.getName(), 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 index b8c3c2f5e..5ed42876c 100644 --- a/src/test/java/fr/xephi/authme/command/executable/login/LoginCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/login/LoginCommandTest.java @@ -12,8 +12,10 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; - -import static org.mockito.Matchers.*; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyBoolean; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -26,7 +28,7 @@ public class LoginCommandTest { @Before public void initializeAuthMeMock() { - AuthMeMockUtil.initialize(); + AuthMeMockUtil.mockAuthMeInstance(); AuthMe pluginMock = AuthMe.getInstance(); Settings.captchaLength = 10; diff --git a/src/test/java/fr/xephi/authme/command/executable/logout/LogoutCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/logout/LogoutCommandTest.java new file mode 100644 index 000000000..8416b5fdc --- /dev/null +++ b/src/test/java/fr/xephi/authme/command/executable/logout/LogoutCommandTest.java @@ -0,0 +1,62 @@ +package fr.xephi.authme.command.executable.logout; + +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.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; + +/** + * Test for {@link LogoutCommand}. + */ +public class LogoutCommandTest { + + private static Management managementMock; + + @Before + public void initializeAuthMeMock() { + AuthMeMockUtil.mockAuthMeInstance(); + 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); + LogoutCommand command = new LogoutCommand(); + + // when + command.executeCommand(sender, new CommandParts(), new CommandParts()); + + // then + Mockito.verify(managementMock, never()).performLogout(any(Player.class)); + } + + @Test + public void shouldCallManagementForPlayerCaller() { + // given + Player sender = mock(Player.class); + LogoutCommand command = new LogoutCommand(); + + // when + command.executeCommand(sender, new CommandParts(), new CommandParts("password")); + + // then + Mockito.verify(managementMock).performLogout(sender); + } + +} diff --git a/src/test/java/fr/xephi/authme/command/executable/register/RegisterCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/register/RegisterCommandTest.java new file mode 100644 index 000000000..0b2ba2cd0 --- /dev/null +++ b/src/test/java/fr/xephi/authme/command/executable/register/RegisterCommandTest.java @@ -0,0 +1,88 @@ +package fr.xephi.authme.command.executable.register; + +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.Messages; +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.ArgumentCaptor; +import org.mockito.Mockito; + +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + +/** + * Test for {@link RegisterCommand}. + */ +public class RegisterCommandTest { + + private static Management managementMock; + private static Messages messagesMock; + + @Before + public void initializeAuthMeMock() { + AuthMeMockUtil.mockMessagesInstance(); + messagesMock = Messages.getInstance(); + + AuthMeMockUtil.mockAuthMeInstance(); + AuthMe pluginMock = AuthMe.getInstance(); + + Settings.captchaLength = 10; + managementMock = mock(Management.class); + Mockito.when(pluginMock.getManagement()).thenReturn(managementMock); + } + + @Test + public void shouldNotRunForNonPlayerSender() { + // given + CommandSender sender = mock(BlockCommandSender.class); + RegisterCommand command = new RegisterCommand(); + ArgumentCaptor messageCaptor = ArgumentCaptor.forClass(String.class); + + // when + command.executeCommand(sender, new CommandParts(), new CommandParts()); + + // then + verify(sender).sendMessage(messageCaptor.capture()); + assertThat(messageCaptor.getValue().contains("Player Only!"), equalTo(true)); + verify(managementMock, never()).performRegister(any(Player.class), anyString(), anyString()); + } + + @Test + public void shouldFailForEmptyArguments() { + // given + CommandSender sender = mock(Player.class); + RegisterCommand command = new RegisterCommand(); + + // when + command.executeCommand(sender, new CommandParts(), new CommandParts()); + + // then + verify(messagesMock).send(sender, "usage_reg"); + verify(managementMock, never()).performRegister(any(Player.class), anyString(), anyString()); + } + + @Test + public void shouldForwardRegister() { + // given + Player sender = mock(Player.class); + RegisterCommand command = new RegisterCommand(); + + // when + command.executeCommand(sender, new CommandParts(), new CommandParts("password")); + + // then + verify(managementMock).performRegister(sender, "password", ""); + } +}