Minor - Use explicit dependencies in ProcessService

- Replace AuthMe in favor of PermissionsManager
- Create missing tests for ProcessService
This commit is contained in:
ljacqu 2016-05-24 18:25:38 +02:00
parent a7d3e60abe
commit 8de1897412
4 changed files with 55 additions and 37 deletions

View File

@ -1,15 +1,17 @@
package fr.xephi.authme.process;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.domain.Property;
import fr.xephi.authme.util.ValidationService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.plugin.PluginManager;
import javax.inject.Inject;
/**
@ -24,11 +26,14 @@ public class ProcessService {
private Messages messages;
@Inject
private AuthMe plugin;
private PluginManager pluginManager;
@Inject
private ValidationService validationService;
@Inject
private PermissionsManager permissionsManager;
/**
* Retrieve a property's value.
*
@ -96,16 +101,7 @@ public class ProcessService {
* @param event the event to emit
*/
public void callEvent(Event event) {
plugin.getServer().getPluginManager().callEvent(event);
}
/**
* Return the plugin instance.
*
* @return AuthMe instance
*/
public AuthMe getAuthMe() {
return plugin;
pluginManager.callEvent(event);
}
/**
@ -128,7 +124,7 @@ public class ProcessService {
}
public boolean hasPermission(Player player, PermissionNode node) {
return plugin.getPermissionsManager().hasPermission(player, node);
return permissionsManager.hasPermission(player, node);
}
}

View File

@ -1,5 +1,6 @@
package fr.xephi.authme.process.register;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.cache.limbo.LimboPlayer;
@ -33,6 +34,9 @@ public class ProcessSyncEmailRegister implements SynchronousProcess {
@Inject
private BukkitService bukkitService;
@Inject
private AuthMe authMe;
public ProcessSyncEmailRegister() { }
public void processEmailRegister(Player player) {
@ -47,7 +51,7 @@ public class ProcessSyncEmailRegister implements SynchronousProcess {
if (limbo != null) {
if (time != 0) {
BukkitTask id = bukkitService.runTaskLater(new TimeoutTask(service.getAuthMe(), name, player), time);
BukkitTask id = bukkitService.runTaskLater(new TimeoutTask(authMe, name, player), time);
limbo.setTimeoutTask(id);
}
BukkitTask messageTask = bukkitService.runTask(new MessageTask(

View File

@ -79,7 +79,7 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess {
int interval = service.getProperty(RegistrationSettings.MESSAGE_INTERVAL);
BukkitTask task;
if (delay != 0) {
task = bukkitService.runTaskLater(new TimeoutTask(service.getAuthMe(), name, player), delay);
task = bukkitService.runTaskLater(new TimeoutTask(plugin, name, player), delay);
cache.getLimboPlayer(name).setTimeoutTask(task);
}
task = bukkitService.runTask(new MessageTask(bukkitService, plugin.getMessages(),

View File

@ -1,16 +1,17 @@
package fr.xephi.authme.process;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.hooks.PluginHooks;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PlayerPermission;
import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.SpawnLoader;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.ValidationService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.plugin.PluginManager;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
@ -31,22 +32,21 @@ public class ProcessServiceTest {
@InjectMocks
private ProcessService processService;
@Mock
private ValidationService validationService;
@Mock
private NewSetting settings;
@Mock
private Messages messages;
@Mock
private AuthMe authMe;
private PluginManager pluginManager;
@Mock
private DataSource dataSource;
@Mock
private SpawnLoader spawnLoader;
@Mock
private PluginHooks pluginHooks;
@Mock
private BukkitService bukkitService;
private PermissionsManager permissionsManager;
@Test
public void shouldGetProperty() {
@ -127,15 +127,6 @@ public class ProcessServiceTest {
verify(messages).retrieveSingle(key);
}
@Test
public void shouldReturnAuthMeInstance() {
// given / when
AuthMe result = processService.getAuthMe();
// then
assertThat(result, equalTo(authMe));
}
@Test
public void shouldValidatePassword() {
// given
@ -180,4 +171,31 @@ public class ProcessServiceTest {
assertThat(result, equalTo(true));
verify(validationService).isEmailFreeForRegistration(email, sender);
}
@Test
public void shouldEmitEvent() {
// given
Event event = mock(Event.class);
// when
processService.callEvent(event);
// then
verify(pluginManager).callEvent(event);
}
@Test
public void shouldCheckPermission() {
// given
Player player = mock(Player.class);
PermissionNode permission = PlayerPermission.CHANGE_PASSWORD;
given(permissionsManager.hasPermission(player, permission)).willReturn(true);
// when
boolean result = processService.hasPermission(player, permission);
// then
verify(permissionsManager).hasPermission(player, permission);
assertThat(result, equalTo(true));
}
}