mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-26 02:27:50 +01:00
#392 Add datasource integration tests
This commit is contained in:
parent
95e3943be0
commit
69092e9a9c
@ -863,23 +863,23 @@ public class MySQL implements DataSource {
|
|||||||
try (Connection con = getConnection()) {
|
try (Connection con = getConnection()) {
|
||||||
Statement st = con.createStatement();
|
Statement st = con.createStatement();
|
||||||
ResultSet rs = st.executeQuery("SELECT * FROM " + tableName);
|
ResultSet rs = st.executeQuery("SELECT * FROM " + tableName);
|
||||||
PreparedStatement pst = con.prepareStatement("SELECT data FROM xf_user_authenticate WHERE " + col.ID + "=?;");
|
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
PlayerAuth pAuth = buildAuthFromResultSet(rs);
|
PlayerAuth pAuth = buildAuthFromResultSet(rs);
|
||||||
if (hashAlgorithm == HashAlgorithm.XFBCRYPT) {
|
if (hashAlgorithm == HashAlgorithm.XFBCRYPT) {
|
||||||
int id = rs.getInt(col.ID);
|
try (PreparedStatement pst = con.prepareStatement("SELECT data FROM xf_user_authenticate WHERE " + col.ID + "=?;")) {
|
||||||
pst.setInt(1, id);
|
int id = rs.getInt(col.ID);
|
||||||
ResultSet rs2 = pst.executeQuery();
|
pst.setInt(1, id);
|
||||||
if (rs2.next()) {
|
ResultSet rs2 = pst.executeQuery();
|
||||||
Blob blob = rs2.getBlob("data");
|
if (rs2.next()) {
|
||||||
byte[] bytes = blob.getBytes(1, (int) blob.length());
|
Blob blob = rs2.getBlob("data");
|
||||||
pAuth.setPassword(new HashedPassword(XFBCRYPT.getHashFromBlob(bytes)));
|
byte[] bytes = blob.getBytes(1, (int) blob.length());
|
||||||
|
pAuth.setPassword(new HashedPassword(XFBCRYPT.getHashFromBlob(bytes)));
|
||||||
|
}
|
||||||
|
rs2.close();
|
||||||
}
|
}
|
||||||
rs2.close();
|
|
||||||
}
|
}
|
||||||
auths.add(pAuth);
|
auths.add(pAuth);
|
||||||
}
|
}
|
||||||
pst.close();
|
|
||||||
rs.close();
|
rs.close();
|
||||||
st.close();
|
st.close();
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
|
@ -4,10 +4,13 @@ import fr.xephi.authme.cache.auth.PlayerAuth;
|
|||||||
import fr.xephi.authme.security.crypts.HashedPassword;
|
import fr.xephi.authme.security.crypts.HashedPassword;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import static fr.xephi.authme.datasource.AuthMeMatchers.equalToHash;
|
import static fr.xephi.authme.datasource.AuthMeMatchers.equalToHash;
|
||||||
import static fr.xephi.authme.datasource.AuthMeMatchers.hasAuthBasicData;
|
import static fr.xephi.authme.datasource.AuthMeMatchers.hasAuthBasicData;
|
||||||
import static fr.xephi.authme.datasource.AuthMeMatchers.hasAuthLocation;
|
import static fr.xephi.authme.datasource.AuthMeMatchers.hasAuthLocation;
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
import static org.hamcrest.Matchers.hasSize;
|
||||||
import static org.hamcrest.Matchers.nullValue;
|
import static org.hamcrest.Matchers.nullValue;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
@ -98,13 +101,104 @@ public abstract class AbstractDataSourceIntegrationTest {
|
|||||||
// when
|
// when
|
||||||
int userMailCount = dataSource.countAuthsByEmail("user@example.ORG");
|
int userMailCount = dataSource.countAuthsByEmail("user@example.ORG");
|
||||||
int invalidMailCount = dataSource.countAuthsByEmail("not.in.db@example.com");
|
int invalidMailCount = dataSource.countAuthsByEmail("not.in.db@example.com");
|
||||||
dataSource.saveAuth(PlayerAuth.builder().name("Test").email("user@EXAMPLE.org").build());
|
boolean response = dataSource.saveAuth(
|
||||||
|
PlayerAuth.builder().name("Test").email("user@EXAMPLE.org").build());
|
||||||
int newUserCount = dataSource.countAuthsByEmail("user@Example.org");
|
int newUserCount = dataSource.countAuthsByEmail("user@Example.org");
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertThat(userMailCount, equalTo(1));
|
assertThat(userMailCount, equalTo(1));
|
||||||
assertThat(invalidMailCount, equalTo(0));
|
assertThat(invalidMailCount, equalTo(0));
|
||||||
|
assertThat(response, equalTo(true));
|
||||||
assertThat(newUserCount, equalTo(2));
|
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(newAuthList, hasSize(3));
|
||||||
|
boolean hasBobby = false;
|
||||||
|
for (PlayerAuth auth : authList) {
|
||||||
|
if (auth.getNickname().equals("bobby")) {
|
||||||
|
hasBobby = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertThat(hasBobby, equalTo(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
@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"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -17,21 +17,26 @@ public final class AuthMeMatchers {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Matcher<? super HashedPassword> equalToHash(final String hash) {
|
public static Matcher<? super HashedPassword> equalToHash(final String hash) {
|
||||||
return equalToHash(hash, null);
|
return equalToHash(new HashedPassword(hash));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Matcher<? super HashedPassword> equalToHash(final String hash, final String salt) {
|
public static Matcher<? super HashedPassword> equalToHash(final String hash, final String salt) {
|
||||||
|
return equalToHash(new HashedPassword(hash, salt));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Matcher<? super HashedPassword> equalToHash(final HashedPassword hash) {
|
||||||
return new TypeSafeMatcher<HashedPassword>() {
|
return new TypeSafeMatcher<HashedPassword>() {
|
||||||
@Override
|
@Override
|
||||||
public boolean matchesSafely(HashedPassword item) {
|
public boolean matchesSafely(HashedPassword item) {
|
||||||
return Objects.equals(hash, item.getHash()) && Objects.equals(salt, item.getSalt());
|
return Objects.equals(hash.getHash(), item.getHash())
|
||||||
|
&& Objects.equals(hash.getSalt(), item.getSalt());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void describeTo(Description description) {
|
public void describeTo(Description description) {
|
||||||
String representation = "'" + hash + "'";
|
String representation = "'" + hash.getHash() + "'";
|
||||||
if (salt != null) {
|
if (hash.getSalt() != null) {
|
||||||
representation += ", '" + salt + "'";
|
representation += ", '" + hash.getSalt() + "'";
|
||||||
}
|
}
|
||||||
description.appendValue("HashedPassword(" + representation + ")");
|
description.appendValue("HashedPassword(" + representation + ")");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user