Fix: 500 Error When Searching by Wallet ID in Users. (#3789)
Co-authored-by: Vlad Stan <stan.v.vlad@gmail.com>
This commit is contained in:
co-authored by
Vlad Stan
parent
8c184356ef
commit
75bae67446
@@ -2,10 +2,16 @@ from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
|
||||
from lnbits.core.crud.users import get_user_from_account
|
||||
from lnbits.core.crud.wallets import delete_wallet, get_wallets
|
||||
from lnbits.core.models.users import Account
|
||||
from lnbits.core.services.users import create_user_account
|
||||
from lnbits.core.crud.users import (
|
||||
create_account,
|
||||
delete_account,
|
||||
get_accounts,
|
||||
get_user_from_account,
|
||||
)
|
||||
from lnbits.core.crud.wallets import delete_wallet, force_delete_wallet, get_wallets
|
||||
from lnbits.core.models.users import Account, AccountFilters
|
||||
from lnbits.core.services.users import create_user_account, create_user_account_no_ckeck
|
||||
from lnbits.db import Filter, Filters, Operator
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
@@ -36,3 +42,151 @@ async def test_get_user_from_account_is_wallet_created():
|
||||
assert (
|
||||
len(user.wallets) == 1
|
||||
), "A new wallet should be created for the user if none exist after deletion"
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_get_accounts_success_flow():
|
||||
# Create a new account
|
||||
username = f"user_{uuid4().hex[:8]}"
|
||||
account = Account(
|
||||
id=uuid4().hex,
|
||||
username=username,
|
||||
email=f"{username}@lnbits.com",
|
||||
)
|
||||
await create_account(account)
|
||||
# Should return the created account
|
||||
filters = Filters[AccountFilters](filters=[], model=AccountFilters)
|
||||
filters.sortby = "created_at"
|
||||
filters.direction = "desc"
|
||||
page = await get_accounts(filters=filters)
|
||||
assert page.total >= 1
|
||||
found = any(a.username == username for a in page.data)
|
||||
assert found
|
||||
await delete_account(account.id)
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_get_accounts_with_wallet_id_filter():
|
||||
# Create account and wallet
|
||||
username = f"user_{uuid4().hex[:8]}"
|
||||
account = Account(
|
||||
id=uuid4().hex,
|
||||
username=username,
|
||||
email=f"{username}@lnbits.com",
|
||||
)
|
||||
await create_user_account_no_ckeck(account)
|
||||
|
||||
wallets = await get_wallets(account.id, deleted=False)
|
||||
assert wallets
|
||||
wallet = wallets[0]
|
||||
# Filter by wallet_id
|
||||
filters = Filters[AccountFilters](
|
||||
filters=[
|
||||
Filter(
|
||||
field="wallet_id",
|
||||
op=Operator.EQ,
|
||||
model=AccountFilters,
|
||||
values={"wallet_id__0": wallet.id},
|
||||
)
|
||||
],
|
||||
model=AccountFilters,
|
||||
)
|
||||
page = await get_accounts(filters=filters)
|
||||
assert page.total == 1
|
||||
assert page.data[0].id == account.id
|
||||
await delete_account(account.id)
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_get_accounts_wallet_id_not_found():
|
||||
|
||||
filters = Filters[AccountFilters](
|
||||
filters=[
|
||||
Filter(
|
||||
field="wallet_id",
|
||||
op=Operator.EQ,
|
||||
model=AccountFilters,
|
||||
values={"wallet_id__0": uuid4().hex},
|
||||
)
|
||||
],
|
||||
model=AccountFilters,
|
||||
)
|
||||
page = await get_accounts(filters=filters)
|
||||
assert page.total == 0
|
||||
assert page.data == []
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_get_accounts_empty_filters():
|
||||
# Should not raise, should return a Page
|
||||
page = await get_accounts()
|
||||
assert hasattr(page, "data")
|
||||
assert hasattr(page, "total")
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_get_accounts_with_deleted_wallet():
|
||||
# Create account and wallet, then delete wallet
|
||||
username = f"user_{uuid4().hex[:8]}"
|
||||
account = Account(
|
||||
id=uuid4().hex,
|
||||
username=username,
|
||||
email=f"{username}@lnbits.com",
|
||||
)
|
||||
await create_user_account_no_ckeck(account)
|
||||
|
||||
wallets = await get_wallets(account.id, deleted=False)
|
||||
assert wallets
|
||||
wallet = wallets[0]
|
||||
await delete_wallet(user_id=account.id, wallet_id=wallet.id)
|
||||
|
||||
filters = Filters[AccountFilters](
|
||||
filters=[
|
||||
Filter(
|
||||
field="wallet_id",
|
||||
op=Operator.EQ,
|
||||
model=AccountFilters,
|
||||
values={"wallet_id__0": wallet.id},
|
||||
)
|
||||
],
|
||||
model=AccountFilters,
|
||||
)
|
||||
page = await get_accounts(filters=filters)
|
||||
assert page.total == 1
|
||||
assert page.data[0].id == account.id
|
||||
|
||||
await force_delete_wallet(wallet_id=wallet.id)
|
||||
|
||||
filters = Filters[AccountFilters](
|
||||
filters=[
|
||||
Filter(
|
||||
field="wallet_id",
|
||||
op=Operator.EQ,
|
||||
model=AccountFilters,
|
||||
values={"wallet_id__0": wallet.id},
|
||||
)
|
||||
],
|
||||
model=AccountFilters,
|
||||
)
|
||||
page = await get_accounts(filters=filters)
|
||||
assert page.total == 0
|
||||
assert page.data == []
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_get_accounts_group_by_and_pagination():
|
||||
# Create multiple accounts
|
||||
accounts = []
|
||||
for _ in range(3):
|
||||
username = f"user_{uuid4().hex[:8]}"
|
||||
account = Account(
|
||||
id=uuid4().hex,
|
||||
username=username,
|
||||
email=f"{username}@lnbits.com",
|
||||
)
|
||||
await create_user_account_no_ckeck(account)
|
||||
accounts.append(account)
|
||||
filters = Filters[AccountFilters](model=AccountFilters, limit=2, offset=0)
|
||||
page = await get_accounts(filters=filters)
|
||||
assert page.total >= 3
|
||||
assert len(page.data) <= 2
|
||||
|
||||
Reference in New Issue
Block a user