test: refactor to not use paid_invoices stream for real invoice tests (#2628)
This commit is contained in:
parent
ddb8fcb986
commit
40ffa7dea0
5 changed files with 55 additions and 58 deletions
|
|
@ -972,7 +972,6 @@ async def update_payment_details(
|
||||||
f"UPDATE apipayments SET {', '.join(set_clause)} WHERE checking_id = ?",
|
f"UPDATE apipayments SET {', '.join(set_clause)} WHERE checking_id = ?",
|
||||||
tuple(set_variables),
|
tuple(set_variables),
|
||||||
)
|
)
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
async def update_payment_extra(
|
async def update_payment_extra(
|
||||||
|
|
|
||||||
|
|
@ -217,6 +217,8 @@ async def invoice_callback_dispatcher(checking_id: str, is_internal: bool = Fals
|
||||||
preimage=status.preimage,
|
preimage=status.preimage,
|
||||||
status=PaymentState.SUCCESS,
|
status=PaymentState.SUCCESS,
|
||||||
)
|
)
|
||||||
|
payment = await get_standalone_payment(checking_id, incoming=True)
|
||||||
|
assert payment, "updated payment not found"
|
||||||
internal = "internal" if is_internal else ""
|
internal = "internal" if is_internal else ""
|
||||||
logger.success(f"{internal} invoice {checking_id} settled")
|
logger.success(f"{internal} invoice {checking_id} settled")
|
||||||
for name, send_chan in invoice_listeners.items():
|
for name, send_chan in invoice_listeners.items():
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,10 @@ from lnbits.db import DB_TYPE, POSTGRES, FromRowModel
|
||||||
from lnbits.wallets import get_funding_source, set_funding_source
|
from lnbits.wallets import get_funding_source, set_funding_source
|
||||||
|
|
||||||
|
|
||||||
|
class FakeError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class DbTestModel(FromRowModel):
|
class DbTestModel(FromRowModel):
|
||||||
id: int
|
id: int
|
||||||
name: str
|
name: str
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,10 @@ from lnbits import bolt11
|
||||||
from lnbits.core.crud import get_standalone_payment, update_payment_details
|
from lnbits.core.crud import get_standalone_payment, update_payment_details
|
||||||
from lnbits.core.models import CreateInvoice, Payment, PaymentState
|
from lnbits.core.models import CreateInvoice, Payment, PaymentState
|
||||||
from lnbits.core.services import fee_reserve_total, get_balance_delta
|
from lnbits.core.services import fee_reserve_total, get_balance_delta
|
||||||
|
from lnbits.tasks import create_task, wait_for_paid_invoices
|
||||||
from lnbits.wallets import get_funding_source
|
from lnbits.wallets import get_funding_source
|
||||||
|
|
||||||
from ..helpers import is_fake, is_regtest
|
from ..helpers import FakeError, is_fake, is_regtest
|
||||||
from .helpers import (
|
from .helpers import (
|
||||||
cancel_invoice,
|
cancel_invoice,
|
||||||
get_real_invoice,
|
get_real_invoice,
|
||||||
|
|
@ -79,33 +80,30 @@ async def test_create_real_invoice(client, adminkey_headers_from, inkey_headers_
|
||||||
payment_status = response.json()
|
payment_status = response.json()
|
||||||
assert not payment_status["paid"]
|
assert not payment_status["paid"]
|
||||||
|
|
||||||
async def listen():
|
async def on_paid(payment: Payment):
|
||||||
async for checking_id in get_funding_source().paid_invoices_stream():
|
|
||||||
if checking_id == invoice["checking_id"]:
|
assert payment.checking_id == invoice["payment_hash"]
|
||||||
# wait for the backend to update the payment status
|
|
||||||
await asyncio.sleep(3)
|
response = await client.get(
|
||||||
return checking_id
|
f'/api/v1/payments/{invoice["payment_hash"]}', headers=inkey_headers_from
|
||||||
|
)
|
||||||
|
assert response.status_code < 300
|
||||||
|
payment_status = response.json()
|
||||||
|
assert payment_status["paid"]
|
||||||
|
|
||||||
async def pay():
|
|
||||||
# wait a sec to paid_invoices_stream to start listening
|
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
pay_real_invoice(invoice["payment_request"])
|
balance = await get_node_balance_sats()
|
||||||
return True
|
assert balance - prev_balance == create_invoice.amount
|
||||||
|
|
||||||
checking_id, paid = await asyncio.gather(listen(), pay())
|
# exit out of infinite loop
|
||||||
assert paid
|
raise FakeError()
|
||||||
assert checking_id == invoice["payment_hash"]
|
|
||||||
|
|
||||||
response = await client.get(
|
task = create_task(wait_for_paid_invoices("test_create_invoice", on_paid)())
|
||||||
f'/api/v1/payments/{invoice["payment_hash"]}', headers=inkey_headers_from
|
pay_real_invoice(invoice["payment_request"])
|
||||||
)
|
|
||||||
assert response.status_code < 300
|
|
||||||
payment_status = response.json()
|
|
||||||
assert payment_status["paid"]
|
|
||||||
|
|
||||||
await asyncio.sleep(1)
|
# wait for the task to exit
|
||||||
balance = await get_node_balance_sats()
|
with pytest.raises(FakeError):
|
||||||
assert balance - prev_balance == create_invoice.amount
|
await task
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
|
|
@ -298,45 +296,38 @@ async def test_receive_real_invoice_set_pending_and_check_state(
|
||||||
payment_status = response.json()
|
payment_status = response.json()
|
||||||
assert not payment_status["paid"]
|
assert not payment_status["paid"]
|
||||||
|
|
||||||
async def listen():
|
async def on_paid(payment: Payment):
|
||||||
async for checking_id in get_funding_source().paid_invoices_stream():
|
assert payment.checking_id == invoice["payment_hash"]
|
||||||
if checking_id == invoice["checking_id"]:
|
|
||||||
# wait for the backend to update the payment status
|
|
||||||
await asyncio.sleep(3)
|
|
||||||
return checking_id
|
|
||||||
|
|
||||||
async def pay():
|
response = await client.get(
|
||||||
# wait a sec to paid_invoices_stream to start listening
|
f'/api/v1/payments/{invoice["payment_hash"]}', headers=inkey_headers_from
|
||||||
await asyncio.sleep(1)
|
)
|
||||||
pay_real_invoice(invoice["payment_request"])
|
assert response.status_code < 300
|
||||||
return True
|
payment_status = response.json()
|
||||||
|
assert payment_status["paid"]
|
||||||
|
|
||||||
checking_id, paid = await asyncio.gather(listen(), pay())
|
assert payment
|
||||||
assert paid
|
assert payment.pending is False
|
||||||
assert checking_id == invoice["payment_hash"]
|
|
||||||
|
|
||||||
response = await client.get(
|
# set the incoming invoice to pending
|
||||||
f'/api/v1/payments/{invoice["payment_hash"]}', headers=inkey_headers_from
|
await update_payment_details(payment.checking_id, status=PaymentState.PENDING)
|
||||||
)
|
|
||||||
assert response.status_code < 300
|
|
||||||
payment_status = response.json()
|
|
||||||
assert payment_status["paid"]
|
|
||||||
|
|
||||||
# get the incoming payment from the db
|
payment_pending = await get_standalone_payment(
|
||||||
payment = await get_standalone_payment(invoice["payment_hash"], incoming=True)
|
invoice["payment_hash"], incoming=True
|
||||||
assert payment
|
)
|
||||||
assert payment.pending is False
|
assert payment_pending
|
||||||
|
assert payment_pending.pending is True
|
||||||
|
assert payment_pending.success is False
|
||||||
|
assert payment_pending.failed is False
|
||||||
|
|
||||||
# set the incoming invoice to pending
|
# exit out of infinite loop
|
||||||
await update_payment_details(payment.checking_id, status=PaymentState.PENDING)
|
raise FakeError()
|
||||||
|
|
||||||
payment_pending = await get_standalone_payment(
|
task = create_task(wait_for_paid_invoices("test_create_invoice", on_paid)())
|
||||||
invoice["payment_hash"], incoming=True
|
pay_real_invoice(invoice["payment_request"])
|
||||||
)
|
|
||||||
assert payment_pending
|
with pytest.raises(FakeError):
|
||||||
assert payment_pending.pending is True
|
await task
|
||||||
assert payment_pending.success is False
|
|
||||||
assert payment_pending.failed is False
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
|
|
|
||||||
|
|
@ -160,7 +160,8 @@ async def test_peer_management(node_client):
|
||||||
|
|
||||||
response = await node_client.delete(f"/node/api/v1/peers/{peer_id}")
|
response = await node_client.delete(f"/node/api/v1/peers/{peer_id}")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
await asyncio.sleep(0.1)
|
# lndrest is slow to remove the peer
|
||||||
|
await asyncio.sleep(0.3)
|
||||||
|
|
||||||
response = await node_client.get("/node/api/v1/peers")
|
response = await node_client.get("/node/api/v1/peers")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue