From 408e8dd0ddd56792f4a2003ccc62f1a6c4951760 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Thu, 2 Jun 2016 00:03:02 +0200 Subject: [PATCH] #739 Create unit tests for PermissionsManager#hasPermission --- .../permission/PermissionsManagerTest.java | 165 ++++++++++++++++++ .../authme/permission/TestPermissions.java | 33 ++++ 2 files changed, 198 insertions(+) create mode 100644 src/test/java/fr/xephi/authme/permission/PermissionsManagerTest.java create mode 100644 src/test/java/fr/xephi/authme/permission/TestPermissions.java diff --git a/src/test/java/fr/xephi/authme/permission/PermissionsManagerTest.java b/src/test/java/fr/xephi/authme/permission/PermissionsManagerTest.java new file mode 100644 index 000000000..225fc899e --- /dev/null +++ b/src/test/java/fr/xephi/authme/permission/PermissionsManagerTest.java @@ -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)); + } +} diff --git a/src/test/java/fr/xephi/authme/permission/TestPermissions.java b/src/test/java/fr/xephi/authme/permission/TestPermissions.java new file mode 100644 index 000000000..89460771d --- /dev/null +++ b/src/test/java/fr/xephi/authme/permission/TestPermissions.java @@ -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; + } + +}