From 9b93e0e4b67e38d58eeb4a30b0775cb6063b8315 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sun, 24 Sep 2023 18:23:55 +0200 Subject: [PATCH] Disable GeoIP with config: remove field, fix tests --- .../fr/xephi/authme/service/GeoIpService.java | 9 ------ .../authme/service/GeoIpServiceTest.java | 30 ++++++++++++++----- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/main/java/fr/xephi/authme/service/GeoIpService.java b/src/main/java/fr/xephi/authme/service/GeoIpService.java index 40c3aa1e..ef0d00b6 100644 --- a/src/main/java/fr/xephi/authme/service/GeoIpService.java +++ b/src/main/java/fr/xephi/authme/service/GeoIpService.java @@ -64,8 +64,6 @@ public class GeoIpService { private GeoIp2Provider databaseReader; private volatile boolean downloading; - private boolean enabled = true; - @Inject GeoIpService(@DataFolder File dataFolder, BukkitService bukkitService, Settings settings) { this.bukkitService = bukkitService; @@ -94,7 +92,6 @@ public class GeoIpService { // If this feature is disabled, just stop if (!settings.getProperty(ProtectionSettings.ENABLE_GEOIP)) { - enabled = false; return false; } @@ -289,9 +286,6 @@ public class GeoIpService { * or "--" if it cannot be fetched. */ public String getCountryCode(String ip) { - if (!enabled) { - return "--"; - } if (InternetProtocolUtils.isLocalAddress(ip)) { return "LOCALHOST"; } @@ -305,9 +299,6 @@ public class GeoIpService { * @return The name of the country, "LocalHost" for local addresses, or "N/A" if it cannot be fetched. */ public String getCountryName(String ip) { - if (!enabled) { - return "N/A"; - } if (InternetProtocolUtils.isLocalAddress(ip)) { return "LocalHost"; } diff --git a/src/test/java/fr/xephi/authme/service/GeoIpServiceTest.java b/src/test/java/fr/xephi/authme/service/GeoIpServiceTest.java index 8654ec8b..351049a8 100644 --- a/src/test/java/fr/xephi/authme/service/GeoIpServiceTest.java +++ b/src/test/java/fr/xephi/authme/service/GeoIpServiceTest.java @@ -3,12 +3,8 @@ package fr.xephi.authme.service; 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 fr.xephi.authme.settings.Settings; +import fr.xephi.authme.settings.properties.ProtectionSettings; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -17,14 +13,18 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.BDDMockito.given; +import java.io.File; +import java.io.IOException; +import java.net.InetAddress; import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; /** * Test for {@link GeoIpService}. @@ -65,6 +65,7 @@ public class GeoIpServiceTest { CountryResponse response = mock(CountryResponse.class); given(response.getCountry()).willReturn(country); given(lookupService.getCountry(ip)).willReturn(response); + given(settings.getProperty(ProtectionSettings.ENABLE_GEOIP)).willReturn(true); // when String result = geoIpService.getCountryCode(ip.getHostAddress()); @@ -99,6 +100,7 @@ public class GeoIpServiceTest { CountryResponse response = mock(CountryResponse.class); given(response.getCountry()).willReturn(country); given(lookupService.getCountry(ip)).willReturn(response); + given(settings.getProperty(ProtectionSettings.ENABLE_GEOIP)).willReturn(true); // when String result = geoIpService.getCountryName(ip.getHostAddress()); @@ -120,4 +122,18 @@ public class GeoIpServiceTest { assertThat(result, equalTo("LocalHost")); verify(lookupService, never()).getCountry(ip); } + + @Test + public void shouldNotLookUpCountryNameIfDisabled() throws Exception { + // given + InetAddress ip = InetAddress.getByName("24.45.167.89"); + given(settings.getProperty(ProtectionSettings.ENABLE_GEOIP)).willReturn(false); + + // when + String result = geoIpService.getCountryName(ip.getHostAddress()); + + // then + assertThat(result, equalTo("N/A")); + verifyNoInteractions(lookupService); + } }