Merge remote-tracking branch 'origin/master' into jsoncache-fix
Conflicts: src/main/java/fr/xephi/authme/settings/Settings.java
This commit is contained in:
+3
-3
@@ -22,12 +22,10 @@ import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.argThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -160,6 +158,8 @@ public class RegisterAdminCommandTest {
|
||||
given(passwordSecurity.computeHash(password, user)).willReturn(hashedPassword);
|
||||
Player player = mock(Player.class);
|
||||
given(bukkitService.getPlayerExact(user)).willReturn(player);
|
||||
String kickForAdminRegister = "Admin registered you -- log in again";
|
||||
given(commandService.retrieveSingle(MessageKey.KICK_FOR_ADMIN_REGISTER)).willReturn(kickForAdminRegister);
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
|
||||
// when
|
||||
@@ -174,7 +174,7 @@ public class RegisterAdminCommandTest {
|
||||
verify(dataSource).saveAuth(captor.capture());
|
||||
assertAuthHasInfo(captor.getValue(), user, hashedPassword);
|
||||
verify(dataSource).setUnlogged(user);
|
||||
verify(player).kickPlayer(argThat(containsString("please log in again")));
|
||||
verify(player).kickPlayer(kickForAdminRegister);
|
||||
}
|
||||
|
||||
private void assertAuthHasInfo(PlayerAuth auth, String name, HashedPassword hashedPassword) {
|
||||
|
||||
@@ -2,6 +2,7 @@ package fr.xephi.authme.command.executable.register;
|
||||
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.command.CommandService;
|
||||
import fr.xephi.authme.mail.SendMailSSL;
|
||||
import fr.xephi.authme.output.MessageKey;
|
||||
import fr.xephi.authme.process.Management;
|
||||
import fr.xephi.authme.security.HashAlgorithm;
|
||||
@@ -49,6 +50,8 @@ public class RegisterCommandTest {
|
||||
@Mock
|
||||
private Management management;
|
||||
|
||||
@Mock
|
||||
private SendMailSSL sendMailSsl;
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() {
|
||||
@@ -72,7 +75,7 @@ public class RegisterCommandTest {
|
||||
|
||||
// then
|
||||
verify(sender).sendMessage(argThat(containsString("Player only!")));
|
||||
verifyZeroInteractions(management);
|
||||
verifyZeroInteractions(management, sendMailSsl);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -86,6 +89,7 @@ public class RegisterCommandTest {
|
||||
|
||||
// then
|
||||
verify(management).performRegister(player, "", "", true);
|
||||
verifyZeroInteractions(sendMailSsl);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -98,7 +102,7 @@ public class RegisterCommandTest {
|
||||
|
||||
// then
|
||||
verify(commandService).send(player, MessageKey.USAGE_REGISTER);
|
||||
verifyZeroInteractions(management);
|
||||
verifyZeroInteractions(management, sendMailSsl);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -112,7 +116,7 @@ public class RegisterCommandTest {
|
||||
|
||||
// then
|
||||
verify(commandService).send(player, MessageKey.USAGE_REGISTER);
|
||||
verifyZeroInteractions(management);
|
||||
verifyZeroInteractions(management, sendMailSsl);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -127,7 +131,7 @@ public class RegisterCommandTest {
|
||||
|
||||
// then
|
||||
verify(commandService).send(player, MessageKey.USAGE_REGISTER);
|
||||
verifyZeroInteractions(management);
|
||||
verifyZeroInteractions(management, sendMailSsl);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -135,14 +139,15 @@ public class RegisterCommandTest {
|
||||
// given
|
||||
given(commandService.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(true);
|
||||
given(commandService.getProperty(RegistrationSettings.ENABLE_CONFIRM_EMAIL)).willReturn(false);
|
||||
given(commandService.getProperty(EmailSettings.MAIL_ACCOUNT)).willReturn("");
|
||||
given(sendMailSsl.hasAllInformation()).willReturn(false);
|
||||
Player player = mock(Player.class);
|
||||
|
||||
// when
|
||||
command.executeCommand(player, Collections.singletonList("myMail@example.tld"));
|
||||
|
||||
// then
|
||||
verify(player).sendMessage(argThat(containsString("no email address")));
|
||||
verify(commandService).send(player, MessageKey.INCOMPLETE_EMAIL_SETTINGS);
|
||||
verify(sendMailSsl).hasAllInformation();
|
||||
verifyZeroInteractions(management);
|
||||
}
|
||||
|
||||
@@ -155,6 +160,7 @@ public class RegisterCommandTest {
|
||||
given(commandService.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(true);
|
||||
given(commandService.getProperty(RegistrationSettings.ENABLE_CONFIRM_EMAIL)).willReturn(true);
|
||||
given(commandService.getProperty(EmailSettings.MAIL_ACCOUNT)).willReturn("server@example.com");
|
||||
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
||||
Player player = mock(Player.class);
|
||||
|
||||
// when
|
||||
@@ -175,6 +181,7 @@ public class RegisterCommandTest {
|
||||
given(commandService.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(true);
|
||||
given(commandService.getProperty(RegistrationSettings.ENABLE_CONFIRM_EMAIL)).willReturn(true);
|
||||
given(commandService.getProperty(EmailSettings.MAIL_ACCOUNT)).willReturn("server@example.com");
|
||||
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
||||
Player player = mock(Player.class);
|
||||
|
||||
// when
|
||||
@@ -182,6 +189,7 @@ public class RegisterCommandTest {
|
||||
|
||||
// then
|
||||
verify(commandService).send(player, MessageKey.USAGE_REGISTER);
|
||||
verify(sendMailSsl).hasAllInformation();
|
||||
verifyZeroInteractions(management);
|
||||
}
|
||||
|
||||
@@ -196,6 +204,7 @@ public class RegisterCommandTest {
|
||||
given(commandService.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(true);
|
||||
given(commandService.getProperty(RegistrationSettings.ENABLE_CONFIRM_EMAIL)).willReturn(true);
|
||||
given(commandService.getProperty(EmailSettings.MAIL_ACCOUNT)).willReturn("server@example.com");
|
||||
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
||||
Player player = mock(Player.class);
|
||||
|
||||
// when
|
||||
@@ -203,6 +212,7 @@ public class RegisterCommandTest {
|
||||
|
||||
// then
|
||||
verify(commandService).validateEmail(playerMail);
|
||||
verify(sendMailSsl).hasAllInformation();
|
||||
verify(management).performRegister(eq(player), argThat(stringWithLength(passLength)), eq(playerMail), eq(true));
|
||||
}
|
||||
|
||||
@@ -217,7 +227,7 @@ public class RegisterCommandTest {
|
||||
|
||||
// then
|
||||
verify(commandService).send(player, MessageKey.PASSWORD_MATCH_ERROR);
|
||||
verifyZeroInteractions(management);
|
||||
verifyZeroInteractions(management, sendMailSsl);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -12,6 +12,9 @@ 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 org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
import static org.hamcrest.Matchers.empty;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
@@ -170,6 +173,22 @@ public abstract class AbstractDataSourceIntegrationTest {
|
||||
assertThat(dataSource.getPassword("user"), equalToHash("new_hash"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldUpdatePasswordWithPlayerAuth() {
|
||||
// given
|
||||
DataSource dataSource = getDataSource("salt");
|
||||
PlayerAuth bobbyAuth = PlayerAuth.builder().name("bobby").password(new HashedPassword("tt", "cc")).build();
|
||||
PlayerAuth invalidAuth = PlayerAuth.builder().name("invalid").password(new HashedPassword("tt", "cc")).build();
|
||||
|
||||
// when
|
||||
boolean response1 = dataSource.updatePassword(bobbyAuth);
|
||||
boolean response2 = dataSource.updatePassword(invalidAuth);
|
||||
|
||||
// then
|
||||
assertThat(response1 && response2, equalTo(true));
|
||||
assertThat(dataSource.getPassword("bobby"), equalToHash("tt", "cc"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRemovePlayerAuth() {
|
||||
// given
|
||||
@@ -321,4 +340,49 @@ public abstract class AbstractDataSourceIntegrationTest {
|
||||
assertThat(dataSource.getAuth("bobby"), hasAuthBasicData("bobby", "BOBBY", "your@email.com", "123.45.67.89"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetRecordsToPurge() {
|
||||
// given
|
||||
DataSource dataSource = getDataSource();
|
||||
// 1453242857 -> user, 1449136800 -> bobby
|
||||
|
||||
// when
|
||||
Set<String> records1 = dataSource.getRecordsToPurge(1450000000);
|
||||
Set<String> records2 = dataSource.getRecordsToPurge(1460000000);
|
||||
|
||||
// then
|
||||
assertThat(records1, contains("bobby"));
|
||||
assertThat(records2, containsInAnyOrder("bobby", "user"));
|
||||
// check that the entry was not deleted because of running this command
|
||||
assertThat(dataSource.isAuthAvailable("bobby"), equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPerformOperationsOnIsLoggedColumnSuccessfully() {
|
||||
DataSource dataSource = getDataSource();
|
||||
// on startup no one should be marked as logged
|
||||
assertThat(dataSource.getLoggedPlayers(), empty());
|
||||
|
||||
// Mark user as logged
|
||||
dataSource.setLogged("user");
|
||||
// non-existent user should not break database
|
||||
dataSource.setLogged("does-not-exist");
|
||||
|
||||
assertThat(dataSource.isLogged("user"), equalTo(true));
|
||||
assertThat(dataSource.isLogged("bobby"), equalTo(false));
|
||||
|
||||
// Set bobby logged and unlog user
|
||||
dataSource.setLogged("bobby");
|
||||
dataSource.setUnlogged("user");
|
||||
assertThat(dataSource.getLoggedPlayers(),
|
||||
contains(hasAuthBasicData("bobby", "Bobby", "your@email.com", "123.45.67.89")));
|
||||
|
||||
// Set both as logged (even if Bobby already is logged)
|
||||
dataSource.setLogged("user");
|
||||
dataSource.setLogged("bobby");
|
||||
dataSource.purgeLogged();
|
||||
assertThat(dataSource.isLogged("user"), equalTo(false));
|
||||
assertThat(dataSource.getLoggedPlayers(), empty());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.settings.NewSetting;
|
||||
import fr.xephi.authme.settings.domain.Property;
|
||||
import fr.xephi.authme.settings.properties.DatabaseSettings;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
@@ -60,7 +61,6 @@ public class MySqlIntegrationTest extends AbstractDataSourceIntegrationTest {
|
||||
|
||||
@Before
|
||||
public void initializeConnectionAndTable() throws SQLException {
|
||||
silentClose(hikariSource);
|
||||
HikariConfig config = new HikariConfig();
|
||||
config.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource");
|
||||
config.setConnectionTestQuery("VALUES 1");
|
||||
@@ -77,6 +77,11 @@ public class MySqlIntegrationTest extends AbstractDataSourceIntegrationTest {
|
||||
hikariSource = ds;
|
||||
}
|
||||
|
||||
@After
|
||||
public void closeConnection() {
|
||||
silentClose(hikariSource);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected DataSource getDataSource(String saltColumn) {
|
||||
when(settings.getProperty(DatabaseSettings.MYSQL_COL_SALT)).thenReturn(saltColumn);
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package fr.xephi.authme.datasource;
|
||||
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.cache.auth.PlayerAuth;
|
||||
import fr.xephi.authme.settings.NewSetting;
|
||||
import fr.xephi.authme.settings.domain.Property;
|
||||
import fr.xephi.authme.settings.properties.DatabaseSettings;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
@@ -17,6 +20,8 @@ import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -61,7 +66,6 @@ public class SQLiteIntegrationTest extends AbstractDataSourceIntegrationTest {
|
||||
|
||||
@Before
|
||||
public void initializeConnectionAndTable() throws SQLException {
|
||||
silentClose(con);
|
||||
Connection connection = DriverManager.getConnection("jdbc:sqlite::memory:");
|
||||
try (Statement st = connection.createStatement()) {
|
||||
st.execute("DROP TABLE IF EXISTS authme");
|
||||
@@ -72,6 +76,50 @@ public class SQLiteIntegrationTest extends AbstractDataSourceIntegrationTest {
|
||||
con = connection;
|
||||
}
|
||||
|
||||
@After
|
||||
public void closeConnection() {
|
||||
silentClose(con);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSetUpTableIfMissing() throws SQLException {
|
||||
// given
|
||||
Statement st = con.createStatement();
|
||||
// table is absent
|
||||
st.execute("DROP TABLE authme");
|
||||
SQLite sqLite = new SQLite(settings, con);
|
||||
|
||||
// when
|
||||
sqLite.setup();
|
||||
|
||||
// then
|
||||
// Save some player to verify database is operational
|
||||
sqLite.saveAuth(PlayerAuth.builder().name("Name").build());
|
||||
assertThat(sqLite.getAllAuths(), hasSize(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateMissingColumns() throws SQLException {
|
||||
// given
|
||||
Statement st = con.createStatement();
|
||||
// drop table and create one with only some of the columns: SQLite doesn't support ALTER TABLE t DROP COLUMN c
|
||||
st.execute("DROP TABLE authme");
|
||||
st.execute("CREATE TABLE authme ("
|
||||
+ "id bigint, "
|
||||
+ "username varchar(255) unique, "
|
||||
+ "password varchar(255) not null, "
|
||||
+ "primary key (id));");
|
||||
SQLite sqLite = new SQLite(settings, con);
|
||||
|
||||
// when
|
||||
sqLite.setup();
|
||||
|
||||
// then
|
||||
// Save some player to verify database is operational
|
||||
sqLite.saveAuth(PlayerAuth.builder().name("Name").build());
|
||||
assertThat(sqLite.getAllAuths(), hasSize(1));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected DataSource getDataSource(String saltColumn) {
|
||||
when(settings.getProperty(DatabaseSettings.MYSQL_COL_SALT)).thenReturn(saltColumn);
|
||||
|
||||
@@ -22,12 +22,11 @@ public class MessageKeyTest {
|
||||
// when / then
|
||||
for (MessageKey messageKey : messageKeys) {
|
||||
String key = messageKey.getKey();
|
||||
if (keys.contains(key)) {
|
||||
if (!keys.add(key)) {
|
||||
fail("Found key '" + messageKey.getKey() + "' twice!");
|
||||
} else if (StringUtils.isEmpty(key)) {
|
||||
fail("Key for message key '" + messageKey + "' is empty");
|
||||
}
|
||||
keys.add(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,10 +33,9 @@ public class AdminPermissionTest {
|
||||
|
||||
// when/then
|
||||
for (AdminPermission permission : AdminPermission.values()) {
|
||||
if (nodes.contains(permission.getNode())) {
|
||||
if (!nodes.add(permission.getNode())) {
|
||||
fail("More than one enum value defines the node '" + permission.getNode() + "'");
|
||||
}
|
||||
nodes.add(permission.getNode());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -33,10 +33,9 @@ public class PlayerPermissionTest {
|
||||
|
||||
// when/then
|
||||
for (PlayerPermission permission : PlayerPermission.values()) {
|
||||
if (nodes.contains(permission.getNode())) {
|
||||
if (!nodes.add(permission.getNode())) {
|
||||
fail("More than one enum value defines the node '" + permission.getNode() + "'");
|
||||
}
|
||||
nodes.add(permission.getNode());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,10 +37,9 @@ public class PlayerStatePermissionTest {
|
||||
|
||||
// when/then
|
||||
for (PlayerStatePermission permission : PlayerStatePermission.values()) {
|
||||
if (nodes.contains(permission.getNode())) {
|
||||
if (!nodes.add(permission.getNode())) {
|
||||
fail("More than one enum value defines the node '" + permission.getNode() + "'");
|
||||
}
|
||||
nodes.add(permission.getNode());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -43,10 +43,9 @@ public class HashAlgorithmIntegrationTest {
|
||||
// when / then
|
||||
for (HashAlgorithm algorithm : HashAlgorithm.values()) {
|
||||
if (!HashAlgorithm.CUSTOM.equals(algorithm)) {
|
||||
if (classes.contains(algorithm.getClazz())) {
|
||||
if (!classes.add(algorithm.getClazz())) {
|
||||
fail("Found class '" + algorithm.getClazz() + "' twice!");
|
||||
}
|
||||
classes.add(algorithm.getClazz());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,10 +74,9 @@ public class SettingsClassConsistencyTest {
|
||||
if (Property.class.isAssignableFrom(field.getType())) {
|
||||
Property<?> property =
|
||||
(Property<?>) ReflectionTestUtils.getFieldValue(clazz, null, field.getName());
|
||||
if (paths.contains(property.getPath())) {
|
||||
if (!paths.add(property.getPath())) {
|
||||
fail("Path '" + property.getPath() + "' should be used by only one constant");
|
||||
}
|
||||
paths.add(property.getPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user