mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-24 01:27:35 +01:00
#739 Create unit tests for PermissionsManager#hasPermission
This commit is contained in:
parent
7fd1ac0856
commit
408e8dd0dd
@ -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));
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user