mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-11-28 13:15:28 +01:00
Add tests for LogoutCommand and RegisterCommand. Add more generic mockUtil
(cherry picked from commit 06cfd13)
This commit is contained in:
parent
987e38c5df
commit
e65319d42c
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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<String> 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", "");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user