#472 Recovery code: allow to configure length, expiration and email

This commit is contained in:
ljacqu
2016-09-10 16:39:35 +02:00
parent c5f5c0d2fd
commit bff344ba8f
12 changed files with 117 additions and 60 deletions
@@ -10,6 +10,7 @@ import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
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;
@@ -23,11 +24,14 @@ import java.util.Arrays;
import java.util.Collections;
import static fr.xephi.authme.AuthMeMatchers.stringWithLength;
import static fr.xephi.authme.util.Utils.MILLIS_PER_HOUR;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.eq;
@@ -170,6 +174,10 @@ public class RecoverEmailCommandTest {
given(playerCache.isAuthenticated(name)).willReturn(false);
String email = "v@example.com";
given(dataSource.getEmailRecoveryData(name)).willReturn(newEmailRecoveryData(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);
// when
command.executeCommand(sender, Collections.singletonList(email.toUpperCase()));
@@ -178,9 +186,13 @@ public class RecoverEmailCommandTest {
verify(sendMailSsl).hasAllInformation();
verify(dataSource).getEmailRecoveryData(name);
ArgumentCaptor<String> codeCaptor = ArgumentCaptor.forClass(String.class);
verify(dataSource).setRecoveryCode(eq(name), codeCaptor.capture(), anyLong());
assertThat(codeCaptor.getValue(), stringWithLength(8));
verify(sendMailSsl).sendRecoveryCode(email, codeCaptor.getValue());
ArgumentCaptor<Long> expirationCaptor = ArgumentCaptor.forClass(Long.class);
verify(dataSource).setRecoveryCode(eq(name), codeCaptor.capture(), expirationCaptor.capture());
assertThat(codeCaptor.getValue(), stringWithLength(codeLength));
// Check expiration with a tolerance
assertThat(expirationCaptor.getValue() - System.currentTimeMillis(),
allOf(lessThan(12L * MILLIS_PER_HOUR), greaterThan((long) (11.9 * MILLIS_PER_HOUR))));
verify(sendMailSsl).sendRecoveryCode(name, email, codeCaptor.getValue());
}
@Test
@@ -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));