2021-11-17 16:53:32 +00:00
|
|
|
import asyncio
|
2022-08-03 12:10:32 +00:00
|
|
|
from typing import Tuple
|
2022-07-07 16:29:26 +00:00
|
|
|
|
2022-08-03 12:10:32 +00:00
|
|
|
import pytest_asyncio
|
2021-11-17 16:53:32 +00:00
|
|
|
from httpx import AsyncClient
|
2022-08-03 12:10:32 +00:00
|
|
|
|
2020-09-05 06:00:44 +00:00
|
|
|
from lnbits.app import create_app
|
2021-11-17 16:53:32 +00:00
|
|
|
from lnbits.commands import migrate_databases
|
2022-06-26 22:11:46 +00:00
|
|
|
from lnbits.core.crud import create_account, create_wallet, get_wallet
|
2022-08-03 12:10:32 +00:00
|
|
|
from lnbits.core.models import BalanceCheck, Payment, User, Wallet
|
|
|
|
from lnbits.core.views.api import CreateInvoiceData, api_payments_create_invoice
|
2022-06-26 22:11:46 +00:00
|
|
|
from lnbits.db import Database
|
2022-10-03 14:46:56 +00:00
|
|
|
from lnbits.settings import settings
|
2022-08-03 12:10:32 +00:00
|
|
|
from tests.helpers import credit_wallet, get_random_invoice_data
|
2022-06-26 22:11:46 +00:00
|
|
|
|
|
|
|
|
2022-07-07 16:29:26 +00:00
|
|
|
@pytest_asyncio.fixture(scope="session")
|
2022-06-26 22:11:46 +00:00
|
|
|
def event_loop():
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
yield loop
|
|
|
|
loop.close()
|
|
|
|
|
2020-04-27 17:01:53 +00:00
|
|
|
|
2021-11-17 16:53:32 +00:00
|
|
|
# use session scope to run once before and once after all tests
|
2022-07-07 16:29:26 +00:00
|
|
|
@pytest_asyncio.fixture(scope="session")
|
2022-06-26 22:11:46 +00:00
|
|
|
def app(event_loop):
|
2020-09-05 06:00:44 +00:00
|
|
|
app = create_app()
|
2022-06-26 22:11:46 +00:00
|
|
|
# use redefined version of the event loop for scope="session"
|
|
|
|
# loop = asyncio.get_event_loop()
|
|
|
|
loop = event_loop
|
2022-10-03 21:41:04 +00:00
|
|
|
loop.run_until_complete(migrate_databases())
|
2021-11-17 16:53:32 +00:00
|
|
|
yield app
|
2022-06-26 22:11:46 +00:00
|
|
|
# # get the current event loop and gracefully stop any running tasks
|
|
|
|
# loop = event_loop
|
2021-11-17 16:53:32 +00:00
|
|
|
loop.run_until_complete(loop.shutdown_asyncgens())
|
2022-06-26 22:11:46 +00:00
|
|
|
# loop.close()
|
2020-04-27 17:01:53 +00:00
|
|
|
|
2022-06-01 12:53:05 +00:00
|
|
|
|
2022-07-07 16:29:26 +00:00
|
|
|
@pytest_asyncio.fixture(scope="session")
|
2021-11-17 16:53:32 +00:00
|
|
|
async def client(app):
|
2022-10-03 14:46:56 +00:00
|
|
|
client = AsyncClient(app=app, base_url=f"http://{settings.host}:{settings.port}")
|
2021-11-17 16:53:32 +00:00
|
|
|
yield client
|
|
|
|
await client.aclose()
|
2022-06-26 22:11:46 +00:00
|
|
|
|
|
|
|
|
2022-07-07 16:29:26 +00:00
|
|
|
@pytest_asyncio.fixture(scope="session")
|
2022-06-26 22:11:46 +00:00
|
|
|
async def db():
|
|
|
|
yield Database("database")
|
|
|
|
|
|
|
|
|
2022-07-07 16:29:26 +00:00
|
|
|
@pytest_asyncio.fixture(scope="session")
|
2022-07-23 08:39:58 +00:00
|
|
|
async def from_user():
|
2022-06-26 22:11:46 +00:00
|
|
|
user = await create_account()
|
2022-07-23 08:39:58 +00:00
|
|
|
yield user
|
|
|
|
|
|
|
|
|
|
|
|
@pytest_asyncio.fixture(scope="session")
|
|
|
|
async def from_wallet(from_user):
|
|
|
|
user = from_user
|
2022-06-26 22:11:46 +00:00
|
|
|
wallet = await create_wallet(user_id=user.id, wallet_name="test_wallet_from")
|
|
|
|
await credit_wallet(
|
|
|
|
wallet_id=wallet.id,
|
2022-11-22 09:37:13 +00:00
|
|
|
amount=999999999,
|
2022-06-26 22:11:46 +00:00
|
|
|
)
|
2022-07-23 08:39:58 +00:00
|
|
|
yield wallet
|
2022-06-26 22:11:46 +00:00
|
|
|
|
|
|
|
|
2022-07-07 16:29:26 +00:00
|
|
|
@pytest_asyncio.fixture(scope="session")
|
2022-07-23 08:39:58 +00:00
|
|
|
async def to_user():
|
2022-06-26 22:11:46 +00:00
|
|
|
user = await create_account()
|
2022-07-23 08:39:58 +00:00
|
|
|
yield user
|
|
|
|
|
|
|
|
|
|
|
|
@pytest_asyncio.fixture(scope="session")
|
|
|
|
async def to_wallet(to_user):
|
|
|
|
user = to_user
|
2022-06-26 22:11:46 +00:00
|
|
|
wallet = await create_wallet(user_id=user.id, wallet_name="test_wallet_to")
|
|
|
|
await credit_wallet(
|
|
|
|
wallet_id=wallet.id,
|
2022-11-22 09:37:13 +00:00
|
|
|
amount=999999999,
|
2022-06-26 22:11:46 +00:00
|
|
|
)
|
2022-07-23 08:39:58 +00:00
|
|
|
yield wallet
|
2022-06-26 22:11:46 +00:00
|
|
|
|
|
|
|
|
2022-07-07 16:29:26 +00:00
|
|
|
@pytest_asyncio.fixture(scope="session")
|
2022-07-23 08:39:58 +00:00
|
|
|
async def inkey_headers_from(from_wallet):
|
|
|
|
wallet = from_wallet
|
2022-06-26 22:11:46 +00:00
|
|
|
yield {
|
|
|
|
"X-Api-Key": wallet.inkey,
|
|
|
|
"Content-type": "application/json",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-07 16:29:26 +00:00
|
|
|
@pytest_asyncio.fixture(scope="session")
|
2022-07-23 08:39:58 +00:00
|
|
|
async def adminkey_headers_from(from_wallet):
|
|
|
|
wallet = from_wallet
|
2022-06-26 22:11:46 +00:00
|
|
|
yield {
|
|
|
|
"X-Api-Key": wallet.adminkey,
|
|
|
|
"Content-type": "application/json",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-07 16:29:26 +00:00
|
|
|
@pytest_asyncio.fixture(scope="session")
|
2022-07-23 08:39:58 +00:00
|
|
|
async def inkey_headers_to(to_wallet):
|
|
|
|
wallet = to_wallet
|
2022-06-26 22:11:46 +00:00
|
|
|
yield {
|
|
|
|
"X-Api-Key": wallet.inkey,
|
|
|
|
"Content-type": "application/json",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-07 16:29:26 +00:00
|
|
|
@pytest_asyncio.fixture(scope="session")
|
2022-07-23 08:39:58 +00:00
|
|
|
async def adminkey_headers_to(to_wallet):
|
|
|
|
wallet = to_wallet
|
2022-06-26 22:11:46 +00:00
|
|
|
yield {
|
|
|
|
"X-Api-Key": wallet.adminkey,
|
|
|
|
"Content-type": "application/json",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-07 16:29:26 +00:00
|
|
|
@pytest_asyncio.fixture(scope="session")
|
2022-07-23 08:39:58 +00:00
|
|
|
async def invoice(to_wallet):
|
2022-06-26 22:11:46 +00:00
|
|
|
data = await get_random_invoice_data()
|
|
|
|
invoiceData = CreateInvoiceData(**data)
|
2022-08-01 14:12:25 +00:00
|
|
|
invoice = await api_payments_create_invoice(invoiceData, to_wallet)
|
2022-06-26 22:11:46 +00:00
|
|
|
yield invoice
|
|
|
|
del invoice
|