#432 Inject in commands: DataSource / AntiBot / PasswordSecurity / PlayerCache

- Inject the services instead of passing them through the command service
This commit is contained in:
ljacqu
2016-05-02 18:52:34 +02:00
parent e6dacd6951
commit 9af596327a
23 changed files with 211 additions and 243 deletions
@@ -1,9 +1,7 @@
package fr.xephi.authme.command;
import fr.xephi.authme.AntiBot;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.help.HelpProvider;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.hooks.PluginHooks;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
@@ -62,8 +60,6 @@ public class CommandServiceTest {
@Mock
private SpawnLoader spawnLoader;
@Mock
private AntiBot antiBot;
@Mock
private ValidationService validationService;
@Mock
private BukkitService bukkitService;
@@ -108,28 +104,6 @@ public class CommandServiceTest {
verify(commandMapper).mapPartsToCommand(sender, commandParts);
}
@Test
public void shouldGetDataSource() {
// given
DataSource dataSource = mock(DataSource.class);
given(authMe.getDataSource()).willReturn(dataSource);
// when
DataSource result = commandService.getDataSource();
// then
assertThat(result, equalTo(dataSource));
}
@Test
public void shouldGetPasswordSecurity() {
// given/when
PasswordSecurity passwordSecurity = commandService.getPasswordSecurity();
// then
assertThat(passwordSecurity, equalTo(this.passwordSecurity));
}
@Test
public void shouldOutputHelp() {
// given
@@ -4,7 +4,6 @@ import fr.xephi.authme.TestHelper;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
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;
@@ -13,6 +12,7 @@ import org.bukkit.command.CommandSender;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
@@ -24,6 +24,7 @@ import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
/**
* Test for {@link ChangePasswordAdminCommand}.
@@ -31,9 +32,21 @@ import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class)
public class ChangePasswordAdminCommandTest {
@InjectMocks
private ChangePasswordAdminCommand command;
@Mock
private CommandService service;
@Mock
private PasswordSecurity passwordSecurity;
@Mock
private DataSource dataSource;
@Mock
private PlayerCache playerCache;
@BeforeClass
public static void setUpLogger() {
TestHelper.setupLogger();
@@ -42,7 +55,6 @@ public class ChangePasswordAdminCommandTest {
@Test
public void shouldRejectInvalidPassword() {
// given
ExecutableCommand command = new ChangePasswordAdminCommand();
CommandSender sender = mock(CommandSender.class);
given(service.validatePassword("Bobby", "bobby")).willReturn(MessageKey.PASSWORD_IS_USERNAME_ERROR);
@@ -52,23 +64,16 @@ public class ChangePasswordAdminCommandTest {
// then
verify(service).validatePassword("Bobby", "bobby");
verify(service).send(sender, MessageKey.PASSWORD_IS_USERNAME_ERROR);
verify(service, never()).getDataSource();
verifyZeroInteractions(dataSource);
}
@Test
public void shouldRejectCommandForUnknownUser() {
// given
ExecutableCommand command = new ChangePasswordAdminCommand();
CommandSender sender = mock(CommandSender.class);
String player = "player";
PlayerCache playerCache = mock(PlayerCache.class);
given(playerCache.isAuthenticated(player)).willReturn(false);
given(service.getPlayerCache()).willReturn(playerCache);
DataSource dataSource = mock(DataSource.class);
given(dataSource.getAuth(player)).willReturn(null);
given(service.getDataSource()).willReturn(dataSource);
// when
command.executeCommand(sender, Arrays.asList(player, "password"), service);
@@ -82,26 +87,17 @@ public class ChangePasswordAdminCommandTest {
@Test
public void shouldUpdatePasswordOfLoggedInUser() {
// given
ExecutableCommand command = new ChangePasswordAdminCommand();
CommandSender sender = mock(CommandSender.class);
String player = "my_user12";
String password = "passPass";
PlayerAuth auth = mock(PlayerAuth.class);
PlayerCache playerCache = mock(PlayerCache.class);
given(playerCache.isAuthenticated(player)).willReturn(true);
given(playerCache.getAuth(player)).willReturn(auth);
given(service.getPlayerCache()).willReturn(playerCache);
PasswordSecurity passwordSecurity = mock(PasswordSecurity.class);
HashedPassword hashedPassword = mock(HashedPassword.class);
given(passwordSecurity.computeHash(password, player)).willReturn(hashedPassword);
given(service.getPasswordSecurity()).willReturn(passwordSecurity);
DataSource dataSource = mock(DataSource.class);
given(dataSource.updatePassword(auth)).willReturn(true);
given(service.getDataSource()).willReturn(dataSource);
// when
command.executeCommand(sender, Arrays.asList(player, password), service);
@@ -118,27 +114,17 @@ public class ChangePasswordAdminCommandTest {
@Test
public void shouldUpdatePasswordOfOfflineUser() {
// given
ExecutableCommand command = new ChangePasswordAdminCommand();
CommandSender sender = mock(CommandSender.class);
String player = "my_user12";
String password = "passPass";
PlayerAuth auth = mock(PlayerAuth.class);
PlayerCache playerCache = mock(PlayerCache.class);
given(playerCache.isAuthenticated(player)).willReturn(false);
given(service.getPlayerCache()).willReturn(playerCache);
DataSource dataSource = mock(DataSource.class);
given(dataSource.isAuthAvailable(player)).willReturn(true);
given(dataSource.getAuth(player)).willReturn(auth);
given(dataSource.updatePassword(auth)).willReturn(true);
given(service.getDataSource()).willReturn(dataSource);
PasswordSecurity passwordSecurity = mock(PasswordSecurity.class);
HashedPassword hashedPassword = mock(HashedPassword.class);
given(passwordSecurity.computeHash(password, player)).willReturn(hashedPassword);
given(service.getPasswordSecurity()).willReturn(passwordSecurity);
// when
command.executeCommand(sender, Arrays.asList(player, password), service);
@@ -155,26 +141,16 @@ public class ChangePasswordAdminCommandTest {
@Test
public void shouldReportWhenSaveFailed() {
// given
ExecutableCommand command = new ChangePasswordAdminCommand();
CommandSender sender = mock(CommandSender.class);
String player = "my_user12";
String password = "passPass";
PlayerAuth auth = mock(PlayerAuth.class);
PlayerCache playerCache = mock(PlayerCache.class);
given(playerCache.isAuthenticated(player)).willReturn(true);
given(playerCache.getAuth(player)).willReturn(auth);
given(service.getPlayerCache()).willReturn(playerCache);
PasswordSecurity passwordSecurity = mock(PasswordSecurity.class);
HashedPassword hashedPassword = mock(HashedPassword.class);
given(passwordSecurity.computeHash(password, player)).willReturn(hashedPassword);
given(service.getPasswordSecurity()).willReturn(passwordSecurity);
DataSource dataSource = mock(DataSource.class);
given(dataSource.updatePassword(auth)).willReturn(false);
given(service.getDataSource()).willReturn(dataSource);
// when
command.executeCommand(sender, Arrays.asList(player, password), service);
@@ -2,11 +2,14 @@ package fr.xephi.authme.command.executable.authme;
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 org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.Collections;
@@ -19,19 +22,26 @@ import static org.mockito.Mockito.verify;
/**
* Test for {@link GetEmailCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class GetEmailCommandTest {
@InjectMocks
private GetEmailCommand command;
@Mock
private DataSource dataSource;
@Mock
private CommandSender sender;
@Mock
private CommandService service;
@Test
public void shouldReportUnknownUser() {
// given
String user = "myTestUser";
DataSource dataSource = mock(DataSource.class);
given(dataSource.getAuth(user)).willReturn(null);
CommandService service = mock(CommandService.class);
given(service.getDataSource()).willReturn(dataSource);
CommandSender sender = mock(CommandSender.class);
ExecutableCommand command = new GetEmailCommand();
// when
command.executeCommand(sender, Collections.singletonList(user), service);
@@ -47,14 +57,7 @@ public class GetEmailCommandTest {
String email = "user.email@example.org";
PlayerAuth auth = mock(PlayerAuth.class);
given(auth.getEmail()).willReturn(email);
DataSource dataSource = mock(DataSource.class);
given(dataSource.getAuth(user)).willReturn(auth);
CommandService service = mock(CommandService.class);
given(service.getDataSource()).willReturn(dataSource);
CommandSender sender = mock(CommandSender.class);
ExecutableCommand command = new GetEmailCommand();
// when
command.executeCommand(sender, Collections.singletonList(user), service);
@@ -2,12 +2,15 @@ package fr.xephi.authme.command.executable.authme;
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 org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.Collections;
import java.util.Date;
@@ -23,8 +26,21 @@ import static org.mockito.Mockito.verify;
/**
* Test for {@link LastLoginCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class LastLoginCommandTest {
@InjectMocks
private LastLoginCommand command;
@Mock
private DataSource dataSource;
@Mock
private CommandService service;
@Mock
private CommandSender sender;
private static final long HOUR_IN_MSEC = 3600 * 1000;
private static final long DAY_IN_MSEC = 24 * HOUR_IN_MSEC;
@@ -32,15 +48,8 @@ public class LastLoginCommandTest {
public void shouldRejectNonExistentUser() {
// given
String player = "tester";
DataSource dataSource = mock(DataSource.class);
given(dataSource.getAuth(player)).willReturn(null);
CommandService service = mock(CommandService.class);
given(service.getDataSource()).willReturn(dataSource);
CommandSender sender = mock(CommandSender.class);
ExecutableCommand command = new LastLoginCommand();
// when
command.executeCommand(sender, Collections.singletonList(player), service);
@@ -58,14 +67,7 @@ public class LastLoginCommandTest {
PlayerAuth auth = mock(PlayerAuth.class);
given(auth.getLastLogin()).willReturn(lastLogin);
given(auth.getIp()).willReturn("123.45.66.77");
DataSource dataSource = mock(DataSource.class);
given(dataSource.getAuth(player)).willReturn(auth);
CommandService service = mock(CommandService.class);
given(service.getDataSource()).willReturn(dataSource);
CommandSender sender = mock(CommandSender.class);
ExecutableCommand command = new LastLoginCommand();
// when
command.executeCommand(sender, Collections.singletonList(player), service);
@@ -85,7 +87,6 @@ public class LastLoginCommandTest {
public void shouldDisplayLastLoginOfCommandSender() {
// given
String name = "CommandSender";
CommandSender sender = mock(CommandSender.class);
given(sender.getName()).willReturn(name);
long lastLogin = System.currentTimeMillis() -
@@ -93,14 +94,7 @@ public class LastLoginCommandTest {
PlayerAuth auth = mock(PlayerAuth.class);
given(auth.getLastLogin()).willReturn(lastLogin);
given(auth.getIp()).willReturn("123.45.66.77");
DataSource dataSource = mock(DataSource.class);
given(dataSource.getAuth(name)).willReturn(auth);
CommandService service = mock(CommandService.class);
given(service.getDataSource()).willReturn(dataSource);
ExecutableCommand command = new LastLoginCommand();
// when
command.executeCommand(sender, Collections.<String>emptyList(), service);
@@ -2,11 +2,14 @@ package fr.xephi.authme.command.executable.authme;
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 org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.Arrays;
import java.util.Collections;
@@ -20,21 +23,27 @@ import static org.mockito.Mockito.verify;
/**
* Test for {@link PurgeLastPositionCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class PurgeLastPositionCommandTest {
@InjectMocks
private PurgeLastPositionCommand command;
@Mock
private DataSource dataSource;
@Mock
private CommandService service;
@Mock
private CommandSender sender;
@Test
public void shouldPurgeLastPosOfUser() {
// given
String player = "_Bobby";
PlayerAuth auth = mock(PlayerAuth.class);
DataSource dataSource = mock(DataSource.class);
given(dataSource.getAuth(player)).willReturn(auth);
CommandService service = mock(CommandService.class);
given(service.getDataSource()).willReturn(dataSource);
CommandSender sender = mock(CommandSender.class);
ExecutableCommand command = new PurgeLastPositionCommand();
// when
command.executeCommand(sender, Collections.singletonList(player), service);
@@ -51,14 +60,8 @@ public class PurgeLastPositionCommandTest {
String player = "_Bobby";
CommandSender sender = mock(CommandSender.class);
given(sender.getName()).willReturn(player);
PlayerAuth auth = mock(PlayerAuth.class);
DataSource dataSource = mock(DataSource.class);
given(dataSource.getAuth(player)).willReturn(auth);
CommandService service = mock(CommandService.class);
given(service.getDataSource()).willReturn(dataSource);
ExecutableCommand command = new PurgeLastPositionCommand();
// when
command.executeCommand(sender, Collections.<String>emptyList(), service);
@@ -72,12 +75,6 @@ public class PurgeLastPositionCommandTest {
@Test
public void shouldHandleNonExistentUser() {
// given
DataSource dataSource = mock(DataSource.class);
CommandService service = mock(CommandService.class);
given(service.getDataSource()).willReturn(dataSource);
ExecutableCommand command = new PurgeLastPositionCommand();
CommandSender sender = mock(CommandSender.class);
String name = "invalidPlayer";
// when
@@ -94,14 +91,7 @@ public class PurgeLastPositionCommandTest {
PlayerAuth auth1 = mock(PlayerAuth.class);
PlayerAuth auth2 = mock(PlayerAuth.class);
PlayerAuth auth3 = mock(PlayerAuth.class);
DataSource dataSource = mock(DataSource.class);
given(dataSource.getAllAuths()).willReturn(Arrays.asList(auth1, auth2, auth3));
CommandService service = mock(CommandService.class);
given(service.getDataSource()).willReturn(dataSource);
ExecutableCommand command = new PurgeLastPositionCommand();
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList("*"), service);
@@ -3,7 +3,6 @@ package fr.xephi.authme.command.executable.authme;
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;
@@ -14,6 +13,7 @@ import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
@@ -35,11 +35,21 @@ import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class)
public class RegisterAdminCommandTest {
@InjectMocks
private RegisterAdminCommand command;
@Mock
private PasswordSecurity passwordSecurity;
@Mock
private CommandSender sender;
@Mock
private CommandService commandService;
@Mock
private DataSource dataSource;
@BeforeClass
public static void setUpLogger() {
TestHelper.setupLogger();
@@ -51,7 +61,6 @@ public class RegisterAdminCommandTest {
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);
@@ -68,10 +77,7 @@ public class RegisterAdminCommandTest {
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);
@@ -89,15 +95,10 @@ public class RegisterAdminCommandTest {
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);
@@ -117,16 +118,11 @@ public class RegisterAdminCommandTest {
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);
@@ -147,17 +143,12 @@ public class RegisterAdminCommandTest {
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);
@@ -2,11 +2,14 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.AntiBot;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.command.FoundCommandResult;
import fr.xephi.authme.command.help.HelpProvider;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.Collections;
@@ -22,17 +25,23 @@ import static org.mockito.Mockito.verify;
/**
* Test for {@link SwitchAntiBotCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class SwitchAntiBotCommandTest {
@InjectMocks
private SwitchAntiBotCommand command;
@Mock
private AntiBot antiBot;
@Mock
private CommandService service;
@Test
public void shouldReturnAntiBotState() {
// given
AntiBot antiBot = mock(AntiBot.class);
given(antiBot.getAntiBotStatus()).willReturn(AntiBot.AntiBotStatus.ACTIVE);
CommandService service = mock(CommandService.class);
given(service.getAntiBot()).willReturn(antiBot);
CommandSender sender = mock(CommandSender.class);
ExecutableCommand command = new SwitchAntiBotCommand();
// when
command.executeCommand(sender, Collections.<String>emptyList(), service);
@@ -44,11 +53,7 @@ public class SwitchAntiBotCommandTest {
@Test
public void shouldActivateAntiBot() {
// given
AntiBot antiBot = mock(AntiBot.class);
CommandService service = mock(CommandService.class);
given(service.getAntiBot()).willReturn(antiBot);
CommandSender sender = mock(CommandSender.class);
ExecutableCommand command = new SwitchAntiBotCommand();
// when
command.executeCommand(sender, Collections.singletonList("on"), service);
@@ -61,11 +66,7 @@ public class SwitchAntiBotCommandTest {
@Test
public void shouldDeactivateAntiBot() {
// given
AntiBot antiBot = mock(AntiBot.class);
CommandService service = mock(CommandService.class);
given(service.getAntiBot()).willReturn(antiBot);
CommandSender sender = mock(CommandSender.class);
ExecutableCommand command = new SwitchAntiBotCommand();
// when
command.executeCommand(sender, Collections.singletonList("Off"), service);
@@ -79,15 +80,9 @@ public class SwitchAntiBotCommandTest {
public void shouldShowHelpForUnknownState() {
// given
CommandSender sender = mock(CommandSender.class);
AntiBot antiBot = mock(AntiBot.class);
FoundCommandResult foundCommandResult = mock(FoundCommandResult.class);
CommandService service = mock(CommandService.class);
given(service.getAntiBot()).willReturn(antiBot);
given(service.mapPartsToCommand(sender, asList("authme", "antibot"))).willReturn(foundCommandResult);
ExecutableCommand command = new SwitchAntiBotCommand();
// when
command.executeCommand(sender, Collections.singletonList("wrong"), service);
@@ -12,7 +12,11 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.ArrayList;
import java.util.Arrays;
@@ -33,26 +37,31 @@ import static org.mockito.Mockito.when;
/**
* Test for {@link ChangePasswordCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public class ChangePasswordCommandTest {
@InjectMocks
private ChangePasswordCommand command;
@Mock
private PlayerCache playerCache;
@Mock
private CommandService commandService;
@Before
public void setUpMocks() {
commandService = mock(CommandService.class);
public void setSettings() {
when(commandService.getProperty(SecuritySettings.MIN_PASSWORD_LENGTH)).thenReturn(2);
when(commandService.getProperty(SecuritySettings.MAX_PASSWORD_LENGTH)).thenReturn(50);
// Only allow passwords with alphanumerical characters for the test
when(commandService.getProperty(RestrictionSettings.ALLOWED_PASSWORD_REGEX)).thenReturn("[a-zA-Z0-9]+");
when(commandService.getProperty(SecuritySettings.UNSAFE_PASSWORDS)).thenReturn(Collections.<String> emptyList());
when(commandService.getProperty(SecuritySettings.UNSAFE_PASSWORDS)).thenReturn(Collections.<String>emptyList());
}
@Test
public void shouldRejectNonPlayerSender() {
// given
CommandSender sender = mock(BlockCommandSender.class);
ChangePasswordCommand command = new ChangePasswordCommand();
// when
command.executeCommand(sender, new ArrayList<String>(), commandService);
@@ -65,7 +74,6 @@ public class ChangePasswordCommandTest {
public void shouldRejectNotLoggedInPlayer() {
// given
CommandSender sender = initPlayerWithName("name", false);
ChangePasswordCommand command = new ChangePasswordCommand();
// when
command.executeCommand(sender, Arrays.asList("pass", "pass"), commandService);
@@ -78,7 +86,6 @@ public class ChangePasswordCommandTest {
public void shouldRejectInvalidPassword() {
// given
CommandSender sender = initPlayerWithName("abc12", true);
ChangePasswordCommand command = new ChangePasswordCommand();
String password = "newPW";
given(commandService.validatePassword(password, "abc12")).willReturn(MessageKey.INVALID_PASSWORD_LENGTH);
@@ -94,7 +101,6 @@ public class ChangePasswordCommandTest {
public void shouldForwardTheDataForValidPassword() {
// given
CommandSender sender = initPlayerWithName("parker", true);
ChangePasswordCommand command = new ChangePasswordCommand();
// when
command.executeCommand(sender, Arrays.asList("abc123", "abc123"), commandService);
@@ -112,9 +118,7 @@ public class ChangePasswordCommandTest {
private Player initPlayerWithName(String name, boolean loggedIn) {
Player player = mock(Player.class);
when(player.getName()).thenReturn(name);
PlayerCache playerCache = mock(PlayerCache.class);
when(playerCache.isAuthenticated(name)).thenReturn(loggedIn);
when(commandService.getPlayerCache()).thenReturn(playerCache);
return player;
}