async def test_propagate_pre_invoke(self): bot = AsyncMock() bot.user.id = 919191 create_handler = AntiSpamHandler(bot) class PreInvoke(BasePlugin): async def propagate(self, msg): return 1 create_handler.register_plugin(PreInvoke()) message = MockedMessage().to_mock() return_data = await create_handler.propagate(message) assert len(return_data.pre_invoke_extensions) == 1 assert return_data.pre_invoke_extensions["PreInvoke"] == 1
def test_unregister_extension(self, create_handler: AntiSpamHandler): class Test(BasePlugin): def __init__(self, invoke=True): self.is_pre_invoke = invoke create_handler.register_plugin(Test(invoke=False)) assert len(create_handler.after_invoke_extensions.values()) == 1 create_handler.unregister_plugin("Test") assert len(create_handler.after_invoke_extensions.values()) == 0 with pytest.raises(PluginError): create_handler.unregister_plugin("Invalid Extension") create_handler.register_plugin(Test()) assert len(create_handler.pre_invoke_plugins.values()) == 1 create_handler.unregister_plugin("Test") assert len(create_handler.pre_invoke_plugins.values()) == 0
async def test_propagate_after_invoke(self): bot = AsyncMock() bot.user.id = 919191 create_handler = AntiSpamHandler(bot) class AfterInvoke(BasePlugin): def __init__(self): super().__init__(False) async def propagate(self, msg, data): return 2 create_handler.register_plugin(AfterInvoke()) message = MockedMessage().to_mock() return_data = await create_handler.propagate(message) assert len(return_data.after_invoke_extensions) == 1 assert return_data.after_invoke_extensions["AfterInvoke"] == 2
def test_register_extension(self, create_handler: AntiSpamHandler): with pytest.raises(PluginError): create_handler.register_plugin(MockClass()) class Test(BasePlugin): def __init__(self, invoke=True): self.is_pre_invoke = invoke assert len(create_handler.pre_invoke_plugins.values()) == 0 create_handler.register_plugin(Test()) assert len(create_handler.pre_invoke_plugins.values()) == 1 # Test overwrite with pytest.raises(PluginError): create_handler.register_plugin(Test()) create_handler.register_plugin(Test(), force_overwrite=True) assert len(create_handler.after_invoke_extensions.values()) == 0 create_handler.register_plugin(Test(invoke=False), force_overwrite=True) assert len(create_handler.after_invoke_extensions.values()) == 1
async def test_save_to_dict_with_plugin(self, create_handler: AntiSpamHandler): await create_handler.cache.set_guild(Guild(1, Options())) class Plugin(BasePlugin): async def save_to_dict(self): return {"Plugin me"} create_handler.register_plugin(Plugin()) data = await create_handler.save_to_dict() with open("tests/raw.json", "r") as file: stored_data = json.load(file) stored_data["pre_invoke_plugins"]["Plugin"] = {"Plugin me"} assert data == stored_data create_handler.unregister_plugin("Plugin") create_handler.register_plugin(Plugin(is_pre_invoke=False)) stored_data["pre_invoke_plugins"] = {} stored_data["after_invoke_plugins"]["Plugin"] = {"Plugin me"} data_two = await create_handler.save_to_dict() assert data_two == stored_data