lnbits-legend/tests/regtest/test_x_node_api.py
dni ⚡ 2940cf97c5
feat: parse nested pydantic models fetchone and fetchall + add shortcuts for insert_query and update_query into Database (#2714)
* feat: add shortcuts for insert_query and update_query into `Database`
example: await db.insert("table_name", base_model)
* remove where from argument
* chore: code clean-up
* extension manager
* lnbits-qrcode  components
* parse date from dict
* refactor: make `settings` a fixture
* chore: remove verbose key names
* fix: time column
* fix: cast balance to `int`
* extension toggle vue3
* vue3 @input migration
* fix: payment extra and payment hash
* fix dynamic fields and ext db migration
* remove shadow on cards in dark theme
* screwed up and made more css pushes to this branch
* attempt to make chip component in settings dynamic fields
* dynamic chips
* qrscanner
* clean init admin settings
* make get_user better
* add dbversion model
* remove update_payment_status/extra/details
* traces for value and assertion errors
* refactor services
* add PaymentFiatAmount
* return Payment on api endpoints
* rename to get_user_from_account
* refactor: just refactor (#2740)
* rc5
* Fix db cache (#2741)
* [refactor] split services.py (#2742)
* refactor: spit `core.py` (#2743)
* refactor: make QR more customizable
* fix: print.html
* fix: qrcode options
* fix: white shadow on dark theme
* fix: datetime wasnt parsed in dict_to_model
* add timezone for conversion
* only parse timestamp for sqlite, postgres does it
* log internal payment success
* fix: export wallet to phone QR
* Adding a customisable border theme, like gradient (#2746)
* fixed mobile scan btn
* fix test websocket
* fix get_payments tests
* dict_to_model skip none values
* preimage none instead of defaulting to 0000...
* fixup test real invoice tests
* fixed pheonixd for wss
* fix nodemanager test settings
* fix lnbits funding
* only insert extension when they dont exist

---------

Co-authored-by: Vlad Stan <stan.v.vlad@gmail.com>
Co-authored-by: Tiago Vasconcelos <talvasconcelos@gmail.com>
Co-authored-by: Arc <ben@arc.wales>
Co-authored-by: Arc <33088785+arcbtc@users.noreply.github.com>
2024-10-29 09:58:22 +01:00

177 lines
5.5 KiB
Python

import asyncio
import random
from http import HTTPStatus
import pytest
from pydantic import parse_obj_as
from lnbits import bolt11
from lnbits.nodes.base import ChannelPoint, ChannelState, NodeChannel
from tests.conftest import pytest_asyncio
from ..helpers import (
funding_source,
get_random_invoice_data,
)
from .helpers import (
get_unconnected_node_uri,
mine_blocks,
)
pytestmark = pytest.mark.skipif(
funding_source.__node_cls__ is None,
reason="Cant test if node implementation isnt available",
)
@pytest_asyncio.fixture()
async def node_client(client, from_super_user, settings):
settings.lnbits_node_ui = True
settings.lnbits_public_node_ui = False
settings.lnbits_node_ui_transactions = True
params = client.params
client.params = {"usr": from_super_user.id}
yield client
client.params = params
settings.lnbits_node_ui = False
@pytest_asyncio.fixture()
async def public_node_client(node_client, settings):
settings.lnbits_public_node_ui = True
yield node_client
settings.lnbits_public_node_ui = False
@pytest.mark.asyncio
async def test_node_info_not_found(client, from_super_user, settings):
settings.lnbits_node_ui = False
response = await client.get("/node/api/v1/info", params={"usr": from_super_user.id})
assert response.status_code == HTTPStatus.SERVICE_UNAVAILABLE
@pytest.mark.asyncio
async def test_public_node_info_not_found(node_client):
response = await node_client.get("/node/public/api/v1/info")
assert response.status_code == HTTPStatus.SERVICE_UNAVAILABLE
@pytest.mark.asyncio
async def test_public_node_info(public_node_client):
response = await public_node_client.get("/node/public/api/v1/info")
assert response.status_code == 200
@pytest.mark.asyncio
async def test_node_info(node_client):
response = await node_client.get("/node/api/v1/info")
assert response.status_code == 200
@pytest.mark.asyncio
async def test_node_invoices(inkey_headers_from, node_client):
data = await get_random_invoice_data()
response = await node_client.post(
"/api/v1/payments", json=data, headers=inkey_headers_from
)
invoice = response.json()
response = await node_client.get("/node/api/v1/invoices", params={"limit": 1})
assert response.status_code == 200
invoices = response.json()["data"]
assert len(invoices) == 1
assert invoices[0]["payment_hash"] == invoice["payment_hash"]
@pytest.mark.asyncio
async def test_node_payments(node_client, real_invoice, adminkey_headers_from):
response = await node_client.post(
"/api/v1/payments", json=real_invoice, headers=adminkey_headers_from
)
assert response.status_code < 300
response = await node_client.get("/node/api/v1/payments", params={"limit": 1})
assert response.status_code == 200
payments = response.json()["data"]
assert len(payments) == 1
assert (
payments[0]["payment_hash"]
== bolt11.decode(real_invoice["bolt11"]).payment_hash
)
@pytest.mark.asyncio
async def test_channel_management(node_client):
async def get_channels():
response = await node_client.get("/node/api/v1/channels")
assert response.status_code == 200
return parse_obj_as(list[NodeChannel], response.json())
data = await get_channels()
close = random.choice(
[channel for channel in data if channel.state == ChannelState.ACTIVE]
)
assert close, "No active channel found"
assert close.point, "No channel point found"
response = await node_client.delete(
"/node/api/v1/channels",
params={"short_id": close.short_id, **close.point.dict()},
)
assert response.status_code == 200
data = await get_channels()
assert any(
channel.point == close.point and channel.state == ChannelState.CLOSED
for channel in data
)
response = await node_client.post(
"/node/api/v1/channels",
json={
"peer_id": close.peer_id,
"funding_amount": 100000,
},
)
assert response.status_code == 200
created = ChannelPoint(**response.json())
data = await get_channels()
assert any(
channel.point == created and channel.state == ChannelState.PENDING
for channel in data
)
# mine some blocks so that the newly created channel eventually
# gets confirmed to avoid a situation where no channels are
# left for testing
mine_blocks(5)
@pytest.mark.asyncio
async def test_peer_management(node_client):
connect_uri = get_unconnected_node_uri()
peer_id = connect_uri.split("@")[0]
response = await node_client.post("/node/api/v1/peers", json={"uri": connect_uri})
assert response.status_code == 200
response = await node_client.get("/node/api/v1/peers")
assert response.status_code == 200
assert any(peer["id"] == peer_id for peer in response.json())
response = await node_client.delete(f"/node/api/v1/peers/{peer_id}")
assert response.status_code == 200
# lndrest is slow to remove the peer
await asyncio.sleep(0.3)
response = await node_client.get("/node/api/v1/peers")
assert response.status_code == 200
assert not any(peer["id"] == peer_id for peer in response.json())
response = await node_client.delete(f"/node/api/v1/peers/{peer_id}")
assert response.status_code == 400
@pytest.mark.asyncio
async def test_connect_invalid_uri(node_client):
response = await node_client.post("/node/api/v1/peers", json={"uri": "invalid"})
assert response.status_code == 400