From 7f0c7138af32cba9eb70b2b67bb933854c6f0f2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Wed, 26 Jul 2023 14:18:14 +0200 Subject: [PATCH] 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> --- tests/conftest.py | 14 +++++++++++++- tests/core/views/test_api.py | 31 ++++++++++++++++++++++++++++++- tests/helpers.py | 17 +++++++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 2e6a28be3..6793edeb8 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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 diff --git a/tests/core/views/test_api.py b/tests/core/views/test_api.py index 57250cdb1..5efe2b508 100644 --- a/tests/core/views/test_api.py +++ b/tests/core/views/test_api.py @@ -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( diff --git a/tests/helpers.py b/tests/helpers.py index 342778fc1..767a03bdf 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -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}")