feat: create wallet if user does not have one (#3566)
This commit is contained in:
parent
7a796c6510
commit
0f4ae5da86
2 changed files with 46 additions and 3 deletions
|
|
@ -4,7 +4,7 @@ from typing import Any
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
from lnbits.core.crud.extensions import get_user_active_extensions_ids
|
from lnbits.core.crud.extensions import get_user_active_extensions_ids
|
||||||
from lnbits.core.crud.wallets import get_wallets
|
from lnbits.core.crud.wallets import create_wallet, get_wallets
|
||||||
from lnbits.core.db import db
|
from lnbits.core.db import db
|
||||||
from lnbits.core.models import UserAcls
|
from lnbits.core.models import UserAcls
|
||||||
from lnbits.db import Connection, Filters, Page
|
from lnbits.db import Connection, Filters, Page
|
||||||
|
|
@ -180,8 +180,13 @@ async def get_user(user_id: str, conn: Connection | None = None) -> User | None:
|
||||||
async def get_user_from_account(
|
async def get_user_from_account(
|
||||||
account: Account, conn: Connection | None = None
|
account: Account, conn: Connection | None = None
|
||||||
) -> User | None:
|
) -> User | None:
|
||||||
extensions = await get_user_active_extensions_ids(account.id, conn)
|
extensions = await get_user_active_extensions_ids(account.id, conn=conn)
|
||||||
wallets = await get_wallets(account.id, False, conn=conn)
|
wallets = await get_wallets(account.id, deleted=False, conn=conn)
|
||||||
|
|
||||||
|
if len(wallets) == 0:
|
||||||
|
wallet = await create_wallet(user_id=account.id, conn=conn)
|
||||||
|
wallets.append(wallet)
|
||||||
|
|
||||||
return User(
|
return User(
|
||||||
id=account.id,
|
id=account.id,
|
||||||
email=account.email,
|
email=account.email,
|
||||||
|
|
|
||||||
38
tests/unit/test_crud_users.py
Normal file
38
tests/unit/test_crud_users.py
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_get_user_from_account_is_wallet_created():
|
||||||
|
|
||||||
|
username = f"user_{uuid4().hex[:8]}"
|
||||||
|
account = Account(
|
||||||
|
id=uuid4().hex,
|
||||||
|
username=username,
|
||||||
|
email=f"{username}@lnbits.com",
|
||||||
|
)
|
||||||
|
account.hash_password("secret1234")
|
||||||
|
user = await create_user_account(account)
|
||||||
|
|
||||||
|
assert user is not None
|
||||||
|
assert (
|
||||||
|
len(user.wallets) == 1
|
||||||
|
), "A wallet should be created for the user if none exist"
|
||||||
|
|
||||||
|
await delete_wallet(user_id=account.id, wallet_id=user.wallets[0].id)
|
||||||
|
|
||||||
|
wallets = await get_wallets(account.id, deleted=False)
|
||||||
|
assert len(wallets) == 0, "User should have no wallets after deletion"
|
||||||
|
|
||||||
|
user = await get_user_from_account(account)
|
||||||
|
|
||||||
|
assert user is not None
|
||||||
|
assert (
|
||||||
|
len(user.wallets) == 1
|
||||||
|
), "A new wallet should be created for the user if none exist after deletion"
|
||||||
Loading…
Add table
Add a link
Reference in a new issue