From 465e6e2d4d29e765f2707b4b92445cc156c59ff6 Mon Sep 17 00:00:00 2001 From: darosior Date: Mon, 9 Sep 2019 02:16:53 +0200 Subject: [PATCH] pytest: test the 'rpc_command' hook --- tests/plugins/rpc_command.py | 27 +++++++++++++++++++++++++++ tests/test_plugin.py | 19 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100755 tests/plugins/rpc_command.py diff --git a/tests/plugins/rpc_command.py b/tests/plugins/rpc_command.py new file mode 100755 index 000000000000..e9057e57004a --- /dev/null +++ b/tests/plugins/rpc_command.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 +""" +This plugin is used to test the `rpc_command` hook. +""" +from lightning import Plugin + +plugin = Plugin(dynamic=False) + + +@plugin.hook("rpc_command") +def on_rpc_command(plugin, rpc_command, **kwargs): + request = rpc_command["rpc_command"] + if request["method"] == "invoice": + # Replace part of this command + request["params"]["description"] = "A plugin modified this description" + return {"replace": request} + elif request["method"] == "listfunds": + # Return a custom result to the command + return {"return": {"result": ["Custom result"]}} + elif request["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 b1e6ef84cb84..d81e2bb40468 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -751,3 +751,22 @@ def test_sendpay_notifications(node_factory, bitcoind): assert results['sendpay_success'][0] == response1 assert results['sendpay_failure'][0] == err.value.error + + +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" + + # The plugin sends a custom response to "listfunds" + funds = l1.rpc.listfunds() + assert funds[0] == "Custom result"