From f983aa5f4486d794b527cb6955366e386f1ecf17 Mon Sep 17 00:00:00 2001 From: tastybento Date: Fri, 1 Feb 2019 20:51:57 -0800 Subject: [PATCH] Fixed permission bug with custom schems and reset https://github.com/BentoBoxWorld/BentoBox/issues/509 --- .../commands/island/IslandResetCommand.java | 4 +- .../island/IslandResetCommandTest.java | 82 +++++++++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/IslandResetCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/IslandResetCommand.java index 663a6a9ac..961dbb079 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/IslandResetCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/IslandResetCommand.java @@ -6,8 +6,8 @@ import java.util.Set; import org.bukkit.GameMode; import org.bukkit.entity.Player; - import org.eclipse.jdt.annotation.Nullable; + import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.commands.ConfirmableCommand; import world.bentobox.bentobox.api.events.island.IslandEvent.Reason; @@ -74,7 +74,7 @@ public class IslandResetCommand extends ConfirmableCommand { // Permission check if the name is not the default one String permission = getPermissionPrefix() + "island.create." + name; - if (!name.equals(SchemsManager.DEFAULT_SCHEM_NAME) && (!user.isOp() || !user.hasPermission(permission))) { + if (!name.equals(SchemsManager.DEFAULT_SCHEM_NAME) && !user.isOp() && !user.hasPermission(permission)) { user.sendMessage("general.errors.no-permission", TextVariables.PERMISSION, permission); return false; } diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandResetCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandResetCommandTest.java index f626c1640..35cafff28 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandResetCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandResetCommandTest.java @@ -7,7 +7,9 @@ import static org.mockito.Mockito.when; import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; +import java.util.Map; import java.util.UUID; import org.bukkit.Bukkit; @@ -33,7 +35,9 @@ import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.PlayersManager; +import world.bentobox.bentobox.managers.SchemsManager; import world.bentobox.bentobox.managers.island.NewIsland; +import world.bentobox.bentobox.schems.Clipboard; /** * @author tastybento @@ -115,6 +119,12 @@ public class IslandResetCommandTest { when(iwm.getResetLimit(Mockito.any())).thenReturn(3); + // Schems manager - custom schem + SchemsManager sm = mock(SchemsManager.class); + Map map = new HashMap<>(); + map.put("custom", null); + when(sm.get(Mockito.any())).thenReturn(map); + when(plugin.getSchemsManager()).thenReturn(sm); } /** @@ -318,4 +328,76 @@ public class IslandResetCommandTest { } + + @Test + public void testNoConfirmationRequiredCustomSchemNoPermission() throws IOException { + IslandResetCommand irc = new IslandResetCommand(ic); + // Now has island, but is not the owner + when(im.hasIsland(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); + // Now is owner, but still has team + when(im.isOwner(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); + // Now has no team + when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(false); + // Give the user some resets + when(pm.getResets(Mockito.eq(world), Mockito.eq(uuid))).thenReturn(1); + // Set so no confirmation required + when(s.isResetConfirmation()).thenReturn(false); + + // Old island mock + Island oldIsland = mock(Island.class); + when(im.getIsland(Mockito.any(), Mockito.eq(uuid))).thenReturn(oldIsland); + + // Mock up NewIsland builder + NewIsland.Builder builder = mock(NewIsland.Builder.class); + when(builder.player(Mockito.any())).thenReturn(builder); + when(builder.oldIsland(Mockito.any())).thenReturn(builder); + when(builder.reason(Mockito.any())).thenReturn(builder); + when(builder.name(Mockito.any())).thenReturn(builder); + when(builder.build()).thenReturn(mock(Island.class)); + PowerMockito.mockStatic(NewIsland.class); + when(NewIsland.builder()).thenReturn(builder); + + + // Reset command, no confirmation required + assertFalse(irc.execute(user, irc.getLabel(), Collections.singletonList("custom"))); + Mockito.verify(user).sendMessage("general.errors.no-permission","[permission]","nullisland.create.custom"); + } + + @Test + public void testNoConfirmationRequiredCustomSchemHasPermission() throws IOException { + IslandResetCommand irc = new IslandResetCommand(ic); + // Now has island, but is not the owner + when(im.hasIsland(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); + // Now is owner, but still has team + when(im.isOwner(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); + // Now has no team + when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(false); + // Give the user some resets + when(pm.getResets(Mockito.eq(world), Mockito.eq(uuid))).thenReturn(1); + // Set so no confirmation required + when(s.isResetConfirmation()).thenReturn(false); + + // Old island mock + Island oldIsland = mock(Island.class); + when(im.getIsland(Mockito.any(), Mockito.eq(uuid))).thenReturn(oldIsland); + + // Mock up NewIsland builder + NewIsland.Builder builder = mock(NewIsland.Builder.class); + when(builder.player(Mockito.any())).thenReturn(builder); + when(builder.oldIsland(Mockito.any())).thenReturn(builder); + when(builder.reason(Mockito.any())).thenReturn(builder); + when(builder.name(Mockito.any())).thenReturn(builder); + when(builder.build()).thenReturn(mock(Island.class)); + PowerMockito.mockStatic(NewIsland.class); + when(NewIsland.builder()).thenReturn(builder); + + // Permission + when(user.hasPermission(Mockito.anyString())).thenReturn(true); + // Reset command, no confirmation required + assertTrue(irc.execute(user, irc.getLabel(), Collections.singletonList("custom"))); + // Verify that build new island was called and the number of resets left shown + Mockito.verify(builder).build(); + // This should not be shown + Mockito.verify(user, Mockito.never()).sendMessage("commands.island.reset.resets-left", "[number]", "1"); + } }