#792 #814 Implement SQLite migration, allow last IP to be nullable in MySQL

- Old SQLite setups have the last IP column as NOT NULL but without a default value. With the new concept (where we don't set a last IP on player registration) it fails.
  - Create an /authme debug child that allows to migrate SQLite (tricky because SQLite does not support dropping or modifying columns)
  - Allow last IP column to be NOT NULL in MySQL as well (extend MySQL /authme debug child)
- Add TODO comments with follow-up issue to extend our commands with new registration IP field
This commit is contained in:
ljacqu
2017-10-21 10:24:40 +02:00
parent b5ea48085c
commit 1651a61063
29 changed files with 688 additions and 130 deletions
@@ -82,7 +82,8 @@ public class AccountsCommandTest {
// given
CommandSender sender = mock(CommandSender.class);
List<String> arguments = Collections.singletonList("SomeUser");
given(dataSource.getAuth("someuser")).willReturn(mock(PlayerAuth.class));
PlayerAuth auth = authWithIp("144.56.77.88");
given(dataSource.getAuth("someuser")).willReturn(auth);
// when
command.executeCommand(sender, arguments);
@@ -11,6 +11,8 @@ import java.util.List;
import java.util.Set;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
@@ -20,11 +22,16 @@ import static org.junit.Assert.fail;
public class DebugSectionConsistencyTest {
private static List<Class<?>> debugClasses;
private static List<DebugSection> debugSections;
@BeforeClass
public static void collectClasses() {
debugClasses = new ClassCollector(
TestHelper.SOURCES_FOLDER, TestHelper.PROJECT_ROOT + "command/executable/authme/debug").collectClasses();
// TODO ljacqu 20171021: Improve ClassCollector (pass pkg by class, improve #getInstancesOfType's instantiation)
ClassCollector classCollector = new ClassCollector(
TestHelper.SOURCES_FOLDER, TestHelper.PROJECT_ROOT + "command/executable/authme/debug");
debugClasses = classCollector.collectClasses();
debugSections = classCollector.getInstancesOfType(DebugSection.class, clz -> instantiate(clz));
}
@Test
@@ -40,13 +47,26 @@ public class DebugSectionConsistencyTest {
@Test
public void shouldHaveDifferentSubcommandName() throws IllegalAccessException, InstantiationException {
Set<String> names = new HashSet<>();
for (Class<?> clazz : debugClasses) {
if (DebugSection.class.isAssignableFrom(clazz) && !clazz.isInterface()) {
DebugSection debugSection = (DebugSection) clazz.newInstance();
if (!names.add(debugSection.getName())) {
fail("Encountered name '" + debugSection.getName() + "' a second time in " + clazz);
}
for (DebugSection debugSection : debugSections) {
if (!names.add(debugSection.getName())) {
fail("Encountered name '" + debugSection.getName() + "' a second time in " + debugSection.getClass());
}
}
}
@Test
public void shouldAllHaveDescription() {
for (DebugSection debugSection : debugSections) {
assertThat("Description of '" + debugSection.getClass() + "' may not be null",
debugSection.getDescription(), not(nullValue()));
}
}
private static DebugSection instantiate(Class<? extends DebugSection> clazz) {
try {
return ClassCollector.canInstantiate(clazz) ? clazz.newInstance() : null;
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
}
@@ -2,8 +2,11 @@ package fr.xephi.authme.command.executable.authme.debug;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.data.limbo.LimboPlayer;
import fr.xephi.authme.data.limbo.LimboService;
import fr.xephi.authme.datasource.CacheDataSource;
import fr.xephi.authme.datasource.DataSource;
import org.bukkit.Location;
import org.junit.Before;
import org.junit.Test;
@@ -89,4 +92,39 @@ public class DebugSectionUtilsTest {
// then
assertThat(result, nullValue());
}
@Test
public void shouldReturnSameDataSourceInstance() {
// given
DataSource dataSource = mock(DataSource.class);
// when
DataSource result = DebugSectionUtils.unwrapSourceFromCacheDataSource(dataSource);
// then
assertThat(result, equalTo(dataSource));
}
@Test
public void shouldUnwrapCacheDataSource() {
// given
DataSource source = mock(DataSource.class);
PlayerCache playerCache = mock(PlayerCache.class);
CacheDataSource cacheDataSource = new CacheDataSource(source, playerCache);
// when
DataSource result = DebugSectionUtils.unwrapSourceFromCacheDataSource(cacheDataSource);
// then
assertThat(result, equalTo(source));
}
@Test
public void shouldCastOrReturnNull() {
// given / when / then
assertThat(DebugSectionUtils.castToTypeOrNull("test", String.class), equalTo("test"));
assertThat(DebugSectionUtils.castToTypeOrNull("test", Integer.class), nullValue());
assertThat(DebugSectionUtils.castToTypeOrNull(5, String.class), nullValue());
assertThat(DebugSectionUtils.castToTypeOrNull(5, Integer.class), equalTo(5));
}
}
@@ -7,7 +7,7 @@ import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.CacheDataSource;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.datasource.MySQL;
import fr.xephi.authme.datasource.MySqlTestUtil;
import fr.xephi.authme.datasource.SqlDataSourceTestUtil;
import fr.xephi.authme.settings.Settings;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -34,32 +34,6 @@ public class MySqlDefaultChangerTest {
@Mock
private Settings settings;
@Test
public void shouldReturnSameDataSourceInstance() {
// given
DataSource dataSource = mock(DataSource.class);
// when
DataSource result = MySqlDefaultChanger.unwrapSourceFromCacheDataSource(dataSource);
// then
assertThat(result, equalTo(dataSource));
}
@Test
public void shouldUnwrapCacheDataSource() {
// given
DataSource source = mock(DataSource.class);
PlayerCache playerCache = mock(PlayerCache.class);
CacheDataSource cacheDataSource = new CacheDataSource(source, playerCache);
// when
DataSource result = MySqlDefaultChanger.unwrapSourceFromCacheDataSource(cacheDataSource);
// then
assertThat(result, equalTo(source));
}
@Test
public void shouldReturnMySqlConnection() throws SQLException {
// given
@@ -68,7 +42,7 @@ public class MySqlDefaultChangerTest {
HikariDataSource dataSource = mock(HikariDataSource.class);
Connection connection = mock(Connection.class);
given(dataSource.getConnection()).willReturn(connection);
MySQL mySQL = MySqlTestUtil.createMySql(settings, dataSource);
MySQL mySQL = SqlDataSourceTestUtil.createMySql(settings, dataSource);
MySqlDefaultChanger defaultChanger = createDefaultChanger(mySQL);
// when
@@ -0,0 +1,109 @@
package fr.xephi.authme.command.executable.authme.debug;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.SQLite;
import fr.xephi.authme.settings.Settings;
import org.bukkit.command.CommandSender;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import static fr.xephi.authme.AuthMeMatchers.hasAuthBasicData;
import static fr.xephi.authme.AuthMeMatchers.hasAuthLocation;
import static fr.xephi.authme.datasource.SqlDataSourceTestUtil.createSqliteAndInitialize;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
/**
* Integration test for {@link SqliteMigrater}. Uses a real SQLite database.
*/
public class SqliteMigraterIntegrationTest {
private static final String CONFIRMATION_CODE = "ABCD";
private SqliteMigrater sqliteMigrater;
private SQLite sqLite;
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Before
public void setup() throws SQLException, IOException, NoSuchMethodException {
TestHelper.setupLogger();
Settings settings = mock(Settings.class);
TestHelper.returnDefaultsForAllProperties(settings);
File sqliteDbFile = TestHelper.getJarFile(TestHelper.PROJECT_ROOT + "datasource/sqlite.april2016.db");
File tempFile = temporaryFolder.newFile();
Files.copy(sqliteDbFile, tempFile);
Connection con = DriverManager.getConnection("jdbc:sqlite:" + tempFile.getPath());
sqLite = createSqliteAndInitialize(settings, con);
sqliteMigrater = new SqliteMigrater();
ReflectionTestUtils.setField(sqliteMigrater, "dataSource", sqLite);
ReflectionTestUtils.setField(sqliteMigrater, "settings", settings);
ReflectionTestUtils.setField(sqliteMigrater, "confirmationCode", CONFIRMATION_CODE);
sqliteMigrater.setSqLiteField();
}
@Test
public void shouldRun() throws ClassNotFoundException, SQLException {
// given
CommandSender sender = mock(CommandSender.class);
// when
sqliteMigrater.execute(sender, Collections.singletonList(CONFIRMATION_CODE));
// then
List<PlayerAuth> auths = sqLite.getAllAuths();
assertThat(Lists.transform(auths, PlayerAuth::getNickname),
containsInAnyOrder("mysql1", "mysql2", "mysql3", "mysql4", "mysql5", "mysql6"));
PlayerAuth auth1 = getByNameOrFail("mysql1", auths);
assertThat(auth1, hasAuthBasicData("mysql1", "mysql1", "user1@example.com", "192.168.4.41"));
assertThat(auth1, hasAuthLocation(0, 0, 0, "world1", 0, 0));
assertThat(auth1.getLastLogin(), equalTo(1472992664137L));
PlayerAuth auth2 = getByNameOrFail("mysql2", auths);
assertThat(auth2, hasAuthBasicData("mysql2", "Player", "user2@example.com", null));
assertThat(auth2, hasAuthLocation(0, 0, 0, "world2", 0, 0));
assertThat(auth2.getLastLogin(), equalTo(1472992668391L));
PlayerAuth auth3 = getByNameOrFail("mysql3", auths);
assertThat(auth3, hasAuthBasicData("mysql3", "mysql3", null, "132.54.76.98"));
assertThat(auth3, hasAuthLocation(0, 0, 0, "world3", 0, 0));
assertThat(auth3.getLastLogin(), equalTo(1472992672790L));
PlayerAuth auth4 = getByNameOrFail("mysql4", auths);
assertThat(auth4, hasAuthBasicData("mysql4", "MySQL4", null, null));
assertThat(auth4, hasAuthLocation(25, 4, 17, "world4", 0, 0));
assertThat(auth4.getLastLogin(), equalTo(1472992676790L));
PlayerAuth auth5 = getByNameOrFail("mysql5", auths);
assertThat(auth5, hasAuthBasicData("mysql5", "mysql5", null, null));
assertThat(auth5, hasAuthLocation(0, 0, 0, "world5", 0, 0));
assertThat(auth5.getLastLogin(), equalTo(1472992680922L));
PlayerAuth auth6 = getByNameOrFail("mysql6", auths);
assertThat(auth6, hasAuthBasicData("mysql6", "MySql6", "user6@example.com", "44.45.67.188"));
assertThat(auth6, hasAuthLocation(28.5, 53.43, -147.23, "world6", 0, 0));
assertThat(auth6.getLastLogin(), equalTo(1472992686300L));
}
private static PlayerAuth getByNameOrFail(String name, List<PlayerAuth> auths) {
return auths.stream()
.filter(auth -> name.equals(auth.getNickname()))
.findFirst().orElseThrow(() -> new IllegalStateException("No PlayerAuth with name '" + name + "'"));
}
}