mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-20 07:37:47 +01:00
Add tests for change email process
This commit is contained in:
parent
5996d58081
commit
8c05c8df98
@ -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();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user