#792 Add columns for registration IP and registration date

- Add columns for reg date and IP
- Rename "ip" to "last IP"
This commit is contained in:
ljacqu
2017-10-14 17:37:34 +02:00
parent 1487fc0d9e
commit 52d6476058
37 changed files with 362 additions and 235 deletions
@@ -44,26 +44,48 @@ public final class AuthMeMatchers {
}
public static Matcher<? super PlayerAuth> hasAuthBasicData(String name, String realName,
String email, String ip) {
String email, String lastIp) {
return new TypeSafeMatcher<PlayerAuth>() {
@Override
public boolean matchesSafely(PlayerAuth item) {
return Objects.equals(name, item.getNickname())
&& Objects.equals(realName, item.getRealName())
&& Objects.equals(email, item.getEmail())
&& Objects.equals(ip, item.getIp());
&& Objects.equals(lastIp, item.getLastIp());
}
@Override
public void describeTo(Description description) {
description.appendValue(String.format("PlayerAuth with name %s, realname %s, email %s, ip %s",
name, realName, email, ip));
description.appendValue(String.format("PlayerAuth with name %s, realname %s, email %s, lastIp %s",
name, realName, email, lastIp));
}
@Override
public void describeMismatchSafely(PlayerAuth item, Description description) {
description.appendValue(String.format("PlayerAuth with name %s, realname %s, email %s, ip %s",
item.getNickname(), item.getRealName(), item.getEmail(), item.getIp()));
description.appendValue(String.format("PlayerAuth with name %s, realname %s, email %s, lastIp %s",
item.getNickname(), item.getRealName(), item.getEmail(), item.getLastIp()));
}
};
}
public static Matcher<? super PlayerAuth> hasRegistrationInfo(String registrationIp, long registrationDate) {
return new TypeSafeMatcher<PlayerAuth>() {
@Override
public boolean matchesSafely(PlayerAuth item) {
return Objects.equals(registrationIp, item.getRegistrationIp())
&& Objects.equals(registrationDate, item.getRegistrationDate());
}
@Override
public void describeTo(Description description) {
description.appendValue(String.format("PlayerAuth with reg. IP %s and reg date %d",
registrationIp, registrationDate));
}
@Override
public void describeMismatchSafely(PlayerAuth item, Description description) {
description.appendValue(String.format("PlayerAuth with reg. IP %s and reg date %d",
item.getRegistrationIp(), item.getRegistrationDate()));
}
};
}
@@ -146,7 +146,7 @@ public class AuthMeApiTest {
String name = "Gabriel";
Player player = mockPlayerWithName(name);
PlayerAuth auth = PlayerAuth.builder().name(name)
.ip("93.23.44.55")
.lastIp("93.23.44.55")
.build();
given(playerCache.getAuth(name)).willReturn(auth);
@@ -171,7 +171,7 @@ public class AccountsCommandTest {
private static PlayerAuth authWithIp(String ip) {
return PlayerAuth.builder()
.name("Test")
.ip(ip)
.lastIp(ip)
.build();
}
}
@@ -1,6 +1,8 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.service.BukkitService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@@ -12,11 +14,12 @@ import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.both;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.verify;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
@@ -32,19 +35,25 @@ public class GetIpCommandTest {
@Mock
private BukkitService bukkitService;
@Mock
private DataSource dataSource;
@Test
public void shouldGetIpOfPlayer() {
// given
given(bukkitService.getPlayerExact(anyString())).willReturn(null);
given(dataSource.getAuth(anyString())).willReturn(null);
CommandSender sender = mock(CommandSender.class);
String name = "Testt";
// when
command.executeCommand(sender, Collections.singletonList("Testt"));
command.executeCommand(sender, Collections.singletonList(name));
// then
verify(bukkitService).getPlayerExact("Testt");
verify(sender).sendMessage(argThat(containsString("not online")));
verify(bukkitService).getPlayerExact(name);
verify(dataSource).getAuth(name);
verify(sender, only()).sendMessage(argThat(containsString("not registered")));
}
@Test
@@ -54,6 +63,8 @@ public class GetIpCommandTest {
String ip = "123.34.56.88";
Player player = mockPlayer(playerName, ip);
given(bukkitService.getPlayerExact(playerName)).willReturn(player);
PlayerAuth auth = PlayerAuth.builder().name("t").lastIp("44.33.22.11").registrationIp("77.11.44.88").build();
given(dataSource.getAuth(playerName)).willReturn(auth);
CommandSender sender = mock(CommandSender.class);
// when
@@ -61,7 +72,29 @@ public class GetIpCommandTest {
// then
verify(bukkitService).getPlayerExact(playerName);
verify(sender).sendMessage(argThat(allOf(containsString(playerName), containsString(ip))));
verify(dataSource).getAuth(playerName);
verify(sender).sendMessage(argThat(both(containsString(playerName)).and(containsString(ip))));
verify(sender).sendMessage(argThat(both(containsString("44.33.22.11")).and(containsString("77.11.44.88"))));
}
@Test
public void shouldHandleUnregisteredOnlinePlayer() {
// given
String playerName = "Test";
String ip = "44.111.22.33";
Player player = mockPlayer(playerName, ip);
given(bukkitService.getPlayerExact(playerName)).willReturn(player);
given(dataSource.getAuth(anyString())).willReturn(null);
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList(playerName));
// then
verify(bukkitService).getPlayerExact(playerName);
verify(dataSource).getAuth(playerName);
verify(sender).sendMessage(argThat(both(containsString(playerName)).and(containsString(ip))));
verify(sender).sendMessage(argThat(containsString("not registered")));
}
private static Player mockPlayer(String name, String ip) {
@@ -65,7 +65,7 @@ public class LastLoginCommandTest {
(412 * DAY_IN_MSEC + 10 * HOUR_IN_MSEC - 9000);
PlayerAuth auth = mock(PlayerAuth.class);
given(auth.getLastLogin()).willReturn(lastLogin);
given(auth.getIp()).willReturn("123.45.66.77");
given(auth.getLastIp()).willReturn("123.45.66.77");
given(dataSource.getAuth(player)).willReturn(auth);
CommandSender sender = mock(CommandSender.class);
@@ -94,7 +94,7 @@ public class LastLoginCommandTest {
- (412 * DAY_IN_MSEC + 10 * HOUR_IN_MSEC - 9000);
PlayerAuth auth = mock(PlayerAuth.class);
given(auth.getLastLogin()).willReturn(lastLogin);
given(auth.getIp()).willReturn("123.45.66.77");
given(auth.getLastIp()).willReturn("123.45.66.77");
given(dataSource.getAuth(name)).willReturn(auth);
// when
@@ -12,6 +12,7 @@ import java.util.Set;
import static fr.xephi.authme.AuthMeMatchers.equalToHash;
import static fr.xephi.authme.AuthMeMatchers.hasAuthBasicData;
import static fr.xephi.authme.AuthMeMatchers.hasAuthLocation;
import static fr.xephi.authme.AuthMeMatchers.hasRegistrationInfo;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
@@ -97,11 +98,13 @@ public abstract class AbstractDataSourceIntegrationTest {
assertThat(bobbyAuth, hasAuthBasicData("bobby", "Bobby", "your@email.com", "123.45.67.89"));
assertThat(bobbyAuth, hasAuthLocation(1.05, 2.1, 4.2, "world", -0.44f, 2.77f));
assertThat(bobbyAuth, hasRegistrationInfo("127.0.4.22", 1436778723L));
assertThat(bobbyAuth.getLastLogin(), equalTo(1449136800L));
assertThat(bobbyAuth.getPassword(), equalToHash("$SHA$11aa0706173d7272$dbba966"));
assertThat(userAuth, hasAuthBasicData("user", "user", "user@example.org", "34.56.78.90"));
assertThat(userAuth, hasAuthLocation(124.1, 76.3, -127.8, "nether", 0.23f, 4.88f));
assertThat(userAuth, hasRegistrationInfo(null, 0));
assertThat(userAuth.getLastLogin(), equalTo(1453242857L));
assertThat(userAuth.getPassword(), equalToHash("b28c32f624a4eb161d6adc9acb5bfc5b", "f750ba32"));
}
@@ -211,7 +214,7 @@ public abstract class AbstractDataSourceIntegrationTest {
DataSource dataSource = getDataSource();
PlayerAuth bobby = PlayerAuth.builder()
.name("bobby").realName("BOBBY").lastLogin(123L)
.ip("12.12.12.12").build();
.lastIp("12.12.12.12").build();
// when
boolean response = dataSource.updateSession(bobby);
@@ -298,7 +301,9 @@ public abstract class AbstractDataSourceIntegrationTest {
List<String> initialList = dataSource.getAllAuthsByIp("123.45.67.89");
List<String> emptyList = dataSource.getAllAuthsByIp("8.8.8.8");
for (int i = 0; i < 3; ++i) {
dataSource.saveAuth(PlayerAuth.builder().name("test-" + i).ip("123.45.67.89").build());
PlayerAuth auth = PlayerAuth.builder().name("test-" + i).lastIp("123.45.67.89").build();
dataSource.saveAuth(auth);
dataSource.updateSession(auth); // trigger storage of last IP
}
List<String> updatedList = dataSource.getAllAuthsByIp("123.45.67.89");
@@ -329,8 +334,9 @@ public abstract class AbstractDataSourceIntegrationTest {
public void shouldGetRecordsToPurge() {
// given
DataSource dataSource = getDataSource();
PlayerAuth auth = PlayerAuth.builder().name("potato").lastLogin(0).build();
PlayerAuth auth = PlayerAuth.builder().name("potato").lastLogin(0L).build();
dataSource.saveAuth(auth);
dataSource.updateSession(auth);
// 1453242857 -> user, 1449136800 -> bobby, 0 -> potato
// when
@@ -79,7 +79,7 @@ public class FlatFileIntegrationTest {
public void shouldAddAuth() {
// given / when
boolean response = dataSource.saveAuth(
PlayerAuth.builder().name("Test").email("user@EXAMPLE.org").ip("123.45.67.77").build());
PlayerAuth.builder().name("Test").email("user@EXAMPLE.org").lastIp("123.45.67.77").build());
List<PlayerAuth> authList = dataSource.getAllAuths();
// then
@@ -98,6 +98,7 @@ public class AbstractDataSourceConverterTest {
verify(destination).getType();
verify(destination, times(3)).isAuthAvailable(anyString());
verify(destination, times(2)).saveAuth(any(PlayerAuth.class));
verify(destination, times(2)).updateSession(any(PlayerAuth.class));
verify(destination, times(2)).updateQuitLoc(any(PlayerAuth.class));
verifyNoMoreInteractions(destination);
verify(sender).sendMessage(argThat(containsString(auths.get(0).getNickname())));
@@ -27,6 +27,8 @@ import java.sql.Statement;
import static fr.xephi.authme.AuthMeMatchers.equalToHash;
import static fr.xephi.authme.AuthMeMatchers.hasAuthLocation;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -71,16 +73,19 @@ public class LoginSecurityConverterTest {
assertThat(captor.getAllValues().get(0).getNickname(), equalTo("player1"));
assertThat(captor.getAllValues().get(0).getRealName(), equalTo("Player1"));
assertThat(captor.getAllValues().get(0).getLastLogin(), equalTo(1494242093652L));
assertThat(captor.getAllValues().get(0).getRegistrationDate(), equalTo(1494242093400L));
assertThat(captor.getAllValues().get(0).getPassword(), equalToHash("$2a$10$E1Ri7XKeIIBv4qVaiPplgepT7QH9xGFh3hbHfcmCjq7hiW.UBTiGK"));
assertThat(captor.getAllValues().get(0).getIp(), equalTo("127.0.0.1"));
assertThat(captor.getAllValues().get(0).getLastIp(), equalTo("127.0.0.1"));
assertThat(captor.getAllValues().get(1).getNickname(), equalTo("player2"));
assertThat(captor.getAllValues().get(1).getLastLogin(), equalTo(1494242174589L));
assertThat(captor.getAllValues().get(1).getIp(), equalTo("127.4.5.6"));
assertThat(captor.getAllValues().get(1).getLastIp(), equalTo("127.4.5.6"));
assertThat(captor.getAllValues().get(2).getRealName(), equalTo("Player3"));
assertThat(captor.getAllValues().get(2).getPassword(), equalToHash("$2a$10$WFui8KSXMLDOVXKFpCLyPukPi4M82w1cv/rNojsAnwJjba3pp8sba"));
assertThat(captor.getAllValues().get(2), hasAuthLocation(14.24, 67.99, -12.83, "hubb", -10f, 185f));
assertThat(captor.getAllValues().get(2).getLastIp(), equalTo("127.0.0.1"));
assertIsCloseTo(captor.getAllValues().get(2).getRegistrationDate(), System.currentTimeMillis(), 500L);
}
@Test
@@ -99,11 +104,12 @@ public class LoginSecurityConverterTest {
assertThat(captor.getAllValues().get(0).getRealName(), equalTo("Player1"));
assertThat(captor.getAllValues().get(0).getLastLogin(), equalTo(1494242093000L));
assertThat(captor.getAllValues().get(0).getPassword(), equalToHash("$2a$10$E1Ri7XKeIIBv4qVaiPplgepT7QH9xGFh3hbHfcmCjq7hiW.UBTiGK"));
assertThat(captor.getAllValues().get(0).getIp(), equalTo("127.0.0.1"));
assertThat(captor.getAllValues().get(0).getLastIp(), equalTo("127.0.0.1"));
assertThat(captor.getAllValues().get(0).getRegistrationDate(), equalTo(1494194400000L));
assertThat(captor.getAllValues().get(1).getNickname(), equalTo("player2"));
assertThat(captor.getAllValues().get(1).getLastLogin(), equalTo(1489317753000L));
assertThat(captor.getAllValues().get(1).getIp(), equalTo("127.4.5.6"));
assertThat(captor.getAllValues().get(1).getLastIp(), equalTo("127.4.5.6"));
assertThat(captor.getAllValues().get(2).getRealName(), equalTo("Player3"));
assertThat(captor.getAllValues().get(2).getPassword(), equalToHash("$2a$10$WFui8KSXMLDOVXKFpCLyPukPi4M82w1cv/rNojsAnwJjba3pp8sba"));
@@ -129,4 +135,8 @@ public class LoginSecurityConverterTest {
}
return connection;
}
private static void assertIsCloseTo(long value1, long value2, long tolerance) {
assertThat(Math.abs(value1 - value2), not(greaterThan(tolerance)));
}
}
@@ -12,8 +12,6 @@ import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.settings.properties.EmailSettings;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -22,9 +20,10 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static fr.xephi.authme.AuthMeMatchers.hasAuthBasicData;
import static fr.xephi.authme.AuthMeMatchers.hasAuthLocation;
import static fr.xephi.authme.AuthMeMatchers.stringWithLength;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
@@ -116,17 +115,15 @@ public class EmailRegisterExecutorProviderTest {
Player player = mock(Player.class);
TestHelper.mockPlayerIp(player, "123.45.67.89");
given(player.getName()).willReturn("Veronica");
World world = mock(World.class);
given(world.getName()).willReturn("someWorld");
given(player.getLocation()).willReturn(new Location(world, 48, 96, 144));
EmailRegisterParams params = EmailRegisterParams.of(player, "test@example.com");
// when
PlayerAuth auth = executor.buildPlayerAuth(params);
// then
assertThat(auth, hasAuthBasicData("veronica", "Veronica", "test@example.com", "123.45.67.89"));
assertThat(auth, hasAuthLocation(48, 96, 144, "someWorld", 0, 0));
assertThat(auth, hasAuthBasicData("veronica", "Veronica", "test@example.com", "127.0.0.1"));
assertThat(auth.getRegistrationIp(), equalTo("123.45.67.89"));
assertIsCloseTo(auth.getRegistrationDate(), System.currentTimeMillis(), 1000);
assertThat(auth.getPassword().getHash(), stringWithLength(12));
}
@@ -167,4 +164,7 @@ public class EmailRegisterExecutorProviderTest {
verifyZeroInteractions(syncProcessManager);
}
private static void assertIsCloseTo(long value1, long value2, long tolerance) {
assertThat(Math.abs(value1 - value2), not(greaterThan(tolerance)));
}
}
@@ -13,8 +13,6 @@ import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.service.ValidationService.ValidationResult;
import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -24,8 +22,9 @@ import org.mockito.junit.MockitoJUnitRunner;
import static fr.xephi.authme.AuthMeMatchers.equalToHash;
import static fr.xephi.authme.AuthMeMatchers.hasAuthBasicData;
import static fr.xephi.authme.AuthMeMatchers.hasAuthLocation;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
@@ -98,17 +97,15 @@ public class PasswordRegisterExecutorTest {
invocation -> new HashedPassword(invocation.getArgument(0)));
Player player = mockPlayerWithName("S1m0N");
TestHelper.mockPlayerIp(player, "123.45.67.89");
World world = mock(World.class);
given(world.getName()).willReturn("someWorld");
given(player.getLocation()).willReturn(new Location(world, 48, 96, 144, 1.1f, 0.28f));
PasswordRegisterParams params = PasswordRegisterParams.of(player, "pass", "mail@example.org");
// when
PlayerAuth auth = executor.buildPlayerAuth(params);
// then
assertThat(auth, hasAuthBasicData("s1m0n", "S1m0N", "mail@example.org", "123.45.67.89"));
assertThat(auth, hasAuthLocation(48, 96, 144, "someWorld", 1.1f, 0.28f));
assertThat(auth, hasAuthBasicData("s1m0n", "S1m0N", "mail@example.org", "127.0.0.1"));
assertThat(auth.getRegistrationIp(), equalTo("123.45.67.89"));
assertIsCloseTo(auth.getRegistrationDate(), System.currentTimeMillis(), 500);
assertThat(auth.getPassword(), equalToHash("pass"));
}
@@ -149,4 +146,8 @@ public class PasswordRegisterExecutorTest {
given(player.getName()).willReturn(name);
return player;
}
private static void assertIsCloseTo(long value1, long value2, long tolerance) {
assertThat(Math.abs(value1 - value2), not(greaterThan(tolerance)));
}
}
@@ -3,13 +3,13 @@ package fr.xephi.authme.process.register.executors;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.security.crypts.HashedPassword;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.junit.Test;
import static fr.xephi.authme.AuthMeMatchers.equalToHash;
import static fr.xephi.authme.AuthMeMatchers.hasAuthBasicData;
import static fr.xephi.authme.AuthMeMatchers.hasAuthLocation;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.lessThan;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -26,10 +26,6 @@ public class PlayerAuthBuilderHelperTest {
given(player.getName()).willReturn("Noah");
String ip = "192.168.34.47";
TestHelper.mockPlayerIp(player, ip);
World world = mock(World.class);
given(world.getName()).willReturn("worldName");
Location location = new Location(world, 123, 80, -99, 2.45f, 7.61f);
given(player.getLocation()).willReturn(location);
HashedPassword hashedPassword = new HashedPassword("myHash0001");
String email = "test@example.org";
@@ -37,8 +33,10 @@ public class PlayerAuthBuilderHelperTest {
PlayerAuth auth = PlayerAuthBuilderHelper.createPlayerAuth(player, hashedPassword, email);
// then
assertThat(auth, hasAuthBasicData("noah", "Noah", email, ip));
assertThat(auth, hasAuthLocation(123, 80, -99, "worldName", 2.45f, 7.61f));
assertThat(auth, hasAuthBasicData("noah", "Noah", email, "127.0.0.1"));
assertThat(auth.getRegistrationIp(), equalTo("192.168.34.47"));
assertThat(Math.abs(auth.getRegistrationDate() - System.currentTimeMillis()), lessThan(1000L));
assertThat(auth.getPassword(), equalToHash("myHash0001"));
}
@Test