Skip to content

Commit

Permalink
Add a test for the 'rpc_command' hook
Browse files Browse the repository at this point in the history
  • Loading branch information
darosior committed Sep 9, 2019
1 parent cadb86d commit 8665f3b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
23 changes: 23 additions & 0 deletions tests/plugins/rpc_command.py
Original file line number Diff line number Diff line change
@@ -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()
15 changes: 15 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 8665f3b

Please sign in to comment.