Fix RegisterAdminCommand test verifying that online player is kicked

This commit is contained in:
ljacqu 2016-05-02 19:57:54 +02:00
parent f76a68cb26
commit 491dc06de4
2 changed files with 21 additions and 4 deletions

View File

@ -63,13 +63,12 @@ public class RegisterAdminCommand implements ExecutableCommand {
commandService.send(sender, MessageKey.REGISTER_SUCCESS);
ConsoleLogger.info(sender.getName() + " registered " + playerName);
Player player = commandService.getPlayer(playerName);
final Player player = commandService.getPlayer(playerName);
if (player != null) {
final Player p = player;
p.getServer().getScheduler().scheduleSyncDelayedTask(commandService.getAuthMe(), new Runnable() {
commandService.getBukkitService().scheduleSyncDelayedTask(new Runnable() {
@Override
public void run() {
p.kickPlayer("An admin just registered you, please log in again");
player.kickPlayer("An admin just registered you, please log in again");
}
});
}

View File

@ -7,7 +7,9 @@ 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 fr.xephi.authme.util.BukkitService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -18,10 +20,13 @@ 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;
@ -143,10 +148,15 @@ public class RegisterAdminCommandTest {
given(dataSource.saveAuth(any(PlayerAuth.class))).willReturn(true);
HashedPassword hashedPassword = new HashedPassword("$aea2345EW235dfsa@#R%987048");
given(passwordSecurity.computeHash(password, user)).willReturn(hashedPassword);
Player player = mock(Player.class);
given(commandService.getPlayer(user)).willReturn(player);
BukkitService bukkitService = mock(BukkitService.class);
given(commandService.getBukkitService()).willReturn(bukkitService);
// when
command.executeCommand(sender, Arrays.asList(user, password), commandService);
TestHelper.runInnerRunnable(commandService);
runSyncDelayedTask(bukkitService);
// then
verify(commandService).validatePassword(password, user);
@ -155,6 +165,7 @@ public class RegisterAdminCommandTest {
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) {
@ -162,4 +173,11 @@ public class RegisterAdminCommandTest {
assertThat(auth.getNickname(), equalTo(name.toLowerCase()));
assertThat(auth.getPassword(), equalTo(hashedPassword));
}
private static void runSyncDelayedTask(BukkitService bukkitService) {
ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class);
verify(bukkitService).scheduleSyncDelayedTask(captor.capture());
Runnable runnable = captor.getValue();
runnable.run();
}
}