From a1d33c37816e7ee7b7cee1293c3486d2d1cadb8c Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Tue, 5 Jan 2021 18:58:41 +0100 Subject: [PATCH] pytest: Add a test for `listinvoices` We now have some more logic in the query, so let's test it exhaustively. --- contrib/pyln-client/pyln/client/lightning.py | 13 ++++-- tests/test_invoices.py | 47 ++++++++++++++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/contrib/pyln-client/pyln/client/lightning.py b/contrib/pyln-client/pyln/client/lightning.py index 2d9b3c946..4e0fb388e 100644 --- a/contrib/pyln-client/pyln/client/lightning.py +++ b/contrib/pyln-client/pyln/client/lightning.py @@ -875,12 +875,17 @@ class LightningRpc(UnixDomainSocketRpc): """ return self.call("listtransactions") - def listinvoices(self, label=None): - """ - Show invoice {label} (or all, if no {label)). + def listinvoices(self, label=None, payment_hash=None, invstring=None): + """Query invoices + + Show invoice matching {label} {payment_hash} or {invstring} (or + all, if no filters are present). + """ payload = { - "label": label + "label": label, + "payment_hash": payment_hash, + "invstring": invstring, } return self.call("listinvoices", payload) diff --git a/tests/test_invoices.py b/tests/test_invoices.py index e2a0cd5d9..881f77097 100644 --- a/tests/test_invoices.py +++ b/tests/test_invoices.py @@ -634,3 +634,50 @@ def test_amountless_invoice(node_factory): assert(i[0]['msatoshi_received'] == 1337) assert(i[0]['amount_received_msat'] == Millisatoshi(1337)) assert(i[0]['status'] == 'paid') + + +def test_listinvoices_filter(node_factory): + """ Test the optional query arguments to listinvoices + """ + + l1 = node_factory.get_node() + + invoices = [l1.rpc.invoice(42, 'label{}'.format(i), 'desc') for i in range(10)] + + def match(node, query, invoice): + r1 = l1.rpc.listinvoices(**query)['invoices'] + assert len(r1) == 1 + assert r1[0]['payment_hash'] == inv['payment_hash'] + assert r1[0]['bolt11'] == inv['bolt11'] + + for i, inv in enumerate(invoices): + match(l1, {'label': "label{}".format(i)}, inv) + match(l1, {'payment_hash': inv['payment_hash']}, inv) + match(l1, {'invstring': inv['bolt11']}, inv) + + # Now test for failures + + inv = invoices[0] + queries = [ + {"payment_hash": inv['payment_hash'], "label": "label0"}, + {"invstring": inv['bolt11'], "label": "label0"}, + {"payment_hash": inv['payment_hash'], "label": "label0"}, + ] + + for q in queries: + with pytest.raises( + RpcError, + match=r'Can only specify one of {label}, {invstring} or {payment_hash}' + ): + l1.rpc.listinvoices(**q) + + # Test querying for non-existent invoices + queries = [ + {'label': 'doesnt exist'}, + {'payment_hash': 'AA' * 32}, + {'invstring': 'lnbcrt420p1p0lfrl6pp5w4zsagnfqu08s93rd44z93s8tt920hd9jec2yph969wluwkzrwpqdq8v3jhxccxqyjw5qcqp9sp52kw0kp75f6v2jusd8nsg2nfmdr82pqj0gf3jc8tqp7a2j48rzweq9qy9qsqtlu8eslmd4yxqrtrz75v8vmqrwknnk64sm79cj4asxhgndnj22r3g2a6axdvfdkhw966zw63cy3uzzn5hxad9ja8amqpp3wputl3ffcpallm2g'}, + ] + + for q in queries: + r = l1.rpc.listinvoices(**q) + assert len(r['invoices']) == 0