Fix tests (due to new Bukkit/GeoIP updates)

This commit is contained in:
ljacqu 2024-04-27 11:04:07 +02:00
parent 6eec2c8634
commit e69016aba5
6 changed files with 36 additions and 27 deletions

View File

@ -5,8 +5,8 @@ import fr.xephi.authme.data.ProxySessionManager;
import fr.xephi.authme.data.limbo.LimboService;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.events.ProtectInventoryEvent;
import fr.xephi.authme.output.ConsoleLoggerFactory;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.output.ConsoleLoggerFactory;
import fr.xephi.authme.permission.PlayerStatePermission;
import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.process.login.AsynchronousLogin;
@ -27,11 +27,8 @@ import fr.xephi.authme.util.PlayerUtils;
import org.bukkit.GameMode;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import javax.inject.Inject;
import java.util.Locale;
import static fr.xephi.authme.service.BukkitService.TICKS_PER_SECOND;
@ -197,7 +194,7 @@ public class AsynchronousJoin implements AsynchronousProcess {
if (service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)) {
// Allow infinite blindness effect
int blindTimeOut = (registrationTimeout <= 0) ? 99999 : registrationTimeout;
player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, blindTimeOut, 2));
player.addPotionEffect(bukkitService.createBlindnessEffect(blindTimeOut));
}
commandManager.runCommandsOnJoin(player);
});

View File

@ -3,9 +3,9 @@ package fr.xephi.authme.process.logout;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.data.limbo.LimboService;
import fr.xephi.authme.events.LogoutEvent;
import fr.xephi.authme.output.ConsoleLoggerFactory;
import fr.xephi.authme.listener.protocollib.ProtocolLibService;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.output.ConsoleLoggerFactory;
import fr.xephi.authme.process.SynchronousProcess;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.CommonService;
@ -14,8 +14,6 @@ import fr.xephi.authme.settings.commandconfig.CommandManager;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import javax.inject.Inject;
@ -75,7 +73,7 @@ public class ProcessSyncPlayerLogout implements SynchronousProcess {
// Apply Blindness effect
if (service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)) {
int timeout = service.getProperty(RestrictionSettings.TIMEOUT) * TICKS_PER_SECOND;
player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, timeout, 2));
player.addPotionEffect(bukkitService.createBlindnessEffect(timeout));
}
// Set player's data to unauthenticated

View File

@ -7,22 +7,20 @@ import fr.xephi.authme.data.limbo.LimboService;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.events.UnregisterByAdminEvent;
import fr.xephi.authme.events.UnregisterByPlayerEvent;
import fr.xephi.authme.output.ConsoleLoggerFactory;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.output.ConsoleLoggerFactory;
import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.TeleportationService;
import fr.xephi.authme.service.bungeecord.BungeeSender;
import fr.xephi.authme.service.bungeecord.MessageType;
import fr.xephi.authme.settings.commandconfig.CommandManager;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.service.bungeecord.BungeeSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import javax.inject.Inject;
@ -144,7 +142,7 @@ public class AsynchronousUnregister implements AsynchronousProcess {
private void applyBlindEffect(Player player) {
if (service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)) {
int timeout = service.getProperty(RestrictionSettings.TIMEOUT) * TICKS_PER_SECOND;
player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, timeout, 2));
player.addPotionEffect(bukkitService.createBlindnessEffect(timeout));
}
}

View File

@ -13,6 +13,8 @@ import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
@ -338,6 +340,16 @@ public class BukkitService implements SettingsDependent {
}
}
/**
* Creates a PotionEffect with blindness for the given duration in ticks.
*
* @param timeoutInTicks duration of the effect in ticks
* @return blindness potion effect
*/
public PotionEffect createBlindnessEffect(int timeoutInTicks) {
return new PotionEffect(PotionEffectType.BLINDNESS, timeoutInTicks, 2);
}
/**
* @return the IP string that this server is bound to, otherwise empty string
*/

View File

@ -115,6 +115,7 @@ public class AsynchronousUnregisterTest {
given(service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)).willReturn(true);
given(service.getProperty(RestrictionSettings.TIMEOUT)).willReturn(21);
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
given(bukkitService.createBlindnessEffect(21 * 20)).willReturn(mock(PotionEffect.class));
// when
asynchronousUnregister.unregister(player, userPassword);
@ -127,7 +128,6 @@ public class AsynchronousUnregisterTest {
verify(teleportationService).teleportOnJoin(player);
verifyCalledUnregisterEventFor(player);
verify(commandManager).runCommandsOnUnregister(player);
verify(player).addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 21 * 20, 2));
}
@Test

View File

@ -3,9 +3,14 @@ package fr.xephi.authme.service;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.google.common.collect.ImmutableMap;
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.model.CountryResponse;
import com.maxmind.geoip2.record.Continent;
import com.maxmind.geoip2.record.Country;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.ProtectionSettings;
@ -58,12 +63,7 @@ public class GeoIpServiceTest {
// given
InetAddress ip = InetAddress.getByName("123.45.67.89");
String countryCode = "XX";
Country country = mock(Country.class);
given(country.getIsoCode()).willReturn(countryCode);
CountryResponse response = mock(CountryResponse.class);
given(response.getCountry()).willReturn(country);
CountryResponse response = createCountryResponse(countryCode, "Unknown");
given(lookupService.country(ip)).willReturn(response);
given(settings.getProperty(ProtectionSettings.ENABLE_GEOIP)).willReturn(true);
@ -93,12 +93,7 @@ public class GeoIpServiceTest {
// given
InetAddress ip = InetAddress.getByName("24.45.167.89");
String countryName = "Ecuador";
Country country = mock(Country.class);
given(country.getName()).willReturn(countryName);
CountryResponse response = mock(CountryResponse.class);
given(response.getCountry()).willReturn(country);
CountryResponse response = createCountryResponse("EC", countryName);
given(lookupService.country(ip)).willReturn(response);
given(settings.getProperty(ProtectionSettings.ENABLE_GEOIP)).willReturn(true);
@ -136,4 +131,13 @@ public class GeoIpServiceTest {
assertThat(result, equalTo("N/A"));
verifyNoInteractions(lookupService);
}
private static CountryResponse createCountryResponse(String countryCode, String countryName) {
List<String> locales = Collections.singletonList("en");
Continent continent = new Continent(locales, "XX", 1L, Collections.emptyMap());
Map<String, String> countryNames = ImmutableMap.of("en", countryName);
Country country = new Country(locales, 100, 3L, false, countryCode, countryNames);
return new CountryResponse(continent, country, null, country, null, null);
}
}