TEST: add hold invoices to test helpers and a basic test (#1838)

* add hold invoices and basic test

* run request in parallel

* create_task

* dont cancel it

---------

Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com>
This commit is contained in:
dni ⚡ 2023-07-26 14:18:14 +02:00 committed by GitHub
parent dda6c1b3c1
commit 7f0c7138af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 2 deletions

View file

@ -11,7 +11,12 @@ from lnbits.core.crud import create_account, create_wallet
from lnbits.core.views.api import CreateInvoiceData, api_payments_create_invoice
from lnbits.db import Database
from lnbits.settings import settings
from tests.helpers import credit_wallet, get_random_invoice_data, get_real_invoice
from tests.helpers import (
credit_wallet,
get_hold_invoice,
get_random_invoice_data,
get_real_invoice,
)
@pytest_asyncio.fixture(scope="session")
@ -149,3 +154,10 @@ async def real_invoice():
invoice = get_real_invoice(100)
yield {"bolt11": invoice["payment_request"]}
del invoice
@pytest_asyncio.fixture(scope="function")
async def hold_invoice():
invoice = get_hold_invoice(100)
yield invoice
del invoice

View file

@ -13,7 +13,12 @@ from lnbits.db import DB_TYPE, SQLITE
from lnbits.wallets import get_wallet_class
from tests.conftest import CreateInvoiceData, api_payments_create_invoice
from ...helpers import get_random_invoice_data, is_fake, pay_real_invoice
from ...helpers import (
get_random_invoice_data,
is_fake,
pay_real_invoice,
settle_invoice,
)
WALLET = get_wallet_class()
@ -459,6 +464,30 @@ async def test_pay_real_invoice_set_pending_and_check_state(
assert payment_not_pending.pending is False
@pytest.mark.asyncio
@pytest.mark.skipif(is_fake, reason="this only works in regtest")
async def test_pay_hold_invoice(client, hold_invoice, adminkey_headers_from):
preimage, invoice = hold_invoice
task = asyncio.create_task(
client.post(
"/api/v1/payments",
json={"bolt11": invoice["payment_request"]},
headers=adminkey_headers_from,
)
)
await asyncio.sleep(1)
# TODO: Proper test calle :)
# settle hold invoice
settle_invoice(preimage)
response = await task
assert response.status_code < 300
# check if paid
# randomly cancel invoice
# cancel_invoice(invoice["payment_hash"])
@pytest.mark.asyncio
@pytest.mark.skipif(is_fake, reason="this only works in regtest")
async def test_receive_real_invoice_set_pending_and_check_state(

View file

@ -1,9 +1,11 @@
import hashlib
import json
import os
import random
import secrets
import string
from subprocess import PIPE, Popen, run
from typing import Tuple
from lnbits.core.crud import create_payment
from lnbits.wallets import get_wallet_class, set_wallet_class
@ -62,6 +64,21 @@ def run_cmd_json(cmd: str) -> dict:
return json.loads(run_cmd(cmd))
def get_hold_invoice(sats: int) -> Tuple[str, dict]:
preimage = os.urandom(32)
preimage_hash = hashlib.sha256(preimage).hexdigest()
json = run_cmd_json(f"{docker_lightning_cli} addholdinvoice {preimage_hash} {sats}")
return preimage.hex(), json
def settle_invoice(preimage: str) -> dict:
return run_cmd_json(f"{docker_lightning_cli} settleinvoice {preimage}")
def cancel_invoice(preimage_hash: str) -> dict:
return run_cmd_json(f"{docker_lightning_cli} cancelinvoice {preimage_hash}")
def get_real_invoice(sats: int) -> dict:
return run_cmd_json(f"{docker_lightning_cli} addinvoice {sats}")