diff --git a/tests/regtest/conftest.py b/tests/regtest/conftest.py index f0134a224..cce10b4b6 100644 --- a/tests/regtest/conftest.py +++ b/tests/regtest/conftest.py @@ -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 diff --git a/tests/regtest/test_services_create_invoice.py b/tests/regtest/test_services_create_invoice.py new file mode 100644 index 000000000..80b5c8f3f --- /dev/null +++ b/tests/regtest/test_services_create_invoice.py @@ -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 diff --git a/tests/regtest/test_services_pay_invoice.py b/tests/regtest/test_services_pay_invoice.py new file mode 100644 index 000000000..849d87872 --- /dev/null +++ b/tests/regtest/test_services_pay_invoice.py @@ -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, + )