Get player via BukkitService; add unit tests for commands
This commit is contained in:
@@ -14,6 +14,7 @@ import fr.xephi.authme.settings.NewSetting;
|
||||
import fr.xephi.authme.settings.SpawnLoader;
|
||||
import fr.xephi.authme.settings.domain.Property;
|
||||
import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||
import fr.xephi.authme.util.BukkitService;
|
||||
import fr.xephi.authme.util.ValidationService;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -63,11 +64,13 @@ public class CommandServiceTest {
|
||||
private AntiBot antiBot;
|
||||
@Mock
|
||||
private ValidationService validationService;
|
||||
@Mock
|
||||
private BukkitService bukkitService;
|
||||
|
||||
@Before
|
||||
public void setUpService() {
|
||||
commandService = new CommandService(authMe, commandMapper, helpProvider, messages, passwordSecurity,
|
||||
permissionsManager, settings, pluginHooks, spawnLoader, antiBot, validationService);
|
||||
permissionsManager, settings, pluginHooks, spawnLoader, antiBot, validationService, bukkitService);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -265,4 +268,19 @@ public class CommandServiceTest {
|
||||
verify(validationService).isEmailFreeForRegistration(email, sender);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetPlayer() {
|
||||
// given
|
||||
String playerName = "_tester";
|
||||
Player player = mock(Player.class);
|
||||
given(bukkitService.getPlayerExact(playerName)).willReturn(player);
|
||||
|
||||
// when
|
||||
Player result = commandService.getPlayer(playerName);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(player));
|
||||
verify(bukkitService).getPlayerExact(playerName);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
package fr.xephi.authme.command.executable.authme;
|
||||
|
||||
import fr.xephi.authme.command.CommandService;
|
||||
import fr.xephi.authme.command.ExecutableCommand;
|
||||
import fr.xephi.authme.permission.PermissionsManager;
|
||||
import fr.xephi.authme.permission.PlayerPermission;
|
||||
import fr.xephi.authme.process.Management;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Matchers.argThat;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Test for {@link ForceLoginCommand}.
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ForceLoginCommandTest {
|
||||
|
||||
@Mock
|
||||
private CommandService commandService;
|
||||
|
||||
@Test
|
||||
public void shouldRejectOfflinePlayer() {
|
||||
// given
|
||||
String playerName = "Bobby";
|
||||
Player player = mockPlayer(false, playerName);
|
||||
given(commandService.getPlayer(playerName)).willReturn(player);
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
ExecutableCommand command = new ForceLoginCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.singletonList(playerName), commandService);
|
||||
|
||||
// then
|
||||
verify(commandService).getPlayer(playerName);
|
||||
verify(sender).sendMessage(argThat(equalTo("Player needs to be online!")));
|
||||
verify(commandService, never()).getManagement();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRejectInexistentPlayer() {
|
||||
// given
|
||||
String playerName = "us3rname01";
|
||||
given(commandService.getPlayer(playerName)).willReturn(null);
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
ExecutableCommand command = new ForceLoginCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.singletonList(playerName), commandService);
|
||||
|
||||
// then
|
||||
verify(commandService).getPlayer(playerName);
|
||||
verify(sender).sendMessage(argThat(equalTo("Player needs to be online!")));
|
||||
verify(commandService, never()).getManagement();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRejectPlayerWithMissingPermission() {
|
||||
// given
|
||||
String playerName = "testTest";
|
||||
Player player = mockPlayer(true, playerName);
|
||||
given(commandService.getPlayer(playerName)).willReturn(player);
|
||||
PermissionsManager permissionsManager = mock(PermissionsManager.class);
|
||||
given(permissionsManager.hasPermission(player, PlayerPermission.CAN_LOGIN_BE_FORCED)).willReturn(false);
|
||||
given(commandService.getPermissionsManager()).willReturn(permissionsManager);
|
||||
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
ExecutableCommand command = new ForceLoginCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.singletonList(playerName), commandService);
|
||||
|
||||
// then
|
||||
verify(commandService).getPlayer(playerName);
|
||||
verify(sender).sendMessage(argThat(containsString("You cannot force login the player")));
|
||||
verify(commandService, never()).getManagement();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldForceLoginPlayer() {
|
||||
// given
|
||||
String playerName = "tester23";
|
||||
Player player = mockPlayer(true, playerName);
|
||||
given(commandService.getPlayer(playerName)).willReturn(player);
|
||||
PermissionsManager permissionsManager = mock(PermissionsManager.class);
|
||||
given(permissionsManager.hasPermission(player, PlayerPermission.CAN_LOGIN_BE_FORCED)).willReturn(true);
|
||||
given(commandService.getPermissionsManager()).willReturn(permissionsManager);
|
||||
Management management = mock(Management.class);
|
||||
given(commandService.getManagement()).willReturn(management);
|
||||
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
ExecutableCommand command = new ForceLoginCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.singletonList(playerName), commandService);
|
||||
|
||||
// then
|
||||
verify(commandService).getPlayer(playerName);
|
||||
verify(management).performLogin(eq(player), anyString(), eq(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldForceLoginSenderSelf() {
|
||||
// given
|
||||
String senderName = "tester23";
|
||||
Player player = mockPlayer(true, senderName);
|
||||
given(commandService.getPlayer(senderName)).willReturn(player);
|
||||
PermissionsManager permissionsManager = mock(PermissionsManager.class);
|
||||
given(permissionsManager.hasPermission(player, PlayerPermission.CAN_LOGIN_BE_FORCED)).willReturn(true);
|
||||
given(commandService.getPermissionsManager()).willReturn(permissionsManager);
|
||||
Management management = mock(Management.class);
|
||||
given(commandService.getManagement()).willReturn(management);
|
||||
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
given(sender.getName()).willReturn(senderName);
|
||||
ExecutableCommand command = new ForceLoginCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.<String>emptyList(), commandService);
|
||||
|
||||
// then
|
||||
verify(commandService).getPlayer(senderName);
|
||||
verify(management).performLogin(eq(player), anyString(), eq(true));
|
||||
}
|
||||
|
||||
private static Player mockPlayer(boolean isOnline, String name) {
|
||||
Player player = mock(Player.class);
|
||||
given(player.isOnline()).willReturn(isOnline);
|
||||
given(player.getName()).willReturn(name);
|
||||
return player;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package fr.xephi.authme.command.executable.authme;
|
||||
|
||||
import fr.xephi.authme.command.CommandService;
|
||||
import fr.xephi.authme.command.ExecutableCommand;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.hamcrest.Matchers.allOf;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Matchers.argThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Test for {@link GetIpCommand}.
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class GetIpCommandTest {
|
||||
|
||||
@Mock
|
||||
private CommandService commandService;
|
||||
@Mock
|
||||
private CommandSender sender;
|
||||
|
||||
@Test
|
||||
public void shouldGetIpOfPlayer() {
|
||||
// given
|
||||
given(commandService.getPlayer(anyString())).willReturn(null);
|
||||
ExecutableCommand command = new GetIpCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.singletonList("Testt"), commandService);
|
||||
|
||||
// then
|
||||
verify(commandService).getPlayer("Testt");
|
||||
verify(sender).sendMessage(argThat(containsString("not online")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnIpAddressOfPlayer() {
|
||||
// given
|
||||
String playerName = "charlie";
|
||||
String ip = "123.34.56.88";
|
||||
Player player = mockPlayer(playerName, ip);
|
||||
given(commandService.getPlayer(playerName)).willReturn(player);
|
||||
ExecutableCommand command = new GetIpCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.singletonList(playerName), commandService);
|
||||
|
||||
// then
|
||||
verify(commandService).getPlayer(playerName);
|
||||
verify(sender).sendMessage(argThat(allOf(containsString(playerName), containsString(ip))));
|
||||
}
|
||||
|
||||
private static Player mockPlayer(String name, String ip) {
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn(name);
|
||||
InetAddress inetAddress = mock(InetAddress.class);
|
||||
given(inetAddress.getHostAddress()).willReturn(ip);
|
||||
InetSocketAddress inetSocketAddress = new InetSocketAddress(inetAddress, 8093);
|
||||
given(player.getAddress()).willReturn(inetSocketAddress);
|
||||
return player;
|
||||
}
|
||||
}
|
||||
+182
@@ -0,0 +1,182 @@
|
||||
package fr.xephi.authme.command.executable.authme;
|
||||
|
||||
import fr.xephi.authme.ConsoleLoggerTestInitializer;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.cache.auth.PlayerAuth;
|
||||
import fr.xephi.authme.command.CommandService;
|
||||
import fr.xephi.authme.command.ExecutableCommand;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.output.MessageKey;
|
||||
import fr.xephi.authme.security.PasswordSecurity;
|
||||
import fr.xephi.authme.security.crypts.HashedPassword;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.argThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Test for {@link RegisterAdminCommand}.
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class RegisterAdminCommandTest {
|
||||
|
||||
@Mock
|
||||
private CommandSender sender;
|
||||
@Mock
|
||||
private CommandService commandService;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpLogger() {
|
||||
ConsoleLoggerTestInitializer.setupLogger();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRejectInvalidPassword() {
|
||||
// given
|
||||
String user = "tester";
|
||||
String password = "myPassword";
|
||||
given(commandService.validatePassword(password, user)).willReturn(MessageKey.INVALID_PASSWORD_LENGTH);
|
||||
ExecutableCommand command = new RegisterAdminCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Arrays.asList(user, password), commandService);
|
||||
|
||||
// then
|
||||
verify(commandService).validatePassword(password, user);
|
||||
verify(commandService).send(sender, MessageKey.INVALID_PASSWORD_LENGTH);
|
||||
verify(commandService, never()).runTaskAsynchronously(any(Runnable.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRejectAlreadyRegisteredAccount() {
|
||||
// given
|
||||
String user = "my_name55";
|
||||
String password = "@some-pass@";
|
||||
given(commandService.validatePassword(password, user)).willReturn(null);
|
||||
DataSource dataSource = mock(DataSource.class);
|
||||
given(dataSource.isAuthAvailable(user)).willReturn(true);
|
||||
given(commandService.getDataSource()).willReturn(dataSource);
|
||||
ExecutableCommand command = new RegisterAdminCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Arrays.asList(user, password), commandService);
|
||||
TestHelper.runInnerRunnable(commandService);
|
||||
|
||||
// then
|
||||
verify(commandService).validatePassword(password, user);
|
||||
verify(commandService).send(sender, MessageKey.NAME_ALREADY_REGISTERED);
|
||||
verify(dataSource, never()).saveAuth(any(PlayerAuth.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleSavingError() {
|
||||
// given
|
||||
String user = "test-test";
|
||||
String password = "afdjhfkt";
|
||||
given(commandService.validatePassword(password, user)).willReturn(null);
|
||||
DataSource dataSource = mock(DataSource.class);
|
||||
given(dataSource.isAuthAvailable(user)).willReturn(false);
|
||||
given(dataSource.saveAuth(any(PlayerAuth.class))).willReturn(false);
|
||||
given(commandService.getDataSource()).willReturn(dataSource);
|
||||
PasswordSecurity passwordSecurity = mock(PasswordSecurity.class);
|
||||
HashedPassword hashedPassword = new HashedPassword("235sdf4w5udsgf");
|
||||
given(passwordSecurity.computeHash(password, user)).willReturn(hashedPassword);
|
||||
given(commandService.getPasswordSecurity()).willReturn(passwordSecurity);
|
||||
ExecutableCommand command = new RegisterAdminCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Arrays.asList(user, password), commandService);
|
||||
TestHelper.runInnerRunnable(commandService);
|
||||
|
||||
// then
|
||||
verify(commandService).validatePassword(password, user);
|
||||
verify(commandService).send(sender, MessageKey.ERROR);
|
||||
ArgumentCaptor<PlayerAuth> captor = ArgumentCaptor.forClass(PlayerAuth.class);
|
||||
verify(dataSource).saveAuth(captor.capture());
|
||||
assertAuthHasInfo(captor.getValue(), user, hashedPassword);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRegisterOfflinePlayer() {
|
||||
// given
|
||||
String user = "someone";
|
||||
String password = "Al1O3P49S5%";
|
||||
given(commandService.validatePassword(password, user)).willReturn(null);
|
||||
DataSource dataSource = mock(DataSource.class);
|
||||
given(dataSource.isAuthAvailable(user)).willReturn(false);
|
||||
given(dataSource.saveAuth(any(PlayerAuth.class))).willReturn(true);
|
||||
given(commandService.getDataSource()).willReturn(dataSource);
|
||||
PasswordSecurity passwordSecurity = mock(PasswordSecurity.class);
|
||||
HashedPassword hashedPassword = new HashedPassword("$aea2345EW235dfsa@#R%987048");
|
||||
given(passwordSecurity.computeHash(password, user)).willReturn(hashedPassword);
|
||||
given(commandService.getPasswordSecurity()).willReturn(passwordSecurity);
|
||||
given(commandService.getPlayer(user)).willReturn(null);
|
||||
ExecutableCommand command = new RegisterAdminCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Arrays.asList(user, password), commandService);
|
||||
TestHelper.runInnerRunnable(commandService);
|
||||
|
||||
// then
|
||||
verify(commandService).validatePassword(password, user);
|
||||
verify(commandService).send(sender, MessageKey.REGISTER_SUCCESS);
|
||||
ArgumentCaptor<PlayerAuth> captor = ArgumentCaptor.forClass(PlayerAuth.class);
|
||||
verify(dataSource).saveAuth(captor.capture());
|
||||
assertAuthHasInfo(captor.getValue(), user, hashedPassword);
|
||||
verify(dataSource).setUnlogged(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRegisterOnlinePlayer() {
|
||||
// given
|
||||
String user = "someone";
|
||||
String password = "Al1O3P49S5%";
|
||||
given(commandService.validatePassword(password, user)).willReturn(null);
|
||||
DataSource dataSource = mock(DataSource.class);
|
||||
given(dataSource.isAuthAvailable(user)).willReturn(false);
|
||||
given(dataSource.saveAuth(any(PlayerAuth.class))).willReturn(true);
|
||||
given(commandService.getDataSource()).willReturn(dataSource);
|
||||
PasswordSecurity passwordSecurity = mock(PasswordSecurity.class);
|
||||
HashedPassword hashedPassword = new HashedPassword("$aea2345EW235dfsa@#R%987048");
|
||||
given(passwordSecurity.computeHash(password, user)).willReturn(hashedPassword);
|
||||
given(commandService.getPasswordSecurity()).willReturn(passwordSecurity);
|
||||
Player player = mock(Player.class);
|
||||
given(commandService.getPlayer(user)).willReturn(player);
|
||||
ExecutableCommand command = new RegisterAdminCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Arrays.asList(user, password), commandService);
|
||||
TestHelper.runInnerRunnable(commandService);
|
||||
|
||||
// then
|
||||
verify(commandService).validatePassword(password, user);
|
||||
verify(commandService).send(sender, MessageKey.REGISTER_SUCCESS);
|
||||
ArgumentCaptor<PlayerAuth> captor = ArgumentCaptor.forClass(PlayerAuth.class);
|
||||
verify(dataSource).saveAuth(captor.capture());
|
||||
assertAuthHasInfo(captor.getValue(), user, hashedPassword);
|
||||
verify(dataSource).setUnlogged(user);
|
||||
verify(player).kickPlayer(argThat(containsString("please log in again")));
|
||||
}
|
||||
|
||||
private void assertAuthHasInfo(PlayerAuth auth, String name, HashedPassword hashedPassword) {
|
||||
assertThat(auth.getRealName(), equalTo(name));
|
||||
assertThat(auth.getNickname(), equalTo(name.toLowerCase()));
|
||||
assertThat(auth.getPassword(), equalTo(hashedPassword));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user