testing the testing

This commit is contained in:
Frisk 2021-05-01 14:25:03 +02:00
parent 6858be4e61
commit edd46767bb
No known key found for this signature in database
GPG key ID: 213F7C15068AF8AC
3 changed files with 34 additions and 12 deletions

View file

@ -29,6 +29,7 @@ if TYPE_CHECKING:
logger = logging.getLogger("src.api.util")
def default_message(event: str, formatter_hooks: dict) -> Callable:
"""Returns a method of a formatter responsible for the event or None if such does not exist."""
return formatter_hooks.get(event, formatter_hooks.get("generic", formatter_hooks["no_formatter"]))
@ -40,12 +41,12 @@ def clean_link(link: str) -> str:
def sanitize_to_markdown(text: str):
"""Sanitizes given text to """
"""Sanitizes given text to escape markdown formatting. It is used in values that will be visible on Discord in messages"""
def sanitize_to_url(text: str) -> str: # TODO ) replaces needed?
"""Formats a link to not embed it"""
return quote(text, " \\/:").replace(' ', "_")
"""Formats a string in a way where it can be safely added to a URL without breaking MediaWiki URL schema"""
return quote(text, " /:").replace(' ', "_").replace(")", "%29")
def parse_mediawiki_changes(ctx: Context, content: str, embed: DiscordMessage) -> None:
@ -69,11 +70,6 @@ def parse_mediawiki_changes(ctx: Context, content: str, embed: DiscordMessage) -
embed.add_field(_("Added"), "{data}".format(data=edit_diff.small_prev_ins), inline=True)
def escape_formatting(data: str) -> str:
"""Escape Discord formatting"""
return re.sub(r"([`_*~<>{}@/|\\])", "\\\\\\1", data, 0)
def create_article_path(article: str) -> str:
"""Takes the string and creates an URL with it as the article name"""
return src.misc.WIKI_ARTICLE_PATH.replace("$1", article)

View file

@ -15,20 +15,17 @@
# You should have received a copy of the GNU General Public License
# along with RcGcDw. If not, see <http://www.gnu.org/licenses/>.
import base64
import json, logging, sys
import json, logging, sys, re
from html.parser import HTMLParser
from urllib.parse import urlparse, urlunparse
import requests
from src.api.util import escape_formatting
from src.configloader import settings
from src.discord.message import DiscordMessage, DiscordMessageMetadata
from src.discord.queue import messagequeue, send_to_discord
from src.exceptions import MediaWikiError
from src.i18n import misc
AUTO_SUPPRESSION_ENABLED = settings.get("auto_suppression", {"enabled": False}).get("enabled")
_ = misc.gettext
# Create a custom logger
@ -138,6 +135,9 @@ class ContentParser(HTMLParser):
self.empty = True
def handle_data(self, data):
def escape_formatting(data: str) -> str:
"""Escape Discord formatting"""
return re.sub(r"([`_*~<>{}@/|\\])", "\\\\\\1", data, 0)
data = escape_formatting(data)
if self.current_tag == "ins" and self.ins_length <= 1000:
self.ins_length += len("**" + data + "**")

26
test/test_util.py Normal file
View file

@ -0,0 +1,26 @@
# This file is part of Recent changes Goat compatible Discord webhook (RcGcDw).
#
# RcGcDw is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# RcGcDw is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with RcGcDw. If not, see <http://www.gnu.org/licenses/>.
from unittest import TestCase, main
from src.api.util import sanitize_to_url
class Test(TestCase):
def test_sanitize_to_url(self):
self.assertEqual(sanitize_to_url("Breaking rcgcdw . \ / : ? = ) & - ~ this is a test)"), "Breaking_rcgcdw_._%5C_/_:_%3F_%3D_%29_%26_-_~_this_is_a_test%29")
if __name__ == '__main__':
main()