LoginSystem/src/test/java/fr/xephi/authme/command/CommandServiceTest.java
ljacqu 55f7e8097a #743 Add proper error message for "invalid chars in password"
- Change password validation to return a ValidationResult object for passing message arguments
- Remove wrapping methods in ProcessService and CommandService and use ValidationService directly
2016-06-03 12:51:49 +02:00

175 lines
5.2 KiB
Java

package fr.xephi.authme.command;
import fr.xephi.authme.command.help.HelpProvider;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.domain.Property;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.ValidationService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.Arrays;
import java.util.List;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
* Test for {@link CommandService}.
*/
@RunWith(MockitoJUnitRunner.class)
public class CommandServiceTest {
@InjectMocks
private CommandService commandService;
@Mock
private CommandMapper commandMapper;
@Mock
private HelpProvider helpProvider;
@Mock
private Messages messages;
@Mock
private NewSetting settings;
@Mock
private ValidationService validationService;
@Test
public void shouldSendMessage() {
// given
CommandSender sender = mock(CommandSender.class);
// when
commandService.send(sender, MessageKey.INVALID_EMAIL);
// then
verify(messages).send(sender, MessageKey.INVALID_EMAIL);
}
@Test
public void shouldSendMessageWithReplacements() {
// given
CommandSender sender = mock(Player.class);
// when
commandService.send(sender, MessageKey.ANTIBOT_AUTO_ENABLED_MESSAGE, "10");
// then
verify(messages).send(sender, MessageKey.ANTIBOT_AUTO_ENABLED_MESSAGE, "10");
}
@Test
public void shouldMapPartsToCommand() {
// given
CommandSender sender = mock(Player.class);
List<String> commandParts = Arrays.asList("authme", "test", "test2");
FoundCommandResult givenResult = mock(FoundCommandResult.class);
given(commandMapper.mapPartsToCommand(sender, commandParts)).willReturn(givenResult);
// when
FoundCommandResult result = commandService.mapPartsToCommand(sender, commandParts);
// then
assertThat(result, equalTo(givenResult));
verify(commandMapper).mapPartsToCommand(sender, commandParts);
}
@Test
public void shouldOutputHelp() {
// given
CommandSender sender = mock(CommandSender.class);
FoundCommandResult result = mock(FoundCommandResult.class);
int options = HelpProvider.SHOW_LONG_DESCRIPTION;
List<String> messages = Arrays.asList("Test message 1", "Other test message", "Third message for test");
given(helpProvider.printHelp(sender, result, options)).willReturn(messages);
// when
commandService.outputHelp(sender, result, options);
// then
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(sender, times(3)).sendMessage(captor.capture());
assertThat(captor.getAllValues(), equalTo(messages));
}
@Test
public void shouldRetrieveMessage() {
// given
MessageKey key = MessageKey.USAGE_CAPTCHA;
String[] givenMessages = new String[]{"Lorem ipsum...", "Test line test"};
given(messages.retrieve(key)).willReturn(givenMessages);
// when
String[] result = commandService.retrieveMessage(key);
// then
assertThat(result, equalTo(givenMessages));
verify(messages).retrieve(key);
}
@Test
public void shouldRetrieveProperty() {
// given
Property<Integer> property = SecuritySettings.CAPTCHA_LENGTH;
given(settings.getProperty(property)).willReturn(7);
// when
int result = commandService.getProperty(property);
// then
assertThat(result, equalTo(7));
verify(settings).getProperty(property);
}
@Test
public void shouldReturnSettings() {
// given/when
NewSetting result = commandService.getSettings();
// then
assertThat(result, equalTo(settings));
}
@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);
}
}