diff --git a/tests/plugins/rpc_command.py b/tests/plugins/rpc_command.py new file mode 100755 index 000000000000..79942169e517 --- /dev/null +++ b/tests/plugins/rpc_command.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +""" +This plugin is used to test the `rpc_command` hook. +""" +import json +from lightning import Plugin + +plugin = Plugin(dynamic=False) + + +@plugin.hook("rpc_command") +def on_rpc_command(plugin, rpc_command, **kwargs): + if rpc_command["method"] == "invoice": + rpc_command["params"] = json.loads(rpc_command["params"]) + rpc_command["params"]["description"] = "A plugin modified this description" + return {"replace": rpc_command} + elif rpc_command["method"] == "sendpay": + # Don't allow this command to be executed + return {"return": {"error": {"code": -1, "message": "You cannot do this"}}} + return "continue" + + +plugin.run() diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 754606854743..70efe0e7e7a2 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -673,3 +673,18 @@ def test_plugin_deprecated_relpath(node_factory): assert l1.daemon.is_in_log('DEPRECATED WARNING.*plugin={}' .format(os.path.join(os.getcwd(), 'tests/plugins/millisatoshis.py'))) + + +def test_rpc_command_hook(node_factory): + """Test the `sensitive_command` hook""" + plugin = os.path.join(os.getcwd(), "tests/plugins/rpc_command.py") + l1 = node_factory.get_node(options={"plugin": plugin}) + + # Usage of "sendpay" has been restricted by the plugin + with pytest.raises(RpcError, match=r"You cannot do this"): + l1.rpc.call("sendpay") + + # The plugin replaces a call made for the "invoice" command + invoice = l1.rpc.invoice(10**6, "test_side", "test_input") + decoded = l1.rpc.decodepay(invoice["bolt11"]) + assert decoded["description"] == "A plugin modified this description"