Skip to content

Commit

Permalink
pytest: Add a test for listinvoices
Browse files Browse the repository at this point in the history
We now have some more logic in the query, so let's test it
exhaustively.
  • Loading branch information
cdecker committed Jan 5, 2021
1 parent 2ee9a18 commit 7f128b1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
13 changes: 9 additions & 4 deletions contrib/pyln-client/pyln/client/lightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,12 +868,17 @@ def listtransactions(self):
"""
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, bolt11=None):
"""Query invoices
Show invoice matching {label} {payment_hash} or {bolt11} (or
all, if no filters are present).
"""
payload = {
"label": label
"label": label,
"payment_hash": payment_hash,
"bolt11": bolt11,
}
return self.call("listinvoices", payload)

Expand Down
47 changes: 47 additions & 0 deletions tests/test_invoices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, {'bolt11': inv['bolt11']}, inv)

# Now test for failures

inv = invoices[0]
queries = [
{"payment_hash": inv['payment_hash'], "label": "label0"},
{"bolt11": inv['bolt11'], "label": "label0"},
{"payment_hash": inv['payment_hash'], "label": inv['bolt11']},
]

for q in queries:
with pytest.raises(
RpcError,
match=r'Can only specify one of {label}, {bolt11} or {payment_hash}'
):
l1.rpc.listinvoices(**q)

# Test querying for non-existent invoices
queries = [
{'label': 'doesnt exist'},
{'payment_hash': 'AA'*32},
{'bolt11': 'lnbcrt420p1p0lfrl6pp5w4zsagnfqu08s93rd44z93s8tt920hd9jec2yph969wluwkzrwpqdq8v3jhxccxqyjw5qcqp9sp52kw0kp75f6v2jusd8nsg2nfmdr82pqj0gf3jc8tqp7a2j48rzweq9qy9qsqtlu8eslmd4yxqrtrz75v8vmqrwknnk64sm79cj4asxhgndnj22r3g2a6axdvfdkhw966zw63cy3uzzn5hxad9ja8amqpp3wputl3ffcpallm2g'},
]

for q in queries:
r = l1.rpc.listinvoices(**q)
assert len(r['invoices']) == 0

0 comments on commit 7f128b1

Please sign in to comment.