mirror of
https://github.com/lnbits/lnbits.git
synced 2025-12-05 02:01:29 +01:00
Automated tests (#566)
* return error for wrong key * payment check use key dependency * more expressive error * re-add optional key * more tests * more * more granular * more testing * custom event_loop * tests work * fix lots of mypy errors * test_public_api * both files * remove unused import * tests * tests working * rm empty file * minimal test * set FAKE_WALLET_SECRET="ToTheMoon1" * set FAKE_WALLET_SECRET="ToTheMoon1" * trial and error * trial and error * test postgres * test postgres * test postgres * test postgres * test postgres * test postgres * test build * skip mypy
This commit is contained in:
@@ -4,26 +4,124 @@ from httpx import AsyncClient
|
||||
from lnbits.app import create_app
|
||||
from lnbits.commands import migrate_databases
|
||||
from lnbits.settings import HOST, PORT
|
||||
import tests.mocks
|
||||
|
||||
# use session scope to run once before and once after all tests
|
||||
from lnbits.core.views.api import api_payments_create_invoice, CreateInvoiceData
|
||||
|
||||
from lnbits.core.crud import create_account, create_wallet, get_wallet
|
||||
from tests.helpers import credit_wallet, get_random_invoice_data
|
||||
|
||||
from lnbits.db import Database
|
||||
from lnbits.core.models import User, Wallet, Payment, BalanceCheck
|
||||
from typing import Tuple
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def app():
|
||||
# yield and pass the app to the test
|
||||
app = create_app()
|
||||
def event_loop():
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.run_until_complete(migrate_databases())
|
||||
yield app
|
||||
# get the current event loop and gracefully stop any running tasks
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.run_until_complete(loop.shutdown_asyncgens())
|
||||
yield loop
|
||||
loop.close()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
# use session scope to run once before and once after all tests
|
||||
@pytest.fixture(scope="session")
|
||||
def app(event_loop):
|
||||
app = create_app()
|
||||
# use redefined version of the event loop for scope="session"
|
||||
# loop = asyncio.get_event_loop()
|
||||
loop = event_loop
|
||||
loop.run_until_complete(migrate_databases())
|
||||
yield app
|
||||
# # get the current event loop and gracefully stop any running tasks
|
||||
# loop = event_loop
|
||||
loop.run_until_complete(loop.shutdown_asyncgens())
|
||||
# loop.close()
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
async def client(app):
|
||||
client = AsyncClient(app=app, base_url=f"http://{HOST}:{PORT}")
|
||||
# yield and pass the client to the test
|
||||
yield client
|
||||
# close the async client after the test has finished
|
||||
await client.aclose()
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
async def db():
|
||||
yield Database("database")
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
async def from_user_wallet():
|
||||
user = await create_account()
|
||||
wallet = await create_wallet(user_id=user.id, wallet_name="test_wallet_from")
|
||||
await credit_wallet(
|
||||
wallet_id=wallet.id,
|
||||
amount=99999999,
|
||||
)
|
||||
# print("new from_user_wallet:", wallet)
|
||||
yield user, wallet
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
async def to_user_wallet():
|
||||
user = await create_account()
|
||||
wallet = await create_wallet(user_id=user.id, wallet_name="test_wallet_to")
|
||||
await credit_wallet(
|
||||
wallet_id=wallet.id,
|
||||
amount=99999999,
|
||||
)
|
||||
# print("new to_user_wallet:", wallet)
|
||||
yield user, wallet
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
async def inkey_headers_from(from_user_wallet):
|
||||
_, wallet = from_user_wallet
|
||||
yield {
|
||||
"X-Api-Key": wallet.inkey,
|
||||
"Content-type": "application/json",
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
async def adminkey_headers_from(from_user_wallet):
|
||||
_, wallet = from_user_wallet
|
||||
yield {
|
||||
"X-Api-Key": wallet.adminkey,
|
||||
"Content-type": "application/json",
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
async def inkey_headers_to(to_user_wallet):
|
||||
_, wallet = to_user_wallet
|
||||
yield {
|
||||
"X-Api-Key": wallet.inkey,
|
||||
"Content-type": "application/json",
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
async def adminkey_headers_to(to_user_wallet):
|
||||
_, wallet = to_user_wallet
|
||||
yield {
|
||||
"X-Api-Key": wallet.adminkey,
|
||||
"Content-type": "application/json",
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
async def invoice(to_user_wallet):
|
||||
_, wallet = to_user_wallet
|
||||
data = await get_random_invoice_data()
|
||||
invoiceData = CreateInvoiceData(**data)
|
||||
# print("--------- New invoice!")
|
||||
# print("wallet:")
|
||||
# print(wallet)
|
||||
stuff_lock = asyncio.Lock()
|
||||
async with stuff_lock:
|
||||
invoice = await api_payments_create_invoice(invoiceData, wallet)
|
||||
await asyncio.sleep(1)
|
||||
# print("invoice")
|
||||
# print(invoice)
|
||||
yield invoice
|
||||
del invoice
|
||||
|
||||
Reference in New Issue
Block a user