288 lines
10 KiB
Java
288 lines
10 KiB
Java
package fr.xephi.authme.datasource;
|
|
|
|
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.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.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.
|
|
*/
|
|
public abstract class AbstractDataSourceIntegrationTest {
|
|
|
|
protected abstract DataSource getDataSource();
|
|
|
|
@Test
|
|
public void shouldReturnIfAuthIsAvailableOrNot() {
|
|
// given
|
|
DataSource dataSource = getDataSource();
|
|
|
|
// when
|
|
boolean isBobbyAvailable = dataSource.isAuthAvailable("bobby");
|
|
boolean isChrisAvailable = dataSource.isAuthAvailable("chris");
|
|
boolean isUserAvailable = dataSource.isAuthAvailable("USER");
|
|
|
|
// then
|
|
assertThat(isBobbyAvailable, equalTo(true));
|
|
assertThat(isChrisAvailable, equalTo(false));
|
|
assertThat(isUserAvailable, equalTo(true));
|
|
}
|
|
|
|
@Test
|
|
public void shouldReturnPassword() {
|
|
// given
|
|
DataSource dataSource = getDataSource();
|
|
|
|
// when
|
|
HashedPassword bobbyPassword = dataSource.getPassword("bobby");
|
|
HashedPassword invalidPassword = dataSource.getPassword("doesNotExist");
|
|
HashedPassword userPassword = dataSource.getPassword("user");
|
|
|
|
// then
|
|
assertThat(bobbyPassword, equalToHash("$SHA$11aa0706173d7272$dbba966"));
|
|
assertThat(invalidPassword, nullValue());
|
|
assertThat(userPassword, equalToHash("b28c32f624a4eb161d6adc9acb5bfc5b", "f750ba32"));
|
|
}
|
|
|
|
@Test
|
|
public void shouldGetAuth() {
|
|
// given
|
|
DataSource dataSource = getDataSource();
|
|
|
|
// when
|
|
PlayerAuth invalidAuth = dataSource.getAuth("notInDB");
|
|
PlayerAuth bobbyAuth = dataSource.getAuth("Bobby");
|
|
PlayerAuth userAuth = dataSource.getAuth("user");
|
|
|
|
// then
|
|
assertThat(invalidAuth, nullValue());
|
|
|
|
assertThat(bobbyAuth, hasAuthBasicData("bobby", "Bobby", "your@email.com", "123.45.67.89"));
|
|
assertThat(bobbyAuth, hasAuthLocation(1.05, 2.1, 4.2, "world"));
|
|
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"));
|
|
assertThat(userAuth.getLastLogin(), equalTo(1453242857L));
|
|
assertThat(userAuth.getPassword(), equalToHash("b28c32f624a4eb161d6adc9acb5bfc5b", "f750ba32"));
|
|
}
|
|
|
|
@Test
|
|
public void shouldCountAuthsByEmail() {
|
|
// given
|
|
DataSource dataSource = getDataSource();
|
|
|
|
// when
|
|
int userMailCount = dataSource.countAuthsByEmail("user@example.ORG");
|
|
int invalidMailCount = dataSource.countAuthsByEmail("not.in.db@example.com");
|
|
boolean response = dataSource.saveAuth(
|
|
PlayerAuth.builder().name("Test").email("user@EXAMPLE.org").build());
|
|
int newUserCount = dataSource.countAuthsByEmail("user@Example.org");
|
|
|
|
// then
|
|
assertThat(userMailCount, equalTo(1));
|
|
assertThat(invalidMailCount, equalTo(0));
|
|
assertThat(response, equalTo(true));
|
|
assertThat(newUserCount, equalTo(2));
|
|
}
|
|
|
|
@Test
|
|
public void shouldReturnAllAuths() {
|
|
// given
|
|
DataSource dataSource = getDataSource();
|
|
|
|
// when
|
|
List<PlayerAuth> authList = dataSource.getAllAuths();
|
|
boolean response = dataSource.saveAuth(
|
|
PlayerAuth.builder().name("Test").email("user@EXAMPLE.org").build());
|
|
List<PlayerAuth> newAuthList = dataSource.getAllAuths();
|
|
|
|
// then
|
|
assertThat(response, equalTo(true));
|
|
assertThat(authList, hasSize(2));
|
|
assertThat(authList, hasItem(hasAuthBasicData("bobby", "Bobby", "your@email.com", "123.45.67.89")));
|
|
assertThat(newAuthList, hasSize(3));
|
|
assertThat(newAuthList, hasItem(hasAuthBasicData("bobby", "Bobby", "your@email.com", "123.45.67.89")));
|
|
}
|
|
|
|
@Test
|
|
public void shouldUpdatePassword() {
|
|
// given
|
|
DataSource dataSource = getDataSource();
|
|
HashedPassword newHash = new HashedPassword("new_hash");
|
|
|
|
// when
|
|
boolean response1 = dataSource.updatePassword("user", newHash);
|
|
boolean response2 = dataSource.updatePassword("non-existent-name", new HashedPassword("sd"));
|
|
|
|
// then
|
|
assertThat(response1 && response2, equalTo(true));
|
|
assertThat(dataSource.getPassword("user"), equalToHash(newHash));
|
|
}
|
|
|
|
@Test
|
|
public void shouldRemovePlayerAuth() {
|
|
// given
|
|
DataSource dataSource = getDataSource();
|
|
|
|
// when
|
|
boolean response1 = dataSource.removeAuth("bobby");
|
|
boolean response2 = dataSource.removeAuth("does-not-exist");
|
|
|
|
// then
|
|
assertThat(response1 && response2, equalTo(true));
|
|
assertThat(dataSource.getAuth("bobby"), nullValue());
|
|
assertThat(dataSource.isAuthAvailable("bobby"), equalTo(false));
|
|
}
|
|
|
|
@Test
|
|
public void shouldUpdateSession() {
|
|
// given
|
|
DataSource dataSource = getDataSource();
|
|
PlayerAuth bobby = PlayerAuth.builder()
|
|
.name("bobby").realName("BOBBY").lastLogin(123L)
|
|
.ip("12.12.12.12").build();
|
|
|
|
// when
|
|
boolean response = dataSource.updateSession(bobby);
|
|
|
|
// then
|
|
assertThat(response, equalTo(true));
|
|
PlayerAuth result = dataSource.getAuth("bobby");
|
|
assertThat(result, hasAuthBasicData("bobby", "BOBBY", "your@email.com", "12.12.12.12"));
|
|
assertThat(result.getLastLogin(), equalTo(123L));
|
|
}
|
|
|
|
@Test
|
|
public void shouldUpdateLastLoc() {
|
|
// given
|
|
DataSource dataSource = getDataSource();
|
|
PlayerAuth user = PlayerAuth.builder()
|
|
.name("user").locX(143).locY(-42.12).locZ(29.47)
|
|
.locWorld("the_end").build();
|
|
|
|
// when
|
|
boolean response = dataSource.updateQuitLoc(user);
|
|
|
|
// then
|
|
assertThat(response, equalTo(true));
|
|
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")));
|
|
}
|
|
|
|
@Test
|
|
public void shouldUpdateRealName() {
|
|
// given
|
|
DataSource dataSource = getDataSource();
|
|
|
|
// when
|
|
boolean response1 = dataSource.updateRealName("bobby", "BOBBY");
|
|
boolean response2 = dataSource.updateRealName("notExists", "NOTEXISTS");
|
|
|
|
// then
|
|
assertThat(response1 && response2, equalTo(true));
|
|
assertThat(dataSource.getAuth("bobby"), hasAuthBasicData("bobby", "BOBBY", "your@email.com", "123.45.67.89"));
|
|
}
|
|
|
|
}
|