websocket internal payment notifications (#1831)

* add send_payment_notification service
payment notifications are sent from multiple places with inconsistent and incomplete data
* adopt new send_payment_notification service
* add tests
This commit is contained in:
jackstar12 2023-07-26 12:08:22 +02:00 committed by GitHub
parent cf0a87582c
commit dda6c1b3c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 28 deletions

View file

@ -1,6 +1,8 @@
import asyncio
import pytest
import pytest_asyncio
from fastapi.testclient import TestClient
from httpx import AsyncClient
from lnbits.app import create_app
@ -41,6 +43,11 @@ async def client(app):
await client.aclose()
@pytest.fixture(scope="session")
def test_client(app):
return TestClient(app)
@pytest_asyncio.fixture(scope="session")
async def db():
yield Database("database")
@ -63,6 +70,12 @@ async def from_wallet(from_user):
yield wallet
@pytest.fixture
def from_wallet_ws(from_wallet, test_client):
with test_client.websocket_connect(f"/api/v1/ws/{from_wallet.id}") as ws:
yield ws
@pytest_asyncio.fixture(scope="session")
async def to_user():
user = await create_account()
@ -80,6 +93,12 @@ async def to_wallet(to_user):
yield wallet
@pytest.fixture
def to_wallet_ws(to_wallet, test_client):
with test_client.websocket_connect(f"/api/v1/ws/{to_wallet.id}") as ws:
yield ws
@pytest_asyncio.fixture(scope="session")
async def inkey_headers_from(from_wallet):
wallet = from_wallet

View file

@ -113,14 +113,28 @@ async def test_create_invoice_custom_expiry(client, inkey_headers_to):
# check POST /api/v1/payments: make payment
@pytest.mark.asyncio
async def test_pay_invoice(client, invoice, adminkey_headers_from):
async def test_pay_invoice(
client, from_wallet_ws, to_wallet_ws, invoice, adminkey_headers_from
):
data = {"out": True, "bolt11": invoice["payment_request"]}
response = await client.post(
"/api/v1/payments", json=data, headers=adminkey_headers_from
)
assert response.status_code < 300
assert len(response.json()["payment_hash"]) == 64
assert len(response.json()["checking_id"]) > 0
invoice = response.json()
assert len(invoice["payment_hash"]) == 64
assert len(invoice["checking_id"]) > 0
data = from_wallet_ws.receive_json()
assert "wallet_balance" in data
payment = Payment(**data["payment"])
assert payment.payment_hash == invoice["payment_hash"]
# websocket from to_wallet cant be tested before https://github.com/lnbits/lnbits/pull/1793
# data = to_wallet_ws.receive_json()
# assert "wallet_balance" in data
# payment = Payment(**data["payment"])
# assert payment.payment_hash == invoice["payment_hash"]
# check GET /api/v1/payments/<hash>: payment status
@ -330,7 +344,7 @@ async def get_node_balance_sats():
@pytest.mark.asyncio
@pytest.mark.skipif(is_fake, reason="this only works in regtest")
async def test_pay_real_invoice(
client, real_invoice, adminkey_headers_from, inkey_headers_from
client, real_invoice, adminkey_headers_from, inkey_headers_from, from_wallet_ws
):
prev_balance = await get_node_balance_sats()
response = await client.post(
@ -341,6 +355,11 @@ async def test_pay_real_invoice(
assert len(invoice["payment_hash"]) == 64
assert len(invoice["checking_id"]) > 0
data = from_wallet_ws.receive_json()
assert "wallet_balance" in data
payment = Payment(**data["payment"])
assert payment.payment_hash == invoice["payment_hash"]
# check the payment status
response = await api_payment(
invoice["payment_hash"], inkey_headers_from["X-Api-Key"]