#1141 Rough version of TOTP commands to add and remove a code for a player

This commit is contained in:
ljacqu
2018-03-07 20:11:53 +01:00
parent 9954c82cb6
commit c3cf9e3ee0
13 changed files with 416 additions and 12 deletions
@@ -44,7 +44,7 @@ public class CommandInitializerTest {
// 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(commands, hasSize(9));
assertThat(commands, hasSize(10));
assertThat(commandsIncludeLabel(commands, "authme"), equalTo(true));
assertThat(commandsIncludeLabel(commands, "register"), equalTo(true));
assertThat(commandsIncludeLabel(commands, "help"), equalTo(false));
@@ -0,0 +1,51 @@
package fr.xephi.authme.security;
import fr.xephi.authme.security.TotpService.TotpGenerationResult;
import fr.xephi.authme.service.BukkitService;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static fr.xephi.authme.AuthMeMatchers.stringWithLength;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Test for {@link TotpService}.
*/
@RunWith(MockitoJUnitRunner.class)
public class TotpServiceTest {
@InjectMocks
private TotpService totpService;
@Mock
private BukkitService bukkitService;
@Test
public void shouldGenerateTotpKey() {
// given
Player player = mock(Player.class);
given(player.getName()).willReturn("Bobby");
given(bukkitService.getIp()).willReturn("127.48.44.4");
// when
TotpGenerationResult key1 = totpService.generateTotpKey(player);
TotpGenerationResult key2 = totpService.generateTotpKey(player);
// then
assertThat(key1.getTotpKey(), stringWithLength(16));
assertThat(key2.getTotpKey(), stringWithLength(16));
assertThat(key1.getAuthenticatorQrCodeUrl(), startsWith("https://chart.googleapis.com/chart?chs=200x200"));
assertThat(key2.getAuthenticatorQrCodeUrl(), startsWith("https://chart.googleapis.com/chart?chs=200x200"));
assertThat(key1.getTotpKey(), not(equalTo(key2.getTotpKey())));
}
}
@@ -332,6 +332,19 @@ public class BukkitServiceTest {
assertThat(event.getPlayer(), equalTo(player));
}
@Test
public void shouldReturnServerIp() {
// given
String ip = "99.99.99.99";
given(server.getIp()).willReturn(ip);
// when
String result = bukkitService.getIp();
// then
assertThat(result, equalTo(ip));
}
// Note: This method is used through reflections
public static Player[] onlinePlayersImpl() {
return new Player[]{