AuthMeReloaded/src/test/java/fr/xephi/authme/service/GeoIpServiceTest.java

140 lines
4.2 KiB
Java
Raw Normal View History

2016-10-07 19:19:12 +02:00
package fr.xephi.authme.service;
2016-06-26 23:09:27 +02:00
import com.maxmind.db.GeoIp2Provider;
import com.maxmind.db.model.Country;
import com.maxmind.db.model.CountryResponse;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.ProtectionSettings;
2016-06-26 23:09:27 +02:00
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Mock;
2016-12-01 19:41:31 +01:00
import org.mockito.junit.MockitoJUnitRunner;
2016-06-26 23:09:27 +02:00
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
2016-06-26 23:09:27 +02:00
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
2016-06-26 23:09:27 +02:00
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
2016-06-26 23:09:27 +02:00
/**
2016-10-07 19:19:12 +02:00
* Test for {@link GeoIpService}.
2016-06-26 23:09:27 +02:00
*/
@RunWith(MockitoJUnitRunner.class)
2016-10-07 19:19:12 +02:00
public class GeoIpServiceTest {
2016-06-26 23:09:27 +02:00
2016-10-07 19:19:12 +02:00
private GeoIpService geoIpService;
2016-06-26 23:09:27 +02:00
private File dataFolder;
2016-06-26 23:09:27 +02:00
@Mock
private GeoIp2Provider lookupService;
@Mock
private BukkitService bukkitService;
2016-06-26 23:09:27 +02:00
@Mock
private Settings settings;
2016-06-26 23:09:27 +02:00
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Before
public void initializeGeoLiteApi() throws IOException {
dataFolder = temporaryFolder.newFolder();
geoIpService = new GeoIpService(dataFolder, bukkitService, settings, lookupService);
2016-06-26 23:09:27 +02:00
}
@Test
public void shouldGetCountry() throws Exception {
2016-06-26 23:09:27 +02:00
// given
InetAddress ip = InetAddress.getByName("123.45.67.89");
2016-06-26 23:09:27 +02:00
String countryCode = "XX";
2016-06-26 23:09:27 +02:00
Country country = mock(Country.class);
given(country.getIsoCode()).willReturn(countryCode);
CountryResponse response = mock(CountryResponse.class);
given(response.getCountry()).willReturn(country);
given(lookupService.getCountry(ip)).willReturn(response);
given(settings.getProperty(ProtectionSettings.ENABLE_GEOIP)).willReturn(true);
2016-06-26 23:09:27 +02:00
// when
String result = geoIpService.getCountryCode(ip.getHostAddress());
2016-06-26 23:09:27 +02:00
// then
assertThat(result, equalTo(countryCode));
verify(lookupService).getCountry(ip);
}
@Test
public void shouldNotLookUpCountryForLocalhostIp() throws Exception {
2016-06-26 23:09:27 +02:00
// given
String ip = "127.0.0.1";
// when
2016-10-07 19:19:12 +02:00
String result = geoIpService.getCountryCode(ip);
2016-06-26 23:09:27 +02:00
// then
2018-08-26 19:23:15 +02:00
assertThat(result, equalTo("LOCALHOST"));
verify(lookupService, never()).getCountry(any());
2016-06-26 23:09:27 +02:00
}
@Test
public void shouldLookUpCountryName() throws Exception {
2016-06-26 23:09:27 +02:00
// given
InetAddress ip = InetAddress.getByName("24.45.167.89");
2016-06-26 23:09:27 +02:00
String countryName = "Ecuador";
2016-06-26 23:09:27 +02:00
Country country = mock(Country.class);
given(country.getName()).willReturn(countryName);
CountryResponse response = mock(CountryResponse.class);
given(response.getCountry()).willReturn(country);
given(lookupService.getCountry(ip)).willReturn(response);
given(settings.getProperty(ProtectionSettings.ENABLE_GEOIP)).willReturn(true);
2016-06-26 23:09:27 +02:00
// when
String result = geoIpService.getCountryName(ip.getHostAddress());
2016-06-26 23:09:27 +02:00
// then
assertThat(result, equalTo(countryName));
verify(lookupService).getCountry(ip);
}
@Test
public void shouldNotLookUpCountryNameForLocalhostIp() throws Exception {
2016-06-26 23:09:27 +02:00
// given
InetAddress ip = InetAddress.getByName("127.0.0.1");
2016-06-26 23:09:27 +02:00
// when
String result = geoIpService.getCountryName(ip.getHostAddress());
2016-06-26 23:09:27 +02:00
// then
2018-08-26 19:23:15 +02:00
assertThat(result, equalTo("LocalHost"));
2016-06-26 23:09:27 +02:00
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);
}
2016-06-26 23:09:27 +02:00
}