#437 Add email should not allow to change email

- Create separate test for adding email
- Check that no email is yet registered for add email
This commit is contained in:
ljacqu 2016-01-13 22:08:40 +01:00
parent 4042ced5f2
commit 8ed8b32589
5 changed files with 112 additions and 55 deletions

View File

@ -2,6 +2,9 @@ package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.PlayerCommand; import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.StringUtils;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.List; import java.util.List;
@ -10,9 +13,15 @@ public class AddEmailCommand extends PlayerCommand {
@Override @Override
public void runCommand(Player player, List<String> arguments, CommandService commandService) { public void runCommand(Player player, List<String> arguments, CommandService commandService) {
String playerMail = arguments.get(0); String email = arguments.get(0);
String playerMailVerify = arguments.get(1); String emailConfirmation = arguments.get(1);
commandService.getManagement().performAddEmail(player, playerMail, playerMailVerify); if (StringUtils.isEmpty(email) || "your@email.com".equals(email) || !Settings.isEmailCorrect(email)) {
commandService.send(player, MessageKey.INVALID_EMAIL);
} else if (email.equals(emailConfirmation)) {
commandService.getManagement().performAddEmail(player, email);
} else {
commandService.send(player, MessageKey.CONFIRM_EMAIL_MESSAGE);
}
} }
} }

View File

@ -1,6 +1,7 @@
package fr.xephi.authme.process; package fr.xephi.authme.process;
import fr.xephi.authme.AuthMe; import fr.xephi.authme.AuthMe;
import fr.xephi.authme.process.email.AsyncAddEmail;
import fr.xephi.authme.process.email.AsyncChangeEmail; import fr.xephi.authme.process.email.AsyncChangeEmail;
import fr.xephi.authme.process.join.AsynchronousJoin; import fr.xephi.authme.process.join.AsynchronousJoin;
import fr.xephi.authme.process.login.AsynchronousLogin; import fr.xephi.authme.process.login.AsynchronousLogin;
@ -90,11 +91,11 @@ public class Management {
}); });
} }
public void performAddEmail(final Player player, final String newEmail, final String newEmailVerify) { public void performAddEmail(final Player player, final String newEmail) {
sched.runTaskAsynchronously(plugin, new Runnable() { sched.runTaskAsynchronously(plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
new AsyncChangeEmail(player, plugin, null, newEmail, newEmailVerify).process(); new AsyncAddEmail(plugin, player, newEmail).process();
} }
}); });
} }

View File

@ -0,0 +1,62 @@
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.StringUtils;
import org.bukkit.entity.Player;
/**
* Async task to add an email to an account.
*/
public class AsyncAddEmail {
private AuthMe plugin;
private Player player;
private String email;
private Messages messages;
public AsyncAddEmail(AuthMe plugin, Player player, String email) {
this.plugin = plugin;
this.messages = plugin.getMessages();
this.player = player;
this.email = email;
}
public void process() {
String playerName = player.getName().toLowerCase();
PlayerCache playerCache = PlayerCache.getInstance();
if (playerCache.isAuthenticated(playerName)) {
PlayerAuth auth = PlayerCache.getInstance().getAuth(playerName);
String currentEmail = auth.getEmail();
if (currentEmail == null) {
messages.send(player, MessageKey.USAGE_CHANGE_EMAIL);
} else if (StringUtils.isEmpty(email) || "your@email.com".equals(email) || Settings.isEmailCorrect(email)) {
messages.send(player, MessageKey.INVALID_EMAIL);
} else {
auth.setEmail(email);
playerCache.updatePlayer(auth);
messages.send(player, MessageKey.EMAIL_ADDED_SUCCESS);
}
} else {
sendUnloggedMessage(plugin.getDataSource());
}
}
private void sendUnloggedMessage(DataSource dataSource) {
if (dataSource.isAuthAvailable(player.getName())) {
messages.send(player, MessageKey.LOGIN_MESSAGE);
} else if (Settings.emailRegistration) {
messages.send(player, MessageKey.REGISTER_EMAIL_MESSAGE);
} else {
messages.send(player, MessageKey.REGISTER_MESSAGE);
}
}
}

View File

@ -3,6 +3,7 @@ package fr.xephi.authme.process.email;
import fr.xephi.authme.AuthMe; import fr.xephi.authme.AuthMe;
import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages; import fr.xephi.authme.output.Messages;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
@ -17,74 +18,56 @@ public class AsyncChangeEmail {
private final AuthMe plugin; private final AuthMe plugin;
private final String oldEmail; private final String oldEmail;
private final String newEmail; private final String newEmail;
private final String newEmailVerify;
private final Messages m; private final Messages m;
public AsyncChangeEmail(Player player, AuthMe plugin, String oldEmail, String newEmail, String newEmailVerify) { public AsyncChangeEmail(Player player, AuthMe plugin, String oldEmail, String newEmail) {
this.m = plugin.getMessages(); this.m = plugin.getMessages();
this.player = player; this.player = player;
this.plugin = plugin; this.plugin = plugin;
this.oldEmail = oldEmail; this.oldEmail = oldEmail;
this.newEmail = newEmail; this.newEmail = newEmail;
this.newEmailVerify = newEmailVerify;
}
public AsyncChangeEmail(Player player, AuthMe plugin, String oldEmail, String newEmail) {
this(player, plugin, oldEmail, newEmail, newEmail);
} }
public void process() { public void process() {
String playerName = player.getName().toLowerCase(); String playerName = player.getName().toLowerCase();
if (PlayerCache.getInstance().isAuthenticated(playerName)) { if (PlayerCache.getInstance().isAuthenticated(playerName)) {
if (!newEmail.equals(newEmailVerify)) {
m.send(player, MessageKey.CONFIRM_EMAIL_MESSAGE);
return;
}
PlayerAuth auth = PlayerCache.getInstance().getAuth(playerName); PlayerAuth auth = PlayerCache.getInstance().getAuth(playerName);
String currentEmail = auth.getEmail(); String currentEmail = auth.getEmail();
if (oldEmail != null) {
if (StringUtils.isEmpty(currentEmail) || currentEmail.equals("your@email.com")) { if (currentEmail == null) {
m.send(player, MessageKey.USAGE_ADD_EMAIL); m.send(player, MessageKey.USAGE_ADD_EMAIL);
return; } else if (StringUtils.isEmpty(newEmail) || "your@email.com".equals(newEmail)) {
} m.send(player, MessageKey.INVALID_EMAIL);
if (!oldEmail.equals(currentEmail)) { } else if (!oldEmail.equals(currentEmail)) {
m.send(player, MessageKey.INVALID_OLD_EMAIL); m.send(player, MessageKey.INVALID_OLD_EMAIL);
return; } else if (Settings.isEmailCorrect(newEmail)) {
m.send(player, MessageKey.INVALID_NEW_EMAIL);
} else {
saveNewEmail(auth);
} }
} else { } else {
if (!StringUtils.isEmpty(currentEmail) && !currentEmail.equals("your@email.com")) { outputUnloggedMessage();
m.send(player, MessageKey.USAGE_CHANGE_EMAIL);
return;
} }
} }
if (!Settings.isEmailCorrect(newEmail)) {
m.send(player, MessageKey.INVALID_NEW_EMAIL); private void saveNewEmail(PlayerAuth auth) {
return;
}
auth.setEmail(newEmail); auth.setEmail(newEmail);
if (!plugin.getDataSource().updateEmail(auth)) { if (plugin.getDataSource().updateEmail(auth)) {
m.send(player, MessageKey.ERROR);
auth.setEmail(currentEmail);
return;
}
PlayerCache.getInstance().updatePlayer(auth); PlayerCache.getInstance().updatePlayer(auth);
if (oldEmail == null) {
m.send(player, MessageKey.EMAIL_ADDED_SUCCESS);
player.sendMessage(auth.getEmail());
return;
}
m.send(player, MessageKey.EMAIL_CHANGED_SUCCESS); m.send(player, MessageKey.EMAIL_CHANGED_SUCCESS);
} else { } else {
if (plugin.getDataSource().isAuthAvailable(playerName)) { m.send(player, MessageKey.ERROR);
auth.setEmail(newEmail);
}
}
private void outputUnloggedMessage() {
if (plugin.getDataSource().isAuthAvailable(player.getName())) {
m.send(player, MessageKey.LOGIN_MESSAGE); m.send(player, MessageKey.LOGIN_MESSAGE);
} else { } else if (Settings.emailRegistration) {
if (Settings.emailRegistration) {
m.send(player, MessageKey.REGISTER_EMAIL_MESSAGE); m.send(player, MessageKey.REGISTER_EMAIL_MESSAGE);
} else { } else {
m.send(player, MessageKey.REGISTER_MESSAGE); m.send(player, MessageKey.REGISTER_MESSAGE);
} }
} }
} }
}
}

View File

@ -2,6 +2,7 @@ package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.process.Management; import fr.xephi.authme.process.Management;
import fr.xephi.authme.util.WrapperMock;
import org.bukkit.command.BlockCommandSender; import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -27,6 +28,7 @@ public class AddEmailCommandTest {
@Before @Before
public void setUpMocks() { public void setUpMocks() {
commandService = mock(CommandService.class); commandService = mock(CommandService.class);
WrapperMock.createInstance();
} }
@Test @Test
@ -51,10 +53,10 @@ public class AddEmailCommandTest {
given(commandService.getManagement()).willReturn(management); given(commandService.getManagement()).willReturn(management);
// when // when
command.executeCommand(sender, Arrays.asList("mail@example", "other_example"), commandService); command.executeCommand(sender, Arrays.asList("mail@example", "mail@example"), commandService);
// then // then
verify(management).performAddEmail(sender, "mail@example", "other_example"); verify(management).performAddEmail(sender, "mail@example");
} }
} }