diff --git a/src/main/java/fr/xephi/authme/process/Management.java b/src/main/java/fr/xephi/authme/process/Management.java index 81b876300..374b9b911 100644 --- a/src/main/java/fr/xephi/authme/process/Management.java +++ b/src/main/java/fr/xephi/authme/process/Management.java @@ -106,7 +106,7 @@ public class Management { sched.runTaskAsynchronously(plugin, new Runnable() { @Override public void run() { - new AsyncChangeEmail(player, plugin, oldEmail, newEmail).process(); + new AsyncChangeEmail(player, plugin, oldEmail, newEmail, plugin.getDataSource(), PlayerCache.getInstance()).process(); } }); } diff --git a/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java b/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java index dface22ee..2d172bb95 100644 --- a/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java +++ b/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java @@ -3,6 +3,7 @@ package fr.xephi.authme.process.email; import fr.xephi.authme.AuthMe; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerCache; +import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.Messages; import fr.xephi.authme.settings.Settings; @@ -15,23 +16,26 @@ import org.bukkit.entity.Player; public class AsyncChangeEmail { private final Player player; - private final AuthMe plugin; private final String oldEmail; private final String newEmail; private final Messages m; + private final PlayerCache playerCache; + private final DataSource dataSource; - public AsyncChangeEmail(Player player, AuthMe plugin, String oldEmail, String newEmail) { + public AsyncChangeEmail(Player player, AuthMe plugin, String oldEmail, + String newEmail, DataSource dataSource, PlayerCache playerCache) { this.m = plugin.getMessages(); this.player = player; - this.plugin = plugin; this.oldEmail = oldEmail; this.newEmail = newEmail; + this.playerCache = playerCache; + this.dataSource = dataSource; } public void process() { String playerName = player.getName().toLowerCase(); - if (PlayerCache.getInstance().isAuthenticated(playerName)) { - PlayerAuth auth = PlayerCache.getInstance().getAuth(playerName); + if (playerCache.isAuthenticated(playerName)) { + PlayerAuth auth = playerCache.getAuth(playerName); String currentEmail = auth.getEmail(); if (currentEmail == null) { @@ -40,7 +44,7 @@ public class AsyncChangeEmail { m.send(player, MessageKey.INVALID_EMAIL); } else if (!oldEmail.equals(currentEmail)) { m.send(player, MessageKey.INVALID_OLD_EMAIL); - } else if (Settings.isEmailCorrect(newEmail)) { + } else if (!Settings.isEmailCorrect(newEmail)) { m.send(player, MessageKey.INVALID_NEW_EMAIL); } else { saveNewEmail(auth); @@ -52,8 +56,8 @@ public class AsyncChangeEmail { private void saveNewEmail(PlayerAuth auth) { auth.setEmail(newEmail); - if (plugin.getDataSource().updateEmail(auth)) { - PlayerCache.getInstance().updatePlayer(auth); + if (dataSource.updateEmail(auth)) { + playerCache.updatePlayer(auth); m.send(player, MessageKey.EMAIL_CHANGED_SUCCESS); } else { m.send(player, MessageKey.ERROR); @@ -62,7 +66,7 @@ public class AsyncChangeEmail { } private void outputUnloggedMessage() { - if (plugin.getDataSource().isAuthAvailable(player.getName())) { + if (dataSource.isAuthAvailable(player.getName())) { m.send(player, MessageKey.LOGIN_MESSAGE); } else if (Settings.emailRegistration) { m.send(player, MessageKey.REGISTER_EMAIL_MESSAGE); diff --git a/src/test/java/fr/xephi/authme/process/email/AsyncChangeEmailTest.java b/src/test/java/fr/xephi/authme/process/email/AsyncChangeEmailTest.java new file mode 100644 index 000000000..268e2663c --- /dev/null +++ b/src/test/java/fr/xephi/authme/process/email/AsyncChangeEmailTest.java @@ -0,0 +1,133 @@ +package fr.xephi.authme.process.email; + +import fr.xephi.authme.AuthMe; +import fr.xephi.authme.cache.auth.PlayerAuth; +import fr.xephi.authme.cache.auth.PlayerCache; +import fr.xephi.authme.datasource.DataSource; +import fr.xephi.authme.output.MessageKey; +import fr.xephi.authme.output.Messages; +import fr.xephi.authme.util.WrapperMock; +import org.bukkit.entity.Player; +import org.junit.After; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Test for {@link AsyncChangeEmail}. + */ +public class AsyncChangeEmailTest { + + private Player player; + private Messages messages; + private PlayerCache playerCache; + private DataSource dataSource; + + @BeforeClass + public static void setUp() { + WrapperMock.createInstance(); + } + + // Prevent the accidental re-use of a field in another test + @After + public void cleanFields() { + player = null; + messages = null; + playerCache = null; + dataSource = null; + } + + @Test + public void shouldAddEmail() { + // given + AsyncChangeEmail process = createProcess("old@mail.tld", "new@mail.tld"); + given(player.getName()).willReturn("Bobby"); + given(playerCache.isAuthenticated("bobby")).willReturn(true); + PlayerAuth auth = authWithMail("old@mail.tld"); + given(playerCache.getAuth("bobby")).willReturn(auth); + given(dataSource.updateEmail(auth)).willReturn(true); + + // when + process.process(); + + // then + verify(dataSource).updateEmail(auth); + verify(playerCache).updatePlayer(auth); + verify(messages).send(player, MessageKey.EMAIL_CHANGED_SUCCESS); + } + + @Test + public void shouldShowAddEmailUsage() { + // given + AsyncChangeEmail process = createProcess("old@mail.tld", "new@mail.tld"); + given(player.getName()).willReturn("Bobby"); + given(playerCache.isAuthenticated("bobby")).willReturn(true); + PlayerAuth auth = authWithMail(null); + given(playerCache.getAuth("bobby")).willReturn(auth); + + // when + process.process(); + + // then + verify(dataSource, never()).updateEmail(auth); + verify(playerCache, never()).updatePlayer(auth); + verify(messages).send(player, MessageKey.USAGE_ADD_EMAIL); + } + + @Test + public void shouldRejectInvalidNewMail() { + // given + AsyncChangeEmail process = createProcess("old@mail.tld", "bogus"); + given(player.getName()).willReturn("Bobby"); + given(playerCache.isAuthenticated("bobby")).willReturn(true); + PlayerAuth auth = authWithMail("old@mail.tld"); + given(playerCache.getAuth("bobby")).willReturn(auth); + + // when + process.process(); + + // then + verify(dataSource, never()).updateEmail(auth); + verify(playerCache, never()).updatePlayer(auth); + verify(messages).send(player, MessageKey.INVALID_NEW_EMAIL); + } + + @Test + public void shouldRejectInvalidOldEmail() { + // given + AsyncChangeEmail process = createProcess("old@mail.tld", "new@mail.tld"); + given(player.getName()).willReturn("Bobby"); + given(playerCache.isAuthenticated("bobby")).willReturn(true); + PlayerAuth auth = authWithMail("other@address.email"); + given(playerCache.getAuth("bobby")).willReturn(auth); + + // when + process.process(); + + // then + verify(dataSource, never()).updateEmail(auth); + verify(playerCache, never()).updatePlayer(auth); + verify(messages).send(player, MessageKey.INVALID_OLD_EMAIL); + } + + private static PlayerAuth authWithMail(String email) { + PlayerAuth auth = mock(PlayerAuth.class); + when(auth.getEmail()).thenReturn(email); + return auth; + } + + private AsyncChangeEmail createProcess(String oldEmail, String newEmail) { + player = mock(Player.class); + messages = mock(Messages.class); + AuthMe authMe = mock(AuthMe.class); + when(authMe.getMessages()).thenReturn(messages); + playerCache = mock(PlayerCache.class); + dataSource = mock(DataSource.class); + return new AsyncChangeEmail(player, authMe, oldEmail, newEmail, dataSource, playerCache); + } +}