Merge branch 'master' of https://github.com/AuthMe/AuthMeReloaded into 1128-camel-case-rename

This commit is contained in:
ljacqu
2017-03-17 18:50:57 +01:00
203 changed files with 5111 additions and 1980 deletions
@@ -9,10 +9,14 @@ import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.scheduler.BukkitTask;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collection;
@@ -20,9 +24,13 @@ import java.util.Collection;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
/**
* Test for {@link BukkitService}.
@@ -38,10 +46,13 @@ public class BukkitServiceTest {
private Settings settings;
@Mock
private Server server;
@Mock
private BukkitScheduler scheduler;
@Before
public void constructBukkitService() {
ReflectionTestUtils.setField(Bukkit.class, null, "server", server);
given(server.getScheduler()).willReturn(scheduler);
given(settings.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(true);
bukkitService = new BukkitService(authMe, settings);
}
@@ -101,6 +112,191 @@ public class BukkitServiceTest {
verify(server).dispatchCommand(consoleSender, command);
}
@Test
public void shouldScheduleSyncDelayedTask() {
// given
Runnable task = () -> {/* noop */};
given(scheduler.scheduleSyncDelayedTask(authMe, task)).willReturn(123);
// when
int taskId = bukkitService.scheduleSyncDelayedTask(task);
// then
verify(scheduler, only()).scheduleSyncDelayedTask(authMe, task);
assertThat(taskId, equalTo(123));
}
@Test
public void shouldScheduleSyncDelayedTaskWithDelay() {
// given
Runnable task = () -> {/* noop */};
int delay = 3;
given(scheduler.scheduleSyncDelayedTask(authMe, task, delay)).willReturn(44);
// when
int taskId = bukkitService.scheduleSyncDelayedTask(task, delay);
// then
verify(scheduler, only()).scheduleSyncDelayedTask(authMe, task, delay);
assertThat(taskId, equalTo(44));
}
@Test
public void shouldScheduleSyncTask() {
// given
BukkitService spy = Mockito.spy(bukkitService);
doReturn(1).when(spy).scheduleSyncDelayedTask(any(Runnable.class));
Runnable task = mock(Runnable.class);
// when
spy.scheduleSyncTaskFromOptionallyAsyncTask(task);
// then
verify(spy).scheduleSyncDelayedTask(task);
verifyZeroInteractions(task);
}
@Test
public void shouldRunTaskDirectly() {
// given
given(settings.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(false);
bukkitService.reload(settings);
BukkitService spy = Mockito.spy(bukkitService);
Runnable task = mock(Runnable.class);
// when
spy.scheduleSyncTaskFromOptionallyAsyncTask(task);
// then
verify(task).run();
verify(spy, only()).scheduleSyncTaskFromOptionallyAsyncTask(task);
}
@Test
public void shouldRunTask() {
// given
Runnable task = () -> {/* noop */};
BukkitTask bukkitTask = mock(BukkitTask.class);
given(scheduler.runTask(authMe, task)).willReturn(bukkitTask);
// when
BukkitTask resultingTask = bukkitService.runTask(task);
// then
assertThat(resultingTask, equalTo(bukkitTask));
verify(scheduler, only()).runTask(authMe, task);
}
@Test
public void shouldRunTaskLater() {
// given
Runnable task = () -> {/* noop */};
BukkitTask bukkitTask = mock(BukkitTask.class);
long delay = 400;
given(scheduler.runTaskLater(authMe, task, delay)).willReturn(bukkitTask);
// when
BukkitTask resultingTask = bukkitService.runTaskLater(task, delay);
// then
assertThat(resultingTask, equalTo(bukkitTask));
verify(scheduler, only()).runTaskLater(authMe, task, delay);
}
@Test
public void shouldRunTaskInAsync() {
// given
Runnable task = mock(Runnable.class);
BukkitService spy = Mockito.spy(bukkitService);
doReturn(null).when(spy).runTaskAsynchronously(task);
// when
spy.runTaskOptionallyAsync(task);
// then
verifyZeroInteractions(task);
verify(spy).runTaskAsynchronously(task);
}
@Test
public void shouldRunTaskDirectlyIfConfigured() {
// given
given(settings.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(false);
bukkitService.reload(settings);
BukkitService spy = Mockito.spy(bukkitService);
Runnable task = mock(Runnable.class);
// when
spy.runTaskOptionallyAsync(task);
// then
verify(task).run();
verify(spy, only()).runTaskOptionallyAsync(task);
}
@Test
public void shouldRunTaskAsynchronously() {
// given
Runnable task = () -> {/* noop */};
BukkitTask bukkitTask = mock(BukkitTask.class);
given(scheduler.runTaskAsynchronously(authMe, task)).willReturn(bukkitTask);
// when
BukkitTask resultingTask = bukkitService.runTaskAsynchronously(task);
// then
assertThat(resultingTask, equalTo(bukkitTask));
verify(scheduler, only()).runTaskAsynchronously(authMe, task);
}
@Test
public void shouldRunTaskTimerAsynchronously() {
// given
Runnable task = () -> {/* */};
long delay = 20L;
long period = 4000L;
BukkitTask bukkitTask = mock(BukkitTask.class);
given(scheduler.runTaskTimerAsynchronously(authMe, task, delay, period)).willReturn(bukkitTask);
// when
BukkitTask resultingTask = bukkitService.runTaskTimerAsynchronously(task, delay, period);
// then
assertThat(resultingTask, equalTo(bukkitTask));
verify(scheduler).runTaskTimerAsynchronously(authMe, task, delay, period);
}
@Test
public void shouldRunTaskTimer() {
// given
BukkitRunnable bukkitRunnable = mock(BukkitRunnable.class);
long delay = 20;
long period = 80;
BukkitTask bukkitTask = mock(BukkitTask.class);
given(bukkitRunnable.runTaskTimer(authMe, delay, period)).willReturn(bukkitTask);
// when
BukkitTask result = bukkitService.runTaskTimer(bukkitRunnable, delay, period);
// then
assertThat(result, equalTo(bukkitTask));
verify(bukkitRunnable).runTaskTimer(authMe, delay, period);
}
@Test
public void shouldBroadcastMessage() {
// given
String message = "Important message to all";
given(server.broadcastMessage(message)).willReturn(24);
// when
int result = bukkitService.broadcastMessage(message);
// then
assertThat(result, equalTo(24));
verify(server).broadcastMessage(message);
}
// Note: This method is used through reflections
public static Player[] onlinePlayersImpl() {
return new Player[]{
@@ -84,21 +84,6 @@ public class CommonServiceTest {
verify(messages).send(sender, key, replacements);
}
@Test
public void shouldRetrieveMessage() {
// given
MessageKey key = MessageKey.ACCOUNT_NOT_ACTIVATED;
String[] lines = new String[]{"First message line", "second line"};
given(messages.retrieve(key)).willReturn(lines);
// when
String[] result = commonService.retrieveMessage(key);
// then
assertThat(result, equalTo(lines));
verify(messages).retrieve(key);
}
@Test
public void shouldRetrieveSingleMessage() {
// given
@@ -134,13 +119,11 @@ public class CommonServiceTest {
// given
Player player = mock(Player.class);
AuthGroupType type = AuthGroupType.LOGGED_IN;
given(authGroupHandler.setGroup(player, type)).willReturn(true);
// when
boolean result = commonService.setGroup(player, type);
commonService.setGroup(player, type);
// then
verify(authGroupHandler).setGroup(player, type);
assertThat(result, equalTo(true));
}
}
@@ -58,7 +58,7 @@ public class PluginHookServiceTest {
assertThat(pluginHookService.isEssentialsAvailable(), equalTo(true));
}
// Note ljacqu 20160312: Cannot test with Multiverse or CombatTagPlus because their classes are declared final
// Note ljacqu 20160312: Cannot test with CombatTagPlus because its class is declared final
@Test
public void shouldHookIntoEssentialsAtInitialization() {
@@ -4,15 +4,13 @@ import ch.jalu.injector.testing.BeforeInjecting;
import ch.jalu.injector.testing.DelayedInjectionRunner;
import ch.jalu.injector.testing.InjectDelayed;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.service.RecoveryCodeService.ExpiringEntry;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.expiring.ExpiringMap;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import java.util.Map;
import static fr.xephi.authme.AuthMeMatchers.stringWithLength;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
@@ -60,22 +58,8 @@ public class RecoveryCodeServiceTest {
recoveryCodeService.generateCode(name);
// then
ExpiringEntry entry = getCodeMap().get(name);
assertThat(entry.getCode(), stringWithLength(5));
}
@Test
public void shouldNotConsiderExpiredCode() {
// given
String player = "Cat";
String code = "11F235";
setCodeInMap(player, code, System.currentTimeMillis() - 500);
// when
boolean result = recoveryCodeService.isCodeValid(player, code);
// then
assertThat(result, equalTo(false));
String code = getCodeMap().get(name);
assertThat(code, stringWithLength(5));
}
@Test
@@ -106,12 +90,7 @@ public class RecoveryCodeServiceTest {
}
private Map<String, ExpiringEntry> getCodeMap() {
private ExpiringMap<String, String> getCodeMap() {
return ReflectionTestUtils.getFieldValue(RecoveryCodeService.class, recoveryCodeService, "recoveryCodes");
}
private void setCodeInMap(String player, String code, long expiration) {
Map<String, ExpiringEntry> map = getCodeMap();
map.put(player, new ExpiringEntry(code, expiration));
}
}
@@ -4,24 +4,30 @@ import ch.jalu.injector.testing.BeforeInjecting;
import ch.jalu.injector.testing.DelayedInjectionRunner;
import ch.jalu.injector.testing.InjectDelayed;
import com.google.common.base.Strings;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PlayerStatePermission;
import fr.xephi.authme.service.ValidationService.ValidationResult;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.settings.properties.ProtectionSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.service.ValidationService.ValidationResult;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import java.util.Arrays;
import java.util.Collections;
import java.util.logging.Logger;
import static java.util.Arrays.asList;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
@@ -55,6 +61,7 @@ public class ValidationServiceTest {
.willReturn(asList("unsafe", "other-unsafe"));
given(settings.getProperty(EmailSettings.MAX_REG_PER_EMAIL)).willReturn(3);
given(settings.getProperty(RestrictionSettings.UNRESTRICTED_NAMES)).willReturn(asList("name01", "npc"));
given(settings.getProperty(RestrictionSettings.ENABLE_RESTRICTED_USERS)).willReturn(false);
}
@Test
@@ -115,8 +122,8 @@ public class ValidationServiceTest {
@Test
public void shouldAcceptEmailWithEmptyLists() {
// given
given(settings.getProperty(EmailSettings.DOMAIN_WHITELIST)).willReturn(Collections.<String>emptyList());
given(settings.getProperty(EmailSettings.DOMAIN_BLACKLIST)).willReturn(Collections.<String>emptyList());
given(settings.getProperty(EmailSettings.DOMAIN_WHITELIST)).willReturn(Collections.emptyList());
given(settings.getProperty(EmailSettings.DOMAIN_BLACKLIST)).willReturn(Collections.emptyList());
// when
boolean result = validationService.validateEmail("test@example.org");
@@ -130,7 +137,7 @@ public class ValidationServiceTest {
// given
given(settings.getProperty(EmailSettings.DOMAIN_WHITELIST))
.willReturn(asList("domain.tld", "example.com"));
given(settings.getProperty(EmailSettings.DOMAIN_BLACKLIST)).willReturn(Collections.<String>emptyList());
given(settings.getProperty(EmailSettings.DOMAIN_BLACKLIST)).willReturn(Collections.emptyList());
// when
boolean result = validationService.validateEmail("TesT@Example.com");
@@ -144,7 +151,7 @@ public class ValidationServiceTest {
// given
given(settings.getProperty(EmailSettings.DOMAIN_WHITELIST))
.willReturn(asList("domain.tld", "example.com"));
given(settings.getProperty(EmailSettings.DOMAIN_BLACKLIST)).willReturn(Collections.<String>emptyList());
given(settings.getProperty(EmailSettings.DOMAIN_BLACKLIST)).willReturn(Collections.emptyList());
// when
boolean result = validationService.validateEmail("email@other-domain.abc");
@@ -156,7 +163,7 @@ public class ValidationServiceTest {
@Test
public void shouldAcceptEmailNotInBlacklist() {
// given
given(settings.getProperty(EmailSettings.DOMAIN_WHITELIST)).willReturn(Collections.<String>emptyList());
given(settings.getProperty(EmailSettings.DOMAIN_WHITELIST)).willReturn(Collections.emptyList());
given(settings.getProperty(EmailSettings.DOMAIN_BLACKLIST))
.willReturn(asList("Example.org", "a-test-name.tld"));
@@ -170,7 +177,7 @@ public class ValidationServiceTest {
@Test
public void shouldRejectEmailInBlacklist() {
// given
given(settings.getProperty(EmailSettings.DOMAIN_WHITELIST)).willReturn(Collections.<String>emptyList());
given(settings.getProperty(EmailSettings.DOMAIN_WHITELIST)).willReturn(Collections.emptyList());
given(settings.getProperty(EmailSettings.DOMAIN_BLACKLIST))
.willReturn(asList("Example.org", "a-test-name.tld"));
@@ -263,8 +270,8 @@ public class ValidationServiceTest {
@Test
public void shouldNotInvokeGeoLiteApiIfCountryListsAreEmpty() {
// given
given(settings.getProperty(ProtectionSettings.COUNTRIES_WHITELIST)).willReturn(Collections.<String>emptyList());
given(settings.getProperty(ProtectionSettings.COUNTRIES_BLACKLIST)).willReturn(Collections.<String>emptyList());
given(settings.getProperty(ProtectionSettings.COUNTRIES_WHITELIST)).willReturn(Collections.emptyList());
given(settings.getProperty(ProtectionSettings.COUNTRIES_BLACKLIST)).willReturn(Collections.emptyList());
// when
boolean result = validationService.isCountryAdmitted("addr");
@@ -278,7 +285,7 @@ public class ValidationServiceTest {
public void shouldAcceptCountryInWhitelist() {
// given
given(settings.getProperty(ProtectionSettings.COUNTRIES_WHITELIST)).willReturn(asList("ch", "it"));
given(settings.getProperty(ProtectionSettings.COUNTRIES_BLACKLIST)).willReturn(Collections.<String>emptyList());
given(settings.getProperty(ProtectionSettings.COUNTRIES_BLACKLIST)).willReturn(Collections.emptyList());
String ip = "127.0.0.1";
given(geoIpService.getCountryCode(ip)).willReturn("CH");
@@ -294,7 +301,7 @@ public class ValidationServiceTest {
public void shouldRejectCountryMissingFromWhitelist() {
// given
given(settings.getProperty(ProtectionSettings.COUNTRIES_WHITELIST)).willReturn(asList("ch", "it"));
given(settings.getProperty(ProtectionSettings.COUNTRIES_BLACKLIST)).willReturn(Collections.<String>emptyList());
given(settings.getProperty(ProtectionSettings.COUNTRIES_BLACKLIST)).willReturn(Collections.emptyList());
String ip = "123.45.67.89";
given(geoIpService.getCountryCode(ip)).willReturn("BR");
@@ -309,7 +316,7 @@ public class ValidationServiceTest {
@Test
public void shouldAcceptCountryAbsentFromBlacklist() {
// given
given(settings.getProperty(ProtectionSettings.COUNTRIES_WHITELIST)).willReturn(Collections.<String>emptyList());
given(settings.getProperty(ProtectionSettings.COUNTRIES_WHITELIST)).willReturn(Collections.emptyList());
given(settings.getProperty(ProtectionSettings.COUNTRIES_BLACKLIST)).willReturn(asList("ch", "it"));
String ip = "127.0.0.1";
given(geoIpService.getCountryCode(ip)).willReturn("BR");
@@ -325,7 +332,7 @@ public class ValidationServiceTest {
@Test
public void shouldRejectCountryInBlacklist() {
// given
given(settings.getProperty(ProtectionSettings.COUNTRIES_WHITELIST)).willReturn(Collections.<String>emptyList());
given(settings.getProperty(ProtectionSettings.COUNTRIES_WHITELIST)).willReturn(Collections.emptyList());
given(settings.getProperty(ProtectionSettings.COUNTRIES_BLACKLIST)).willReturn(asList("ch", "it"));
String ip = "123.45.67.89";
given(geoIpService.getCountryCode(ip)).willReturn("IT");
@@ -338,6 +345,54 @@ public class ValidationServiceTest {
verify(geoIpService).getCountryCode(ip);
}
@Test
public void shouldCheckNameRestrictions() {
// given
given(settings.getProperty(RestrictionSettings.ENABLE_RESTRICTED_USERS)).willReturn(true);
given(settings.getProperty(RestrictionSettings.RESTRICTED_USERS))
.willReturn(Arrays.asList("Bobby;127.0.0.4", "Tamara;32.24.16.8"));
validationService.reload();
Player bobby = mockPlayer("bobby", "127.0.0.4");
Player tamara = mockPlayer("taMARA", "8.8.8.8");
Player notRestricted = mockPlayer("notRestricted", "0.0.0.0");
// when
boolean isBobbyAdmitted = validationService.fulfillsNameRestrictions(bobby);
boolean isTamaraAdmitted = validationService.fulfillsNameRestrictions(tamara);
boolean isNotRestrictedAdmitted = validationService.fulfillsNameRestrictions(notRestricted);
// then
assertThat(isBobbyAdmitted, equalTo(true));
assertThat(isTamaraAdmitted, equalTo(false));
assertThat(isNotRestrictedAdmitted, equalTo(true));
}
@Test
public void shouldLogWarningForInvalidRestrictionRule() {
// given
Logger logger = TestHelper.setupLogger();
given(settings.getProperty(RestrictionSettings.ENABLE_RESTRICTED_USERS)).willReturn(true);
given(settings.getProperty(RestrictionSettings.RESTRICTED_USERS))
.willReturn(Arrays.asList("Bobby;127.0.0.4", "Tamara;"));
// when
validationService.reload();
// then
ArgumentCaptor<String> stringCaptor = ArgumentCaptor.forClass(String.class);
verify(logger).warning(stringCaptor.capture());
assertThat(stringCaptor.getValue(), containsString("Tamara;"));
}
private static Player mockPlayer(String name, String ip) {
Player player = mock(Player.class);
given(player.getName()).willReturn(name);
TestHelper.mockPlayerIp(player, ip);
given(player.getAddress().getHostName()).willReturn("--");
return player;
}
private static void assertErrorEquals(ValidationResult validationResult, MessageKey messageKey, String... args) {
assertThat(validationResult.hasError(), equalTo(true));
assertThat(validationResult.getMessageKey(), equalTo(messageKey));