test: additional cases for internal payments (#3155)
Co-authored-by: dni ⚡ <office@dnilabs.com>
This commit is contained in:
parent
3b350858c7
commit
39e4fa724a
3 changed files with 122 additions and 7 deletions
|
|
@ -5,8 +5,12 @@ import pytest
|
|||
|
||||
from lnbits import bolt11
|
||||
from lnbits.core.crud import get_standalone_payment, update_payment
|
||||
from lnbits.core.crud.wallets import create_wallet, get_wallet
|
||||
from lnbits.core.models import CreateInvoice, Payment, PaymentState
|
||||
from lnbits.core.services import fee_reserve_total, get_balance_delta
|
||||
from lnbits.core.services.payments import pay_invoice, update_wallet_balance
|
||||
from lnbits.core.services.users import create_user_account
|
||||
from lnbits.exceptions import PaymentError
|
||||
from lnbits.tasks import create_task, wait_for_paid_invoices
|
||||
from lnbits.wallets import get_funding_source
|
||||
|
||||
|
|
@ -153,6 +157,39 @@ async def test_pay_real_invoice_set_pending_and_check_state(
|
|||
assert payment.success
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
@pytest.mark.skipif(is_fake, reason="this only works in regtest")
|
||||
async def test_pay_real_invoices_in_parallel():
|
||||
user = await create_user_account()
|
||||
wallet = await create_wallet(user_id=user.id)
|
||||
|
||||
# more to cover routing feems
|
||||
await update_wallet_balance(wallet, 1100)
|
||||
|
||||
# these must be external invoices
|
||||
real_invoice_one = get_real_invoice(1000)
|
||||
real_invoice_two = get_real_invoice(1000)
|
||||
|
||||
async def pay_first():
|
||||
return await pay_invoice(
|
||||
wallet_id=wallet.id,
|
||||
payment_request=real_invoice_one["payment_request"],
|
||||
)
|
||||
|
||||
async def pay_second():
|
||||
return await pay_invoice(
|
||||
wallet_id=wallet.id,
|
||||
payment_request=real_invoice_two["payment_request"],
|
||||
)
|
||||
|
||||
with pytest.raises(PaymentError, match="Insufficient balance."):
|
||||
await asyncio.gather(pay_first(), pay_second())
|
||||
|
||||
wallet_after = await get_wallet(wallet.id)
|
||||
assert wallet_after
|
||||
assert 0 <= wallet_after.balance <= 100, "One payment should be deducted."
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
@pytest.mark.skipif(is_fake, reason="this only works in regtest")
|
||||
async def test_pay_hold_invoice_check_pending(
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ from lnbits.core.crud import create_wallet, get_standalone_payment, get_wallet
|
|||
from lnbits.core.crud.payments import get_payment, get_payments_paginated
|
||||
from lnbits.core.models import Payment, PaymentState, Wallet
|
||||
from lnbits.core.services import create_invoice, create_user_account, pay_invoice
|
||||
from lnbits.core.services.payments import update_wallet_balance
|
||||
from lnbits.exceptions import InvoiceError, PaymentError
|
||||
from lnbits.settings import Settings
|
||||
from lnbits.tasks import (
|
||||
|
|
@ -114,6 +115,62 @@ async def test_pay_twice(to_wallet: Wallet):
|
|||
)
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_pay_twice_fast():
|
||||
user = await create_user_account()
|
||||
wallet_one = await create_wallet(user_id=user.id)
|
||||
wallet_two = await create_wallet(user_id=user.id)
|
||||
|
||||
await update_wallet_balance(wallet_one, 1000)
|
||||
payment_a = await create_invoice(wallet_id=wallet_two.id, amount=1000, memo="AAA")
|
||||
payment_b = await create_invoice(wallet_id=wallet_two.id, amount=1000, memo="BBB")
|
||||
|
||||
async def pay_first():
|
||||
return await pay_invoice(
|
||||
wallet_id=wallet_one.id,
|
||||
payment_request=payment_a.bolt11,
|
||||
)
|
||||
|
||||
async def pay_second():
|
||||
return await pay_invoice(
|
||||
wallet_id=wallet_one.id,
|
||||
payment_request=payment_b.bolt11,
|
||||
)
|
||||
|
||||
with pytest.raises(PaymentError, match="Insufficient balance."):
|
||||
await asyncio.gather(pay_first(), pay_second())
|
||||
|
||||
wallet_one_after = await get_wallet(wallet_one.id)
|
||||
assert wallet_one_after
|
||||
assert wallet_one_after.balance == 0, "One payment should be deducted."
|
||||
|
||||
wallet_two_after = await get_wallet(wallet_two.id)
|
||||
assert wallet_two_after
|
||||
assert wallet_two_after.balance == 1000, "One payment received."
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_pay_twice_fast_same_invoice(to_wallet: Wallet):
|
||||
payment = await create_invoice(
|
||||
wallet_id=to_wallet.id, amount=3, memo="Twice fast same invoice"
|
||||
)
|
||||
|
||||
async def pay_first():
|
||||
return await pay_invoice(
|
||||
wallet_id=to_wallet.id,
|
||||
payment_request=payment.bolt11,
|
||||
)
|
||||
|
||||
async def pay_second():
|
||||
return await pay_invoice(
|
||||
wallet_id=to_wallet.id,
|
||||
payment_request=payment.bolt11,
|
||||
)
|
||||
|
||||
with pytest.raises(PaymentError, match="Payment already paid."):
|
||||
await asyncio.gather(pay_first(), pay_second())
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_fake_wallet_pay_external(
|
||||
to_wallet: Wallet, external_funding_source: FakeWallet
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue