Merge branch 'master' of https://github.com/AuthMe/AuthMeReloaded into 1141-optional-additional-2fa-auth
# Conflicts: # src/main/java/fr/xephi/authme/datasource/MySQL.java
This commit is contained in:
@@ -11,6 +11,7 @@ import java.util.Objects;
|
||||
/**
|
||||
* Custom matchers for AuthMe entities.
|
||||
*/
|
||||
@SuppressWarnings("checkstyle:JavadocMethod") // Justification: Javadoc would be huge because of the many parameters
|
||||
public final class AuthMeMatchers {
|
||||
|
||||
private AuthMeMatchers() {
|
||||
|
||||
@@ -67,7 +67,7 @@ public class ClassCollector {
|
||||
public List<Class<?>> collectClasses(Predicate<Class<?>> filter) {
|
||||
File rootFolder = new File(root);
|
||||
List<Class<?>> collection = new ArrayList<>();
|
||||
collectClasses(rootFolder, filter, collection);
|
||||
gatherClassesFromFile(rootFolder, filter, collection);
|
||||
return collection;
|
||||
}
|
||||
|
||||
@@ -124,14 +124,14 @@ public class ClassCollector {
|
||||
* @param filter the class predicate
|
||||
* @param collection collection to add classes to
|
||||
*/
|
||||
private void collectClasses(File folder, Predicate<Class<?>> filter, List<Class<?>> collection) {
|
||||
private void gatherClassesFromFile(File folder, Predicate<Class<?>> filter, List<Class<?>> collection) {
|
||||
File[] files = folder.listFiles();
|
||||
if (files == null) {
|
||||
throw new IllegalStateException("Could not read files from '" + folder + "'");
|
||||
}
|
||||
for (File file : files) {
|
||||
if (file.isDirectory()) {
|
||||
collectClasses(file, filter, collection);
|
||||
gatherClassesFromFile(file, filter, collection);
|
||||
} else if (file.isFile()) {
|
||||
Class<?> clazz = loadTaskClassFromFile(file);
|
||||
if (clazz != null && filter.test(clazz)) {
|
||||
|
||||
@@ -82,7 +82,6 @@ public final class ReflectionTestUtils {
|
||||
* @param clazz the class to retrieve a method from
|
||||
* @param methodName the name of the method
|
||||
* @param parameterTypes the parameter types the method to retrieve has
|
||||
*
|
||||
* @return the method of the class, set to be accessible
|
||||
*/
|
||||
public static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
|
||||
@@ -96,6 +95,15 @@ public final class ReflectionTestUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes the given method on the provided instance with the given parameters.
|
||||
*
|
||||
* @param method the method to invoke
|
||||
* @param instance the instance to invoke the method on (null for static methods)
|
||||
* @param parameters the parameters to pass to the method
|
||||
* @param <V> return value of the method
|
||||
* @return method return value
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <V> V invokeMethod(Method method, Object instance, Object... parameters) {
|
||||
method.setAccessible(true);
|
||||
|
||||
+1
-1
@@ -160,7 +160,7 @@ public class RegisterAdminCommandTest {
|
||||
Player player = mock(Player.class);
|
||||
given(bukkitService.getPlayerExact(user)).willReturn(player);
|
||||
String kickForAdminRegister = "Admin registered you -- log in again";
|
||||
given(commandService.retrieveSingleMessage(MessageKey.KICK_FOR_ADMIN_REGISTER)).willReturn(kickForAdminRegister);
|
||||
given(commandService.retrieveSingleMessage(player, MessageKey.KICK_FOR_ADMIN_REGISTER)).willReturn(kickForAdminRegister);
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
|
||||
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
|
||||
|
||||
@@ -149,7 +149,7 @@ public class TempbanManagerTest {
|
||||
String ip = "123.45.67.89";
|
||||
TestHelper.mockPlayerIp(player, ip);
|
||||
String banReason = "IP ban too many logins";
|
||||
given(messages.retrieveSingle(MessageKey.TEMPBAN_MAX_LOGINS)).willReturn(banReason);
|
||||
given(messages.retrieveSingle(player, MessageKey.TEMPBAN_MAX_LOGINS)).willReturn(banReason);
|
||||
Settings settings = mockSettings(2, 100, "");
|
||||
TempbanManager manager = new TempbanManager(bukkitService, messages, settings);
|
||||
setBukkitServiceToScheduleSyncDelayedTask(bukkitService);
|
||||
@@ -195,7 +195,7 @@ public class TempbanManagerTest {
|
||||
String ip = "22.44.66.88";
|
||||
TestHelper.mockPlayerIp(player, ip);
|
||||
String banReason = "kick msg";
|
||||
given(messages.retrieveSingle(MessageKey.TEMPBAN_MAX_LOGINS)).willReturn(banReason);
|
||||
given(messages.retrieveSingle(player, MessageKey.TEMPBAN_MAX_LOGINS)).willReturn(banReason);
|
||||
Settings settings = mockSettings(10, 60, "");
|
||||
TempbanManager manager = new TempbanManager(bukkitService, messages, settings);
|
||||
manager.increaseCount(ip, "user");
|
||||
|
||||
@@ -14,6 +14,7 @@ import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
|
||||
/**
|
||||
* Contains matchers for LimboPlayer.
|
||||
*/
|
||||
@SuppressWarnings("checkstyle:JavadocMethod") // Justification: Javadoc would be huge because of the many parameters
|
||||
public final class LimboPlayerMatchers {
|
||||
|
||||
private LimboPlayerMatchers() {
|
||||
@@ -45,7 +46,8 @@ public final class LimboPlayerMatchers {
|
||||
@Override
|
||||
public void describeMismatchSafely(LimboPlayer item, Description description) {
|
||||
description.appendText(format("Limbo with isOp=%s, groups={%s}, canFly=%s, walkSpeed=%f, flySpeed=%f",
|
||||
item.isOperator(), String.join(" ,", item.getGroups()), item.isCanFly(), item.getWalkSpeed(), item.getFlySpeed()));
|
||||
item.isOperator(), String.join(" ,", item.getGroups()), item.isCanFly(),
|
||||
item.getWalkSpeed(), item.getFlySpeed()));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ public class LimboPlayerTaskManagerTest {
|
||||
Player player = mock(Player.class);
|
||||
LimboPlayer limboPlayer = mock(LimboPlayer.class);
|
||||
MessageKey key = MessageKey.REGISTER_MESSAGE;
|
||||
given(messages.retrieveSingle(key)).willReturn("Please register!");
|
||||
given(messages.retrieveSingle(player, key)).willReturn("Please register!");
|
||||
int interval = 12;
|
||||
given(settings.getProperty(RegistrationSettings.MESSAGE_INTERVAL)).willReturn(interval);
|
||||
|
||||
@@ -80,7 +80,7 @@ public class LimboPlayerTaskManagerTest {
|
||||
|
||||
// then
|
||||
verify(limboPlayer).setMessageTask(any(MessageTask.class));
|
||||
verify(messages).retrieveSingle(key);
|
||||
verify(messages).retrieveSingle(player, key);
|
||||
verify(bukkitService).runTaskTimer(
|
||||
any(MessageTask.class), eq(2L * TICKS_PER_SECOND), eq((long) interval * TICKS_PER_SECOND));
|
||||
}
|
||||
@@ -110,7 +110,7 @@ public class LimboPlayerTaskManagerTest {
|
||||
MessageTask existingMessageTask = mock(MessageTask.class);
|
||||
limboPlayer.setMessageTask(existingMessageTask);
|
||||
given(settings.getProperty(RegistrationSettings.MESSAGE_INTERVAL)).willReturn(8);
|
||||
given(messages.retrieveSingle(MessageKey.REGISTER_MESSAGE)).willReturn("Please register!");
|
||||
given(messages.retrieveSingle(player, MessageKey.REGISTER_MESSAGE)).willReturn("Please register!");
|
||||
|
||||
// when
|
||||
limboPlayerTaskManager.registerMessageTask(player, limboPlayer, false);
|
||||
@@ -119,7 +119,7 @@ public class LimboPlayerTaskManagerTest {
|
||||
assertThat(limboPlayer.getMessageTask(), not(nullValue()));
|
||||
assertThat(limboPlayer.getMessageTask(), not(sameInstance(existingMessageTask)));
|
||||
verify(registrationCaptchaManager).isCaptchaRequired(name);
|
||||
verify(messages).retrieveSingle(MessageKey.REGISTER_MESSAGE);
|
||||
verify(messages).retrieveSingle(player, MessageKey.REGISTER_MESSAGE);
|
||||
verify(existingMessageTask).cancel();
|
||||
}
|
||||
|
||||
@@ -134,14 +134,14 @@ public class LimboPlayerTaskManagerTest {
|
||||
given(registrationCaptchaManager.isCaptchaRequired(name)).willReturn(true);
|
||||
String captcha = "M032";
|
||||
given(registrationCaptchaManager.getCaptchaCodeOrGenerateNew(name)).willReturn(captcha);
|
||||
given(messages.retrieveSingle(MessageKey.CAPTCHA_FOR_REGISTRATION_REQUIRED, captcha)).willReturn("Need to use captcha");
|
||||
given(messages.retrieveSingle(player, MessageKey.CAPTCHA_FOR_REGISTRATION_REQUIRED, captcha)).willReturn("Need to use captcha");
|
||||
|
||||
// when
|
||||
limboPlayerTaskManager.registerMessageTask(player, limboPlayer, false);
|
||||
|
||||
// then
|
||||
assertThat(limboPlayer.getMessageTask(), not(nullValue()));
|
||||
verify(messages).retrieveSingle(MessageKey.CAPTCHA_FOR_REGISTRATION_REQUIRED, captcha);
|
||||
verify(messages).retrieveSingle(player, MessageKey.CAPTCHA_FOR_REGISTRATION_REQUIRED, captcha);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -159,7 +159,7 @@ public class LimboPlayerTaskManagerTest {
|
||||
// then
|
||||
verify(limboPlayer).setTimeoutTask(bukkitTask);
|
||||
verify(bukkitService).runTaskLater(any(TimeoutTask.class), eq(600L)); // 30 * TICKS_PER_SECOND
|
||||
verify(messages).retrieveSingle(MessageKey.LOGIN_TIMEOUT_ERROR);
|
||||
verify(messages).retrieveSingle(player, MessageKey.LOGIN_TIMEOUT_ERROR);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -194,7 +194,7 @@ public class LimboPlayerTaskManagerTest {
|
||||
verify(existingTask).cancel();
|
||||
assertThat(limboPlayer.getTimeoutTask(), equalTo(bukkitTask));
|
||||
verify(bukkitService).runTaskLater(any(TimeoutTask.class), eq(360L)); // 18 * TICKS_PER_SECOND
|
||||
verify(messages).retrieveSingle(MessageKey.LOGIN_TIMEOUT_ERROR);
|
||||
verify(messages).retrieveSingle(player, MessageKey.LOGIN_TIMEOUT_ERROR);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,6 +27,15 @@ public final class SqlDataSourceTestUtil {
|
||||
return new MySQL(settings, hikariDataSource, extensionsFactory);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a SQLite implementation for testing purposes. Methods are overridden so the
|
||||
* provided connection is never overridden.
|
||||
*
|
||||
* @param settings settings instance
|
||||
* @param dataFolder data folder
|
||||
* @param connection connection to use
|
||||
* @return the created SQLite instance
|
||||
*/
|
||||
public static SQLite createSqlite(Settings settings, File dataFolder, Connection connection) {
|
||||
return new SQLite(settings, dataFolder, connection) {
|
||||
// Override reload() so it doesn't run SQLite#connect, since we're given a specific Connection to use
|
||||
|
||||
@@ -101,7 +101,7 @@ public class OnJoinVerifierTest {
|
||||
event.setResult(PlayerLoginEvent.Result.KICK_FULL);
|
||||
given(permissionsManager.hasPermission(player, PlayerStatePermission.IS_VIP)).willReturn(false);
|
||||
String serverFullMessage = "server is full";
|
||||
given(messages.retrieveSingle(MessageKey.KICK_FULL_SERVER)).willReturn(serverFullMessage);
|
||||
given(messages.retrieveSingle(player, MessageKey.KICK_FULL_SERVER)).willReturn(serverFullMessage);
|
||||
|
||||
// when
|
||||
boolean result = onJoinVerifier.refusePlayerForFullServer(event);
|
||||
@@ -125,7 +125,7 @@ public class OnJoinVerifierTest {
|
||||
given(permissionsManager.hasPermission(onlinePlayers.get(1), PlayerStatePermission.IS_VIP)).willReturn(false);
|
||||
returnOnlineListFromBukkitServer(onlinePlayers);
|
||||
given(server.getMaxPlayers()).willReturn(onlinePlayers.size());
|
||||
given(messages.retrieveSingle(MessageKey.KICK_FOR_VIP)).willReturn("kick for vip");
|
||||
given(messages.retrieveSingle(player, MessageKey.KICK_FOR_VIP)).willReturn("kick for vip");
|
||||
|
||||
// when
|
||||
boolean result = onJoinVerifier.refusePlayerForFullServer(event);
|
||||
@@ -149,7 +149,7 @@ public class OnJoinVerifierTest {
|
||||
given(permissionsManager.hasPermission(onlinePlayers.get(0), PlayerStatePermission.IS_VIP)).willReturn(true);
|
||||
returnOnlineListFromBukkitServer(onlinePlayers);
|
||||
given(server.getMaxPlayers()).willReturn(onlinePlayers.size());
|
||||
given(messages.retrieveSingle(MessageKey.KICK_FULL_SERVER)).willReturn("kick full server");
|
||||
given(messages.retrieveSingle(player, MessageKey.KICK_FULL_SERVER)).willReturn("kick full server");
|
||||
|
||||
// when
|
||||
boolean result = onJoinVerifier.refusePlayerForFullServer(event);
|
||||
|
||||
@@ -596,7 +596,7 @@ public class PlayerListenerTest {
|
||||
MessageKey.INVALID_NAME_CHARACTERS, "[a-z]");
|
||||
doThrow(exception).when(onJoinVerifier).checkIsValidName(name);
|
||||
String message = "Invalid characters!";
|
||||
given(messages.retrieveSingle(exception.getReason(), exception.getArgs())).willReturn(message);
|
||||
given(messages.retrieveSingle(player, exception.getReason(), exception.getArgs())).willReturn(message);
|
||||
|
||||
// when
|
||||
listener.onPlayerLogin(event);
|
||||
|
||||
@@ -81,9 +81,11 @@ public class MessagesIntegrationTest {
|
||||
public void shouldLoadMessageAndSplitAtNewLines() {
|
||||
// given
|
||||
MessageKey key = MessageKey.UNKNOWN_USER;
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
given(sender.getName()).willReturn("Tester");
|
||||
|
||||
// when
|
||||
String[] message = messages.retrieve(key);
|
||||
String[] message = messages.retrieve(key, sender);
|
||||
|
||||
// then
|
||||
String[] lines = new String[]{"We've got", "new lines", "and ' apostrophes"};
|
||||
@@ -94,9 +96,11 @@ public class MessagesIntegrationTest {
|
||||
public void shouldLoadMessageAsStringWithNewLines() {
|
||||
// given
|
||||
MessageKey key = MessageKey.UNKNOWN_USER;
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
given(sender.getName()).willReturn("Tester");
|
||||
|
||||
// when
|
||||
String message = messages.retrieveSingle(key);
|
||||
String message = messages.retrieveSingle(sender, key);
|
||||
|
||||
// then
|
||||
assertThat(message, equalTo("We've got\nnew lines\nand ' apostrophes"));
|
||||
@@ -106,9 +110,11 @@ public class MessagesIntegrationTest {
|
||||
public void shouldFormatColorCodes() {
|
||||
// given
|
||||
MessageKey key = MessageKey.LOGIN_SUCCESS;
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
given(sender.getName()).willReturn("Tester");
|
||||
|
||||
// when
|
||||
String[] message = messages.retrieve(key);
|
||||
String[] message = messages.retrieve(key, sender);
|
||||
|
||||
// then
|
||||
assertThat(message, arrayWithSize(1));
|
||||
@@ -120,6 +126,7 @@ public class MessagesIntegrationTest {
|
||||
// given
|
||||
MessageKey key = MessageKey.EMAIL_ALREADY_USED_ERROR;
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
given(sender.getName()).willReturn("Tester");
|
||||
|
||||
// when
|
||||
messages.send(sender, key);
|
||||
@@ -133,6 +140,8 @@ public class MessagesIntegrationTest {
|
||||
// given
|
||||
MessageKey key = MessageKey.LOGIN_SUCCESS;
|
||||
Player player = Mockito.mock(Player.class);
|
||||
given(player.getName()).willReturn("Tester");
|
||||
given(player.getDisplayName()).willReturn("§cTesty");
|
||||
|
||||
// when
|
||||
messages.send(player, key);
|
||||
@@ -146,6 +155,8 @@ public class MessagesIntegrationTest {
|
||||
// given
|
||||
MessageKey key = MessageKey.UNKNOWN_USER;
|
||||
Player player = Mockito.mock(Player.class);
|
||||
given(player.getName()).willReturn("Tester");
|
||||
given(player.getDisplayName()).willReturn("§cTesty");
|
||||
|
||||
// when
|
||||
messages.send(player, key);
|
||||
@@ -157,11 +168,27 @@ public class MessagesIntegrationTest {
|
||||
assertThat(captor.getAllValues(), contains(lines));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSendMessageToPlayerWithNameReplacement() {
|
||||
// given
|
||||
MessageKey key = MessageKey.REGISTER_MESSAGE;
|
||||
Player player = Mockito.mock(Player.class);
|
||||
given(player.getName()).willReturn("Tester");
|
||||
given(player.getDisplayName()).willReturn("§cTesty");
|
||||
|
||||
// when
|
||||
messages.send(player, key);
|
||||
|
||||
// then
|
||||
verify(player).sendMessage("§3Please Tester, register to the §cTesty§3.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSendMessageToPlayerWithTagReplacement() {
|
||||
// given
|
||||
MessageKey key = MessageKey.CAPTCHA_WRONG_ERROR;
|
||||
CommandSender sender = Mockito.mock(CommandSender.class);
|
||||
given(sender.getName()).willReturn("Tester");
|
||||
|
||||
// when
|
||||
messages.send(sender, key, "1234");
|
||||
@@ -175,6 +202,7 @@ public class MessagesIntegrationTest {
|
||||
// given
|
||||
MessageKey key = MessageKey.CAPTCHA_WRONG_ERROR;
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
given(sender.getName()).willReturn("Tester");
|
||||
|
||||
// when
|
||||
messages.send(sender, key);
|
||||
@@ -189,9 +217,11 @@ public class MessagesIntegrationTest {
|
||||
Logger logger = mock(Logger.class);
|
||||
ConsoleLogger.setLogger(logger);
|
||||
MessageKey key = MessageKey.CAPTCHA_WRONG_ERROR;
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
given(sender.getName()).willReturn("Tester");
|
||||
|
||||
// when
|
||||
messages.send(mock(CommandSender.class), key, "rep", "rep2");
|
||||
messages.send(sender, key, "rep", "rep2");
|
||||
|
||||
// then
|
||||
verify(logger).warning(argThat(containsString("Invalid number of replacements")));
|
||||
@@ -203,9 +233,11 @@ public class MessagesIntegrationTest {
|
||||
Logger logger = mock(Logger.class);
|
||||
ConsoleLogger.setLogger(logger);
|
||||
MessageKey key = MessageKey.UNKNOWN_USER;
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
given(sender.getName()).willReturn("Tester");
|
||||
|
||||
// when
|
||||
messages.send(mock(CommandSender.class), key, "Replacement");
|
||||
messages.send(sender, key, "Replacement");
|
||||
|
||||
// then
|
||||
verify(logger).warning(argThat(containsString("Invalid number of replacements")));
|
||||
@@ -216,9 +248,11 @@ public class MessagesIntegrationTest {
|
||||
// given
|
||||
// Key is present in both files
|
||||
MessageKey key = MessageKey.WRONG_PASSWORD;
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
given(sender.getName()).willReturn("Tester");
|
||||
|
||||
// when
|
||||
String message = messages.retrieveSingle(key);
|
||||
String message = messages.retrieveSingle(sender, key);
|
||||
|
||||
// then
|
||||
assertThat(message, equalTo("§cWrong password!"));
|
||||
@@ -228,9 +262,11 @@ public class MessagesIntegrationTest {
|
||||
public void shouldRetrieveMessageWithReplacements() {
|
||||
// given
|
||||
MessageKey key = MessageKey.CAPTCHA_WRONG_ERROR;
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
given(sender.getName()).willReturn("Tester");
|
||||
|
||||
// when
|
||||
String result = messages.retrieveSingle(key, "24680");
|
||||
String result = messages.retrieveSingle(sender.getName(), key, "24680");
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo("Use /captcha 24680 to solve the captcha"));
|
||||
|
||||
@@ -83,15 +83,16 @@ public class CommonServiceTest {
|
||||
public void shouldRetrieveSingleMessage() {
|
||||
// given
|
||||
MessageKey key = MessageKey.ACCOUNT_NOT_ACTIVATED;
|
||||
Player player = mock(Player.class);
|
||||
String text = "Test text";
|
||||
given(messages.retrieveSingle(key)).willReturn(text);
|
||||
given(messages.retrieveSingle(player, key)).willReturn(text);
|
||||
|
||||
// when
|
||||
String result = commonService.retrieveSingleMessage(key);
|
||||
String result = commonService.retrieveSingleMessage(player, key);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(text));
|
||||
verify(messages).retrieveSingle(key);
|
||||
verify(messages).retrieveSingle(player, key);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
package fr.xephi.authme.service;
|
||||
|
||||
import com.maxmind.geoip.Country;
|
||||
import com.maxmind.geoip.LookupService;
|
||||
import com.maxmind.db.GeoIp2Provider;
|
||||
import com.maxmind.db.model.Country;
|
||||
import com.maxmind.db.model.CountryResponse;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
@@ -10,13 +16,11 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -29,8 +33,12 @@ public class GeoIpServiceTest {
|
||||
|
||||
private GeoIpService geoIpService;
|
||||
private File dataFolder;
|
||||
|
||||
@Mock
|
||||
private LookupService lookupService;
|
||||
private GeoIp2Provider lookupService;
|
||||
|
||||
@Mock
|
||||
private BukkitService bukkitService;
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
@@ -38,20 +46,24 @@ public class GeoIpServiceTest {
|
||||
@Before
|
||||
public void initializeGeoLiteApi() throws IOException {
|
||||
dataFolder = temporaryFolder.newFolder();
|
||||
geoIpService = new GeoIpService(dataFolder, lookupService);
|
||||
geoIpService = new GeoIpService(dataFolder, bukkitService, lookupService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetCountry() {
|
||||
public void shouldGetCountry() throws Exception {
|
||||
// given
|
||||
String ip = "123.45.67.89";
|
||||
InetAddress ip = InetAddress.getByName("123.45.67.89");
|
||||
String countryCode = "XX";
|
||||
|
||||
Country country = mock(Country.class);
|
||||
given(country.getCode()).willReturn(countryCode);
|
||||
given(lookupService.getCountry(ip)).willReturn(country);
|
||||
given(country.getIsoCode()).willReturn(countryCode);
|
||||
|
||||
CountryResponse response = mock(CountryResponse.class);
|
||||
given(response.getCountry()).willReturn(country);
|
||||
given(lookupService.getCountry(ip)).willReturn(response);
|
||||
|
||||
// when
|
||||
String result = geoIpService.getCountryCode(ip);
|
||||
String result = geoIpService.getCountryCode(ip.getHostAddress());
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(countryCode));
|
||||
@@ -59,7 +71,7 @@ public class GeoIpServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotLookUpCountryForLocalhostIp() {
|
||||
public void shouldNotLookUpCountryForLocalhostIp() throws Exception {
|
||||
// given
|
||||
String ip = "127.0.0.1";
|
||||
|
||||
@@ -68,20 +80,24 @@ public class GeoIpServiceTest {
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo("--"));
|
||||
verify(lookupService, never()).getCountry(anyString());
|
||||
verify(lookupService, never()).getCountry(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldLookUpCountryName() {
|
||||
public void shouldLookUpCountryName() throws Exception {
|
||||
// given
|
||||
String ip = "24.45.167.89";
|
||||
InetAddress ip = InetAddress.getByName("24.45.167.89");
|
||||
String countryName = "Ecuador";
|
||||
|
||||
Country country = mock(Country.class);
|
||||
given(country.getName()).willReturn(countryName);
|
||||
given(lookupService.getCountry(ip)).willReturn(country);
|
||||
|
||||
CountryResponse response = mock(CountryResponse.class);
|
||||
given(response.getCountry()).willReturn(country);
|
||||
given(lookupService.getCountry(ip)).willReturn(response);
|
||||
|
||||
// when
|
||||
String result = geoIpService.getCountryName(ip);
|
||||
String result = geoIpService.getCountryName(ip.getHostAddress());
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(countryName));
|
||||
@@ -89,16 +105,15 @@ public class GeoIpServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotLookUpCountryNameForLocalhostIp() {
|
||||
public void shouldNotLookUpCountryNameForLocalhostIp() throws Exception {
|
||||
// given
|
||||
String ip = "127.0.0.1";
|
||||
InetAddress ip = InetAddress.getByName("127.0.0.1");
|
||||
|
||||
// when
|
||||
String result = geoIpService.getCountryName(ip);
|
||||
String result = geoIpService.getCountryName(ip.getHostAddress());
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo("N/A"));
|
||||
verify(lookupService, never()).getCountry(ip);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -106,7 +106,6 @@ public class SessionServiceTest {
|
||||
// then
|
||||
assertThat(result, equalTo(false));
|
||||
verify(commonService).getProperty(PluginSettings.SESSIONS_ENABLED);
|
||||
verify(commonService).send(player, MessageKey.SESSION_EXPIRED);
|
||||
verify(dataSource).hasSession(name);
|
||||
verify(dataSource).setUnlogged(name);
|
||||
verify(dataSource).revokeSession(name);
|
||||
@@ -132,7 +131,6 @@ public class SessionServiceTest {
|
||||
// then
|
||||
assertThat(result, equalTo(false));
|
||||
verify(commonService).getProperty(PluginSettings.SESSIONS_ENABLED);
|
||||
verify(commonService).send(player, MessageKey.SESSION_EXPIRED);
|
||||
verify(dataSource).hasSession(name);
|
||||
verify(dataSource).setUnlogged(name);
|
||||
verify(dataSource).revokeSession(name);
|
||||
@@ -145,9 +143,10 @@ public class SessionServiceTest {
|
||||
String ip = "127.3.12.15";
|
||||
Player player = mockPlayerWithNameAndIp(name, ip);
|
||||
given(dataSource.hasSession(name)).willReturn(true);
|
||||
given(commonService.getProperty(PluginSettings.SESSIONS_TIMEOUT)).willReturn(8);
|
||||
PlayerAuth auth = PlayerAuth.builder()
|
||||
.name(name)
|
||||
.lastLogin(System.currentTimeMillis())
|
||||
.lastLogin(System.currentTimeMillis() - 7 * 60 * 1000)
|
||||
.lastIp("8.8.8.8").build();
|
||||
given(dataSource.getAuth(name)).willReturn(auth);
|
||||
|
||||
@@ -219,6 +218,7 @@ public class SessionServiceTest {
|
||||
String name = "Charles";
|
||||
Player player = mockPlayerWithNameAndIp(name, "144.117.118.145");
|
||||
given(dataSource.hasSession(name)).willReturn(true);
|
||||
given(commonService.getProperty(PluginSettings.SESSIONS_TIMEOUT)).willReturn(8);
|
||||
PlayerAuth auth = PlayerAuth.builder()
|
||||
.name(name)
|
||||
.lastIp(null)
|
||||
|
||||
Reference in New Issue
Block a user