mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-27 11:07:36 +01:00
parent
2d2c14eb0a
commit
7e2912cc60
@ -33,7 +33,7 @@ import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||
import fr.xephi.authme.task.CleanupTask;
|
||||
import fr.xephi.authme.task.purge.PurgeService;
|
||||
import fr.xephi.authme.service.BukkitService;
|
||||
import fr.xephi.authme.geoip.GeoIpManager;
|
||||
import fr.xephi.authme.geoip.GeoLiteAPI;
|
||||
import fr.xephi.authme.service.MigrationService;
|
||||
import fr.xephi.authme.util.PlayerUtils;
|
||||
import org.bukkit.Server;
|
||||
@ -74,7 +74,7 @@ public class AuthMe extends JavaPlugin {
|
||||
private DataSource database;
|
||||
private BukkitService bukkitService;
|
||||
private Injector injector;
|
||||
private GeoIpManager geoIpManager;
|
||||
private GeoLiteAPI geoLiteApi;
|
||||
private PlayerCache playerCache;
|
||||
|
||||
/**
|
||||
@ -248,7 +248,7 @@ public class AuthMe extends JavaPlugin {
|
||||
permsMan = injector.getSingleton(PermissionsManager.class);
|
||||
bukkitService = injector.getSingleton(BukkitService.class);
|
||||
commandHandler = injector.getSingleton(CommandHandler.class);
|
||||
geoIpManager = injector.getSingleton(GeoIpManager.class);
|
||||
geoLiteApi = injector.getSingleton(GeoLiteAPI.class);
|
||||
|
||||
// Trigger construction of API classes; they will keep track of the singleton
|
||||
injector.getSingleton(NewAPI.class);
|
||||
@ -374,7 +374,7 @@ public class AuthMe extends JavaPlugin {
|
||||
.replace("{SERVER}", server.getServerName())
|
||||
.replace("{VERSION}", server.getBukkitVersion())
|
||||
// TODO: We should cache info like this, maybe with a class that extends Player?
|
||||
.replace("{COUNTRY}", geoIpManager.getCountryName(ipAddress));
|
||||
.replace("{COUNTRY}", geoLiteApi.getCountryName(ipAddress));
|
||||
}
|
||||
|
||||
|
||||
|
@ -17,7 +17,7 @@ import java.net.URLConnection;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
public class GeoIpManager {
|
||||
public class GeoLiteAPI {
|
||||
private static final String LICENSE =
|
||||
"[LICENSE] This product uses data from the GeoLite API created by MaxMind, available at http://www.maxmind.com";
|
||||
private static final String GEOIP_URL =
|
||||
@ -28,14 +28,14 @@ public class GeoIpManager {
|
||||
private final File dataFile;
|
||||
|
||||
@Inject
|
||||
GeoIpManager(@DataFolder File dataFolder) {
|
||||
GeoLiteAPI(@DataFolder File dataFolder) {
|
||||
this.dataFile = new File(dataFolder, "GeoIP.dat");
|
||||
// Fires download of recent data or the initialization of the look up service
|
||||
isDataAvailable();
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
GeoIpManager(@DataFolder File dataFolder, LookupService lookupService) {
|
||||
GeoLiteAPI(@DataFolder File dataFolder, LookupService lookupService) {
|
||||
this.dataFile = dataFolder;
|
||||
this.lookupService = lookupService;
|
||||
}
|
@ -2,7 +2,7 @@ package fr.xephi.authme.service;
|
||||
|
||||
import com.github.authme.configme.properties.Property;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.geoip.GeoIpManager;
|
||||
import fr.xephi.authme.geoip.GeoLiteAPI;
|
||||
import fr.xephi.authme.initialization.Reloadable;
|
||||
import fr.xephi.authme.output.MessageKey;
|
||||
import fr.xephi.authme.permission.PermissionsManager;
|
||||
@ -36,7 +36,7 @@ public class ValidationService implements Reloadable {
|
||||
@Inject
|
||||
private PermissionsManager permissionsManager;
|
||||
@Inject
|
||||
private GeoIpManager geoIpManager;
|
||||
private GeoLiteAPI geoLiteApi;
|
||||
|
||||
private Pattern passwordRegex;
|
||||
private Set<String> unrestrictedNames;
|
||||
@ -115,7 +115,7 @@ public class ValidationService implements Reloadable {
|
||||
return true;
|
||||
}
|
||||
|
||||
String countryCode = geoIpManager.getCountryCode(hostAddress);
|
||||
String countryCode = geoLiteApi.getCountryCode(hostAddress);
|
||||
return validateWhitelistAndBlacklist(countryCode,
|
||||
ProtectionSettings.COUNTRIES_WHITELIST,
|
||||
ProtectionSettings.COUNTRIES_BLACKLIST);
|
||||
|
@ -2,6 +2,7 @@ package fr.xephi.authme.geoip;
|
||||
|
||||
import com.maxmind.geoip.Country;
|
||||
import com.maxmind.geoip.LookupService;
|
||||
import fr.xephi.authme.geoip.GeoLiteAPI;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
@ -22,12 +23,12 @@ import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Test for {@link GeoIpManager}.
|
||||
* Test for {@link GeoLiteAPI}.
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class GeoIpManagerTest {
|
||||
|
||||
private GeoIpManager geoIpManager;
|
||||
private GeoLiteAPI geoLiteApi;
|
||||
private File dataFolder;
|
||||
@Mock
|
||||
private LookupService lookupService;
|
||||
@ -38,7 +39,7 @@ public class GeoIpManagerTest {
|
||||
@Before
|
||||
public void initializeGeoLiteApi() throws IOException {
|
||||
dataFolder = temporaryFolder.newFolder();
|
||||
geoIpManager = new GeoIpManager(dataFolder, lookupService);
|
||||
geoLiteApi = new GeoLiteAPI(dataFolder, lookupService);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -51,7 +52,7 @@ public class GeoIpManagerTest {
|
||||
given(lookupService.getCountry(ip)).willReturn(country);
|
||||
|
||||
// when
|
||||
String result = geoIpManager.getCountryCode(ip);
|
||||
String result = geoLiteApi.getCountryCode(ip);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(countryCode));
|
||||
@ -64,7 +65,7 @@ public class GeoIpManagerTest {
|
||||
String ip = "127.0.0.1";
|
||||
|
||||
// when
|
||||
String result = geoIpManager.getCountryCode(ip);
|
||||
String result = geoLiteApi.getCountryCode(ip);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo("--"));
|
||||
@ -81,7 +82,7 @@ public class GeoIpManagerTest {
|
||||
given(lookupService.getCountry(ip)).willReturn(country);
|
||||
|
||||
// when
|
||||
String result = geoIpManager.getCountryName(ip);
|
||||
String result = geoLiteApi.getCountryName(ip);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(countryName));
|
||||
@ -94,7 +95,7 @@ public class GeoIpManagerTest {
|
||||
String ip = "127.0.0.1";
|
||||
|
||||
// when
|
||||
String result = geoIpManager.getCountryName(ip);
|
||||
String result = geoLiteApi.getCountryName(ip);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo("N/A"));
|
||||
|
@ -5,10 +5,11 @@ import ch.jalu.injector.testing.DelayedInjectionRunner;
|
||||
import ch.jalu.injector.testing.InjectDelayed;
|
||||
import com.google.common.base.Strings;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.geoip.GeoIpManager;
|
||||
import fr.xephi.authme.geoip.GeoLiteAPI;
|
||||
import fr.xephi.authme.output.MessageKey;
|
||||
import fr.xephi.authme.permission.PermissionsManager;
|
||||
import fr.xephi.authme.permission.PlayerStatePermission;
|
||||
import fr.xephi.authme.service.ValidationService;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.EmailSettings;
|
||||
import fr.xephi.authme.settings.properties.ProtectionSettings;
|
||||
@ -45,7 +46,7 @@ public class ValidationServiceTest {
|
||||
@Mock
|
||||
private PermissionsManager permissionsManager;
|
||||
@Mock
|
||||
private GeoIpManager geoIpManager;
|
||||
private GeoLiteAPI geoLiteApi;
|
||||
|
||||
@BeforeInjecting
|
||||
public void createService() {
|
||||
@ -266,7 +267,7 @@ public class ValidationServiceTest {
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
verifyZeroInteractions(geoIpManager);
|
||||
verifyZeroInteractions(geoLiteApi);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -275,14 +276,14 @@ public class ValidationServiceTest {
|
||||
given(settings.getProperty(ProtectionSettings.COUNTRIES_WHITELIST)).willReturn(asList("ch", "it"));
|
||||
given(settings.getProperty(ProtectionSettings.COUNTRIES_BLACKLIST)).willReturn(Collections.<String>emptyList());
|
||||
String ip = "127.0.0.1";
|
||||
given(geoIpManager.getCountryCode(ip)).willReturn("CH");
|
||||
given(geoLiteApi.getCountryCode(ip)).willReturn("CH");
|
||||
|
||||
// when
|
||||
boolean result = validationService.isCountryAdmitted(ip);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
verify(geoIpManager).getCountryCode(ip);
|
||||
verify(geoLiteApi).getCountryCode(ip);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -291,14 +292,14 @@ public class ValidationServiceTest {
|
||||
given(settings.getProperty(ProtectionSettings.COUNTRIES_WHITELIST)).willReturn(asList("ch", "it"));
|
||||
given(settings.getProperty(ProtectionSettings.COUNTRIES_BLACKLIST)).willReturn(Collections.<String>emptyList());
|
||||
String ip = "123.45.67.89";
|
||||
given(geoIpManager.getCountryCode(ip)).willReturn("BR");
|
||||
given(geoLiteApi.getCountryCode(ip)).willReturn("BR");
|
||||
|
||||
// when
|
||||
boolean result = validationService.isCountryAdmitted(ip);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(false));
|
||||
verify(geoIpManager).getCountryCode(ip);
|
||||
verify(geoLiteApi).getCountryCode(ip);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -307,14 +308,14 @@ public class ValidationServiceTest {
|
||||
given(settings.getProperty(ProtectionSettings.COUNTRIES_WHITELIST)).willReturn(Collections.<String>emptyList());
|
||||
given(settings.getProperty(ProtectionSettings.COUNTRIES_BLACKLIST)).willReturn(asList("ch", "it"));
|
||||
String ip = "127.0.0.1";
|
||||
given(geoIpManager.getCountryCode(ip)).willReturn("BR");
|
||||
given(geoLiteApi.getCountryCode(ip)).willReturn("BR");
|
||||
|
||||
// when
|
||||
boolean result = validationService.isCountryAdmitted(ip);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
verify(geoIpManager).getCountryCode(ip);
|
||||
verify(geoLiteApi).getCountryCode(ip);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -323,14 +324,14 @@ public class ValidationServiceTest {
|
||||
given(settings.getProperty(ProtectionSettings.COUNTRIES_WHITELIST)).willReturn(Collections.<String>emptyList());
|
||||
given(settings.getProperty(ProtectionSettings.COUNTRIES_BLACKLIST)).willReturn(asList("ch", "it"));
|
||||
String ip = "123.45.67.89";
|
||||
given(geoIpManager.getCountryCode(ip)).willReturn("IT");
|
||||
given(geoLiteApi.getCountryCode(ip)).willReturn("IT");
|
||||
|
||||
// when
|
||||
boolean result = validationService.isCountryAdmitted(ip);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(false));
|
||||
verify(geoIpManager).getCountryCode(ip);
|
||||
verify(geoLiteApi).getCountryCode(ip);
|
||||
}
|
||||
|
||||
private static void assertErrorEquals(ValidationResult validationResult, MessageKey messageKey, String... args) {
|
||||
|
@ -15,7 +15,7 @@ import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Test for {@link PlayerUtils}.
|
||||
* Test for {@link Utils}.
|
||||
*/
|
||||
public class PlayerUtilsTest {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user