mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-10-31 07:39:42 +01:00
Add tests for LogoutCommand and RegisterCommand. Add more generic mockUtil
This commit is contained in:
parent
4e8614fdf7
commit
58dc15123c
@ -23,7 +23,7 @@ public class LogoutCommand extends ExecutableCommand {
|
|||||||
final Player player = (Player) sender;
|
final Player player = (Player) sender;
|
||||||
|
|
||||||
// Logout the player
|
// Logout the player
|
||||||
plugin.management.performLogout(player);
|
plugin.getManagement().performLogout(player);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package fr.xephi.authme.command.executable.register;
|
package fr.xephi.authme.command.executable.register;
|
||||||
|
|
||||||
|
import fr.xephi.authme.process.Management;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
@ -31,7 +32,7 @@ public class RegisterCommand extends ExecutableCommand {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
final AuthMe plugin = AuthMe.getInstance();
|
final Management management = AuthMe.getInstance().getManagement();
|
||||||
if (Settings.emailRegistration && !Settings.getmailAccount.isEmpty()) {
|
if (Settings.emailRegistration && !Settings.getmailAccount.isEmpty()) {
|
||||||
if (Settings.doubleEmailCheck) {
|
if (Settings.doubleEmailCheck) {
|
||||||
if (commandArguments.getCount() < 2 || !commandArguments.get(0).equals(commandArguments.get(1))) {
|
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);
|
RandomString rand = new RandomString(Settings.getRecoveryPassLength);
|
||||||
final String thePass = rand.nextString();
|
final String thePass = rand.nextString();
|
||||||
plugin.management.performRegister(player, thePass, email);
|
management.performRegister(player, thePass, email);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (commandArguments.getCount() > 1 && Settings.getEnablePasswordVerifier)
|
if (commandArguments.getCount() > 1 && Settings.getEnablePasswordVerifier)
|
||||||
@ -54,7 +55,7 @@ public class RegisterCommand extends ExecutableCommand {
|
|||||||
m.send(player, "password_error");
|
m.send(player, "password_error");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
plugin.management.performRegister(player, commandArguments.get(0), "");
|
management.performRegister(player, commandArguments.get(0), "");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package fr.xephi.authme;
|
package fr.xephi.authme;
|
||||||
|
|
||||||
|
import fr.xephi.authme.settings.Messages;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
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);
|
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 {
|
try {
|
||||||
Field instance = AuthMe.class.getDeclaredField("plugin");
|
Field instance = clazz.getDeclaredField(fieldName);
|
||||||
instance.setAccessible(true);
|
instance.setAccessible(true);
|
||||||
instance.set(null, mock);
|
instance.set(null, mock);
|
||||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
} 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.junit.Test;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
import static org.mockito.Matchers.any;
|
||||||
import static org.mockito.Matchers.*;
|
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.mock;
|
||||||
import static org.mockito.Mockito.never;
|
import static org.mockito.Mockito.never;
|
||||||
|
|
||||||
@ -26,7 +28,7 @@ public class LoginCommandTest {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void initializeAuthMeMock() {
|
public void initializeAuthMeMock() {
|
||||||
AuthMeMockUtil.initialize();
|
AuthMeMockUtil.mockAuthMeInstance();
|
||||||
AuthMe pluginMock = AuthMe.getInstance();
|
AuthMe pluginMock = AuthMe.getInstance();
|
||||||
|
|
||||||
Settings.captchaLength = 10;
|
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