#567 Move email validation logic to validation service
This commit is contained in:
@@ -246,4 +246,35 @@ public class CommandServiceTest {
|
||||
assertThat(result, equalTo(MessageKey.INVALID_PASSWORD_LENGTH));
|
||||
verify(validationService).validatePassword(password, user);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldValidateEmail() {
|
||||
// given
|
||||
String email = "test@example.tld";
|
||||
given(validationService.validateEmail(email)).willReturn(true);
|
||||
|
||||
// when
|
||||
boolean result = commandService.validateEmail(email);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
verify(validationService).validateEmail(email);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCheckIfEmailCanBeUsed() {
|
||||
// given
|
||||
String email = "mail@example.com";
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
given(validationService.isEmailFreeForRegistration(email, sender))
|
||||
.willReturn(true);
|
||||
|
||||
// when
|
||||
boolean result = commandService.isEmailFreeForRegistration(email, sender);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
verify(validationService).isEmailFreeForRegistration(email, sender);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
package fr.xephi.authme.command.executable.email;
|
||||
|
||||
import fr.xephi.authme.command.CommandService;
|
||||
import fr.xephi.authme.output.MessageKey;
|
||||
import fr.xephi.authme.process.Management;
|
||||
import fr.xephi.authme.settings.NewSetting;
|
||||
import org.bukkit.command.BlockCommandSender;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -21,19 +22,16 @@ import static org.mockito.Mockito.verify;
|
||||
/**
|
||||
* Test for {@link AddEmailCommand}.
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class AddEmailCommandTest {
|
||||
|
||||
@Mock
|
||||
private CommandService commandService;
|
||||
|
||||
@Before
|
||||
public void setUpMocks() {
|
||||
commandService = mock(CommandService.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRejectNonPlayerSender() {
|
||||
// given
|
||||
CommandSender sender = Mockito.mock(BlockCommandSender.class);
|
||||
CommandSender sender = mock(BlockCommandSender.class);
|
||||
AddEmailCommand command = new AddEmailCommand();
|
||||
|
||||
// when
|
||||
@@ -46,18 +44,34 @@ public class AddEmailCommandTest {
|
||||
@Test
|
||||
public void shouldForwardData() {
|
||||
// given
|
||||
Player sender = Mockito.mock(Player.class);
|
||||
AddEmailCommand command = new AddEmailCommand();
|
||||
Player sender = mock(Player.class);
|
||||
String email = "mail@example";
|
||||
given(commandService.validateEmail(email)).willReturn(true);
|
||||
Management management = mock(Management.class);
|
||||
given(commandService.getManagement()).willReturn(management);
|
||||
NewSetting settings = mock(NewSetting.class);
|
||||
given(commandService.getSettings()).willReturn(settings);
|
||||
AddEmailCommand command = new AddEmailCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Arrays.asList("mail@example", "mail@example"), commandService);
|
||||
command.executeCommand(sender, Arrays.asList(email, email), commandService);
|
||||
|
||||
// then
|
||||
verify(management).performAddEmail(sender, "mail@example");
|
||||
verify(management).performAddEmail(sender, email);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFailForConfirmationMismatch() {
|
||||
// given
|
||||
Player sender = mock(Player.class);
|
||||
String email = "asdfasdf@example.com";
|
||||
given(commandService.validateEmail(email)).willReturn(true);
|
||||
AddEmailCommand command = new AddEmailCommand();
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Arrays.asList(email, "wrongConf"), commandService);
|
||||
|
||||
// then
|
||||
verify(commandService, never()).getManagement();
|
||||
verify(commandService).send(sender, MessageKey.CONFIRM_EMAIL_MESSAGE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package fr.xephi.authme.command.executable.register;
|
||||
import fr.xephi.authme.command.CommandService;
|
||||
import fr.xephi.authme.output.MessageKey;
|
||||
import fr.xephi.authme.process.Management;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.RegistrationSettings;
|
||||
import fr.xephi.authme.util.WrapperMock;
|
||||
import org.bukkit.command.BlockCommandSender;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@@ -14,6 +14,7 @@ import org.junit.Test;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
import static fr.xephi.authme.settings.properties.RestrictionSettings.ENABLE_PASSWORD_CONFIRMATION;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Matchers.argThat;
|
||||
@@ -31,7 +32,6 @@ public class RegisterCommandTest {
|
||||
@Before
|
||||
public void initializeAuthMeMock() {
|
||||
WrapperMock.createInstance();
|
||||
Settings.captchaLength = 10;
|
||||
commandService = mock(CommandService.class);
|
||||
}
|
||||
|
||||
@@ -70,6 +70,8 @@ public class RegisterCommandTest {
|
||||
RegisterCommand command = new RegisterCommand();
|
||||
Management management = mock(Management.class);
|
||||
given(commandService.getManagement()).willReturn(management);
|
||||
given(commandService.getProperty(ENABLE_PASSWORD_CONFIRMATION)).willReturn(false);
|
||||
given(commandService.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(false);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.singletonList("password"), commandService);
|
||||
|
||||
Reference in New Issue
Block a user