Make AuthMe.management private; create test for CaptchaCommand

This commit is contained in:
ljacqu 2015-12-02 22:13:43 +01:00
parent f698c9241b
commit 1ca6bcffe1
9 changed files with 86 additions and 13 deletions

View File

@ -73,7 +73,7 @@ public class AuthMe extends JavaPlugin {
private static Server server;
private static Wrapper wrapper = Wrapper.getInstance();
public Management management;
private Management management;
public NewAPI api;
public SendMailSSL mail;
public DataManager dataManager;

View File

@ -170,7 +170,7 @@ public class API {
*/
@Deprecated
public static void forceLogin(Player player) {
instance.management.performLogin(player, "dontneed", true);
instance.getManagement().performLogin(player, "dontneed", true);
}
/**

View File

@ -170,7 +170,7 @@ public class NewAPI {
* @param player * player
*/
public void forceLogin(Player player) {
plugin.management.performLogin(player, "dontneed", true);
plugin.getManagement().performLogin(player, "dontneed", true);
}
/**
@ -179,7 +179,7 @@ public class NewAPI {
* @param player * player
*/
public void forceLogout(Player player) {
plugin.management.performLogout(player);
plugin.getManagement().performLogout(player);
}
/**
@ -189,7 +189,7 @@ public class NewAPI {
* @param password String
*/
public void forceRegister(Player player, String password) {
plugin.management.performRegister(player, password, null);
plugin.getManagement().performRegister(player, password, null);
}
/**
@ -198,6 +198,6 @@ public class NewAPI {
* @param player * player
*/
public void forceUnregister(Player player) {
plugin.management.performUnregister(player, "", true);
plugin.getManagement().performUnregister(player, "", true);
}
}

View File

@ -34,7 +34,7 @@ public class ForceLoginCommand extends ExecutableCommand {
sender.sendMessage("You cannot force login for the player " + playerName + "!");
return true;
}
plugin.management.performLogin(player, "dontneed", true);
plugin.getManagement().performLogin(player, "dontneed", true);
sender.sendMessage("Force Login for " + playerName + " performed!");
} catch (Exception e) {
sender.sendMessage("An error occurred while trying to get that player!");

View File

@ -8,6 +8,7 @@ import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.Wrapper;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -30,10 +31,11 @@ public class CaptchaCommand extends ExecutableCommand {
String captcha = commandArguments.get(0);
// AuthMe plugin instance
final AuthMe plugin = AuthMe.getInstance();
final Wrapper wrapper = Wrapper.getInstance();
final AuthMe plugin = wrapper.getAuthMe();
// Messages instance
final Messages m = plugin.getMessages();
final Messages m = wrapper.getMessages();
// Command logic
if (PlayerCache.getInstance().isAuthenticated(playerNameLowerCase)) {

View File

@ -49,7 +49,7 @@ public class UnregisterCommand extends ExecutableCommand {
}
// Unregister the player
plugin.management.performUnregister(player, playerPass, false);
plugin.getManagement().performUnregister(player, playerPass, false);
return true;
}
}

View File

@ -333,7 +333,7 @@ public class AuthMePlayerListener implements Listener {
event.setQuitMessage(null);
}
plugin.management.performQuit(player, false);
plugin.getManagement().performQuit(player, false);
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
@ -349,7 +349,7 @@ public class AuthMePlayerListener implements Listener {
}
Player player = event.getPlayer();
plugin.management.performQuit(player, true);
plugin.getManagement().performQuit(player, true);
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)

View File

@ -238,7 +238,7 @@ public class AsynchronousJoin {
PlayerCache.getInstance().removePlayer(name);
if (auth != null && auth.getIp().equals(ip)) {
m.send(player, MessageKey.SESSION_RECONNECTION);
plugin.management.performLogin(player, "dontneed", true);
plugin.getManagement().performLogin(player, "dontneed", true);
return;
} else if (Settings.sessionExpireOnIpChange) {
m.send(player, MessageKey.SESSION_EXPIRED);

View File

@ -0,0 +1,71 @@
package fr.xephi.authme.command.executable.captcha;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.CommandParts;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.WrapperMock;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* Test for {@link CaptchaCommand}.
*/
public class CaptchaCommandTest {
private WrapperMock wrapperMock;
@Before
public void setUpWrapperMock() {
wrapperMock = WrapperMock.createInstance();
Settings.useCaptcha = true;
}
@Test
public void shouldRejectNonPlayerSender() {
// given
CommandSender sender = Mockito.mock(BlockCommandSender.class);
ExecutableCommand command = new CaptchaCommand();
// when
boolean result = command.executeCommand(sender, new CommandParts(), new CommandParts());
// then
assertThat(result, equalTo(true));
assertThat(wrapperMock.wasMockCalled(AuthMe.class), equalTo(false));
assertThat(wrapperMock.wasMockCalled(Messages.class), equalTo(false));
}
@Test
@Ignore
public void shouldRejectIfCaptchaIsNotUsed() {
// given
Player player = mockPlayerWithName("testplayer");
ExecutableCommand command = new CaptchaCommand();
// when
boolean result = command.executeCommand(player, new CommandParts(), new CommandParts());
// then
assertThat(result, equalTo(true));
verify(wrapperMock.getMessages()).send(player, MessageKey.USAGE_LOGIN);
}
private static Player mockPlayerWithName(String name) {
Player player = Mockito.mock(Player.class);
when(player.getName()).thenReturn(name);
return player;
}
}