From e69016aba5ecceccaaaee680e5b9802a83ce9da7 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sat, 27 Apr 2024 11:04:07 +0200 Subject: [PATCH] Fix tests (due to new Bukkit/GeoIP updates) --- .../authme/process/join/AsynchronousJoin.java | 7 ++--- .../logout/ProcessSyncPlayerLogout.java | 6 ++-- .../unregister/AsynchronousUnregister.java | 8 ++---- .../xephi/authme/service/BukkitService.java | 12 ++++++++ .../AsynchronousUnregisterTest.java | 2 +- .../authme/service/GeoIpServiceTest.java | 28 +++++++++++-------- 6 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java b/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java index d5cf1cef5..d60ce4e83 100644 --- a/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java +++ b/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java @@ -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); }); diff --git a/src/main/java/fr/xephi/authme/process/logout/ProcessSyncPlayerLogout.java b/src/main/java/fr/xephi/authme/process/logout/ProcessSyncPlayerLogout.java index 46fd7fefc..96865e21c 100644 --- a/src/main/java/fr/xephi/authme/process/logout/ProcessSyncPlayerLogout.java +++ b/src/main/java/fr/xephi/authme/process/logout/ProcessSyncPlayerLogout.java @@ -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 diff --git a/src/main/java/fr/xephi/authme/process/unregister/AsynchronousUnregister.java b/src/main/java/fr/xephi/authme/process/unregister/AsynchronousUnregister.java index 7beea4be6..5d37c8ad6 100644 --- a/src/main/java/fr/xephi/authme/process/unregister/AsynchronousUnregister.java +++ b/src/main/java/fr/xephi/authme/process/unregister/AsynchronousUnregister.java @@ -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)); } } diff --git a/src/main/java/fr/xephi/authme/service/BukkitService.java b/src/main/java/fr/xephi/authme/service/BukkitService.java index a09b18508..8864a902e 100644 --- a/src/main/java/fr/xephi/authme/service/BukkitService.java +++ b/src/main/java/fr/xephi/authme/service/BukkitService.java @@ -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 */ diff --git a/src/test/java/fr/xephi/authme/process/unregister/AsynchronousUnregisterTest.java b/src/test/java/fr/xephi/authme/process/unregister/AsynchronousUnregisterTest.java index c6e26b882..810d73b94 100644 --- a/src/test/java/fr/xephi/authme/process/unregister/AsynchronousUnregisterTest.java +++ b/src/test/java/fr/xephi/authme/process/unregister/AsynchronousUnregisterTest.java @@ -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 diff --git a/src/test/java/fr/xephi/authme/service/GeoIpServiceTest.java b/src/test/java/fr/xephi/authme/service/GeoIpServiceTest.java index 06df10e98..329f97341 100644 --- a/src/test/java/fr/xephi/authme/service/GeoIpServiceTest.java +++ b/src/test/java/fr/xephi/authme/service/GeoIpServiceTest.java @@ -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 locales = Collections.singletonList("en"); + Continent continent = new Continent(locales, "XX", 1L, Collections.emptyMap()); + + Map countryNames = ImmutableMap.of("en", countryName); + Country country = new Country(locales, 100, 3L, false, countryCode, countryNames); + return new CountryResponse(continent, country, null, country, null, null); + } }