Merge enum branch into master

This commit is contained in:
ljacqu
2015-11-26 21:45:06 +01:00
21 changed files with 537 additions and 155 deletions
@@ -0,0 +1,41 @@
package fr.xephi.authme.command;
import org.junit.Test;
import java.util.List;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
/**
* Test for {@link CommandManager}.
*/
public class CommandManagerTest {
@Test
public void shouldInitializeCommands() {
// given/when
CommandManager manager = new CommandManager(true);
int commandCount = manager.getCommandDescriptionCount();
List<CommandDescription> commands = manager.getCommandDescriptions();
// then
// It obviously doesn't make sense to test much of the concrete data
// that is being initialized; we just want to guarantee with this test
// that data is indeed being initialized and we take a few "probes"
assertThat(commandCount, equalTo(9));
assertThat(commandsIncludeLabel(commands, "authme"), equalTo(true));
assertThat(commandsIncludeLabel(commands, "register"), equalTo(true));
assertThat(commandsIncludeLabel(commands, "help"), equalTo(false));
}
private static boolean commandsIncludeLabel(Iterable<CommandDescription> commands, String label) {
for (CommandDescription command : commands) {
if (command.getLabels().contains(label)) {
return true;
}
}
return false;
}
}
@@ -0,0 +1,166 @@
package fr.xephi.authme.command.executable.changepassword;
import fr.xephi.authme.AuthMeMockUtil;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.command.CommandParts;
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 java.util.Arrays;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* Test for {@link ChangePasswordCommand}.
*/
public class ChangePasswordCommandTest {
private Messages messagesMock;
private PlayerCache cacheMock;
@Before
public void setUpMocks() {
AuthMeMockUtil.mockAuthMeInstance();
AuthMeMockUtil.mockPlayerCacheInstance();
cacheMock = PlayerCache.getInstance();
AuthMeMockUtil.mockMessagesInstance();
messagesMock = Messages.getInstance();
// Only allow passwords with alphanumerical characters for the test
Settings.getPassRegex = "[a-zA-Z0-9]+";
Settings.getPasswordMinLen = 2;
Settings.passwordMaxLength = 50;
// TODO ljacqu 20151121: Cannot mock getServer() as it's final
// Probably the Command class should delegate as the others do
/*
Server server = Mockito.mock(Server.class);
schedulerMock = Mockito.mock(BukkitScheduler.class);
Mockito.when(server.getScheduler()).thenReturn(schedulerMock);
Mockito.when(pluginMock.getServer()).thenReturn(server);
*/
}
@Test
public void shouldRejectNonPlayerSender() {
// given
CommandSender sender = mock(BlockCommandSender.class);
ChangePasswordCommand command = new ChangePasswordCommand();
CommandParts arguments = mock(CommandParts.class);
// when
command.executeCommand(sender, new CommandParts(), arguments);
// then
verify(arguments, never()).get(anyInt());
//verify(pluginMock, never()).getServer();
}
@Test
public void shouldRejectNotLoggedInPlayer() {
// given
CommandSender sender = initPlayerWithName("name", false);
ChangePasswordCommand command = new ChangePasswordCommand();
// when
command.executeCommand(sender, new CommandParts(), new CommandParts("pass"));
// then
verify(messagesMock).send(sender, "not_logged_in");
//verify(pluginMock, never()).getServer();
}
@Test
public void shouldDenyInvalidPassword() {
// given
CommandSender sender = initPlayerWithName("name", true);
ChangePasswordCommand command = new ChangePasswordCommand();
// when
command.executeCommand(sender, new CommandParts(), new CommandParts("!pass"));
// then
verify(messagesMock).send(sender, "password_error");
//verify(pluginMock, never()).getServer();
}
@Test
public void shouldRejectPasswordEqualToNick() {
// given
CommandSender sender = initPlayerWithName("tester", true);
ChangePasswordCommand command = new ChangePasswordCommand();
// when
command.executeCommand(sender, new CommandParts(), new CommandParts("Tester"));
// then
verify(messagesMock).send(sender, "password_error_nick");
//verify(pluginMock, never()).getServer();
}
@Test
public void shouldRejectTooLongPassword() {
// given
CommandSender sender = initPlayerWithName("abc12", true);
ChangePasswordCommand command = new ChangePasswordCommand();
Settings.passwordMaxLength = 3;
// when
command.executeCommand(sender, new CommandParts(), new CommandParts("test"));
// then
verify(messagesMock).send(sender, "pass_len");
//verify(pluginMock, never()).getServer();
}
@Test
public void shouldRejectTooShortPassword() {
// given
CommandSender sender = initPlayerWithName("abc12", true);
ChangePasswordCommand command = new ChangePasswordCommand();
Settings.getPasswordMinLen = 7;
// when
command.executeCommand(sender, new CommandParts(), new CommandParts("tester"));
// then
verify(messagesMock).send(sender, "pass_len");
//verify(pluginMock, never()).getServer();
}
@Test
public void shouldRejectUnsafeCustomPassword() {
// given
CommandSender sender = initPlayerWithName("player", true);
ChangePasswordCommand command = new ChangePasswordCommand();
Settings.unsafePasswords = Arrays.asList("test", "abc123");
// when
command.executeCommand(sender, new CommandParts(), new CommandParts("abc123"));
// then
verify(messagesMock).send(sender, "password_error_unsafe");
//verify(pluginMock, never()).getServer();
}
private Player initPlayerWithName(String name, boolean loggedIn) {
Player player = mock(Player.class);
when(player.getName()).thenReturn(name);
when(cacheMock.isAuthenticated(name)).thenReturn(loggedIn);
return player;
}
}
@@ -0,0 +1,63 @@
package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.AuthMeMockUtil;
import fr.xephi.authme.command.CommandParts;
import fr.xephi.authme.process.Management;
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 java.util.Arrays;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* Test for {@link AddEmailCommand}.
*/
public class AddEmailCommandTest {
private AuthMe authMeMock;
private Management managementMock;
@Before
public void setUpMocks() {
AuthMeMockUtil.mockAuthMeInstance();
authMeMock = AuthMe.getInstance();
managementMock = Mockito.mock(Management.class);
when(authMeMock.getManagement()).thenReturn(managementMock);
}
@Test
public void shouldRejectNonPlayerSender() {
// given
CommandSender sender = Mockito.mock(BlockCommandSender.class);
AddEmailCommand command = new AddEmailCommand();
// when
command.executeCommand(sender, new CommandParts(), new CommandParts());
// then
verify(authMeMock, never()).getManagement();
}
@Test
public void shouldForwardData() {
// given
Player sender = Mockito.mock(Player.class);
AddEmailCommand command = new AddEmailCommand();
// when
command.executeCommand(sender, new CommandParts(),
new CommandParts(Arrays.asList("mail@example", "other_example")));
// then
verify(authMeMock).getManagement();
verify(managementMock).performAddEmail(sender, "mail@example", "other_example");
}
}
@@ -0,0 +1,63 @@
package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.AuthMeMockUtil;
import fr.xephi.authme.command.CommandParts;
import fr.xephi.authme.process.Management;
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 java.util.Arrays;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* Test for {@link ChangeEmailCommand}.
*/
public class ChangeEmailCommandTest {
private AuthMe authMeMock;
private Management managementMock;
@Before
public void setUpMocks() {
AuthMeMockUtil.mockAuthMeInstance();
authMeMock = AuthMe.getInstance();
managementMock = Mockito.mock(Management.class);
when(authMeMock.getManagement()).thenReturn(managementMock);
}
@Test
public void shouldRejectNonPlayerSender() {
// given
CommandSender sender = Mockito.mock(BlockCommandSender.class);
ChangeEmailCommand command = new ChangeEmailCommand();
// when
command.executeCommand(sender, new CommandParts(), new CommandParts());
// then
verify(authMeMock, never()).getManagement();
}
@Test
public void shouldForwardData() {
// given
Player sender = Mockito.mock(Player.class);
ChangeEmailCommand command = new ChangeEmailCommand();
// when
command.executeCommand(sender, new CommandParts(),
new CommandParts(Arrays.asList("new.mail@example.org", "old_mail@example.org")));
// then
verify(authMeMock).getManagement();
verify(managementMock).performChangeEmail(sender, "new.mail@example.org", "old_mail@example.org");
}
}
@@ -0,0 +1,36 @@
package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.AuthMeMockUtil;
import fr.xephi.authme.command.CommandParts;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
/**
* Test for {@link RecoverEmailCommand}.
*/
public class RecoverEmailCommandTest {
@Before
public void setUpMocks() {
AuthMeMockUtil.mockAuthMeInstance();
}
@Test
public void shouldRejectNonPlayerSender() {
// given
CommandSender sender = Mockito.mock(BlockCommandSender.class);
RecoverEmailCommand command = new RecoverEmailCommand();
// when
command.executeCommand(sender, new CommandParts(), new CommandParts());
// then
}
// TODO ljacqu 20151121: Expand tests. This command doesn't use a scheduler and has all of its
// logic inside here.
}