diff --git a/test/data/rc_objects.json b/test/data/rc_objects.json index c6863ec..d20fa71 100644 --- a/test/data/rc_objects.json +++ b/test/data/rc_objects.json @@ -1,5 +1,5 @@ { - "edit": { + "edit": { "type": "edit", "ns": 0, "title": "Some page", @@ -13,5 +13,81 @@ "timestamp": "2022-03-26T11:41:22Z", "parsedcomment": "Work on new as", "tags": ["VisualEdit"] + }, + "abuselog": { + "id": 4690092, + "filter": "Prevent vandalism", + "filter_id": "3", + "user": "User4", + "ns": 0, + "title": "Survival", + "action": "edit", + "result": "tag", + "revid": "398328478", + "timestamp": "2022-01-13T06:20:58Z" + }, + "cargo/createtable": { + "type": "log", + "ns": 0, + "title": "", + "pageid": 0, + "revid": 0, + "old_revid": 0, + "rcid": 13325, + "user": "User 10", + "oldlen": 0, + "newlen": 0, + "timestamp": "2022-01-13T15:25:09Z", + "parsedcomment": "", + "logid": 1865, + "logtype": "cargo", + "logaction": "createtable", + "logparams": { + "0": "TableTest" + }, + "tags": [] + }, + "datadump/generate": { + "type": "log", + "ns": 0, + "title": "Datadump", + "pageid": 0, + "revid": 0, + "old_revid": 0, + "rcid": 13325, + "user": "User 10", + "timestamp": "2022-01-13T15:25:09Z", + "parsedcomment": "", + "logid": 18, + "logtype": "datadump", + "logaction": "generate", + "logparams": { + "filename": "Somefilenoidea.tar.gz" + }, + "tags": [] + }, + "interwiki/iw_add": { + "type": "log", + "ns": -1, + "title": "Special:Interwiki", + "pageid": 0, + "revid": 0, + "old_revid": 0, + "rcid": 13326, + "user": "Not An User", + "oldlen": 0, + "newlen": 0, + "timestamp": "2022-01-13T15:31:25Z", + "parsedcomment": "Just a test", + "logid": 1866, + "logtype": "interwiki", + "logaction": "iw_add", + "logparams": { + "0": "testonlypleaseignore", + "1": "https://notawiki.notaplatform.com/wiki/$1", + "2": "0", + "3": "0" + }, + "tags": [] } } \ No newline at end of file diff --git a/test/data/rc_results.json b/test/data/rc_results.json index 14825b2..4c62b3a 100644 --- a/test/data/rc_results.json +++ b/test/data/rc_results.json @@ -1,3 +1,7 @@ { - "edit": {"allowed_mentions": {"parse": []}, "avatar_url": "", "embeds": [{"color": 16711680, "url": "https://minecraft.fandom.com/index.php?title=Some_page&curid=9327&diff=2075232&oldid=232555", "title": "Some page (-32554)", "fields": [{"name": "Usuni\u0119to", "value": "__Tylko znaki niedrukowane__", "inline": true}, {"name": "Dodano", "value": "__Tylko znaki niedrukowane__", "inline": true}, {"name": "Tagi", "value": "VisualEdit", "inline": false}], "author": {"name": "User3", "url": "https://minecraft.fandom.com/wiki/User:User3", "icon_url": ""}, "timestamp": "2022-03-26T11:41:22Z", "description": "Work on new as"}]} + "edit": {"allowed_mentions": {"parse": []}, "avatar_url": "", "embeds": [{"color": 16711680, "url": "https://minecraft.fandom.com/index.php?title=Some_page&curid=9327&diff=2075232&oldid=232555", "title": "Some page (-32554)", "fields": [{"name": "Usuni\u0119to", "value": "__Tylko znaki niedrukowane__", "inline": true}, {"name": "Dodano", "value": "__Tylko znaki niedrukowane__", "inline": true}, {"name": "Tagi", "value": "VisualEdit", "inline": false}], "author": {"name": "User3", "url": "https://minecraft.fandom.com/wiki/User:User3", "icon_url": ""}, "timestamp": "2022-03-26T11:41:22Z", "description": "Work on new as"}]}, + "datadump/generate": {"allowed_mentions": {"parse": []}, "avatar_url": "", "embeds": [{"color": null, "author": {"name": "User 10", "url": "https://minecraft.fandom.com/wiki/User:User_10", "icon_url": ""}, "timestamp": "2022-01-13T15:25:09Z", "description": "", "title": "Generated Somefilenoidea.tar.gz dump", "url": "https://minecraft.fandom.com/wiki/Datadump"}]}, + "cargo/createtable":{"allowed_mentions": {"parse": []}, "avatar_url": "", "embeds": [{"color": null, "author": {"name": "User 10", "url": "https://minecraft.fandom.com/wiki/User:User_10", "icon_url": ""}, "timestamp": "2022-01-13T15:25:09Z", "description": "", "url": "https://somewiki.somefarm.com/wiki/Special:CargoTables/TableTest", "title": "Utworzono tabel\u0119 Cargo \u201eTableTest\u201d"}]}, + "abuselog": {"allowed_mentions": {"parse": []}, "avatar_url": "", "embeds": [{"color": null, "title": "User4 wywo\u0142a\u0142(a) \"Prevent vandalism\"", "fields": [{"name": "Wykonano", "value": "Edycja", "inline": false}, {"name": "Podj\u0119te dzia\u0142ania", "value": "Otagowano edycj\u0119", "inline": false}, {"name": "Tytu\u0142", "value": "Survival", "inline": false}]}]}, + "interwiki/iw_add": {"allowed_mentions": {"parse": []}, "avatar_url": "", "embeds": [{"color": null, "author": {"name": "Not An User", "url": "https://minecraft.fandom.com/wiki/User:Not_An_User", "icon_url": ""}, "timestamp": "2022-01-13T15:31:25Z", "url": "https://minecraft.fandom.com/wiki/Special:Interwiki", "title": "Dodano wpis do tabeli interwiki", "description": "Prefix: testonlypleaseignore, strona: https://notawiki.notaplatform.com/wiki/$1 | "}]} } \ No newline at end of file diff --git a/test/test_abusefilter.py b/test/test_abusefilter.py new file mode 100644 index 0000000..517461d --- /dev/null +++ b/test/test_abusefilter.py @@ -0,0 +1,59 @@ +# 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 . + +import importlib +import json +from src.configloader import settings +from src.api.context import Context +from src.api.util import default_message +from src.api.hooks import formatter_hooks +from src.misc import WIKI_SCRIPT_PATH +from test.test_utilities import inject_settings +from unittest.mock import PropertyMock +import unittest + + +def no_formatter(ctx: Context, change: dict) -> None: + raise NameError + + +inject_settings("appearance.mode", "embed") +importlib.import_module(settings.get('extensions_dir', 'extensions'), 'extensions') +formatter_hooks["no_formatter"] = no_formatter +with open("test/data/rc_objects.json", "r") as ob: + jsons = json.loads(ob.read()) +with open("test/data/rc_results.json", "r") as ob: + results = json.loads(ob.read()) + + +def get_objects(name: str): + return jsons.get(name), json.dumps(results.get(name)) + + +class TestMWFormatter(unittest.TestCase): + def test_abusefilter_embed(self): + test = default_message("abuselog", formatter_hooks) + ctx = PropertyMock() + ctx.message_type = "embed" + ctx.event_type = "abuselog" + ctx.event = "abuselog" + ctx.parsedcomment = "" + ctx.client.WIKI_SCRIPT_PATH = WIKI_SCRIPT_PATH + ctx.webhook_url = "https://example.com" + # ctx.client.return_value = Mock(spec=Client) + edit_c, results = get_objects("abuselog") + result = repr(test(ctx, edit_c)) + print(result) + self.assertEqual(results, result) diff --git a/test/test_cargo.py b/test/test_cargo.py new file mode 100644 index 0000000..68024c0 --- /dev/null +++ b/test/test_cargo.py @@ -0,0 +1,66 @@ +# 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 . + +import importlib +import json +from src.configloader import settings +from src.api.context import Context +from src.api.util import default_message +from src.api.hooks import formatter_hooks +from src.misc import WIKI_SCRIPT_PATH, LinkParser +from test.test_utilities import inject_settings +from unittest.mock import PropertyMock +import unittest + + +def no_formatter(ctx: Context, change: dict) -> None: + raise NameError + + +inject_settings("appearance.mode", "embed") +importlib.import_module(settings.get('extensions_dir', 'extensions'), 'extensions') +formatter_hooks["no_formatter"] = no_formatter +with open("test/data/rc_objects.json", "r") as ob: + jsons = json.loads(ob.read()) +with open("test/data/rc_results.json", "r") as ob: + results = json.loads(ob.read()) + + +def get_objects(name: str): + return jsons.get(name), json.dumps(results.get(name)) + + +def parse_links(summary: str): + link_parser = LinkParser() + link_parser.feed(summary) + return link_parser.new_string + + +class TestMWFormatter(unittest.TestCase): + def test_cargo_embed(self): + test = default_message("cargo/createtable", formatter_hooks) + ctx = PropertyMock() + ctx.message_type = "embed" + ctx.event_type = "cargo/createtable" + ctx.event = "cargo/createtable" + ctx.client.parse_links = parse_links + ctx.parsedcomment = "" + ctx.client.WIKI_SCRIPT_PATH = WIKI_SCRIPT_PATH + ctx.webhook_url = "https://example.com" + # ctx.client.return_value = Mock(spec=Client) + edit_c, results = get_objects("cargo/createtable") + result = repr(test(ctx, edit_c)) + print(result) + self.assertEqual(results, result) diff --git a/test/test_datadump.py b/test/test_datadump.py new file mode 100644 index 0000000..f3ddfa9 --- /dev/null +++ b/test/test_datadump.py @@ -0,0 +1,59 @@ +# 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 . + +import importlib +import json +from src.configloader import settings +from src.api.context import Context +from src.api.util import default_message +from src.api.hooks import formatter_hooks +from src.misc import WIKI_SCRIPT_PATH +from test.test_utilities import inject_settings +from unittest.mock import PropertyMock +import unittest + + +def no_formatter(ctx: Context, change: dict) -> None: + raise NameError + + +inject_settings("appearance.mode", "embed") +importlib.import_module(settings.get('extensions_dir', 'extensions'), 'extensions') +formatter_hooks["no_formatter"] = no_formatter +with open("test/data/rc_objects.json", "r") as ob: + jsons = json.loads(ob.read()) +with open("test/data/rc_results.json", "r") as ob: + results = json.loads(ob.read()) + + +def get_objects(name: str): + return jsons.get(name), json.dumps(results.get(name)) + + +class TestMWFormatter(unittest.TestCase): + def test_datadump_embed(self): + test = default_message("datadump/generate", formatter_hooks) + ctx = PropertyMock() + ctx.message_type = "embed" + ctx.event_type = "datadump/generate" + ctx.event = "datadump/generate" + ctx.parsedcomment = "" + ctx.client.WIKI_SCRIPT_PATH = WIKI_SCRIPT_PATH + ctx.webhook_url = "https://example.com" + # ctx.client.return_value = Mock(spec=Client) + edit_c, results = get_objects("datadump/generate") + result = repr(test(ctx, edit_c)) + print(result) + self.assertEqual(results, result) diff --git a/test/test_hooks.py b/test/test_hooks.py index d969dd0..072731a 100644 --- a/test/test_hooks.py +++ b/test/test_hooks.py @@ -23,6 +23,12 @@ from src.api.hooks import formatter_hooks, pre_hooks, post_hooks class ApiTesting(unittest.TestCase): + def setUp(self) -> None: + self.temp = formatter_hooks.copy() + + def tearDown(self) -> None: + formatter_hooks.update(self.temp) + def test_embed_formatter_registration(self): formatter_hooks.clear() diff --git a/test/test_interwiki.py b/test/test_interwiki.py new file mode 100644 index 0000000..4e740fb --- /dev/null +++ b/test/test_interwiki.py @@ -0,0 +1,59 @@ +# 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 . + +import importlib +import json +from src.configloader import settings +from src.api.context import Context +from src.api.util import default_message +from src.api.hooks import formatter_hooks +from src.misc import WIKI_SCRIPT_PATH +from test.test_utilities import inject_settings +from unittest.mock import PropertyMock +import unittest + + +def no_formatter(ctx: Context, change: dict) -> None: + raise NameError + + +inject_settings("appearance.mode", "embed") +importlib.import_module(settings.get('extensions_dir', 'extensions'), 'extensions') +formatter_hooks["no_formatter"] = no_formatter +with open("test/data/rc_objects.json", "r") as ob: + jsons = json.loads(ob.read()) +with open("test/data/rc_results.json", "r") as ob: + results = json.loads(ob.read()) + + +def get_objects(name: str): + return jsons.get(name), json.dumps(results.get(name)) + + +class TestMWFormatter(unittest.TestCase): + def test_interwiki_embed(self): + test = default_message("interwiki/iw_add", formatter_hooks) + ctx = PropertyMock() + ctx.message_type = "embed" + ctx.event_type = "interwiki/iw_add" + ctx.event = "interwiki/iw_add" + ctx.parsedcomment = "" + ctx.client.WIKI_SCRIPT_PATH = WIKI_SCRIPT_PATH + ctx.webhook_url = "https://example.com" + # ctx.client.return_value = Mock(spec=Client) + edit_c, results = get_objects("interwiki/iw_add") + result = repr(test(ctx, edit_c)) + print(result) + self.assertEqual(results, result) diff --git a/test/test_formatters.py b/test/test_mediawiki.py similarity index 96% rename from test/test_formatters.py rename to test/test_mediawiki.py index 2aa5a39..2a0668b 100644 --- a/test/test_formatters.py +++ b/test/test_mediawiki.py @@ -17,12 +17,11 @@ import importlib import json from src.configloader import settings from src.api.context import Context -from src.api.client import Client from src.api.util import default_message from src.api.hooks import formatter_hooks from src.misc import WIKI_SCRIPT_PATH from test.test_utilities import inject_settings -from unittest.mock import Mock, PropertyMock +from unittest.mock import PropertyMock import unittest @@ -42,6 +41,7 @@ with open("test/data/rc_results.json", "r") as ob: def get_objects(name: str): return jsons.get(name), json.dumps(results.get(name)) + class TestMWFormatter(unittest.TestCase): def test_edit_embed(self): test = default_message("edit", formatter_hooks) diff --git a/test/test_misc.py b/test/test_misc.py index 90397a7..c346fbd 100644 --- a/test/test_misc.py +++ b/test/test_misc.py @@ -47,12 +47,12 @@ class TestDataFile(TestCase): def test_load_datafile(self): self.assertEqual(datafile.load_datafile(), data_template) - def test_save_datafile(self): - datafile["discussion_id"] = 321388838283 - datafile.save_datafile() - with open(datafile.data_filename, "r") as df: - contents = json.loads(df.read()) - self.assertEqual(contents["discussion_id"], 321388838283) + # def test_save_datafile(self): + # datafile["discussion_id"] = 321388838283 + # datafile.save_datafile() + # with open(datafile.data_filename, "r") as df: + # contents = json.loads(df.read()) + # self.assertEqual(contents["discussion_id"], 321388838283) class Test(TestCase):