-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added rudimentary SignChangeEvent permission check
- MOD: Cleaned up imports - NEW: SignEditCommits now run validation by pretending to blank the sign to see if the player has permission to edit the sign. - MOD: Reorganized the tests - NEW: Added SignEditCommitTest for SignEditCommit testing
- Loading branch information
Showing
7 changed files
with
131 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import org.bukkit.event.block.SignChangeEvent; | ||
import org.deltik.mc.signedit.committers.SignEditCommit; | ||
import org.deltik.mc.signedit.committers.UiSignEditCommit; | ||
import org.junit.Test; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
|
||
import static org.mockito.ArgumentMatchers.matches; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
import static org.powermock.api.mockito.PowerMockito.*; | ||
|
||
@PrepareForTest({SignEditCommit.class}) | ||
public class SignEditCommitTest extends SignEditTest { | ||
@Test | ||
public void validatedCommitForbiddenSignEdit() throws Exception { | ||
SignEditCommit spySignEditCommit = spy(new UiSignEditCommit(null, null)); | ||
SignChangeEvent event = mock(SignChangeEvent.class); | ||
whenNew(SignChangeEvent.class).withAnyArguments().thenReturn(event); | ||
doReturn(true).when(event).isCancelled(); | ||
doNothing().when(spySignEditCommit).commit(player, sign); | ||
|
||
spySignEditCommit.validatedCommit(player, sign); | ||
|
||
verify(spySignEditCommit, never()).commit(player, sign); | ||
verify(player).sendMessage(matches("(?i)^.*edit.*forbidden.*$")); | ||
} | ||
|
||
@Test | ||
public void validatedCommitPermittedSignEdit() throws Exception { | ||
SignEditCommit spySignEditCommit = spy(new UiSignEditCommit(null, null)); | ||
SignChangeEvent event = mock(SignChangeEvent.class); | ||
whenNew(SignChangeEvent.class).withAnyArguments().thenReturn(event); | ||
doReturn(false).when(event).isCancelled(); | ||
doNothing().when(spySignEditCommit).commit(player, sign); | ||
|
||
spySignEditCommit.validatedCommit(player, sign); | ||
|
||
verify(spySignEditCommit).commit(player, sign); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.Server; | ||
import org.bukkit.block.Block; | ||
import org.bukkit.block.Sign; | ||
import org.bukkit.command.Command; | ||
import org.bukkit.configuration.file.YamlConfiguration; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.player.PlayerInteractEvent; | ||
import org.bukkit.plugin.PluginManager; | ||
import org.deltik.mc.signedit.Configuration; | ||
import org.deltik.mc.signedit.commands.SignCommand; | ||
import org.deltik.mc.signedit.listeners.Interact; | ||
import org.deltik.mc.signedit.subcommands.UiSignSubcommand; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.runner.RunWith; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
|
||
import java.io.File; | ||
|
||
import static org.mockito.Mockito.validateMockitoUsage; | ||
import static org.powermock.api.mockito.PowerMockito.*; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest({Bukkit.class, PlayerInteractEvent.class, SignCommand.class}) | ||
public abstract class SignEditTest { | ||
SignCommand signCommand; | ||
Player player; | ||
Command command; | ||
Sign sign; | ||
Block block; | ||
Configuration spyConfig; | ||
Interact listener; | ||
String cString = "signedit"; | ||
UiSignSubcommand uiSignSubcommand; | ||
Server server; | ||
PluginManager pluginManager; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
Configuration config = new Configuration(File.createTempFile("SignEdit-", "-config.yml")); | ||
spyConfig = spy(config); | ||
listener = new Interact(); | ||
doReturn(false).when(spyConfig).writeFullConfig(new YamlConfiguration()); | ||
|
||
uiSignSubcommand = mock(UiSignSubcommand.class); | ||
whenNew(UiSignSubcommand.class).withAnyArguments().thenReturn(uiSignSubcommand); | ||
|
||
signCommand = new SignCommand(spyConfig, listener); | ||
|
||
player = mock(Player.class); | ||
command = mock(Command.class); | ||
sign = mock(Sign.class); | ||
block = mock(Block.class); | ||
when(player.hasPermission("SignEdit.use")).thenReturn(true); | ||
when(block.getState()).thenReturn(sign); | ||
|
||
server = mock(Server.class); | ||
pluginManager = mock(PluginManager.class); | ||
mockStatic(Bukkit.class); | ||
when(Bukkit.getServer()).thenReturn(server); | ||
when(server.getPluginManager()).thenReturn(pluginManager); | ||
} | ||
|
||
@After | ||
public void validate() { | ||
validateMockitoUsage(); | ||
} | ||
} |