mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2025-01-31 03:51:31 +01:00
#437 Create test for AddEmail task; fix bugs
This commit is contained in:
parent
8ed8b32589
commit
5996d58081
@ -1,6 +1,7 @@
|
||||
package fr.xephi.authme.process;
|
||||
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.cache.auth.PlayerCache;
|
||||
import fr.xephi.authme.process.email.AsyncAddEmail;
|
||||
import fr.xephi.authme.process.email.AsyncChangeEmail;
|
||||
import fr.xephi.authme.process.join.AsynchronousJoin;
|
||||
@ -95,7 +96,8 @@ public class Management {
|
||||
sched.runTaskAsynchronously(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
new AsyncAddEmail(plugin, player, newEmail).process();
|
||||
new AsyncAddEmail(plugin, player, newEmail, plugin.getDataSource(), PlayerCache.getInstance())
|
||||
.process();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -15,29 +15,30 @@ import org.bukkit.entity.Player;
|
||||
*/
|
||||
public class AsyncAddEmail {
|
||||
|
||||
private AuthMe plugin;
|
||||
private Player player;
|
||||
private String email;
|
||||
private Messages messages;
|
||||
private final Player player;
|
||||
private final String email;
|
||||
private final Messages messages;
|
||||
private final DataSource dataSource;
|
||||
private final PlayerCache playerCache;
|
||||
|
||||
public AsyncAddEmail(AuthMe plugin, Player player, String email) {
|
||||
this.plugin = plugin;
|
||||
public AsyncAddEmail(AuthMe plugin, Player player, String email, DataSource dataSource, PlayerCache playerCache) {
|
||||
this.messages = plugin.getMessages();
|
||||
this.player = player;
|
||||
this.email = email;
|
||||
this.dataSource = dataSource;
|
||||
this.playerCache = playerCache;
|
||||
}
|
||||
|
||||
public void process() {
|
||||
String playerName = player.getName().toLowerCase();
|
||||
PlayerCache playerCache = PlayerCache.getInstance();
|
||||
|
||||
if (playerCache.isAuthenticated(playerName)) {
|
||||
PlayerAuth auth = PlayerCache.getInstance().getAuth(playerName);
|
||||
PlayerAuth auth = playerCache.getAuth(playerName);
|
||||
String currentEmail = auth.getEmail();
|
||||
|
||||
if (currentEmail == null) {
|
||||
if (currentEmail != null) {
|
||||
messages.send(player, MessageKey.USAGE_CHANGE_EMAIL);
|
||||
} else if (StringUtils.isEmpty(email) || "your@email.com".equals(email) || Settings.isEmailCorrect(email)) {
|
||||
} else if (isEmailInvalid(email)) {
|
||||
messages.send(player, MessageKey.INVALID_EMAIL);
|
||||
} else {
|
||||
auth.setEmail(email);
|
||||
@ -45,10 +46,15 @@ public class AsyncAddEmail {
|
||||
messages.send(player, MessageKey.EMAIL_ADDED_SUCCESS);
|
||||
}
|
||||
} else {
|
||||
sendUnloggedMessage(plugin.getDataSource());
|
||||
sendUnloggedMessage(dataSource);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isEmailInvalid(String email) {
|
||||
return StringUtils.isEmpty(email) || "your@email.com".equals(email)
|
||||
|| !Settings.isEmailCorrect(email);
|
||||
}
|
||||
|
||||
private void sendUnloggedMessage(DataSource dataSource) {
|
||||
if (dataSource.isAuthAvailable(player.getName())) {
|
||||
messages.send(player, MessageKey.LOGIN_MESSAGE);
|
||||
|
@ -3,7 +3,6 @@ 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;
|
||||
@ -11,6 +10,7 @@ import fr.xephi.authme.util.StringUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
/**
|
||||
* Async task for changing the email.
|
||||
*/
|
||||
public class AsyncChangeEmail {
|
||||
|
||||
|
@ -0,0 +1,168 @@
|
||||
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;
|
||||
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.Matchers.any;
|
||||
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 AsyncAddEmail}.
|
||||
*/
|
||||
public class AsyncAddEmailTest {
|
||||
|
||||
private Messages messages;
|
||||
private Player player;
|
||||
private DataSource dataSource;
|
||||
private PlayerCache playerCache;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() {
|
||||
WrapperMock.createInstance();
|
||||
}
|
||||
|
||||
// Clean up the fields to ensure that no test uses elements of another test
|
||||
@After
|
||||
public void removeFieldValues() {
|
||||
messages = null;
|
||||
player = null;
|
||||
dataSource = null;
|
||||
playerCache = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldAddEmail() {
|
||||
// given
|
||||
AsyncAddEmail process = createProcess("my.mail@example.org");
|
||||
given(player.getName()).willReturn("testEr");
|
||||
given(playerCache.isAuthenticated("tester")).willReturn(true);
|
||||
PlayerAuth auth = mock(PlayerAuth.class);
|
||||
given(auth.getEmail()).willReturn(null);
|
||||
given(playerCache.getAuth("tester")).willReturn(auth);
|
||||
|
||||
// when
|
||||
process.process();
|
||||
|
||||
// then
|
||||
verify(messages).send(player, MessageKey.EMAIL_ADDED_SUCCESS);
|
||||
verify(auth).setEmail("my.mail@example.org");
|
||||
verify(playerCache).updatePlayer(auth);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotAddMailIfPlayerAlreadyHasEmail() {
|
||||
// given
|
||||
AsyncAddEmail process = createProcess("some.mail@example.org");
|
||||
given(player.getName()).willReturn("my_Player");
|
||||
given(playerCache.isAuthenticated("my_player")).willReturn(true);
|
||||
PlayerAuth auth = mock(PlayerAuth.class);
|
||||
given(auth.getEmail()).willReturn("another@mail.tld");
|
||||
given(playerCache.getAuth("my_player")).willReturn(auth);
|
||||
|
||||
// when
|
||||
process.process();
|
||||
|
||||
// then
|
||||
verify(messages).send(player, MessageKey.USAGE_CHANGE_EMAIL);
|
||||
verify(playerCache, never()).updatePlayer(any(PlayerAuth.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotAddMailIfItIsInvalid() {
|
||||
// given
|
||||
AsyncAddEmail process = createProcess("invalid_mail");
|
||||
given(player.getName()).willReturn("my_Player");
|
||||
given(playerCache.isAuthenticated("my_player")).willReturn(true);
|
||||
PlayerAuth auth = mock(PlayerAuth.class);
|
||||
given(auth.getEmail()).willReturn(null);
|
||||
given(playerCache.getAuth("my_player")).willReturn(auth);
|
||||
|
||||
// when
|
||||
process.process();
|
||||
|
||||
// then
|
||||
verify(messages).send(player, MessageKey.INVALID_EMAIL);
|
||||
verify(playerCache, never()).updatePlayer(any(PlayerAuth.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldShowLoginMessage() {
|
||||
// given
|
||||
AsyncAddEmail process = createProcess("test@mail.com");
|
||||
given(player.getName()).willReturn("Username12");
|
||||
given(playerCache.isAuthenticated("username12")).willReturn(false);
|
||||
given(dataSource.isAuthAvailable("Username12")).willReturn(true);
|
||||
|
||||
// when
|
||||
process.process();
|
||||
|
||||
// then
|
||||
verify(messages).send(player, MessageKey.LOGIN_MESSAGE);
|
||||
verify(playerCache, never()).updatePlayer(any(PlayerAuth.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldShowEmailRegisterMessage() {
|
||||
// given
|
||||
AsyncAddEmail process = createProcess("test@mail.com");
|
||||
given(player.getName()).willReturn("user");
|
||||
given(playerCache.isAuthenticated("user")).willReturn(false);
|
||||
given(dataSource.isAuthAvailable("user")).willReturn(false);
|
||||
Settings.emailRegistration = true;
|
||||
|
||||
// when
|
||||
process.process();
|
||||
|
||||
// then
|
||||
verify(messages).send(player, MessageKey.REGISTER_EMAIL_MESSAGE);
|
||||
verify(playerCache, never()).updatePlayer(any(PlayerAuth.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldShowRegularRegisterMessage() {
|
||||
// given
|
||||
AsyncAddEmail process = createProcess("test@mail.com");
|
||||
given(player.getName()).willReturn("user");
|
||||
given(playerCache.isAuthenticated("user")).willReturn(false);
|
||||
given(dataSource.isAuthAvailable("user")).willReturn(false);
|
||||
Settings.emailRegistration = false;
|
||||
|
||||
// when
|
||||
process.process();
|
||||
|
||||
// then
|
||||
verify(messages).send(player, MessageKey.REGISTER_MESSAGE);
|
||||
verify(playerCache, never()).updatePlayer(any(PlayerAuth.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance of {@link AsyncAddEmail} and save the mcoks to this class' fields.
|
||||
*
|
||||
* @param email The email to use
|
||||
* @return The created process
|
||||
*/
|
||||
private AsyncAddEmail createProcess(String email) {
|
||||
messages = mock(Messages.class);
|
||||
AuthMe authMe = mock(AuthMe.class);
|
||||
when(authMe.getMessages()).thenReturn(messages);
|
||||
player = mock(Player.class);
|
||||
dataSource = mock(DataSource.class);
|
||||
playerCache = mock(PlayerCache.class);
|
||||
return new AsyncAddEmail(authMe, player, email, dataSource, playerCache);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user