-
-
Notifications
You must be signed in to change notification settings - Fork 101
Add tests for TopHelperMessageListener #661
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
39f6d76
Added test for top-helper-anticheat
Nxllpointer aff272b
Refactored TopHelpersMessageListener to be fully testable and added t…
Nxllpointer c69003c
Removed unnecessary spying on topHelpersListener
Nxllpointer 86af37c
Fixed typo createFakeMessageRecievedEvent to createFakeMessageReceive…
Nxllpointer 209e9f5
Removed unnecessary test display names
Nxllpointer fcfb13a
Forgot to run spotless
Nxllpointer a196d00
Added thread support for createMessageReceiveEvent
Nxllpointer fccbb33
Inlined isHelpThread check
Nxllpointer aa73dca
Improved unsupported channel type exception message
Nxllpointer 4f02333
Added missing createMessageReceiveEvent javadoc param
Nxllpointer feace5c
Fixed copy paste javadoc
Nxllpointer 1df02e3
Renamed createFakeMessageReceivedEvent to createMessageReceivedEvent
Nxllpointer 57ea639
Changed checks message content to "Any"
Nxllpointer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
177 changes: 177 additions & 0 deletions
177
...src/test/java/org/togetherjava/tjbot/commands/tophelper/TopHelperMessageListenerTest.java
This file contains hidden or 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,177 @@ | ||
package org.togetherjava.tjbot.commands.tophelper; | ||
|
||
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; | ||
import org.junit.jupiter.api.BeforeAll; | ||
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.mock; | ||
import static org.mockito.Mockito.when; | ||
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 topHelpersListener; | ||
|
||
@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(); | ||
topHelpersListener = new TopHelpersMessageListener(database, config); | ||
} | ||
|
||
@Test | ||
void recognizesValidMessages() { | ||
// GIVEN a message by a human in a help channel | ||
MessageReceivedEvent event = | ||
createMessageReceivedEvent(false, false, true, OVERVIEW_CHANNEL_PATTERN); | ||
|
||
// WHEN checking if the message should be ignored | ||
boolean shouldBeIgnored = topHelpersListener.shouldIgnoreMessage(event); | ||
|
||
// THEN the message is not ignored | ||
assertFalse(shouldBeIgnored); | ||
} | ||
|
||
@Test | ||
void ignoresBots() { | ||
// GIVEN a message from a bot | ||
MessageReceivedEvent event = | ||
createMessageReceivedEvent(true, false, true, OVERVIEW_CHANNEL_PATTERN); | ||
|
||
// WHEN checking if the message should be ignored | ||
boolean shouldBeIgnored = topHelpersListener.shouldIgnoreMessage(event); | ||
|
||
// THEN the message is ignored | ||
assertTrue(shouldBeIgnored); | ||
} | ||
|
||
@Test | ||
void ignoresWebhooks() { | ||
// GIVEN a message from a webhook | ||
MessageReceivedEvent event = | ||
createMessageReceivedEvent(false, true, true, OVERVIEW_CHANNEL_PATTERN); | ||
|
||
// WHEN checking if the message should be ignored | ||
boolean shouldBeIgnored = topHelpersListener.shouldIgnoreMessage(event); | ||
|
||
// THEN the message is ignored | ||
assertTrue(shouldBeIgnored); | ||
} | ||
|
||
@Test | ||
void ignoresWrongChannels() { | ||
// GIVEN a message outside a help thread | ||
MessageReceivedEvent eventNotAThread = | ||
createMessageReceivedEvent(false, false, false, OVERVIEW_CHANNEL_PATTERN); | ||
MessageReceivedEvent eventWrongParentName = | ||
createMessageReceivedEvent(false, false, true, "memes"); | ||
|
||
// WHEN checking if the message should be ignored | ||
boolean ignoresNonThreadChannels = topHelpersListener.shouldIgnoreMessage(eventNotAThread); | ||
boolean ignoresWrongParentNames = | ||
topHelpersListener.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 createMessageReceivedEvent(boolean isBot, boolean isWebhook, | ||
boolean isThread, String parentChannelName) { | ||
try (MessageCreateData message = new MessageCreateBuilder().setContent("Any").build()) { | ||
MessageReceivedEvent event = jdaTester.createMessageReceiveEvent(message, List.of(), | ||
isThread ? ChannelType.GUILD_PUBLIC_THREAD : ChannelType.TEXT); | ||
|
||
when(jdaTester.getMemberSpy().getUser().isBot()).thenReturn(isBot); | ||
when(event.getMessage().isWebhookMessage()).thenReturn(isWebhook); | ||
when(jdaTester.getTextChannelSpy().getName()).thenReturn(parentChannelName); | ||
|
||
return event; | ||
} | ||
} | ||
|
||
|
||
@ParameterizedTest | ||
@MethodSource("provideInvalidCharactersWithDescription") | ||
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") | ||
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<Arguments> 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<String> 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 | ||
); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.