#739 Create unit tests for PermissionsManager#hasPermission

This commit is contained in:
ljacqu 2016-06-02 00:03:02 +02:00
parent 7fd1ac0856
commit 408e8dd0dd
2 changed files with 198 additions and 0 deletions

View File

@ -0,0 +1,165 @@
package fr.xephi.authme.permission;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Test for {@link PermissionsManager}.
*/
// TODO #739: Unignore tests after they pass
@RunWith(MockitoJUnitRunner.class)
public class PermissionsManagerTest {
@InjectMocks
private PermissionsManager permissionsManager;
@Mock
private Server server;
@Mock
private PluginManager pluginManager;
@Test
public void shouldUseDefaultPermissionForCommandSender() {
// given
PermissionNode node = TestPermissions.LOGIN;
CommandSender sender = mock(CommandSender.class);
// when
boolean result = permissionsManager.hasPermission(sender, node);
// then
assertThat(result, equalTo(false));
}
@Test
public void shouldGrantToOpCommandSender() {
// given
PermissionNode node = TestPermissions.DELETE_USER;
CommandSender sender = mock(CommandSender.class);
given(sender.isOp()).willReturn(true);
// when
boolean result = permissionsManager.hasPermission(sender, node);
// then
assertThat(result, equalTo(true));
}
@Test
@Ignore
// TODO ljacqu 20160601: This test should pass - tested permission node has DefaultPermission.NOT_ALLOWED
public void shouldDenyPermissionEvenForOpCommandSender() {
// given
PermissionNode node = TestPermissions.WORLD_DOMINATION;
CommandSender sender = mock(CommandSender.class);
given(sender.isOp()).willReturn(true);
// when
boolean result = permissionsManager.hasPermission(sender, node);
// then
assertThat(result, equalTo(false));
}
@Test
@Ignore
// TODO ljacqu 20160601: This test MUST pass! -> tested node has DefaultPermission.ALLOW
public void shouldAllowForNonOpPlayer() {
// given
PermissionNode node = TestPermissions.LOGIN;
Player player = mock(Player.class);
// when
boolean result = permissionsManager.hasPermission(player, node);
// then
assertThat(result, equalTo(true));
}
@Test
public void shouldDenyForNonOpPlayer() {
// given
PermissionNode node = TestPermissions.DELETE_USER;
Player player = mock(Player.class);
// when
boolean result = permissionsManager.hasPermission(player, node);
// then
assertThat(result, equalTo(false));
}
@Test
public void shouldAllowForOpPlayer() {
// given
PermissionNode node = TestPermissions.DELETE_USER;
Player player = mock(Player.class);
given(player.isOp()).willReturn(true);
// when
boolean result = permissionsManager.hasPermission(player, node);
// then
assertThat(result, equalTo(true));
}
@Test
@Ignore
// TODO ljacqu 20160601: This should pass -> tested node has DefaultPermission.NOT_ALLOWED so result should be false
public void shouldDenyEvenForOpPlayer() {
// given
PermissionNode node = TestPermissions.WORLD_DOMINATION;
Player player = mock(Player.class);
given(player.isOp()).willReturn(true);
// when
boolean result = permissionsManager.hasPermission(player, node);
// then
assertThat(result, equalTo(false));
}
@Test
@Ignore
// TODO ljacqu 20160601: This must pass. null permission => true
public void shouldHandleNullPermissionForCommandSender() {
// given
PermissionNode node = null;
CommandSender sender = mock(CommandSender.class);
// when
boolean result = permissionsManager.hasPermission(sender, node);
// then
assertThat(result, equalTo(true));
}
@Test
@Ignore
// TODO ljacqu 20160601: This must pass. null permission => true
public void shouldHandleNullPermissionForPlayer() {
// given
PermissionNode node = null;
Player player = mock(Player.class);
// when
boolean result = permissionsManager.hasPermission(player, node);
// then
assertThat(result, equalTo(true));
}
}

View File

@ -0,0 +1,33 @@
package fr.xephi.authme.permission;
/**
* Sample permission nodes for testing.
*/
public enum TestPermissions implements PermissionNode {
LOGIN("authme.login", DefaultPermission.ALLOWED),
DELETE_USER("authme.admin.delete", DefaultPermission.OP_ONLY),
WORLD_DOMINATION("global.own", DefaultPermission.NOT_ALLOWED);
private final String node;
private final DefaultPermission defaultPermission;
TestPermissions(String node, DefaultPermission defaultPermission) {
this.node = node;
this.defaultPermission = defaultPermission;
}
@Override
public String getNode() {
return node;
}
@Override
public DefaultPermission getDefaultPermission() {
return defaultPermission;
}
}