#1435 Send password recovery emails in async

This commit is contained in:
ljacqu 2017-12-21 21:54:23 +01:00
parent 779c4a2b33
commit 8bae71e1bd
2 changed files with 20 additions and 7 deletions

View File

@ -7,6 +7,7 @@ import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.datasource.DataSourceResult;
import fr.xephi.authme.mail.EmailService;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.PasswordRecoveryService;
import fr.xephi.authme.service.RecoveryCodeService;
@ -39,6 +40,9 @@ public class RecoverEmailCommand extends PlayerCommand {
@Inject
private RecoveryCodeService recoveryCodeService;
@Inject
private BukkitService bukkitService;
@Override
protected void runCommand(Player player, List<String> arguments) {
final String playerMail = arguments.get(0);
@ -66,13 +70,15 @@ public class RecoverEmailCommand extends PlayerCommand {
return;
}
if (recoveryCodeService.isRecoveryCodeNeeded()) {
// Recovery code is needed; generate and send one
recoveryService.createAndSendRecoveryCode(player, email);
} else {
// Code not needed, just send them a new password
recoveryService.generateAndSendNewPassword(player, email);
}
bukkitService.runTaskAsynchronously(() -> {
if (recoveryCodeService.isRecoveryCodeNeeded()) {
// Recovery code is needed; generate and send one
recoveryService.createAndSendRecoveryCode(player, email);
} else {
// Code not needed, just send them a new password
recoveryService.generateAndSendNewPassword(player, email);
}
});
}
@Override

View File

@ -10,6 +10,7 @@ import fr.xephi.authme.datasource.DataSourceResult;
import fr.xephi.authme.mail.EmailService;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.PasswordRecoveryService;
import fr.xephi.authme.service.RecoveryCodeService;
@ -22,6 +23,7 @@ import org.mockito.Mock;
import java.util.Collections;
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToRunTaskAsynchronously;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
@ -63,6 +65,9 @@ public class RecoverEmailCommandTest {
@Mock
private RecoveryCodeService recoveryCodeService;
@Mock
private BukkitService bukkitService;
@BeforeClass
public static void initLogger() {
TestHelper.setupLogger();
@ -179,6 +184,7 @@ public class RecoverEmailCommandTest {
String code = "a94f37";
given(recoveryCodeService.isRecoveryCodeNeeded()).willReturn(true);
given(recoveryCodeService.generateCode(name)).willReturn(code);
setBukkitServiceToRunTaskAsynchronously(bukkitService);
// when
command.executeCommand(sender, Collections.singletonList(email.toUpperCase()));
@ -201,6 +207,7 @@ public class RecoverEmailCommandTest {
String email = "vulture@example.com";
given(dataSource.getEmail(name)).willReturn(DataSourceResult.of(email));
given(recoveryCodeService.isRecoveryCodeNeeded()).willReturn(false);
setBukkitServiceToRunTaskAsynchronously(bukkitService);
// when
command.executeCommand(sender, Collections.singletonList(email));