From 39f6d7623a91bd66634ac3db3c96d93aab658022 Mon Sep 17 00:00:00 2001 From: Nxllpointer Date: Wed, 26 Oct 2022 19:56:00 +0200 Subject: [PATCH 01/13] Added test for top-helper-anticheat --- .../tophelper/TopHelpersMessageListener.java | 2 +- .../tophelper/TopHelperAntiCheatTest.java | 71 +++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperAntiCheatTest.java diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersMessageListener.java b/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersMessageListener.java index f7b7611fc4..2374f20548 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersMessageListener.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersMessageListener.java @@ -24,7 +24,7 @@ public final class TopHelpersMessageListener extends MessageReceiverAdapter { * @see Unicode * Categories */ - private static final Pattern UNCOUNTED_CHARS = Pattern.compile("\\p{C}"); + public static final Pattern UNCOUNTED_CHARS = Pattern.compile("\\p{C}"); private final Database database; diff --git a/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperAntiCheatTest.java b/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperAntiCheatTest.java new file mode 100644 index 0000000000..e8038b24c6 --- /dev/null +++ b/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperAntiCheatTest.java @@ -0,0 +1,71 @@ +package org.togetherjava.tjbot.commands.tophelper; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.List; +import java.util.function.Predicate; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +final class TopHelperAntiCheatTest { + + Predicate uncountedCharsMatchPredicate = + TopHelpersMessageListener.UNCOUNTED_CHARS.asMatchPredicate(); + + + @ParameterizedTest + @MethodSource("provideInvisibleNamedCharacters") + @DisplayName("Does exclude invisible characters") + void excludesInvisibleCharacters(char character, String name) { + String characterString = String.valueOf(character); + + String errorMessage = "Character [%s] was unrightfully matched".formatted(name); + + assertTrue(uncountedCharsMatchPredicate.test(characterString), errorMessage); + } + + + @ParameterizedTest + @MethodSource("provideVisibleCharacters") + @DisplayName("Does count visible characters") + void countsVisibleCharacters(char character) { + String characterString = String.valueOf(character); + + String errorMessage = "Character [%c] was unrightfully matched".formatted(character); + + assertFalse(uncountedCharsMatchPredicate.test(characterString), errorMessage); + } + + + private static Stream provideInvisibleNamedCharacters() { + return Stream.of( // Invisible characters + Arguments.of('\u061C', "Arabic Letter Mark"), + Arguments.of('\u0600', "Arabic Number Sign"), + Arguments.of('\u180E', "Mongolian Vowel Separator"), + Arguments.of('\u200B', "Zero Width Space"), + Arguments.of('\u200C', "Zero Width Non-Joiner"), + Arguments.of('\u200D', "Zero Width Joiner"), + Arguments.of('\u200E', "Left-to-Right Mark"), + Arguments.of('\u200F', "Right-to-Left Mark")); + } + + + private static List provideVisibleCharacters() { + return List.of( // Visible characters + 'a', 'A', 'b', 'B', 'c', 'C', 'x', 'X', 'y', 'Y', 'z', 'Z', // Latin alphabet + '1', '2', '9', '0', // Numbers + '!', '"', '§', '%', '&', '/', '\\', '(', ')', '{', '}', '[', ']', '<', '>', // Other + '.', ':', ',', ';', '?', '-', '_', '#', '=', '\'', '+', '*', '´', '`', '|', // Other + 'α', 'Α', 'β', 'Β', 'γ', 'Γ', 'χ', 'Χ', 'ψ', 'Ψ', 'ω', 'Ω', // Greek alphabet + 'ä', 'ö', 'ü', 'ß', // German + 'á', 'è', 'î', // French + '天', '四', '永' // Chinese + ); + } + +} From aff272b249d51629c362cd64b9215f3ab6ac19d8 Mon Sep 17 00:00:00 2001 From: Nxllpointer Date: Thu, 27 Oct 2022 17:06:16 +0200 Subject: [PATCH 02/13] Refactored TopHelpersMessageListener to be fully testable and added tests for all checks --- .../tophelper/TopHelpersMessageListener.java | 47 +++-- .../tophelper/TopHelperAntiCheatTest.java | 71 ------- .../TopHelperMessageListenerTest.java | 194 ++++++++++++++++++ 3 files changed, 221 insertions(+), 91 deletions(-) delete mode 100644 application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperAntiCheatTest.java create mode 100644 application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersMessageListener.java b/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersMessageListener.java index 2374f20548..0ea0fd1ffd 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersMessageListener.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersMessageListener.java @@ -2,6 +2,7 @@ import net.dv8tion.jda.api.entities.channel.ChannelType; import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; +import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import org.togetherjava.tjbot.commands.MessageReceiverAdapter; @@ -20,11 +21,11 @@ public final class TopHelpersMessageListener extends MessageReceiverAdapter { /** * Matches invisible control characters and unused code points - * + * * @see Unicode * Categories */ - public static final Pattern UNCOUNTED_CHARS = Pattern.compile("\\p{C}"); + private static final Pattern INVALID_CHARACTERS = Pattern.compile("\\p{C}"); private final Database database; @@ -50,31 +51,15 @@ public TopHelpersMessageListener(Database database, Config config) { @Override public void onMessageReceived(MessageReceivedEvent event) { - if (event.getAuthor().isBot() || event.isWebhookMessage()) { - return; - } - - if (!isHelpThread(event)) { + if (shouldIgnoreMessage(event)) { return; } addMessageRecord(event); } - private boolean isHelpThread(MessageReceivedEvent event) { - if (event.getChannelType() != ChannelType.GUILD_PUBLIC_THREAD) { - return false; - } - - ThreadChannel thread = event.getChannel().asThreadChannel(); - String rootChannelName = thread.getParentChannel().getName(); - return isStagingChannelName.test(rootChannelName) - || isOverviewChannelName.test(rootChannelName); - } - private void addMessageRecord(MessageReceivedEvent event) { - String messageContent = event.getMessage().getContentRaw(); - long messageLength = UNCOUNTED_CHARS.matcher(messageContent).replaceAll("").length(); + long messageLength = countValidCharacters(event.getMessage().getContentRaw()); database.write(context -> context.newRecord(HELP_CHANNEL_MESSAGES) .setMessageId(event.getMessage().getIdLong()) @@ -85,4 +70,26 @@ private void addMessageRecord(MessageReceivedEvent event) { .setMessageLength(messageLength) .insert()); } + + boolean shouldIgnoreMessage(MessageReceivedEvent event) { + MessageChannelUnion channel = event.getChannel(); + + return event.getAuthor().isBot() || event.isWebhookMessage() || !isHelpThread(channel); + } + + boolean isHelpThread(MessageChannelUnion channel) { + if (channel.getType() != ChannelType.GUILD_PUBLIC_THREAD) { + return false; + } + + ThreadChannel thread = channel.asThreadChannel(); + String rootChannelName = thread.getParentChannel().getName(); + return isStagingChannelName.test(rootChannelName) + || isOverviewChannelName.test(rootChannelName); + } + + static long countValidCharacters(String messageContent) { + return INVALID_CHARACTERS.matcher(messageContent).replaceAll("").length(); + } + } diff --git a/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperAntiCheatTest.java b/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperAntiCheatTest.java deleted file mode 100644 index e8038b24c6..0000000000 --- a/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperAntiCheatTest.java +++ /dev/null @@ -1,71 +0,0 @@ -package org.togetherjava.tjbot.commands.tophelper; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; - -import java.util.List; -import java.util.function.Predicate; -import java.util.stream.Stream; - -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - -final class TopHelperAntiCheatTest { - - Predicate uncountedCharsMatchPredicate = - TopHelpersMessageListener.UNCOUNTED_CHARS.asMatchPredicate(); - - - @ParameterizedTest - @MethodSource("provideInvisibleNamedCharacters") - @DisplayName("Does exclude invisible characters") - void excludesInvisibleCharacters(char character, String name) { - String characterString = String.valueOf(character); - - String errorMessage = "Character [%s] was unrightfully matched".formatted(name); - - assertTrue(uncountedCharsMatchPredicate.test(characterString), errorMessage); - } - - - @ParameterizedTest - @MethodSource("provideVisibleCharacters") - @DisplayName("Does count visible characters") - void countsVisibleCharacters(char character) { - String characterString = String.valueOf(character); - - String errorMessage = "Character [%c] was unrightfully matched".formatted(character); - - assertFalse(uncountedCharsMatchPredicate.test(characterString), errorMessage); - } - - - private static Stream provideInvisibleNamedCharacters() { - return Stream.of( // Invisible characters - Arguments.of('\u061C', "Arabic Letter Mark"), - Arguments.of('\u0600', "Arabic Number Sign"), - Arguments.of('\u180E', "Mongolian Vowel Separator"), - Arguments.of('\u200B', "Zero Width Space"), - Arguments.of('\u200C', "Zero Width Non-Joiner"), - Arguments.of('\u200D', "Zero Width Joiner"), - Arguments.of('\u200E', "Left-to-Right Mark"), - Arguments.of('\u200F', "Right-to-Left Mark")); - } - - - private static List provideVisibleCharacters() { - return List.of( // Visible characters - 'a', 'A', 'b', 'B', 'c', 'C', 'x', 'X', 'y', 'Y', 'z', 'Z', // Latin alphabet - '1', '2', '9', '0', // Numbers - '!', '"', '§', '%', '&', '/', '\\', '(', ')', '{', '}', '[', ']', '<', '>', // Other - '.', ':', ',', ';', '?', '-', '_', '#', '=', '\'', '+', '*', '´', '`', '|', // Other - 'α', 'Α', 'β', 'Β', 'γ', 'Γ', 'χ', 'Χ', 'ψ', 'Ψ', 'ω', 'Ω', // Greek alphabet - 'ä', 'ö', 'ü', 'ß', // German - 'á', 'è', 'î', // French - '天', '四', '永' // Chinese - ); - } - -} diff --git a/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java b/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java new file mode 100644 index 0000000000..701fea1cc9 --- /dev/null +++ b/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java @@ -0,0 +1,194 @@ +package org.togetherjava.tjbot.commands.tophelper; + +import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.entities.channel.ChannelType; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.internal.JDAImpl; +import net.dv8tion.jda.internal.entities.UserImpl; +import net.dv8tion.jda.internal.entities.channel.concrete.ThreadChannelImpl; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import org.togetherjava.tjbot.config.Config; +import org.togetherjava.tjbot.config.HelpSystemConfig; +import org.togetherjava.tjbot.db.Database; +import org.togetherjava.tjbot.jda.JdaTester; + +import java.util.List; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; +import static org.togetherjava.tjbot.db.generated.tables.HelpChannelMessages.HELP_CHANNEL_MESSAGES; + +final class TopHelperMessageListenerTest { + + private static final String STAGING_CHANNEL_PATTERN = "ask_here"; + private static final String OVERVIEW_CHANNEL_PATTERN = "active_questions"; + + private static JdaTester jdaTester; + private static TopHelpersMessageListener topHelpersListenerSpy; + + @BeforeAll + static void setUp() { + Database database = Database.createMemoryDatabase(HELP_CHANNEL_MESSAGES); + Config config = mock(Config.class); + HelpSystemConfig helpSystemConfig = mock(HelpSystemConfig.class); + + when(helpSystemConfig.getStagingChannelPattern()).thenReturn(STAGING_CHANNEL_PATTERN); + when(helpSystemConfig.getOverviewChannelPattern()).thenReturn(OVERVIEW_CHANNEL_PATTERN); + + when(config.getHelpSystem()).thenReturn(helpSystemConfig); + + jdaTester = new JdaTester(); + topHelpersListenerSpy = spy(new TopHelpersMessageListener(database, config)); + } + + @Test + @DisplayName("Recognizes valid messages") + void recognizesValidMessages() { + // GIVEN a message by a human in a help channel + MessageReceivedEvent event = + createFakeMessageRecievedEvent(false, false, true, OVERVIEW_CHANNEL_PATTERN); + + // WHEN checking if the message should be ignored + boolean shouldBeIgnored = topHelpersListenerSpy.shouldIgnoreMessage(event); + + // THEN the message is not ignored + assertFalse(shouldBeIgnored); + } + + @Test + @DisplayName("Ignores bots") + void ignoresBots() { + // GIVEN a message from a bot + MessageReceivedEvent event = + createFakeMessageRecievedEvent(true, false, true, OVERVIEW_CHANNEL_PATTERN); + + // WHEN checking if the message should be ignored + boolean shouldBeIgnored = topHelpersListenerSpy.shouldIgnoreMessage(event); + + // THEN the message is ignored + assertTrue(shouldBeIgnored); + } + + @Test + @DisplayName("Ignores webhooks") + void ignoresWebhooks() { + // GIVEN a message from a webhook + MessageReceivedEvent event = + createFakeMessageRecievedEvent(false, true, true, OVERVIEW_CHANNEL_PATTERN); + + // WHEN checking if the message should be ignored + boolean shouldBeIgnored = topHelpersListenerSpy.shouldIgnoreMessage(event); + + // THEN the message is ignored + assertTrue(shouldBeIgnored); + } + + @Test + @DisplayName("Ignores wrong channels") + void ignoresWrongChannels() { + // GIVEN a message outside a help thread + MessageReceivedEvent eventNotAThread = + createFakeMessageRecievedEvent(false, false, false, OVERVIEW_CHANNEL_PATTERN); + MessageReceivedEvent eventWrongParentName = + createFakeMessageRecievedEvent(false, false, true, "memes"); + + // WHEN checking if the message should be ignored + boolean ignoresNonThreadChannels = + topHelpersListenerSpy.shouldIgnoreMessage(eventNotAThread); + boolean ignoresWrongParentNames = + topHelpersListenerSpy.shouldIgnoreMessage(eventWrongParentName); + + // THEN the message is ignored + assertTrue(ignoresNonThreadChannels, "Failed to ignore non-thread channels"); + assertTrue(ignoresWrongParentNames, "Failed to ignore wrong parent channel names"); + } + + + MessageReceivedEvent createFakeMessageRecievedEvent(boolean isBot, boolean isWebhook, + boolean isThread, String parentChannelName) { + Message messageMock = mock(Message.class); + ThreadChannelImpl threadMock = mock(ThreadChannelImpl.class, RETURNS_DEEP_STUBS); + + User user = new UserImpl(123456789, (JDAImpl) jdaTester.getJdaMock()).setName("John Doe") + .setDiscriminator("1234") + .setBot(isBot); + + when(threadMock.getType()) + .thenReturn(isThread ? ChannelType.GUILD_PUBLIC_THREAD : ChannelType.TEXT); + when(threadMock.getParentChannel().getName()).thenReturn(parentChannelName); + when(threadMock.asThreadChannel()).thenReturn(threadMock); + + when(messageMock.isWebhookMessage()).thenReturn(isWebhook); + when(messageMock.getAuthor()).thenReturn(user); + when(messageMock.getChannel()).thenReturn(threadMock); + + return new MessageReceivedEvent(jdaTester.getJdaMock(), 0, messageMock); + } + + + @ParameterizedTest + @MethodSource("provideInvalidCharactersWithDescription") + @DisplayName("Does ignore invalid characters") + void excludesInvalidCharacters(String invalidChars, String description) { + // GIVEN a string of invalid characters + + // WHEN counting the amount of valid characters + long validCharacterCount = TopHelpersMessageListener.countValidCharacters(invalidChars); + + // THEN no characters are counted + assertEquals(0, validCharacterCount, + "Characters [%s] were not fully ignored".formatted(description)); + } + + + @ParameterizedTest + @MethodSource("provideValidCharacters") + @DisplayName("Does count valid characters") + void countsValidCharacters(String validChars) { + // GIVEN a string of valid characters + + // WHEN counting the amount of valid characters + long validCharCount = TopHelpersMessageListener.countValidCharacters(validChars); + + // THEN all characters are counted + assertEquals(validChars.length(), validCharCount, + "Characters [%s] were not fully ignored".formatted(validChars)); + } + + + private static Stream provideInvalidCharactersWithDescription() { + return Stream.of( // Invalid characters + Arguments.of("\u061C", "Arabic Letter Mark"), + Arguments.of("\u0600", "Arabic Number Sign"), + Arguments.of("\u180E", "Mongolian Vowel Separator"), + Arguments.of("\u200B", "Zero Width Space"), + Arguments.of("\u200C", "Zero Width Non-Joiner"), + Arguments.of("\u200D", "Zero Width Joiner"), + Arguments.of("\u200E", "Left-to-Right Mark"), + Arguments.of("\u200F", "Right-to-Left Mark")); + } + + + private static List provideValidCharacters() { + return List.of( // Valid characters + "a", "A", "b", "B", "c", "C", "x", "X,", "y", "Y", "z", "Z", // Latin alphabet + "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", // Numbers + "°", "!", "§", "§", "$", "%", "&", "/", "(", ")", "{", "}", "[", "]", "=", // Other + "+", "*", "~", "-", "_", ".", ",", "?", ":", ";", "|", "<", ">", "@", "€", "µ", // Other + "α", "Α", "β", "Β", "γ", "Γ", "χ", "Χ", "ψ", "Ψ", "ω", "Ω", // Greek alphabet + "ä", "ö", "ü", "ß", // German + "á", "è", "î", // French + "天", "四", "永", // Chinese + "😀", "😛", "❤️", "💚", "⛔" // Emojis + ); + } + +} From c69003c254f079f9f179b7470c19506d0fc7c4ce Mon Sep 17 00:00:00 2001 From: Nxllpointer Date: Fri, 28 Oct 2022 14:20:17 +0200 Subject: [PATCH 03/13] Removed unnecessary spying on topHelpersListener --- .../tophelper/TopHelperMessageListenerTest.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java b/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java index 701fea1cc9..7f13187cbd 100644 --- a/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java +++ b/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java @@ -32,7 +32,7 @@ final class TopHelperMessageListenerTest { private static final String OVERVIEW_CHANNEL_PATTERN = "active_questions"; private static JdaTester jdaTester; - private static TopHelpersMessageListener topHelpersListenerSpy; + private static TopHelpersMessageListener topHelpersListener; @BeforeAll static void setUp() { @@ -46,7 +46,7 @@ static void setUp() { when(config.getHelpSystem()).thenReturn(helpSystemConfig); jdaTester = new JdaTester(); - topHelpersListenerSpy = spy(new TopHelpersMessageListener(database, config)); + topHelpersListener = new TopHelpersMessageListener(database, config); } @Test @@ -57,7 +57,7 @@ void recognizesValidMessages() { createFakeMessageRecievedEvent(false, false, true, OVERVIEW_CHANNEL_PATTERN); // WHEN checking if the message should be ignored - boolean shouldBeIgnored = topHelpersListenerSpy.shouldIgnoreMessage(event); + boolean shouldBeIgnored = topHelpersListener.shouldIgnoreMessage(event); // THEN the message is not ignored assertFalse(shouldBeIgnored); @@ -71,7 +71,7 @@ void ignoresBots() { createFakeMessageRecievedEvent(true, false, true, OVERVIEW_CHANNEL_PATTERN); // WHEN checking if the message should be ignored - boolean shouldBeIgnored = topHelpersListenerSpy.shouldIgnoreMessage(event); + boolean shouldBeIgnored = topHelpersListener.shouldIgnoreMessage(event); // THEN the message is ignored assertTrue(shouldBeIgnored); @@ -85,7 +85,7 @@ void ignoresWebhooks() { createFakeMessageRecievedEvent(false, true, true, OVERVIEW_CHANNEL_PATTERN); // WHEN checking if the message should be ignored - boolean shouldBeIgnored = topHelpersListenerSpy.shouldIgnoreMessage(event); + boolean shouldBeIgnored = topHelpersListener.shouldIgnoreMessage(event); // THEN the message is ignored assertTrue(shouldBeIgnored); @@ -102,9 +102,9 @@ void ignoresWrongChannels() { // WHEN checking if the message should be ignored boolean ignoresNonThreadChannels = - topHelpersListenerSpy.shouldIgnoreMessage(eventNotAThread); + topHelpersListener.shouldIgnoreMessage(eventNotAThread); boolean ignoresWrongParentNames = - topHelpersListenerSpy.shouldIgnoreMessage(eventWrongParentName); + topHelpersListener.shouldIgnoreMessage(eventWrongParentName); // THEN the message is ignored assertTrue(ignoresNonThreadChannels, "Failed to ignore non-thread channels"); From 86af37c3d68d3dbebd69befeaffae4daaf9a5c80 Mon Sep 17 00:00:00 2001 From: Nxllpointer Date: Fri, 28 Oct 2022 14:22:31 +0200 Subject: [PATCH 04/13] Fixed typo createFakeMessageRecievedEvent to createFakeMessageReceivedEvent --- .../tophelper/TopHelperMessageListenerTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java b/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java index 7f13187cbd..368c2d0ba6 100644 --- a/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java +++ b/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java @@ -54,7 +54,7 @@ static void setUp() { void recognizesValidMessages() { // GIVEN a message by a human in a help channel MessageReceivedEvent event = - createFakeMessageRecievedEvent(false, false, true, OVERVIEW_CHANNEL_PATTERN); + createFakeMessageReceivedEvent(false, false, true, OVERVIEW_CHANNEL_PATTERN); // WHEN checking if the message should be ignored boolean shouldBeIgnored = topHelpersListener.shouldIgnoreMessage(event); @@ -68,7 +68,7 @@ void recognizesValidMessages() { void ignoresBots() { // GIVEN a message from a bot MessageReceivedEvent event = - createFakeMessageRecievedEvent(true, false, true, OVERVIEW_CHANNEL_PATTERN); + createFakeMessageReceivedEvent(true, false, true, OVERVIEW_CHANNEL_PATTERN); // WHEN checking if the message should be ignored boolean shouldBeIgnored = topHelpersListener.shouldIgnoreMessage(event); @@ -82,7 +82,7 @@ void ignoresBots() { void ignoresWebhooks() { // GIVEN a message from a webhook MessageReceivedEvent event = - createFakeMessageRecievedEvent(false, true, true, OVERVIEW_CHANNEL_PATTERN); + createFakeMessageReceivedEvent(false, true, true, OVERVIEW_CHANNEL_PATTERN); // WHEN checking if the message should be ignored boolean shouldBeIgnored = topHelpersListener.shouldIgnoreMessage(event); @@ -96,9 +96,9 @@ void ignoresWebhooks() { void ignoresWrongChannels() { // GIVEN a message outside a help thread MessageReceivedEvent eventNotAThread = - createFakeMessageRecievedEvent(false, false, false, OVERVIEW_CHANNEL_PATTERN); + createFakeMessageReceivedEvent(false, false, false, OVERVIEW_CHANNEL_PATTERN); MessageReceivedEvent eventWrongParentName = - createFakeMessageRecievedEvent(false, false, true, "memes"); + createFakeMessageReceivedEvent(false, false, true, "memes"); // WHEN checking if the message should be ignored boolean ignoresNonThreadChannels = @@ -112,7 +112,7 @@ void ignoresWrongChannels() { } - MessageReceivedEvent createFakeMessageRecievedEvent(boolean isBot, boolean isWebhook, + MessageReceivedEvent createFakeMessageReceivedEvent(boolean isBot, boolean isWebhook, boolean isThread, String parentChannelName) { Message messageMock = mock(Message.class); ThreadChannelImpl threadMock = mock(ThreadChannelImpl.class, RETURNS_DEEP_STUBS); From 209e9f554fde4ee19941341da0a636bb48b3c24f Mon Sep 17 00:00:00 2001 From: Nxllpointer Date: Fri, 28 Oct 2022 14:26:39 +0200 Subject: [PATCH 05/13] Removed unnecessary test display names --- .../commands/tophelper/TopHelperMessageListenerTest.java | 8 -------- 1 file changed, 8 deletions(-) diff --git a/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java b/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java index 368c2d0ba6..3e4976736a 100644 --- a/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java +++ b/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java @@ -8,12 +8,10 @@ import net.dv8tion.jda.internal.entities.UserImpl; import net.dv8tion.jda.internal.entities.channel.concrete.ThreadChannelImpl; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; - import org.togetherjava.tjbot.config.Config; import org.togetherjava.tjbot.config.HelpSystemConfig; import org.togetherjava.tjbot.db.Database; @@ -50,7 +48,6 @@ static void setUp() { } @Test - @DisplayName("Recognizes valid messages") void recognizesValidMessages() { // GIVEN a message by a human in a help channel MessageReceivedEvent event = @@ -64,7 +61,6 @@ void recognizesValidMessages() { } @Test - @DisplayName("Ignores bots") void ignoresBots() { // GIVEN a message from a bot MessageReceivedEvent event = @@ -78,7 +74,6 @@ void ignoresBots() { } @Test - @DisplayName("Ignores webhooks") void ignoresWebhooks() { // GIVEN a message from a webhook MessageReceivedEvent event = @@ -92,7 +87,6 @@ void ignoresWebhooks() { } @Test - @DisplayName("Ignores wrong channels") void ignoresWrongChannels() { // GIVEN a message outside a help thread MessageReceivedEvent eventNotAThread = @@ -136,7 +130,6 @@ MessageReceivedEvent createFakeMessageReceivedEvent(boolean isBot, boolean isWeb @ParameterizedTest @MethodSource("provideInvalidCharactersWithDescription") - @DisplayName("Does ignore invalid characters") void excludesInvalidCharacters(String invalidChars, String description) { // GIVEN a string of invalid characters @@ -151,7 +144,6 @@ void excludesInvalidCharacters(String invalidChars, String description) { @ParameterizedTest @MethodSource("provideValidCharacters") - @DisplayName("Does count valid characters") void countsValidCharacters(String validChars) { // GIVEN a string of valid characters From fcfb13a636facf5b68cd2b8c3122e1159f889fd3 Mon Sep 17 00:00:00 2001 From: Nxllpointer Date: Fri, 28 Oct 2022 14:28:15 +0200 Subject: [PATCH 06/13] Forgot to run spotless --- .../commands/tophelper/TopHelperMessageListenerTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java b/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java index 3e4976736a..ea4445aa0a 100644 --- a/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java +++ b/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java @@ -12,6 +12,7 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; + import org.togetherjava.tjbot.config.Config; import org.togetherjava.tjbot.config.HelpSystemConfig; import org.togetherjava.tjbot.db.Database; @@ -95,8 +96,7 @@ void ignoresWrongChannels() { createFakeMessageReceivedEvent(false, false, true, "memes"); // WHEN checking if the message should be ignored - boolean ignoresNonThreadChannels = - topHelpersListener.shouldIgnoreMessage(eventNotAThread); + boolean ignoresNonThreadChannels = topHelpersListener.shouldIgnoreMessage(eventNotAThread); boolean ignoresWrongParentNames = topHelpersListener.shouldIgnoreMessage(eventWrongParentName); From a196d00cfcc0d1eff846daef7401c8339791beb3 Mon Sep 17 00:00:00 2001 From: Nxllpointer Date: Sat, 29 Oct 2022 14:14:20 +0200 Subject: [PATCH 07/13] Added thread support for createMessageReceiveEvent --- .../MediaOnlyChannelListenerTest.java | 4 +- .../TopHelperMessageListenerTest.java | 33 ++++++--------- .../org/togetherjava/tjbot/jda/JdaTester.java | 42 ++++++++++++++++--- 3 files changed, 52 insertions(+), 27 deletions(-) diff --git a/application/src/test/java/org/togetherjava/tjbot/commands/mediaonly/MediaOnlyChannelListenerTest.java b/application/src/test/java/org/togetherjava/tjbot/commands/mediaonly/MediaOnlyChannelListenerTest.java index 63f6ef9520..fa5f3476ea 100644 --- a/application/src/test/java/org/togetherjava/tjbot/commands/mediaonly/MediaOnlyChannelListenerTest.java +++ b/application/src/test/java/org/togetherjava/tjbot/commands/mediaonly/MediaOnlyChannelListenerTest.java @@ -3,6 +3,7 @@ import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.MessageEmbed; +import net.dv8tion.jda.api.entities.channel.ChannelType; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder; import net.dv8tion.jda.api.utils.messages.MessageCreateData; @@ -102,7 +103,8 @@ private MessageReceivedEvent sendMessage(MessageCreateData message) { private MessageReceivedEvent sendMessage(MessageCreateData message, List attachments) { - MessageReceivedEvent event = jdaTester.createMessageReceiveEvent(message, attachments); + MessageReceivedEvent event = + jdaTester.createMessageReceiveEvent(message, attachments, ChannelType.TEXT); mediaOnlyChannelListener.onMessageReceived(event); return event; } diff --git a/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java b/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java index ea4445aa0a..a4f5816807 100644 --- a/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java +++ b/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java @@ -1,12 +1,9 @@ package org.togetherjava.tjbot.commands.tophelper; -import net.dv8tion.jda.api.entities.Message; -import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.channel.ChannelType; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; -import net.dv8tion.jda.internal.JDAImpl; -import net.dv8tion.jda.internal.entities.UserImpl; -import net.dv8tion.jda.internal.entities.channel.concrete.ThreadChannelImpl; +import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder; +import net.dv8tion.jda.api.utils.messages.MessageCreateData; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -22,7 +19,8 @@ import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import static org.togetherjava.tjbot.db.generated.tables.HelpChannelMessages.HELP_CHANNEL_MESSAGES; final class TopHelperMessageListenerTest { @@ -108,23 +106,16 @@ void ignoresWrongChannels() { MessageReceivedEvent createFakeMessageReceivedEvent(boolean isBot, boolean isWebhook, boolean isThread, String parentChannelName) { - Message messageMock = mock(Message.class); - ThreadChannelImpl threadMock = mock(ThreadChannelImpl.class, RETURNS_DEEP_STUBS); + try (MessageCreateData message = new MessageCreateBuilder().setContent("Test").build()) { + MessageReceivedEvent event = jdaTester.createMessageReceiveEvent(message, List.of(), + isThread ? ChannelType.GUILD_PUBLIC_THREAD : ChannelType.TEXT); - User user = new UserImpl(123456789, (JDAImpl) jdaTester.getJdaMock()).setName("John Doe") - .setDiscriminator("1234") - .setBot(isBot); + when(jdaTester.getMemberSpy().getUser().isBot()).thenReturn(isBot); + when(event.getMessage().isWebhookMessage()).thenReturn(isWebhook); + when(jdaTester.getTextChannelSpy().getName()).thenReturn(parentChannelName); - when(threadMock.getType()) - .thenReturn(isThread ? ChannelType.GUILD_PUBLIC_THREAD : ChannelType.TEXT); - when(threadMock.getParentChannel().getName()).thenReturn(parentChannelName); - when(threadMock.asThreadChannel()).thenReturn(threadMock); - - when(messageMock.isWebhookMessage()).thenReturn(isWebhook); - when(messageMock.getAuthor()).thenReturn(user); - when(messageMock.getChannel()).thenReturn(threadMock); - - return new MessageReceivedEvent(jdaTester.getJdaMock(), 0, messageMock); + return event; + } } diff --git a/application/src/test/java/org/togetherjava/tjbot/jda/JdaTester.java b/application/src/test/java/org/togetherjava/tjbot/jda/JdaTester.java index 3ab89df2f2..fcd45e72c4 100644 --- a/application/src/test/java/org/togetherjava/tjbot/jda/JdaTester.java +++ b/application/src/test/java/org/togetherjava/tjbot/jda/JdaTester.java @@ -4,10 +4,13 @@ import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.*; +import net.dv8tion.jda.api.entities.channel.ChannelType; import net.dv8tion.jda.api.entities.channel.concrete.PrivateChannel; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel; import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; +import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion; import net.dv8tion.jda.api.entities.emoji.Emoji; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; @@ -32,6 +35,7 @@ import net.dv8tion.jda.internal.entities.*; import net.dv8tion.jda.internal.entities.channel.concrete.PrivateChannelImpl; import net.dv8tion.jda.internal.entities.channel.concrete.TextChannelImpl; +import net.dv8tion.jda.internal.entities.channel.concrete.ThreadChannelImpl; import net.dv8tion.jda.internal.requests.Requester; import net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl; import net.dv8tion.jda.internal.requests.restaction.MessageCreateActionImpl; @@ -98,6 +102,7 @@ public final class JdaTester { private static final long PRIVATE_CHANNEL_ID = 1; private static final long GUILD_ID = 1; private static final long TEXT_CHANNEL_ID = 1; + private static final long THREAD_CHANNEL_ID = 2; private final JDAImpl jda; private final MemberImpl member; private final GuildImpl guild; @@ -106,6 +111,7 @@ public final class JdaTester { private final MessageCreateActionImpl messageCreateAction; private final WebhookMessageEditActionImpl webhookMessageEditAction; private final TextChannelImpl textChannel; + private final ThreadChannelImpl threadChannel; private final PrivateChannelImpl privateChannel; private final InteractionHook interactionHook; private final ReplyCallbackAction replyCallbackAction; @@ -131,6 +137,8 @@ public JdaTester() { Member selfMember = spy(new MemberImpl(guild, selfUser)); member = spy(new MemberImpl(guild, user)); textChannel = spy(new TextChannelImpl(TEXT_CHANNEL_ID, guild)); + threadChannel = spy( + new ThreadChannelImpl(THREAD_CHANNEL_ID, guild, ChannelType.GUILD_PUBLIC_THREAD)); privateChannel = spy(new PrivateChannelImpl(jda, PRIVATE_CHANNEL_ID, user)); messageCreateAction = mock(MessageCreateActionImpl.class); webhookMessageEditAction = mock(WebhookMessageEditActionImpl.class); @@ -151,6 +159,7 @@ public JdaTester() { doReturn(selfUser).when(jda).getSelfUser(); when(jda.getGuildChannelById(anyLong())).thenReturn(textChannel); when(jda.getTextChannelById(anyLong())).thenReturn(textChannel); + when(jda.getThreadChannelById(anyLong())).thenReturn(threadChannel); when(jda.getChannelById(ArgumentMatchers.>any(), anyLong())) .thenReturn(textChannel); when(jda.getPrivateChannelById(anyLong())).thenReturn(privateChannel); @@ -188,6 +197,7 @@ public JdaTester() { when(jda.retrieveUserById(anyLong())).thenReturn(userAction); doReturn(null).when(textChannel).retrieveMessageById(any()); + doReturn(null).when(threadChannel).retrieveMessageById(any()); interactionHook = mock(InteractionHook.class); when(interactionHook.editOriginal(anyString())).thenReturn(webhookMessageEditAction); @@ -197,8 +207,11 @@ public JdaTester() { .thenReturn(webhookMessageEditAction); doReturn(messageCreateAction).when(textChannel).sendMessageEmbeds(any(), any()); + doReturn(messageCreateAction).when(threadChannel).sendMessageEmbeds(any(), any()); doReturn(messageCreateAction).when(textChannel).sendMessageEmbeds(any()); + doReturn(messageCreateAction).when(threadChannel).sendMessageEmbeds(any()); doReturn(privateChannel).when(textChannel).asPrivateChannel(); + doReturn(textChannel).when(threadChannel).getParentChannel(); doNothing().when(messageCreateAction).queue(); when(messageCreateAction.setContent(any())).thenReturn(messageCreateAction); @@ -265,7 +278,7 @@ public ButtonClickEventBuilder createButtonInteractionEvent() { Message message = mockingDetails.isMock() || mockingDetails.isSpy() ? event : spy(event); - mockMessage(message); + mockMessage(message, ChannelType.TEXT); return message; }; @@ -354,6 +367,19 @@ public TextChannel getTextChannelSpy() { return textChannel; } + /** + * Gets the text channel spy used as universal text channel by all mocks created by this tester + * instance. + *

+ * For example the events created by {@link #createSlashCommandInteractionEvent(SlashCommand)} + * will return this spy on several of their methods. + * + * @return the text channel spy used by this tester + */ + public ThreadChannel getThreadChannelSpy() { + return threadChannel; + } + /** * Gets the private channel spy used as universal private channel by all mocks created by this * tester instance. @@ -557,9 +583,9 @@ public ErrorResponseException createErrorResponseException(ErrorResponse reason) * @return the event of receiving the given message */ public MessageReceivedEvent createMessageReceiveEvent(MessageCreateData message, - List attachments) { + List attachments, ChannelType channelType) { Message receivedMessage = clientMessageToReceivedMessageMock(message); - mockMessage(receivedMessage); + mockMessage(receivedMessage, channelType); doReturn(attachments).when(receivedMessage).getAttachments(); return new MessageReceivedEvent(jda, responseNumber.getAndIncrement(), receivedMessage); @@ -638,7 +664,13 @@ private void mockButtonClickEvent(ButtonInteractionEvent event) { doReturn(replyAction).when(event).editButton(any()); } - private void mockMessage(Message message) { + private void mockMessage(Message message, ChannelType channelType) { + MessageChannelUnion channel = switch (channelType) { + case TEXT -> textChannel; + case GUILD_PUBLIC_THREAD -> threadChannel; + default -> throw new IllegalArgumentException("Unimplemented channel type"); + }; + doReturn(messageCreateAction).when(message).reply(anyString()); doReturn(messageCreateAction).when(message) .replyEmbeds(ArgumentMatchers.any()); @@ -651,7 +683,7 @@ private void mockMessage(Message message) { doReturn(member).when(message).getMember(); doReturn(member.getUser()).when(message).getAuthor(); - doReturn(textChannel).when(message).getChannel(); + doReturn(channel).when(message).getChannel(); doReturn(1L).when(message).getIdLong(); doReturn(false).when(message).isWebhookMessage(); From fccbb33cc140e48c7611e0799e9c76ede9e2c807 Mon Sep 17 00:00:00 2001 From: Nxllpointer Date: Fri, 4 Nov 2022 17:04:24 +0100 Subject: [PATCH 08/13] Inlined isHelpThread check --- .../tjbot/commands/tophelper/TopHelpersMessageListener.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersMessageListener.java b/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersMessageListener.java index 0ea0fd1ffd..e1019ae39d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersMessageListener.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersMessageListener.java @@ -72,9 +72,8 @@ private void addMessageRecord(MessageReceivedEvent event) { } boolean shouldIgnoreMessage(MessageReceivedEvent event) { - MessageChannelUnion channel = event.getChannel(); - - return event.getAuthor().isBot() || event.isWebhookMessage() || !isHelpThread(channel); + return event.getAuthor().isBot() || event.isWebhookMessage() + || !isHelpThread(event.getChannel()); } boolean isHelpThread(MessageChannelUnion channel) { From aa73dcac7b5ca59375e3001413aeadd5ce729750 Mon Sep 17 00:00:00 2001 From: Nxllpointer Date: Fri, 4 Nov 2022 17:06:13 +0100 Subject: [PATCH 09/13] Improved unsupported channel type exception message --- .../src/test/java/org/togetherjava/tjbot/jda/JdaTester.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/application/src/test/java/org/togetherjava/tjbot/jda/JdaTester.java b/application/src/test/java/org/togetherjava/tjbot/jda/JdaTester.java index fcd45e72c4..e871e168f9 100644 --- a/application/src/test/java/org/togetherjava/tjbot/jda/JdaTester.java +++ b/application/src/test/java/org/togetherjava/tjbot/jda/JdaTester.java @@ -668,7 +668,8 @@ private void mockMessage(Message message, ChannelType channelType) { MessageChannelUnion channel = switch (channelType) { case TEXT -> textChannel; case GUILD_PUBLIC_THREAD -> threadChannel; - default -> throw new IllegalArgumentException("Unimplemented channel type"); + default -> throw new IllegalArgumentException( + "Unsupported channel type: " + channelType); }; doReturn(messageCreateAction).when(message).reply(anyString()); From 4f02333e8c0604454668620f4c7b83e21391bcee Mon Sep 17 00:00:00 2001 From: Nxllpointer Date: Fri, 4 Nov 2022 17:10:01 +0100 Subject: [PATCH 10/13] Added missing createMessageReceiveEvent javadoc param --- .../src/test/java/org/togetherjava/tjbot/jda/JdaTester.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/application/src/test/java/org/togetherjava/tjbot/jda/JdaTester.java b/application/src/test/java/org/togetherjava/tjbot/jda/JdaTester.java index e871e168f9..1a01e29254 100644 --- a/application/src/test/java/org/togetherjava/tjbot/jda/JdaTester.java +++ b/application/src/test/java/org/togetherjava/tjbot/jda/JdaTester.java @@ -580,6 +580,8 @@ public ErrorResponseException createErrorResponseException(ErrorResponse reason) * * @param message the message that has been received * @param attachments attachments of the message, empty if none + * @param channelType the type of the channel the message was sent in. See + * {@link #mockMessage(Message, ChannelType)} for supported channel types * @return the event of receiving the given message */ public MessageReceivedEvent createMessageReceiveEvent(MessageCreateData message, From feace5c52199784b1c1a33af26c7fb20896163a4 Mon Sep 17 00:00:00 2001 From: Nxllpointer Date: Fri, 4 Nov 2022 17:13:02 +0100 Subject: [PATCH 11/13] Fixed copy paste javadoc --- .../java/org/togetherjava/tjbot/jda/JdaTester.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/application/src/test/java/org/togetherjava/tjbot/jda/JdaTester.java b/application/src/test/java/org/togetherjava/tjbot/jda/JdaTester.java index 1a01e29254..66734c2c64 100644 --- a/application/src/test/java/org/togetherjava/tjbot/jda/JdaTester.java +++ b/application/src/test/java/org/togetherjava/tjbot/jda/JdaTester.java @@ -368,13 +368,14 @@ public TextChannel getTextChannelSpy() { } /** - * Gets the text channel spy used as universal text channel by all mocks created by this tester - * instance. + * Gets the thread channel spy used as universal thread channel by all mocks created by this + * tester instance. *

- * For example the events created by {@link #createSlashCommandInteractionEvent(SlashCommand)} - * will return this spy on several of their methods. + * For example the events created by + * {@link #createMessageReceiveEvent(MessageCreateData, List, ChannelType)} can return this + * channel spy * - * @return the text channel spy used by this tester + * @return the thread channel spy used by this tester */ public ThreadChannel getThreadChannelSpy() { return threadChannel; From 1df02e336501d4fb1bf48de4d92f906327ff43cb Mon Sep 17 00:00:00 2001 From: Nxllpointer Date: Fri, 4 Nov 2022 17:16:25 +0100 Subject: [PATCH 12/13] Renamed createFakeMessageReceivedEvent to createMessageReceivedEvent --- .../tophelper/TopHelperMessageListenerTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java b/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java index a4f5816807..bf7fec3d5b 100644 --- a/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java +++ b/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java @@ -50,7 +50,7 @@ static void setUp() { void recognizesValidMessages() { // GIVEN a message by a human in a help channel MessageReceivedEvent event = - createFakeMessageReceivedEvent(false, false, true, OVERVIEW_CHANNEL_PATTERN); + createMessageReceivedEvent(false, false, true, OVERVIEW_CHANNEL_PATTERN); // WHEN checking if the message should be ignored boolean shouldBeIgnored = topHelpersListener.shouldIgnoreMessage(event); @@ -63,7 +63,7 @@ void recognizesValidMessages() { void ignoresBots() { // GIVEN a message from a bot MessageReceivedEvent event = - createFakeMessageReceivedEvent(true, false, true, OVERVIEW_CHANNEL_PATTERN); + createMessageReceivedEvent(true, false, true, OVERVIEW_CHANNEL_PATTERN); // WHEN checking if the message should be ignored boolean shouldBeIgnored = topHelpersListener.shouldIgnoreMessage(event); @@ -76,7 +76,7 @@ void ignoresBots() { void ignoresWebhooks() { // GIVEN a message from a webhook MessageReceivedEvent event = - createFakeMessageReceivedEvent(false, true, true, OVERVIEW_CHANNEL_PATTERN); + createMessageReceivedEvent(false, true, true, OVERVIEW_CHANNEL_PATTERN); // WHEN checking if the message should be ignored boolean shouldBeIgnored = topHelpersListener.shouldIgnoreMessage(event); @@ -89,9 +89,9 @@ void ignoresWebhooks() { void ignoresWrongChannels() { // GIVEN a message outside a help thread MessageReceivedEvent eventNotAThread = - createFakeMessageReceivedEvent(false, false, false, OVERVIEW_CHANNEL_PATTERN); + createMessageReceivedEvent(false, false, false, OVERVIEW_CHANNEL_PATTERN); MessageReceivedEvent eventWrongParentName = - createFakeMessageReceivedEvent(false, false, true, "memes"); + createMessageReceivedEvent(false, false, true, "memes"); // WHEN checking if the message should be ignored boolean ignoresNonThreadChannels = topHelpersListener.shouldIgnoreMessage(eventNotAThread); @@ -104,7 +104,7 @@ void ignoresWrongChannels() { } - MessageReceivedEvent createFakeMessageReceivedEvent(boolean isBot, boolean isWebhook, + MessageReceivedEvent createMessageReceivedEvent(boolean isBot, boolean isWebhook, boolean isThread, String parentChannelName) { try (MessageCreateData message = new MessageCreateBuilder().setContent("Test").build()) { MessageReceivedEvent event = jdaTester.createMessageReceiveEvent(message, List.of(), From 57ea639d2e68bb5c87c648d76fbf6ad7df0cf89a Mon Sep 17 00:00:00 2001 From: Nxllpointer Date: Fri, 4 Nov 2022 17:23:27 +0100 Subject: [PATCH 13/13] Changed checks message content to "Any" --- .../tjbot/commands/tophelper/TopHelperMessageListenerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java b/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java index bf7fec3d5b..f58fbb20a4 100644 --- a/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java +++ b/application/src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java @@ -106,7 +106,7 @@ void ignoresWrongChannels() { MessageReceivedEvent createMessageReceivedEvent(boolean isBot, boolean isWebhook, boolean isThread, String parentChannelName) { - try (MessageCreateData message = new MessageCreateBuilder().setContent("Test").build()) { + try (MessageCreateData message = new MessageCreateBuilder().setContent("Any").build()) { MessageReceivedEvent event = jdaTester.createMessageReceiveEvent(message, List.of(), isThread ? ChannelType.GUILD_PUBLIC_THREAD : ChannelType.TEXT);