test: services create and pay invoice (#2452)

* test: services create and pay invoice
* add more tests
* check with fundingsource
* check status
This commit is contained in:
dni ⚡ 2024-05-13 16:55:38 +02:00 committed by GitHub
parent 6730c6ed67
commit 1e752dc3d2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 98 additions and 0 deletions

View File

@ -15,3 +15,10 @@ async def real_invoice():
invoice = get_real_invoice(100)
yield {"bolt11": invoice["payment_request"]}
del invoice
@pytest_asyncio.fixture(scope="function")
async def real_amountless_invoice():
invoice = get_real_invoice(0)
yield invoice["payment_request"]
del invoice

View File

@ -0,0 +1,46 @@
import pytest
from bolt11 import decode
from lnbits.core.services import (
PaymentStatus,
create_invoice,
)
from lnbits.wallets import get_funding_source
description = "test create invoice"
@pytest.mark.asyncio
async def test_create_invoice(from_wallet):
payment_hash, pr = await create_invoice(
wallet_id=from_wallet.id,
amount=1000,
memo=description,
)
invoice = decode(pr)
assert invoice.payment_hash == payment_hash
assert invoice.amount_msat == 1000000
assert invoice.description == description
funding_source = get_funding_source()
status = await funding_source.get_invoice_status(payment_hash)
assert isinstance(status, PaymentStatus)
assert status.pending
@pytest.mark.asyncio
async def test_create_internal_invoice(from_wallet):
payment_hash, pr = await create_invoice(
wallet_id=from_wallet.id, amount=1000, memo=description, internal=True
)
invoice = decode(pr)
assert invoice.payment_hash == payment_hash
assert invoice.amount_msat == 1000000
assert invoice.description == description
# Internal invoices are not on fundingsource. so we should get some kind of error
# that the invoice is not found, but we get status pending
funding_source = get_funding_source()
status = await funding_source.get_invoice_status(payment_hash)
assert isinstance(status, PaymentStatus)
assert status.pending

View File

@ -0,0 +1,45 @@
import pytest
from lnbits.core.crud import (
get_standalone_payment,
)
from lnbits.core.services import (
PaymentError,
pay_invoice,
)
description = "test pay invoice"
@pytest.mark.asyncio
async def test_services_pay_invoice(to_wallet, real_invoice):
payment_hash = await pay_invoice(
wallet_id=to_wallet.id,
payment_request=real_invoice.get("bolt11"),
description=description,
)
assert payment_hash
payment = await get_standalone_payment(payment_hash)
assert payment
assert not payment.pending
assert payment.memo == description
@pytest.mark.asyncio
async def test_services_pay_invoice_invalid_bolt11(to_wallet):
with pytest.raises(PaymentError):
await pay_invoice(
wallet_id=to_wallet.id,
payment_request="lnbcr1123123n",
)
@pytest.mark.asyncio
async def test_services_pay_invoice_0_amount_invoice(
to_wallet, real_amountless_invoice
):
with pytest.raises(PaymentError):
await pay_invoice(
wallet_id=to_wallet.id,
payment_request=real_amountless_invoice,
)