Merge branch 'master' of https://github.com/AuthMe/AuthMeReloaded into 1141-optional-additional-2fa-auth
# Conflicts: # src/main/java/fr/xephi/authme/datasource/MySQL.java
This commit is contained in:
@@ -83,15 +83,16 @@ public class CommonServiceTest {
|
||||
public void shouldRetrieveSingleMessage() {
|
||||
// given
|
||||
MessageKey key = MessageKey.ACCOUNT_NOT_ACTIVATED;
|
||||
Player player = mock(Player.class);
|
||||
String text = "Test text";
|
||||
given(messages.retrieveSingle(key)).willReturn(text);
|
||||
given(messages.retrieveSingle(player, key)).willReturn(text);
|
||||
|
||||
// when
|
||||
String result = commonService.retrieveSingleMessage(key);
|
||||
String result = commonService.retrieveSingleMessage(player, key);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(text));
|
||||
verify(messages).retrieveSingle(key);
|
||||
verify(messages).retrieveSingle(player, key);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
package fr.xephi.authme.service;
|
||||
|
||||
import com.maxmind.geoip.Country;
|
||||
import com.maxmind.geoip.LookupService;
|
||||
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 org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
@@ -10,13 +16,11 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -29,8 +33,12 @@ public class GeoIpServiceTest {
|
||||
|
||||
private GeoIpService geoIpService;
|
||||
private File dataFolder;
|
||||
|
||||
@Mock
|
||||
private LookupService lookupService;
|
||||
private GeoIp2Provider lookupService;
|
||||
|
||||
@Mock
|
||||
private BukkitService bukkitService;
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
@@ -38,20 +46,24 @@ public class GeoIpServiceTest {
|
||||
@Before
|
||||
public void initializeGeoLiteApi() throws IOException {
|
||||
dataFolder = temporaryFolder.newFolder();
|
||||
geoIpService = new GeoIpService(dataFolder, lookupService);
|
||||
geoIpService = new GeoIpService(dataFolder, bukkitService, lookupService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetCountry() {
|
||||
public void shouldGetCountry() throws Exception {
|
||||
// given
|
||||
String ip = "123.45.67.89";
|
||||
InetAddress ip = InetAddress.getByName("123.45.67.89");
|
||||
String countryCode = "XX";
|
||||
|
||||
Country country = mock(Country.class);
|
||||
given(country.getCode()).willReturn(countryCode);
|
||||
given(lookupService.getCountry(ip)).willReturn(country);
|
||||
given(country.getIsoCode()).willReturn(countryCode);
|
||||
|
||||
CountryResponse response = mock(CountryResponse.class);
|
||||
given(response.getCountry()).willReturn(country);
|
||||
given(lookupService.getCountry(ip)).willReturn(response);
|
||||
|
||||
// when
|
||||
String result = geoIpService.getCountryCode(ip);
|
||||
String result = geoIpService.getCountryCode(ip.getHostAddress());
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(countryCode));
|
||||
@@ -59,7 +71,7 @@ public class GeoIpServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotLookUpCountryForLocalhostIp() {
|
||||
public void shouldNotLookUpCountryForLocalhostIp() throws Exception {
|
||||
// given
|
||||
String ip = "127.0.0.1";
|
||||
|
||||
@@ -68,20 +80,24 @@ public class GeoIpServiceTest {
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo("--"));
|
||||
verify(lookupService, never()).getCountry(anyString());
|
||||
verify(lookupService, never()).getCountry(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldLookUpCountryName() {
|
||||
public void shouldLookUpCountryName() throws Exception {
|
||||
// given
|
||||
String ip = "24.45.167.89";
|
||||
InetAddress ip = InetAddress.getByName("24.45.167.89");
|
||||
String countryName = "Ecuador";
|
||||
|
||||
Country country = mock(Country.class);
|
||||
given(country.getName()).willReturn(countryName);
|
||||
given(lookupService.getCountry(ip)).willReturn(country);
|
||||
|
||||
CountryResponse response = mock(CountryResponse.class);
|
||||
given(response.getCountry()).willReturn(country);
|
||||
given(lookupService.getCountry(ip)).willReturn(response);
|
||||
|
||||
// when
|
||||
String result = geoIpService.getCountryName(ip);
|
||||
String result = geoIpService.getCountryName(ip.getHostAddress());
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(countryName));
|
||||
@@ -89,16 +105,15 @@ public class GeoIpServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotLookUpCountryNameForLocalhostIp() {
|
||||
public void shouldNotLookUpCountryNameForLocalhostIp() throws Exception {
|
||||
// given
|
||||
String ip = "127.0.0.1";
|
||||
InetAddress ip = InetAddress.getByName("127.0.0.1");
|
||||
|
||||
// when
|
||||
String result = geoIpService.getCountryName(ip);
|
||||
String result = geoIpService.getCountryName(ip.getHostAddress());
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo("N/A"));
|
||||
verify(lookupService, never()).getCountry(ip);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -106,7 +106,6 @@ public class SessionServiceTest {
|
||||
// then
|
||||
assertThat(result, equalTo(false));
|
||||
verify(commonService).getProperty(PluginSettings.SESSIONS_ENABLED);
|
||||
verify(commonService).send(player, MessageKey.SESSION_EXPIRED);
|
||||
verify(dataSource).hasSession(name);
|
||||
verify(dataSource).setUnlogged(name);
|
||||
verify(dataSource).revokeSession(name);
|
||||
@@ -132,7 +131,6 @@ public class SessionServiceTest {
|
||||
// then
|
||||
assertThat(result, equalTo(false));
|
||||
verify(commonService).getProperty(PluginSettings.SESSIONS_ENABLED);
|
||||
verify(commonService).send(player, MessageKey.SESSION_EXPIRED);
|
||||
verify(dataSource).hasSession(name);
|
||||
verify(dataSource).setUnlogged(name);
|
||||
verify(dataSource).revokeSession(name);
|
||||
@@ -145,9 +143,10 @@ public class SessionServiceTest {
|
||||
String ip = "127.3.12.15";
|
||||
Player player = mockPlayerWithNameAndIp(name, ip);
|
||||
given(dataSource.hasSession(name)).willReturn(true);
|
||||
given(commonService.getProperty(PluginSettings.SESSIONS_TIMEOUT)).willReturn(8);
|
||||
PlayerAuth auth = PlayerAuth.builder()
|
||||
.name(name)
|
||||
.lastLogin(System.currentTimeMillis())
|
||||
.lastLogin(System.currentTimeMillis() - 7 * 60 * 1000)
|
||||
.lastIp("8.8.8.8").build();
|
||||
given(dataSource.getAuth(name)).willReturn(auth);
|
||||
|
||||
@@ -219,6 +218,7 @@ public class SessionServiceTest {
|
||||
String name = "Charles";
|
||||
Player player = mockPlayerWithNameAndIp(name, "144.117.118.145");
|
||||
given(dataSource.hasSession(name)).willReturn(true);
|
||||
given(commonService.getProperty(PluginSettings.SESSIONS_TIMEOUT)).willReturn(8);
|
||||
PlayerAuth auth = PlayerAuth.builder()
|
||||
.name(name)
|
||||
.lastIp(null)
|
||||
|
||||
Reference in New Issue
Block a user