2021-11-17 10:53:32 -06:00
|
|
|
import asyncio
|
2024-10-29 09:58:22 +01:00
|
|
|
from datetime import datetime, timezone
|
|
|
|
from uuid import uuid4
|
2023-08-28 11:59:56 +02:00
|
|
|
|
2023-07-26 12:08:22 +02:00
|
|
|
import pytest
|
2024-12-11 10:39:28 +01:00
|
|
|
import uvloop
|
2024-10-29 09:58:22 +01:00
|
|
|
from asgi_lifespan import LifespanManager
|
2023-07-26 12:08:22 +02:00
|
|
|
from fastapi.testclient import TestClient
|
2024-10-01 11:07:13 +02:00
|
|
|
from httpx import ASGITransport, AsyncClient
|
2022-08-03 14:10:32 +02:00
|
|
|
|
2020-09-05 08:00:44 +02:00
|
|
|
from lnbits.app import create_app
|
2023-09-12 14:38:30 +02:00
|
|
|
from lnbits.core.crud import (
|
|
|
|
create_wallet,
|
2024-10-29 09:58:22 +01:00
|
|
|
delete_account,
|
|
|
|
get_account,
|
2024-09-30 14:53:38 +03:00
|
|
|
get_account_by_username,
|
2024-10-29 09:58:22 +01:00
|
|
|
get_payment,
|
|
|
|
get_user_from_account,
|
|
|
|
update_payment,
|
2023-09-12 14:38:30 +02:00
|
|
|
)
|
2024-10-29 09:58:22 +01:00
|
|
|
from lnbits.core.models import Account, CreateInvoice, PaymentState, User
|
2024-09-30 14:53:38 +03:00
|
|
|
from lnbits.core.services import create_user_account, update_wallet_balance
|
2024-12-11 10:39:28 +01:00
|
|
|
from lnbits.core.views.payment_api import _api_payments_create_invoice
|
2023-09-12 14:38:30 +02:00
|
|
|
from lnbits.db import DB_TYPE, SQLITE, Database
|
2024-10-29 09:58:22 +01:00
|
|
|
from lnbits.settings import AuthMethods, Settings
|
|
|
|
from lnbits.settings import settings as lnbits_settings
|
2024-12-11 10:39:28 +01:00
|
|
|
from lnbits.wallets.fake import FakeWallet
|
2023-09-12 12:02:01 +02:00
|
|
|
from tests.helpers import (
|
|
|
|
get_random_invoice_data,
|
|
|
|
)
|
2022-06-27 00:11:46 +02:00
|
|
|
|
2024-12-11 10:39:28 +01:00
|
|
|
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def anyio_backend():
|
|
|
|
return "asyncio"
|
2023-08-16 19:19:21 +02:00
|
|
|
|
2024-12-11 10:39:28 +01:00
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
2024-10-29 09:58:22 +01:00
|
|
|
def settings():
|
|
|
|
# override settings for tests
|
|
|
|
lnbits_settings.lnbits_admin_extensions = []
|
|
|
|
lnbits_settings.lnbits_data_folder = "./tests/data"
|
|
|
|
lnbits_settings.lnbits_admin_ui = True
|
|
|
|
lnbits_settings.lnbits_extensions_default_install = []
|
|
|
|
lnbits_settings.lnbits_extensions_deactivate_all = True
|
2022-06-27 00:11:46 +02:00
|
|
|
|
2024-10-29 09:58:22 +01:00
|
|
|
yield lnbits_settings
|
2024-10-01 17:17:42 +03:00
|
|
|
|
|
|
|
|
2024-10-29 09:58:22 +01:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def run_before_and_after_tests(settings: Settings):
|
|
|
|
"""Fixture to execute asserts before and after a test is run"""
|
|
|
|
_settings_cleanup(settings)
|
2024-10-01 17:17:42 +03:00
|
|
|
yield # this is where the testing happens
|
2024-10-29 09:58:22 +01:00
|
|
|
_settings_cleanup(settings)
|
2024-10-01 17:17:42 +03:00
|
|
|
|
|
|
|
|
2021-11-17 10:53:32 -06:00
|
|
|
# use session scope to run once before and once after all tests
|
2024-12-11 10:39:28 +01:00
|
|
|
@pytest.fixture(scope="session")
|
2024-10-29 09:58:22 +01:00
|
|
|
async def app(settings: Settings):
|
2020-09-05 08:00:44 +02:00
|
|
|
app = create_app()
|
2024-04-05 07:05:26 +02:00
|
|
|
async with LifespanManager(app) as manager:
|
|
|
|
settings.first_install = False
|
|
|
|
yield manager.app
|
2020-04-27 19:01:53 +02:00
|
|
|
|
2022-06-01 14:53:05 +02:00
|
|
|
|
2024-12-11 10:39:28 +01:00
|
|
|
@pytest.fixture(scope="session")
|
2024-10-29 09:58:22 +01:00
|
|
|
async def client(app, settings: Settings):
|
2024-04-05 07:05:26 +02:00
|
|
|
url = f"http://{settings.host}:{settings.port}"
|
2024-10-01 11:07:13 +02:00
|
|
|
async with AsyncClient(transport=ASGITransport(app=app), base_url=url) as client:
|
2024-04-05 07:05:26 +02:00
|
|
|
yield client
|
2022-06-27 00:11:46 +02:00
|
|
|
|
|
|
|
|
2024-10-01 11:07:13 +02:00
|
|
|
# function scope
|
2024-12-11 10:39:28 +01:00
|
|
|
@pytest.fixture(scope="function")
|
2024-10-29 09:58:22 +01:00
|
|
|
async def http_client(app, settings: Settings):
|
2024-09-30 14:53:38 +03:00
|
|
|
url = f"http://{settings.host}:{settings.port}"
|
2024-10-01 11:07:13 +02:00
|
|
|
|
|
|
|
async with AsyncClient(transport=ASGITransport(app=app), base_url=url) as client:
|
2024-09-30 14:53:38 +03:00
|
|
|
yield client
|
|
|
|
|
|
|
|
|
2023-07-26 12:08:22 +02:00
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def test_client(app):
|
|
|
|
return TestClient(app)
|
|
|
|
|
|
|
|
|
2024-12-11 10:39:28 +01:00
|
|
|
@pytest.fixture(scope="session")
|
2022-06-27 00:11:46 +02:00
|
|
|
async def db():
|
|
|
|
yield Database("database")
|
|
|
|
|
|
|
|
|
2024-12-11 10:39:28 +01:00
|
|
|
@pytest.fixture(scope="session")
|
2024-09-30 14:53:38 +03:00
|
|
|
async def user_alan():
|
2024-10-29 09:58:22 +01:00
|
|
|
account = await get_account_by_username("alan")
|
|
|
|
if account:
|
|
|
|
await delete_account(account.id)
|
|
|
|
|
|
|
|
account = Account(
|
|
|
|
id=uuid4().hex,
|
|
|
|
email="alan@lnbits.com",
|
|
|
|
username="alan",
|
|
|
|
)
|
|
|
|
account.hash_password("secret1234")
|
|
|
|
user = await create_user_account(account)
|
|
|
|
|
2024-09-30 14:53:38 +03:00
|
|
|
yield user
|
|
|
|
|
|
|
|
|
2024-12-11 10:39:28 +01:00
|
|
|
@pytest.fixture(scope="session")
|
2022-07-23 10:39:58 +02:00
|
|
|
async def from_user():
|
2024-10-29 09:58:22 +01:00
|
|
|
user = await create_user_account()
|
2022-07-23 10:39:58 +02:00
|
|
|
yield user
|
|
|
|
|
|
|
|
|
2024-12-11 10:39:28 +01:00
|
|
|
@pytest.fixture(scope="session")
|
2022-07-23 10:39:58 +02:00
|
|
|
async def from_wallet(from_user):
|
|
|
|
user = from_user
|
2024-10-29 09:58:22 +01:00
|
|
|
|
2022-06-27 00:11:46 +02:00
|
|
|
wallet = await create_wallet(user_id=user.id, wallet_name="test_wallet_from")
|
2023-08-07 21:49:37 +02:00
|
|
|
await update_wallet_balance(
|
2022-06-27 00:11:46 +02:00
|
|
|
wallet_id=wallet.id,
|
2022-11-22 10:37:13 +01:00
|
|
|
amount=999999999,
|
2022-06-27 00:11:46 +02:00
|
|
|
)
|
2022-07-23 10:39:58 +02:00
|
|
|
yield wallet
|
2022-06-27 00:11:46 +02:00
|
|
|
|
|
|
|
|
2024-12-11 10:39:28 +01:00
|
|
|
@pytest.fixture(scope="session")
|
2024-10-29 09:58:22 +01:00
|
|
|
async def to_wallet_pagination_tests(to_user):
|
|
|
|
user = to_user
|
|
|
|
wallet = await create_wallet(
|
|
|
|
user_id=user.id, wallet_name="test_wallet_to_pagination_tests"
|
|
|
|
)
|
|
|
|
yield wallet
|
|
|
|
|
|
|
|
|
2024-12-11 10:39:28 +01:00
|
|
|
@pytest.fixture
|
2023-08-28 11:59:56 +02:00
|
|
|
async def from_wallet_ws(from_wallet, test_client):
|
|
|
|
# wait a bit in order to avoid receiving topup notification
|
|
|
|
await asyncio.sleep(0.1)
|
2024-05-28 14:07:33 +03:00
|
|
|
with test_client.websocket_connect(f"/api/v1/ws/{from_wallet.inkey}") as ws:
|
2023-07-26 12:08:22 +02:00
|
|
|
yield ws
|
|
|
|
|
|
|
|
|
2024-12-11 10:39:28 +01:00
|
|
|
@pytest.fixture(scope="session")
|
2022-07-23 10:39:58 +02:00
|
|
|
async def to_user():
|
2024-10-29 09:58:22 +01:00
|
|
|
user = await create_user_account()
|
2022-07-23 10:39:58 +02:00
|
|
|
yield user
|
|
|
|
|
|
|
|
|
2023-09-25 15:04:44 +02:00
|
|
|
@pytest.fixture()
|
2024-10-29 09:58:22 +01:00
|
|
|
def from_super_user(from_user: User, settings: Settings):
|
2023-09-25 15:04:44 +02:00
|
|
|
prev = settings.super_user
|
|
|
|
settings.super_user = from_user.id
|
|
|
|
yield from_user
|
|
|
|
settings.super_user = prev
|
|
|
|
|
|
|
|
|
2024-12-11 10:39:28 +01:00
|
|
|
@pytest.fixture(scope="session")
|
2024-10-29 09:58:22 +01:00
|
|
|
async def superuser(settings: Settings):
|
|
|
|
account = await get_account(settings.super_user)
|
|
|
|
assert account, "Superuser not found"
|
|
|
|
user = await get_user_from_account(account)
|
2023-09-12 11:59:32 +02:00
|
|
|
yield user
|
|
|
|
|
|
|
|
|
2024-12-11 10:39:28 +01:00
|
|
|
@pytest.fixture(scope="session")
|
2022-07-23 10:39:58 +02:00
|
|
|
async def to_wallet(to_user):
|
|
|
|
user = to_user
|
2022-06-27 00:11:46 +02:00
|
|
|
wallet = await create_wallet(user_id=user.id, wallet_name="test_wallet_to")
|
2023-08-07 21:49:37 +02:00
|
|
|
await update_wallet_balance(
|
2022-06-27 00:11:46 +02:00
|
|
|
wallet_id=wallet.id,
|
2022-11-22 10:37:13 +01:00
|
|
|
amount=999999999,
|
2022-06-27 00:11:46 +02:00
|
|
|
)
|
2022-07-23 10:39:58 +02:00
|
|
|
yield wallet
|
2022-06-27 00:11:46 +02:00
|
|
|
|
|
|
|
|
2024-12-11 10:39:28 +01:00
|
|
|
@pytest.fixture(scope="session")
|
2024-10-29 09:58:22 +01:00
|
|
|
async def to_fresh_wallet(to_user):
|
|
|
|
user = to_user
|
|
|
|
wallet = await create_wallet(user_id=user.id, wallet_name="test_wallet_to_fresh")
|
|
|
|
yield wallet
|
|
|
|
|
|
|
|
|
2024-12-11 10:39:28 +01:00
|
|
|
@pytest.fixture
|
2023-08-28 11:59:56 +02:00
|
|
|
async def to_wallet_ws(to_wallet, test_client):
|
|
|
|
# wait a bit in order to avoid receiving topup notification
|
|
|
|
await asyncio.sleep(0.1)
|
2024-05-28 14:07:33 +03:00
|
|
|
with test_client.websocket_connect(f"/api/v1/ws/{to_wallet.inkey}") as ws:
|
2023-07-26 12:08:22 +02:00
|
|
|
yield ws
|
|
|
|
|
|
|
|
|
2024-12-11 10:39:28 +01:00
|
|
|
@pytest.fixture(scope="session")
|
2024-10-29 09:58:22 +01:00
|
|
|
async def inkey_fresh_headers_to(to_fresh_wallet):
|
|
|
|
wallet = to_fresh_wallet
|
|
|
|
yield {
|
|
|
|
"X-Api-Key": wallet.inkey,
|
|
|
|
"Content-type": "application/json",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-12-11 10:39:28 +01:00
|
|
|
@pytest.fixture(scope="session")
|
2022-07-23 10:39:58 +02:00
|
|
|
async def inkey_headers_from(from_wallet):
|
|
|
|
wallet = from_wallet
|
2022-06-27 00:11:46 +02:00
|
|
|
yield {
|
|
|
|
"X-Api-Key": wallet.inkey,
|
|
|
|
"Content-type": "application/json",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-12-11 10:39:28 +01:00
|
|
|
@pytest.fixture(scope="session")
|
2022-07-23 10:39:58 +02:00
|
|
|
async def adminkey_headers_from(from_wallet):
|
|
|
|
wallet = from_wallet
|
2022-06-27 00:11:46 +02:00
|
|
|
yield {
|
|
|
|
"X-Api-Key": wallet.adminkey,
|
|
|
|
"Content-type": "application/json",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-12-11 10:39:28 +01:00
|
|
|
@pytest.fixture(scope="session")
|
2022-07-23 10:39:58 +02:00
|
|
|
async def inkey_headers_to(to_wallet):
|
|
|
|
wallet = to_wallet
|
2022-06-27 00:11:46 +02:00
|
|
|
yield {
|
|
|
|
"X-Api-Key": wallet.inkey,
|
|
|
|
"Content-type": "application/json",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-12-11 10:39:28 +01:00
|
|
|
@pytest.fixture(scope="session")
|
2022-07-23 10:39:58 +02:00
|
|
|
async def adminkey_headers_to(to_wallet):
|
|
|
|
wallet = to_wallet
|
2022-06-27 00:11:46 +02:00
|
|
|
yield {
|
|
|
|
"X-Api-Key": wallet.adminkey,
|
|
|
|
"Content-type": "application/json",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-12-11 10:39:28 +01:00
|
|
|
@pytest.fixture(scope="session")
|
2022-07-23 10:39:58 +02:00
|
|
|
async def invoice(to_wallet):
|
2022-06-27 00:11:46 +02:00
|
|
|
data = await get_random_invoice_data()
|
2024-04-15 09:02:21 +02:00
|
|
|
invoice_data = CreateInvoice(**data)
|
2024-10-29 09:58:22 +01:00
|
|
|
invoice = await _api_payments_create_invoice(invoice_data, to_wallet)
|
2022-06-27 00:11:46 +02:00
|
|
|
yield invoice
|
|
|
|
del invoice
|
2023-02-01 16:54:07 +01:00
|
|
|
|
|
|
|
|
2024-12-11 10:39:28 +01:00
|
|
|
@pytest.fixture(scope="function")
|
2024-10-17 11:27:36 +03:00
|
|
|
async def external_funding_source():
|
|
|
|
yield FakeWallet()
|
|
|
|
|
|
|
|
|
2024-12-11 10:39:28 +01:00
|
|
|
@pytest.fixture(scope="session")
|
2024-10-29 09:58:22 +01:00
|
|
|
async def fake_payments(client, inkey_fresh_headers_to):
|
|
|
|
|
|
|
|
ts = datetime.now(timezone.utc).timestamp()
|
|
|
|
|
2023-09-12 14:38:30 +02:00
|
|
|
# Because sqlite only stores timestamps with milliseconds
|
|
|
|
# we have to wait a second to ensure a different timestamp than previous invoices
|
|
|
|
if DB_TYPE == SQLITE:
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
|
|
|
|
fake_data = [
|
|
|
|
CreateInvoice(amount=10, memo="aaaa", out=False),
|
|
|
|
CreateInvoice(amount=100, memo="bbbb", out=False),
|
|
|
|
CreateInvoice(amount=1000, memo="aabb", out=False),
|
|
|
|
]
|
|
|
|
|
|
|
|
for invoice in fake_data:
|
|
|
|
response = await client.post(
|
2024-10-29 09:58:22 +01:00
|
|
|
"/api/v1/payments", headers=inkey_fresh_headers_to, json=invoice.dict()
|
2023-09-12 14:38:30 +02:00
|
|
|
)
|
|
|
|
assert response.is_success
|
2024-09-24 10:56:03 +02:00
|
|
|
data = response.json()
|
|
|
|
assert data["checking_id"]
|
2024-10-29 09:58:22 +01:00
|
|
|
payment = await get_payment(data["checking_id"])
|
|
|
|
payment.status = PaymentState.SUCCESS
|
|
|
|
await update_payment(payment)
|
2023-09-12 14:38:30 +02:00
|
|
|
|
2024-10-29 09:58:22 +01:00
|
|
|
params = {
|
|
|
|
"created_at[ge]": ts,
|
|
|
|
"created_at[le]": datetime.now(timezone.utc).timestamp(),
|
|
|
|
}
|
2023-09-12 14:38:30 +02:00
|
|
|
return fake_data, params
|
2024-10-29 09:58:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _settings_cleanup(settings: Settings):
|
|
|
|
settings.lnbits_allow_new_accounts = True
|
|
|
|
settings.lnbits_allowed_users = []
|
|
|
|
settings.auth_allowed_methods = AuthMethods.all()
|
|
|
|
settings.auth_credetials_update_threshold = 120
|
|
|
|
settings.lnbits_reserve_fee_percent = 1
|
|
|
|
settings.lnbits_reserve_fee_min = 2000
|
|
|
|
settings.lnbits_service_fee = 0
|
|
|
|
settings.lnbits_wallet_limit_daily_max_withdraw = 0
|
|
|
|
settings.lnbits_admin_extensions = []
|