From 8bae71e1bd6acfacd9f287039c324b99420c2bff Mon Sep 17 00:00:00 2001 From: ljacqu Date: Thu, 21 Dec 2017 21:54:23 +0100 Subject: [PATCH] #1435 Send password recovery emails in async --- .../executable/email/RecoverEmailCommand.java | 20 ++++++++++++------- .../email/RecoverEmailCommandTest.java | 7 +++++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/main/java/fr/xephi/authme/command/executable/email/RecoverEmailCommand.java b/src/main/java/fr/xephi/authme/command/executable/email/RecoverEmailCommand.java index 7787bb569..339980a34 100644 --- a/src/main/java/fr/xephi/authme/command/executable/email/RecoverEmailCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/email/RecoverEmailCommand.java @@ -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 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 diff --git a/src/test/java/fr/xephi/authme/command/executable/email/RecoverEmailCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/email/RecoverEmailCommandTest.java index 9ca0bc090..416649e05 100644 --- a/src/test/java/fr/xephi/authme/command/executable/email/RecoverEmailCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/email/RecoverEmailCommandTest.java @@ -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));