From c5d3c5ab2b3ed0a7a5099f4071152ac50a2746b3 Mon Sep 17 00:00:00 2001 From: GDWR Date: Tue, 10 May 2022 18:03:02 +0100 Subject: Add tests for `DISCORD_INVITE` regex --- tests/botcore/utils/test_regex.py | 48 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/botcore/utils/test_regex.py diff --git a/tests/botcore/utils/test_regex.py b/tests/botcore/utils/test_regex.py new file mode 100644 index 00000000..d4ca2934 --- /dev/null +++ b/tests/botcore/utils/test_regex.py @@ -0,0 +1,48 @@ +from typing import Optional + +from botcore.utils.regex import DISCORD_INVITE + + +def use_regex(s: str) -> Optional[str]: + """Helper function to run the Regex on a string. + + Return the invite capture group, if the string matches the pattern + else return None + """ + result = DISCORD_INVITE.search(s) + return result if result is None else result.group("invite") + + +def test_discord_invite_positive(): + """Test the DISCORD_INVITE regex on a set of strings we would expect to capture.""" + + assert use_regex("discord.gg/python") == "python" + assert use_regex("https://discord.gg/python") == "python" + assert use_regex("discord.com/invite/python") == "python" + assert use_regex("discordapp.com/invite/python") == "python" + assert use_regex("discord.me/python") == "python" + assert use_regex("discord.li/python") == "python" + assert use_regex("discord.io/python") == "python" + assert use_regex(".gg/python") == "python" + + assert use_regex("discord.gg/python/but/extra") == "python/but/extra" + assert use_regex("discord.me/this/isnt/python") == "this/isnt/python" + assert use_regex(".gg/a/a/a/a/a/a/a/a/a/a/a") == "a/a/a/a/a/a/a/a/a/a/a" + assert use_regex("discordapp.com/invite/python/snakescord") == "python/snakescord" + assert use_regex("http://discord.gg/python/%20/notpython") == "python/%20/notpython" + assert use_regex("discord.gg/python?=ts/notpython") == "python?=ts/notpython" + assert use_regex("https://discord.gg/python#fragment/notpython") == "python#fragment/notpython" + assert use_regex("https://discord.gg/python/~/notpython") == "python/~/notpython" + + assert use_regex("https://discord.gg/python with whitespace") == "python" + assert use_regex(" https://discord.gg/python ") == "python" + + +def test_discord_invite_negatives(): + """Test the DISCORD_INVITE regex on a set of strings we would expect to not capture.""" + + assert use_regex("another string") is None + assert use_regex("https://pythondiscord.com") is None + assert use_regex("https://discord.com") is None + assert use_regex("https://discord.gg") is None + assert use_regex("https://discord.gg/ python") is None -- cgit v1.2.3 From c9daa6f3774380794968cf28faf9ba4bde1f68e2 Mon Sep 17 00:00:00 2001 From: GDWR Date: Tue, 10 May 2022 18:49:12 +0100 Subject: `DISCORD_INVITE` captures until a whitespace --- botcore/utils/regex.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/botcore/utils/regex.py b/botcore/utils/regex.py index abcaf299..5146a113 100644 --- a/botcore/utils/regex.py +++ b/botcore/utils/regex.py @@ -11,13 +11,17 @@ DISCORD_INVITE = re.compile( r"discord([.,]|dot)io|" # or discord.io. r"((?[a-zA-Z0-9\-]+)", # the invite code itself + r"(?P\S+)", # the invite code itself flags=re.IGNORECASE ) """ Regex for Discord server invites. :meta hide-value: +.. warning:: + This regex pattern will capture until a whitespace, if you are to use the 'invite' capture group in + any HTTP requests or similar. Please ensure you sanitise the output using something similar to + https://docs.python.org/3/library/urllib.parse.html#urllib.parse.quote. """ FORMATTED_CODE_REGEX = re.compile( -- cgit v1.2.3 From 05c17c34da18e3ac4c1d291bc318a1a407e0e308 Mon Sep 17 00:00:00 2001 From: GDWR Date: Tue, 10 May 2022 18:51:57 +0100 Subject: Make test name plural for consistency --- tests/botcore/utils/test_regex.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/botcore/utils/test_regex.py b/tests/botcore/utils/test_regex.py index d4ca2934..70d4252f 100644 --- a/tests/botcore/utils/test_regex.py +++ b/tests/botcore/utils/test_regex.py @@ -13,7 +13,7 @@ def use_regex(s: str) -> Optional[str]: return result if result is None else result.group("invite") -def test_discord_invite_positive(): +def test_discord_invite_positives(): """Test the DISCORD_INVITE regex on a set of strings we would expect to capture.""" assert use_regex("discord.gg/python") == "python" -- cgit v1.2.3 From 9cc624103248f7b4b6fe1971b544d60fbea70136 Mon Sep 17 00:00:00 2001 From: GDWR Date: Tue, 10 May 2022 19:07:31 +0100 Subject: Remove trailing whitespace from docstring --- botcore/utils/regex.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/botcore/utils/regex.py b/botcore/utils/regex.py index 5146a113..8eb38ba2 100644 --- a/botcore/utils/regex.py +++ b/botcore/utils/regex.py @@ -18,9 +18,9 @@ DISCORD_INVITE = re.compile( Regex for Discord server invites. :meta hide-value: -.. warning:: - This regex pattern will capture until a whitespace, if you are to use the 'invite' capture group in - any HTTP requests or similar. Please ensure you sanitise the output using something similar to +.. warning:: + This regex pattern will capture until a whitespace, if you are to use the 'invite' capture group in + any HTTP requests or similar. Please ensure you sanitise the output using something similar to https://docs.python.org/3/library/urllib.parse.html#urllib.parse.quote. """ -- cgit v1.2.3 From 45b809167849ba950f271f9466463042560535c7 Mon Sep 17 00:00:00 2001 From: GDWR Date: Tue, 10 May 2022 20:06:35 +0100 Subject: Convert tests from pytest style to unittest style --- tests/botcore/utils/test_regex.py | 68 ++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/tests/botcore/utils/test_regex.py b/tests/botcore/utils/test_regex.py index 70d4252f..2ffd0e46 100644 --- a/tests/botcore/utils/test_regex.py +++ b/tests/botcore/utils/test_regex.py @@ -1,3 +1,4 @@ +import unittest from typing import Optional from botcore.utils.regex import DISCORD_INVITE @@ -13,36 +14,37 @@ def use_regex(s: str) -> Optional[str]: return result if result is None else result.group("invite") -def test_discord_invite_positives(): - """Test the DISCORD_INVITE regex on a set of strings we would expect to capture.""" - - assert use_regex("discord.gg/python") == "python" - assert use_regex("https://discord.gg/python") == "python" - assert use_regex("discord.com/invite/python") == "python" - assert use_regex("discordapp.com/invite/python") == "python" - assert use_regex("discord.me/python") == "python" - assert use_regex("discord.li/python") == "python" - assert use_regex("discord.io/python") == "python" - assert use_regex(".gg/python") == "python" - - assert use_regex("discord.gg/python/but/extra") == "python/but/extra" - assert use_regex("discord.me/this/isnt/python") == "this/isnt/python" - assert use_regex(".gg/a/a/a/a/a/a/a/a/a/a/a") == "a/a/a/a/a/a/a/a/a/a/a" - assert use_regex("discordapp.com/invite/python/snakescord") == "python/snakescord" - assert use_regex("http://discord.gg/python/%20/notpython") == "python/%20/notpython" - assert use_regex("discord.gg/python?=ts/notpython") == "python?=ts/notpython" - assert use_regex("https://discord.gg/python#fragment/notpython") == "python#fragment/notpython" - assert use_regex("https://discord.gg/python/~/notpython") == "python/~/notpython" - - assert use_regex("https://discord.gg/python with whitespace") == "python" - assert use_regex(" https://discord.gg/python ") == "python" - - -def test_discord_invite_negatives(): - """Test the DISCORD_INVITE regex on a set of strings we would expect to not capture.""" - - assert use_regex("another string") is None - assert use_regex("https://pythondiscord.com") is None - assert use_regex("https://discord.com") is None - assert use_regex("https://discord.gg") is None - assert use_regex("https://discord.gg/ python") is None +class UtilsRegexTests(unittest.TestCase): + + def test_discord_invite_positives(self): + """Test the DISCORD_INVITE regex on a set of strings we would expect to capture.""" + + self.assertEqual(use_regex("discord.gg/python"), "python") + self.assertEqual(use_regex("https://discord.gg/python"), "python") + self.assertEqual(use_regex("discord.com/invite/python"), "python") + self.assertEqual(use_regex("discordapp.com/invite/python"), "python") + self.assertEqual(use_regex("discord.me/python"), "python") + self.assertEqual(use_regex("discord.li/python"), "python") + self.assertEqual(use_regex("discord.io/python"), "python") + self.assertEqual(use_regex(".gg/python"), "python") + + self.assertEqual(use_regex("discord.gg/python/but/extra"), "python/but/extra") + self.assertEqual(use_regex("discord.me/this/isnt/python"), "this/isnt/python") + self.assertEqual(use_regex(".gg/a/a/a/a/a/a/a/a/a/a/a"), "a/a/a/a/a/a/a/a/a/a/a") + self.assertEqual(use_regex("discordapp.com/invite/python/snakescord"), "python/snakescord") + self.assertEqual(use_regex("http://discord.gg/python/%20/notpython"), "python/%20/notpython") + self.assertEqual(use_regex("discord.gg/python?=ts/notpython"), "python?=ts/notpython") + self.assertEqual(use_regex("https://discord.gg/python#fragment/notpython"), "python#fragment/notpython") + self.assertEqual(use_regex("https://discord.gg/python/~/notpython"), "python/~/notpython") + + self.assertEqual(use_regex("https://discord.gg/python with whitespace"), "python") + self.assertEqual(use_regex(" https://discord.gg/python "), "python") + + def test_discord_invite_negatives(self): + """Test the DISCORD_INVITE regex on a set of strings we would expect to not capture.""" + + self.assertEqual(use_regex("another string"), None) + self.assertEqual(use_regex("https://pythondiscord.com"), None) + self.assertEqual(use_regex("https://discord.com"), None) + self.assertEqual(use_regex("https://discord.gg"), None) + self.assertEqual(use_regex("https://discord.gg/ python"), None) -- cgit v1.2.3