From 29bb7e667e22969e2dcb3db9b396af18ba2d6fe4 Mon Sep 17 00:00:00 2001 From: tueem Date: Mon, 2 Dec 2024 21:21:00 +0100 Subject: [PATCH] add PermissionCHeck test --- .../tomatentum/marinara/test/ButtonTest.java | 2 +- .../marinara/test/InteractionCheckTest.java | 20 ++++++++++++++++++- .../tomatentum/marinara/test/TestButton.java | 11 ++++++++++ .../test/mocks/ButtonInteractionMock.java | 8 +++++++- .../marinara/test/mocks/ServerMock.java | 10 ++++++++++ 5 files changed, 48 insertions(+), 3 deletions(-) diff --git a/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/ButtonTest.java b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/ButtonTest.java index b8ffa0f..db08352 100644 --- a/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/ButtonTest.java +++ b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/ButtonTest.java @@ -20,7 +20,7 @@ public class ButtonTest { LibraryWrapper wrapper = new JavacordWrapper(new DiscordApiMock()); //null okay as we don't use the discord API in this test. Marinara marinara = Marinara.load(wrapper); marinara.getRegistry().addInteractions(new TestButton()); - wrapper.handleInteraction(new ButtonInteractionMock()); + wrapper.handleInteraction(new ButtonInteractionMock("test")); assertTrue(TestButton.didRun); } diff --git a/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/InteractionCheckTest.java b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/InteractionCheckTest.java index 95773f3..aed9f5a 100644 --- a/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/InteractionCheckTest.java +++ b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/InteractionCheckTest.java @@ -1,7 +1,9 @@ package net.tomatentum.marinara.test; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import org.javacord.api.entity.permission.PermissionType; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.TestInstance.Lifecycle; @@ -9,8 +11,10 @@ import org.junit.jupiter.api.TestInstance.Lifecycle; import net.tomatentum.marinara.Marinara; import net.tomatentum.marinara.test.mocks.ButtonInteractionMock; import net.tomatentum.marinara.test.mocks.DiscordApiMock; +import net.tomatentum.marinara.test.mocks.ServerMock; import net.tomatentum.marinara.wrapper.LibraryWrapper; import net.tomatentum.marinara.wrapper.javacord.JavacordWrapper; +import net.tomatentum.marinara.wrapper.javacord.checks.PermissionCheck; @TestInstance(Lifecycle.PER_CLASS) public class InteractionCheckTest { @@ -21,9 +25,23 @@ public class InteractionCheckTest { Marinara marinara = Marinara.load(wrapper); marinara.getCheckRegistry().addCheck(new TestInteractionCheck()); marinara.getRegistry().addInteractions(new TestButton()); - wrapper.handleInteraction(new ButtonInteractionMock()); + wrapper.handleInteraction(new ButtonInteractionMock("test")); assertTrue(TestInteractionCheck.preExecuted); assertTrue(TestInteractionCheck.postExecuted); } + @Test + public void testPermissionCheck() { + LibraryWrapper wrapper = new JavacordWrapper(new DiscordApiMock()); + Marinara marinara = Marinara.load(wrapper); + marinara.getCheckRegistry().addCheck(new PermissionCheck()); + marinara.getRegistry().addInteractions(new TestButton()); + wrapper.handleInteraction(new ButtonInteractionMock("permissionCheck")); + assertTrue(TestButton.didPermRun); + TestButton.didPermRun = false; + ServerMock.TESTPERMISSION = PermissionType.ATTACH_FILE; + wrapper.handleInteraction(new ButtonInteractionMock("permissionCheck")); + assertFalse(TestButton.didPermRun); + } + } diff --git a/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/TestButton.java b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/TestButton.java index eb9c9c8..23a7dbf 100644 --- a/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/TestButton.java +++ b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/TestButton.java @@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import org.javacord.api.entity.channel.TextChannel; import org.javacord.api.entity.message.Message; +import org.javacord.api.entity.permission.PermissionType; import org.javacord.api.entity.server.Server; import org.javacord.api.entity.user.User; import org.javacord.api.interaction.ButtonInteraction; @@ -11,6 +12,7 @@ import org.javacord.api.interaction.ButtonInteraction; import net.tomatentum.marinara.interaction.InteractionHandler; import net.tomatentum.marinara.interaction.annotation.Button; import net.tomatentum.marinara.test.TestInteractionCheck.TestCheck; +import net.tomatentum.marinara.wrapper.javacord.checks.PermissionCheck.HasPermission; public class TestButton implements InteractionHandler { @@ -27,5 +29,14 @@ public class TestButton implements InteractionHandler { didRun = true; System.out.println("Success!"); } + + public static boolean didPermRun = false; + + @Button("permissionCheck") + @HasPermission({PermissionType.ADMINISTRATOR}) + public void exec(ButtonInteraction interaction) { + didPermRun = true; + System.out.println("It worked!"); + } } diff --git a/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/mocks/ButtonInteractionMock.java b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/mocks/ButtonInteractionMock.java index c9ee36b..3e3e8fd 100644 --- a/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/mocks/ButtonInteractionMock.java +++ b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/mocks/ButtonInteractionMock.java @@ -23,6 +23,12 @@ import org.javacord.api.interaction.callback.InteractionOriginalResponseUpdater; public class ButtonInteractionMock implements ButtonInteraction { + private String customId; + + public ButtonInteractionMock(String customId) { + this.customId = customId; + } + @Override public Message getMessage() { return new MessageMock(); @@ -30,7 +36,7 @@ public class ButtonInteractionMock implements ButtonInteraction { @Override public String getCustomId() { - return "test"; + return this.customId; } @Override diff --git a/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/mocks/ServerMock.java b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/mocks/ServerMock.java index 5a0daaa..275c8ab 100644 --- a/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/mocks/ServerMock.java +++ b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/mocks/ServerMock.java @@ -1,5 +1,7 @@ package net.tomatentum.marinara.test.mocks; +import static org.junit.jupiter.api.Assertions.assertNotNull; + import java.time.Instant; import java.util.Collection; import java.util.EnumSet; @@ -27,6 +29,7 @@ import org.javacord.api.entity.channel.ServerTextChannel; import org.javacord.api.entity.channel.ServerThreadChannel; import org.javacord.api.entity.channel.ServerVoiceChannel; import org.javacord.api.entity.emoji.KnownCustomEmoji; +import org.javacord.api.entity.permission.PermissionType; import org.javacord.api.entity.permission.Role; import org.javacord.api.entity.server.ActiveThreads; import org.javacord.api.entity.server.Ban; @@ -2259,5 +2262,12 @@ public class ServerMock implements Server { // TODO Auto-generated method stub throw new UnsupportedOperationException("Unimplemented method 'getSystemChannelFlags'"); } + public static PermissionType TESTPERMISSION = PermissionType.ADMINISTRATOR; + @Override + public boolean hasPermissions(User user, PermissionType... type) { + assertNotNull(user); + assertNotNull(type); + return TESTPERMISSION.equals(type[0]); + } }