From ace95f750abd36e72bca6b70f9862f292f3bf082 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sat, 16 Jan 2016 07:57:43 +0100 Subject: [PATCH] Add more tests for async change email process --- .../process/email/AsyncChangeEmail.java | 11 ++- .../process/email/AsyncChangeEmailTest.java | 86 +++++++++++++++++-- 2 files changed, 87 insertions(+), 10 deletions(-) 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 2d172bb95..c69e37952 100644 --- a/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java +++ b/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java @@ -40,12 +40,10 @@ public class AsyncChangeEmail { if (currentEmail == null) { m.send(player, MessageKey.USAGE_ADD_EMAIL); - } else if (StringUtils.isEmpty(newEmail) || "your@email.com".equals(newEmail)) { - m.send(player, MessageKey.INVALID_EMAIL); + } else if (isEmailInvalid(newEmail)) { + m.send(player, MessageKey.INVALID_NEW_EMAIL); } else if (!oldEmail.equals(currentEmail)) { m.send(player, MessageKey.INVALID_OLD_EMAIL); - } else if (!Settings.isEmailCorrect(newEmail)) { - m.send(player, MessageKey.INVALID_NEW_EMAIL); } else { saveNewEmail(auth); } @@ -54,6 +52,11 @@ public class AsyncChangeEmail { } } + private static boolean isEmailInvalid(String email) { + return StringUtils.isEmpty(email) || "your@email.com".equals(email) + || !Settings.isEmailCorrect(email); + } + private void saveNewEmail(PlayerAuth auth) { auth.setEmail(newEmail); if (dataSource.updateEmail(auth)) { diff --git a/src/test/java/fr/xephi/authme/process/email/AsyncChangeEmailTest.java b/src/test/java/fr/xephi/authme/process/email/AsyncChangeEmailTest.java index 268e2663c..92cfec679 100644 --- a/src/test/java/fr/xephi/authme/process/email/AsyncChangeEmailTest.java +++ b/src/test/java/fr/xephi/authme/process/email/AsyncChangeEmailTest.java @@ -6,6 +6,7 @@ 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; import fr.xephi.authme.util.WrapperMock; import org.bukkit.entity.Player; import org.junit.After; @@ -13,6 +14,7 @@ import org.junit.BeforeClass; import org.junit.Test; import static org.mockito.BDDMockito.given; +import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; @@ -61,6 +63,25 @@ public class AsyncChangeEmailTest { verify(messages).send(player, MessageKey.EMAIL_CHANGED_SUCCESS); } + @Test + public void shouldShowErrorIfSaveFails() { + // 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(false); + + // when + process.process(); + + // then + verify(dataSource).updateEmail(auth); + verify(playerCache, never()).updatePlayer(auth); + verify(messages).send(player, MessageKey.ERROR); + } + @Test public void shouldShowAddEmailUsage() { // given @@ -74,8 +95,8 @@ public class AsyncChangeEmailTest { process.process(); // then - verify(dataSource, never()).updateEmail(auth); - verify(playerCache, never()).updatePlayer(auth); + verify(dataSource, never()).updateEmail(any(PlayerAuth.class)); + verify(playerCache, never()).updatePlayer(any(PlayerAuth.class)); verify(messages).send(player, MessageKey.USAGE_ADD_EMAIL); } @@ -92,8 +113,8 @@ public class AsyncChangeEmailTest { process.process(); // then - verify(dataSource, never()).updateEmail(auth); - verify(playerCache, never()).updatePlayer(auth); + verify(dataSource, never()).updateEmail(any(PlayerAuth.class)); + verify(playerCache, never()).updatePlayer(any(PlayerAuth.class)); verify(messages).send(player, MessageKey.INVALID_NEW_EMAIL); } @@ -110,11 +131,64 @@ public class AsyncChangeEmailTest { process.process(); // then - verify(dataSource, never()).updateEmail(auth); - verify(playerCache, never()).updatePlayer(auth); + verify(dataSource, never()).updateEmail(any(PlayerAuth.class)); + verify(playerCache, never()).updatePlayer(any(PlayerAuth.class)); verify(messages).send(player, MessageKey.INVALID_OLD_EMAIL); } + @Test + public void shouldSendLoginMessage() { + // given + AsyncChangeEmail process = createProcess("old@mail.tld", "new@mail.tld"); + given(player.getName()).willReturn("Bobby"); + given(playerCache.isAuthenticated("bobby")).willReturn(false); + given(dataSource.isAuthAvailable("Bobby")).willReturn(true); + + // when + process.process(); + + // then + verify(dataSource, never()).updateEmail(any(PlayerAuth.class)); + verify(playerCache, never()).updatePlayer(any(PlayerAuth.class)); + verify(messages).send(player, MessageKey.LOGIN_MESSAGE); + } + + @Test + public void shouldShowEmailRegistrationMessage() { + // given + AsyncChangeEmail process = createProcess("old@mail.tld", "new@mail.tld"); + given(player.getName()).willReturn("Bobby"); + given(playerCache.isAuthenticated("bobby")).willReturn(false); + given(dataSource.isAuthAvailable("Bobby")).willReturn(false); + Settings.emailRegistration = true; + + // when + process.process(); + + // then + verify(dataSource, never()).updateEmail(any(PlayerAuth.class)); + verify(playerCache, never()).updatePlayer(any(PlayerAuth.class)); + verify(messages).send(player, MessageKey.REGISTER_EMAIL_MESSAGE); + } + + @Test + public void shouldShowRegistrationMessage() { + // given + AsyncChangeEmail process = createProcess("old@mail.tld", "new@mail.tld"); + given(player.getName()).willReturn("Bobby"); + given(playerCache.isAuthenticated("bobby")).willReturn(false); + given(dataSource.isAuthAvailable("Bobby")).willReturn(false); + Settings.emailRegistration = false; + + // when + process.process(); + + // then + verify(dataSource, never()).updateEmail(any(PlayerAuth.class)); + verify(playerCache, never()).updatePlayer(any(PlayerAuth.class)); + verify(messages).send(player, MessageKey.REGISTER_MESSAGE); + } + private static PlayerAuth authWithMail(String email) { PlayerAuth auth = mock(PlayerAuth.class); when(auth.getEmail()).thenReturn(email);