#392 Add datasource integration tests

This commit is contained in:
ljacqu 2016-02-26 22:12:16 +01:00
parent 12691a12f1
commit 27b1fa770f
6 changed files with 93 additions and 49 deletions

View File

@ -224,12 +224,6 @@ public class CacheDataSource implements DataSource {
return source.getAccountsRegistered();
}
@Override
public void updateName(final String oldOne, final String newOne) { // unused method
source.updateName(oldOne, newOne);
cachedAuths.invalidate(oldOne);
}
@Override
public boolean updateRealName(String user, String realName) {
boolean result = source.updateRealName(user, realName);

View File

@ -169,14 +169,6 @@ public interface DataSource {
*/
int getAccountsRegistered();
/**
* Method updateName.
*
* @param oldOne String
* @param newOne String
*/
void updateName(String oldOne, String newOne);
boolean updateRealName(String user, String realName);
boolean updateIp(String user, String ip);

View File

@ -599,14 +599,6 @@ public class FlatFile implements DataSource {
return result;
}
@Override
public void updateName(String oldOne, String newOne) {
PlayerAuth auth = this.getAuth(oldOne);
auth.setNickname(newOne);
this.saveAuth(auth);
this.removeAuth(oldOne);
}
@Override
public boolean updateRealName(String user, String realName) {
return false;

View File

@ -1,5 +1,6 @@
package fr.xephi.authme.datasource;
import com.google.common.annotations.VisibleForTesting;
import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.pool.HikariPool.PoolInitializationException;
import fr.xephi.authme.AuthMe;
@ -81,6 +82,7 @@ public class MySQL implements DataSource {
}
}
@VisibleForTesting
MySQL(NewSetting settings, HikariDataSource hikariDataSource) {
this.host = settings.getProperty(DatabaseSettings.MYSQL_HOST);
this.port = settings.getProperty(DatabaseSettings.MYSQL_PORT);
@ -817,18 +819,6 @@ public class MySQL implements DataSource {
return result;
}
@Override
public void updateName(String oldOne, String newOne) {
String sql = "UPDATE " + tableName + " SET " + col.NAME + "=? WHERE " + col.NAME + "=?;";
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, newOne);
pst.setString(2, oldOne);
pst.executeUpdate();
} catch (SQLException ex) {
logSqlException(ex);
}
}
@Override
public boolean updateRealName(String user, String realName) {
String sql = "UPDATE " + tableName + " SET " + col.REAL_NAME + "=? WHERE " + col.NAME + "=?;";

View File

@ -523,21 +523,6 @@ public class SQLite implements DataSource {
return 0;
}
@Override
public void updateName(String oldOne, String newOne) {
PreparedStatement pst = null;
try {
pst = con.prepareStatement("UPDATE " + tableName + " SET " + col.NAME + "=? WHERE " + col.NAME + "=?;");
pst.setString(1, newOne);
pst.setString(2, oldOne);
pst.executeUpdate();
} catch (SQLException ex) {
logSqlException(ex);
} finally {
close(pst);
}
}
@Override
public boolean updateRealName(String user, String realName) {
String sql = "UPDATE " + tableName + " SET " + col.REAL_NAME + "=? WHERE " + col.NAME + "=?;";

View File

@ -4,15 +4,18 @@ import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.security.crypts.HashedPassword;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import static fr.xephi.authme.datasource.AuthMeMatchers.equalToHash;
import static fr.xephi.authme.datasource.AuthMeMatchers.hasAuthBasicData;
import static fr.xephi.authme.datasource.AuthMeMatchers.hasAuthLocation;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeThat;
/**
* Abstract class for data source integration tests.
@ -201,4 +204,92 @@ public abstract class AbstractDataSourceIntegrationTest {
assertThat(dataSource.getAuth("user"), hasAuthLocation(143, -42.12, 29.47, "the_end"));
}
@Test
public void shouldDeletePlayers() {
// given
DataSource dataSource = getDataSource();
List<String> playersToDelete = Arrays.asList("bobby", "doesNotExist");
assumeThat(dataSource.getAccountsRegistered(), equalTo(2));
// when
dataSource.purgeBanned(playersToDelete);
// then
assertThat(dataSource.getAccountsRegistered(), equalTo(1));
assertThat(dataSource.isAuthAvailable("bobby"), equalTo(false));
assertThat(dataSource.isAuthAvailable("user"), equalTo(true));
}
@Test
public void shouldUpdateEmail() {
// given
DataSource dataSource = getDataSource();
String email = "new-user@mail.tld";
PlayerAuth userAuth = PlayerAuth.builder().name("user").email(email).build();
PlayerAuth invalidAuth = PlayerAuth.builder().name("invalid").email("addr@example.com").build();
// when
boolean response1 = dataSource.updateEmail(userAuth);
boolean response2 = dataSource.updateEmail(invalidAuth);
// then
assertThat(response1 && response2, equalTo(true));
assertThat(dataSource.getAllAuths(), hasItem(hasAuthBasicData("user", "user", email, "34.56.78.90")));
}
@Test
public void shouldUpdateIp() {
// given
DataSource dataSource = getDataSource();
String ip = "250.230.67.73";
// when
boolean response1 = dataSource.updateIp("bobby", ip);
boolean response2 = dataSource.updateIp("bogus", "123.123.123.123");
// then
assertThat(response1 && response2, equalTo(true));
assertThat(dataSource.getAllAuths(), hasItem(hasAuthBasicData("bobby", "Bobby", "your@email.com", ip)));
}
@Test
public void shouldCountAuths() {
// given
DataSource dataSource = getDataSource();
// when
int initialCount = dataSource.getAccountsRegistered();
for (int i = 0; i < 4; ++i) {
dataSource.saveAuth(PlayerAuth.builder().name("test-" + i).build());
}
int endCount = dataSource.getAccountsRegistered();
// then
assertThat(initialCount, equalTo(2));
assertThat(endCount, equalTo(6));
}
@Test
public void shouldGetAllUsersByIp() {
// given
DataSource dataSource = getDataSource();
// when
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());
}
List<String> updatedList = dataSource.getAllAuthsByIp("123.45.67.89");
// then
assertThat(initialList, hasSize(1));
assertThat(initialList.get(0), equalTo("bobby"));
assertThat(emptyList, hasSize(0));
assertThat(updatedList, hasSize(4));
assertThat(updatedList, hasItem(equalTo("bobby")));
assertThat(updatedList, hasItem(equalTo("test-1")));
}
}