Merge branch '472-confirm-email-recovery' of https://github.com/AuthMe-Team/AuthMeReloaded
This commit is contained in:
@@ -126,6 +126,17 @@ public final class TestHelper {
|
||||
return logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set ConsoleLogger to use a new real logger.
|
||||
*
|
||||
* @return The real logger used by ConsoleLogger
|
||||
*/
|
||||
public static Logger setRealLogger() {
|
||||
Logger logger = Logger.getAnonymousLogger();
|
||||
ConsoleLogger.setLogger(logger);
|
||||
return logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that a class only has a hidden, zero-argument constructor, preventing the
|
||||
* instantiation of such classes (utility classes). Invokes the hidden constructor
|
||||
|
||||
+111
-20
@@ -9,26 +9,29 @@ import fr.xephi.authme.mail.SendMailSSL;
|
||||
import fr.xephi.authme.output.MessageKey;
|
||||
import fr.xephi.authme.security.PasswordSecurity;
|
||||
import fr.xephi.authme.security.crypts.HashedPassword;
|
||||
import fr.xephi.authme.service.RecoveryCodeManager;
|
||||
import fr.xephi.authme.settings.properties.EmailSettings;
|
||||
import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import static fr.xephi.authme.AuthMeMatchers.stringWithLength;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Matchers.argThat;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.only;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
@@ -58,6 +61,9 @@ public class RecoverEmailCommandTest {
|
||||
|
||||
@Mock
|
||||
private SendMailSSL sendMailSsl;
|
||||
|
||||
@Mock
|
||||
private RecoveryCodeManager recoveryCodeManager;
|
||||
|
||||
@BeforeClass
|
||||
public static void initLogger() {
|
||||
@@ -124,7 +130,7 @@ public class RecoverEmailCommandTest {
|
||||
given(sender.getName()).willReturn(name);
|
||||
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||
given(dataSource.getAuth(name)).willReturn(authWithEmail(DEFAULT_EMAIL));
|
||||
given(dataSource.getAuth(name)).willReturn(newAuthWithEmail(DEFAULT_EMAIL));
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.singletonList(DEFAULT_EMAIL));
|
||||
@@ -144,7 +150,7 @@ public class RecoverEmailCommandTest {
|
||||
given(sender.getName()).willReturn(name);
|
||||
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||
given(dataSource.getAuth(name)).willReturn(authWithEmail("raptor@example.org"));
|
||||
given(dataSource.getAuth(name)).willReturn(newAuthWithEmail("raptor@example.org"));
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.singletonList("wrong-email@example.com"));
|
||||
@@ -156,6 +162,60 @@ public class RecoverEmailCommandTest {
|
||||
verify(commandService).send(sender, MessageKey.INVALID_EMAIL);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGenerateRecoveryCode() {
|
||||
// given
|
||||
String name = "Vultur3";
|
||||
Player sender = mock(Player.class);
|
||||
given(sender.getName()).willReturn(name);
|
||||
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||
String email = "v@example.com";
|
||||
given(dataSource.getAuth(name)).willReturn(newAuthWithEmail(email));
|
||||
int codeLength = 7;
|
||||
given(commandService.getProperty(SecuritySettings.RECOVERY_CODE_LENGTH)).willReturn(codeLength);
|
||||
int hoursValid = 12;
|
||||
given(commandService.getProperty(SecuritySettings.RECOVERY_CODE_HOURS_VALID)).willReturn(hoursValid);
|
||||
String code = "a94f37";
|
||||
given(recoveryCodeManager.isRecoveryCodeNeeded()).willReturn(true);
|
||||
given(recoveryCodeManager.generateCode(name)).willReturn(code);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.singletonList(email.toUpperCase()));
|
||||
|
||||
// then
|
||||
verify(sendMailSsl).hasAllInformation();
|
||||
verify(dataSource).getAuth(name);
|
||||
verify(recoveryCodeManager).generateCode(name);
|
||||
verify(commandService).send(sender, MessageKey.RECOVERY_CODE_SENT);
|
||||
verify(sendMailSsl).sendRecoveryCode(name, email, code);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSendErrorForInvalidRecoveryCode() {
|
||||
// given
|
||||
String name = "Vultur3";
|
||||
Player sender = mock(Player.class);
|
||||
given(sender.getName()).willReturn(name);
|
||||
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||
String email = "vulture@example.com";
|
||||
PlayerAuth auth = newAuthWithEmail(email);
|
||||
given(dataSource.getAuth(name)).willReturn(auth);
|
||||
given(commandService.getProperty(EmailSettings.RECOVERY_PASSWORD_LENGTH)).willReturn(20);
|
||||
given(recoveryCodeManager.isRecoveryCodeNeeded()).willReturn(true);
|
||||
given(recoveryCodeManager.isCodeValid(name, "bogus")).willReturn(false);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Arrays.asList(email, "bogus"));
|
||||
|
||||
// then
|
||||
verify(sendMailSsl).hasAllInformation();
|
||||
verify(dataSource, only()).getAuth(name);
|
||||
verify(commandService).send(sender, MessageKey.INCORRECT_RECOVERY_CODE);
|
||||
verifyNoMoreInteractions(sendMailSsl);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldResetPasswordAndSendEmail() {
|
||||
// given
|
||||
@@ -165,36 +225,67 @@ public class RecoverEmailCommandTest {
|
||||
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||
String email = "vulture@example.com";
|
||||
PlayerAuth auth = authWithEmail(email);
|
||||
String code = "A6EF3AC8";
|
||||
PlayerAuth auth = newAuthWithEmail(email);
|
||||
given(dataSource.getAuth(name)).willReturn(auth);
|
||||
given(commandService.getProperty(EmailSettings.RECOVERY_PASSWORD_LENGTH)).willReturn(20);
|
||||
given(passwordSecurity.computeHash(anyString(), eq(name)))
|
||||
.willAnswer(new Answer<HashedPassword>() {
|
||||
@Override
|
||||
public HashedPassword answer(InvocationOnMock invocationOnMock) {
|
||||
return new HashedPassword((String) invocationOnMock.getArguments()[0]);
|
||||
}
|
||||
});
|
||||
.willAnswer(invocation -> new HashedPassword((String) invocation.getArguments()[0]));
|
||||
given(recoveryCodeManager.isRecoveryCodeNeeded()).willReturn(true);
|
||||
given(recoveryCodeManager.isCodeValid(name, code)).willReturn(true);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.singletonList(email.toUpperCase()));
|
||||
command.executeCommand(sender, Arrays.asList(email, code));
|
||||
|
||||
// then
|
||||
verify(sendMailSsl).hasAllInformation();
|
||||
verify(dataSource).getAuth(name);
|
||||
verify(passwordSecurity).computeHash(anyString(), eq(name));
|
||||
verify(dataSource).updatePassword(auth);
|
||||
assertThat(auth.getPassword().getHash(), stringWithLength(20));
|
||||
verify(sendMailSsl).sendPasswordMail(eq(auth), argThat(stringWithLength(20)));
|
||||
ArgumentCaptor<String> passwordCaptor = ArgumentCaptor.forClass(String.class);
|
||||
verify(passwordSecurity).computeHash(passwordCaptor.capture(), eq(name));
|
||||
String generatedPassword = passwordCaptor.getValue();
|
||||
assertThat(generatedPassword, stringWithLength(20));
|
||||
verify(dataSource).updatePassword(eq(name), any(HashedPassword.class));
|
||||
verify(recoveryCodeManager).removeCode(name);
|
||||
verify(sendMailSsl).sendPasswordMail(name, email, generatedPassword);
|
||||
verify(commandService).send(sender, MessageKey.RECOVERY_EMAIL_SENT_MESSAGE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGenerateNewPasswordWithoutRecoveryCode() {
|
||||
// given
|
||||
String name = "sh4rK";
|
||||
Player sender = mock(Player.class);
|
||||
given(sender.getName()).willReturn(name);
|
||||
given(sendMailSsl.hasAllInformation()).willReturn(true);
|
||||
given(playerCache.isAuthenticated(name)).willReturn(false);
|
||||
String email = "shark@example.org";
|
||||
PlayerAuth auth = newAuthWithEmail(email);
|
||||
given(dataSource.getAuth(name)).willReturn(auth);
|
||||
given(commandService.getProperty(EmailSettings.RECOVERY_PASSWORD_LENGTH)).willReturn(20);
|
||||
given(passwordSecurity.computeHash(anyString(), eq(name)))
|
||||
.willAnswer(invocation -> new HashedPassword((String) invocation.getArguments()[0]));
|
||||
given(recoveryCodeManager.isRecoveryCodeNeeded()).willReturn(false);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.singletonList(email));
|
||||
|
||||
// then
|
||||
verify(sendMailSsl).hasAllInformation();
|
||||
verify(dataSource).getAuth(name);
|
||||
ArgumentCaptor<String> passwordCaptor = ArgumentCaptor.forClass(String.class);
|
||||
verify(passwordSecurity).computeHash(passwordCaptor.capture(), eq(name));
|
||||
String generatedPassword = passwordCaptor.getValue();
|
||||
assertThat(generatedPassword, stringWithLength(20));
|
||||
verify(dataSource).updatePassword(eq(name), any(HashedPassword.class));
|
||||
verify(sendMailSsl).sendPasswordMail(name, email, generatedPassword);
|
||||
verify(commandService).send(sender, MessageKey.RECOVERY_EMAIL_SENT_MESSAGE);
|
||||
}
|
||||
|
||||
|
||||
private static PlayerAuth authWithEmail(String email) {
|
||||
private static PlayerAuth newAuthWithEmail(String email) {
|
||||
return PlayerAuth.builder()
|
||||
.name("tester")
|
||||
.name("name")
|
||||
.email(email)
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -381,5 +381,4 @@ public abstract class AbstractDataSourceIntegrationTest {
|
||||
// then
|
||||
assertThat(dataSource.getAllAuths(), empty());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ public class MySqlIntegrationTest extends AbstractDataSourceIntegrationTest {
|
||||
});
|
||||
set(DatabaseSettings.MYSQL_DATABASE, "h2_test");
|
||||
set(DatabaseSettings.MYSQL_TABLE, "authme");
|
||||
TestHelper.setupLogger();
|
||||
TestHelper.setRealLogger();
|
||||
|
||||
Path sqlInitFile = TestHelper.getJarPath(TestHelper.PROJECT_ROOT + "datasource/sql-initialize.sql");
|
||||
sqlInitialize = new String(Files.readAllBytes(sqlInitFile));
|
||||
|
||||
@@ -56,7 +56,7 @@ public class SQLiteIntegrationTest extends AbstractDataSourceIntegrationTest {
|
||||
});
|
||||
set(DatabaseSettings.MYSQL_DATABASE, "sqlite-test");
|
||||
set(DatabaseSettings.MYSQL_TABLE, "authme");
|
||||
TestHelper.setupLogger();
|
||||
TestHelper.setRealLogger();
|
||||
|
||||
Path sqlInitFile = TestHelper.getJarPath(TestHelper.PROJECT_ROOT + "datasource/sql-initialize.sql");
|
||||
// Note ljacqu 20160221: It appears that we can only run one statement per Statement.execute() so we split
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
package fr.xephi.authme.service;
|
||||
|
||||
import ch.jalu.injector.testing.BeforeInjecting;
|
||||
import ch.jalu.injector.testing.DelayedInjectionRunner;
|
||||
import ch.jalu.injector.testing.InjectDelayed;
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import fr.xephi.authme.service.RecoveryCodeManager.ExpiringEntry;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static fr.xephi.authme.AuthMeMatchers.stringWithLength;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
* Test for {@link RecoveryCodeManager}.
|
||||
*/
|
||||
@RunWith(DelayedInjectionRunner.class)
|
||||
public class RecoveryCodeManagerTest {
|
||||
|
||||
@InjectDelayed
|
||||
private RecoveryCodeManager recoveryCodeManager;
|
||||
|
||||
@Mock
|
||||
private Settings settings;
|
||||
|
||||
@BeforeInjecting
|
||||
public void initSettings() {
|
||||
given(settings.getProperty(SecuritySettings.RECOVERY_CODE_HOURS_VALID)).willReturn(4);
|
||||
given(settings.getProperty(SecuritySettings.RECOVERY_CODE_LENGTH)).willReturn(5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldBeDisabledForNonPositiveLength() {
|
||||
assertThat(recoveryCodeManager.isRecoveryCodeNeeded(), equalTo(true));
|
||||
|
||||
// given
|
||||
given(settings.getProperty(SecuritySettings.RECOVERY_CODE_LENGTH)).willReturn(0);
|
||||
|
||||
// when
|
||||
recoveryCodeManager.reload(settings);
|
||||
|
||||
// then
|
||||
assertThat(recoveryCodeManager.isRecoveryCodeNeeded(), equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGenerateAndStoreCode() {
|
||||
// given
|
||||
String name = "Bobbers";
|
||||
|
||||
// when
|
||||
recoveryCodeManager.generateCode(name);
|
||||
|
||||
// then
|
||||
ExpiringEntry entry = getCodeMap().get(name);
|
||||
assertThat(entry.getCode(), stringWithLength(5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotConsiderExpiredCode() {
|
||||
// given
|
||||
String player = "Cat";
|
||||
String code = "11F235";
|
||||
setCodeInMap(player, code, System.currentTimeMillis() - 500);
|
||||
|
||||
// when
|
||||
boolean result = recoveryCodeManager.isCodeValid(player, code);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRecognizeCorrectCode() {
|
||||
// given
|
||||
String player = "dragon";
|
||||
String code = recoveryCodeManager.generateCode(player);
|
||||
|
||||
// when
|
||||
boolean result = recoveryCodeManager.isCodeValid(player, code);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRemoveCode() {
|
||||
// given
|
||||
String player = "Tester";
|
||||
String code = recoveryCodeManager.generateCode(player);
|
||||
|
||||
// when
|
||||
recoveryCodeManager.removeCode(player);
|
||||
|
||||
// then
|
||||
assertThat(recoveryCodeManager.isCodeValid(player, code), equalTo(false));
|
||||
assertThat(getCodeMap().get(player), nullValue());
|
||||
}
|
||||
|
||||
|
||||
private Map<String, ExpiringEntry> getCodeMap() {
|
||||
return ReflectionTestUtils.getFieldValue(RecoveryCodeManager.class, recoveryCodeManager, "recoveryCodes");
|
||||
}
|
||||
|
||||
private void setCodeInMap(String player, String code, long expiration) {
|
||||
Map<String, ExpiringEntry> map = getCodeMap();
|
||||
map.put(player, new ExpiringEntry(code, expiration));
|
||||
}
|
||||
}
|
||||
@@ -23,9 +23,10 @@ import java.util.List;
|
||||
|
||||
import static fr.xephi.authme.settings.properties.PluginSettings.MESSAGES_LANGUAGE;
|
||||
import static fr.xephi.authme.util.StringUtils.makePath;
|
||||
import static org.hamcrest.Matchers.arrayContaining;
|
||||
import static org.hamcrest.Matchers.arrayWithSize;
|
||||
import static org.hamcrest.Matchers.endsWith;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
@@ -127,12 +128,11 @@ public class SettingsTest {
|
||||
TestSettingsMigrationServices.alwaysFulfilled(), knownProperties);
|
||||
|
||||
// when
|
||||
List<String> result = settings.getWelcomeMessage();
|
||||
String[] result = settings.getWelcomeMessage();
|
||||
|
||||
// then
|
||||
assertThat(result, hasSize(2));
|
||||
assertThat(result.get(0), equalTo(welcomeMessage.split("\\n")[0]));
|
||||
assertThat(result.get(1), equalTo(welcomeMessage.split("\\n")[1]));
|
||||
assertThat(result, arrayWithSize(2));
|
||||
assertThat(result, arrayContaining(welcomeMessage.split("\\n")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -148,7 +148,7 @@ public class SettingsTest {
|
||||
TestSettingsMigrationServices.alwaysFulfilled(), knownProperties);
|
||||
|
||||
// when
|
||||
String result = settings.getEmailMessage();
|
||||
String result = settings.getPasswordEmailMessage();
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(emailMessage));
|
||||
|
||||
@@ -13,6 +13,8 @@ CREATE TABLE authme (
|
||||
email VARCHAR(255) DEFAULT 'your@email.com',
|
||||
isLogged INT DEFAULT '0', realname VARCHAR(255) NOT NULL DEFAULT 'Player',
|
||||
salt varchar(255),
|
||||
recoverycode VARCHAR(20),
|
||||
recoveryexpiration BIGINT,
|
||||
CONSTRAINT table_const_prim PRIMARY KEY (id)
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user