fix: some tests

This commit is contained in:
Vlad Stan
2024-10-17 10:37:43 +02:00
committed by dni ⚡
parent b4c57c0faf
commit 376081b369
4 changed files with 26 additions and 21 deletions
+2
View File
@@ -50,6 +50,7 @@ test-wallets:
test-unit: test-unit:
LNBITS_DATA_FOLDER="./tests/data" \ LNBITS_DATA_FOLDER="./tests/data" \
LNBITS_DATABASE_URL="" \
LNBITS_BACKEND_WALLET_CLASS="FakeWallet" \ LNBITS_BACKEND_WALLET_CLASS="FakeWallet" \
PYTHONUNBUFFERED=1 \ PYTHONUNBUFFERED=1 \
DEBUG=true \ DEBUG=true \
@@ -57,6 +58,7 @@ test-unit:
test-api: test-api:
LNBITS_DATA_FOLDER="./tests/data" \ LNBITS_DATA_FOLDER="./tests/data" \
LNBITS_DATABASE_URL="" \
LNBITS_BACKEND_WALLET_CLASS="FakeWallet" \ LNBITS_BACKEND_WALLET_CLASS="FakeWallet" \
PYTHONUNBUFFERED=1 \ PYTHONUNBUFFERED=1 \
DEBUG=true \ DEBUG=true \
+12 -17
View File
@@ -67,7 +67,7 @@ async def login(data: LoginUsernamePassword) -> JSONResponse:
raise HTTPException( raise HTTPException(
status_code=HTTPStatus.UNAUTHORIZED, detail="Invalid credentials." status_code=HTTPStatus.UNAUTHORIZED, detail="Invalid credentials."
) )
return _auth_success_response(account.username, account.id) return _auth_success_response(account.username, account.id, account.email)
@auth_router.post("/nostr", description="Login via Nostr") @auth_router.post("/nostr", description="Login via Nostr")
@@ -100,7 +100,7 @@ async def login_usr(data: LoginUsr) -> JSONResponse:
raise HTTPException( raise HTTPException(
status_code=HTTPStatus.UNAUTHORIZED, detail="User ID does not exist." status_code=HTTPStatus.UNAUTHORIZED, detail="User ID does not exist."
) )
return _auth_success_response(account.username, account.id) return _auth_success_response(account.username, account.id, account.email)
@auth_router.get("/{provider}", description="SSO Provider") @auth_router.get("/{provider}", description="SSO Provider")
@@ -188,7 +188,7 @@ async def register(data: CreateUser) -> JSONResponse:
) )
account.hash_password(data.password) account.hash_password(data.password)
await create_account(account) await create_account(account)
return _auth_success_response(account.username) return _auth_success_response(account.username, account.id, account.email)
@auth_router.put("/pubkey") @auth_router.put("/pubkey")
@@ -271,22 +271,19 @@ async def reset_password(data: ResetUserPassword) -> JSONResponse:
raise HTTPException( raise HTTPException(
HTTPStatus.UNAUTHORIZED, "Auth by 'Username and Password' not allowed." HTTPStatus.UNAUTHORIZED, "Auth by 'Username and Password' not allowed."
) )
if not data.reset_key[:10].startswith("reset_key_"):
raise HTTPException(HTTPStatus.BAD_REQUEST, "This is not a reset key.") assert data.password == data.password_repeat, "Passwords do not match."
assert data.reset_key[:10].startswith("reset_key_"), "This is not a reset key."
reset_data_json = decrypt_internal_message( reset_data_json = decrypt_internal_message(
base64.b64decode(data.reset_key[10:]).decode() base64.b64decode(data.reset_key[10:]).decode()
) )
if not reset_data_json: assert reset_data_json, "Cannot process reset key."
raise HTTPException(HTTPStatus.BAD_REQUEST, "Cannot process reset key.")
action, user_id, request_time = json.loads(reset_data_json) action, user_id, request_time = json.loads(reset_data_json)
if not action: assert action, "Missing action."
raise HTTPException(HTTPStatus.BAD_REQUEST, "Missing action.") assert user_id, "Missing user ID."
if not user_id: assert request_time, "Missing reset time."
raise HTTPException(HTTPStatus.BAD_REQUEST, "Missing user ID.")
if not request_time:
raise HTTPException(HTTPStatus.BAD_REQUEST, "Missing reset time.")
_validate_auth_timeout(request_time) _validate_auth_timeout(request_time)
@@ -296,9 +293,7 @@ async def reset_password(data: ResetUserPassword) -> JSONResponse:
account.hash_password(data.password) account.hash_password(data.password)
await update_account(account) await update_account(account)
return _auth_success_response( return _auth_success_response(account.username, user_id, account.email)
username=account.username, user_id=user_id, email=account.email
)
@auth_router.put("/update") @auth_router.put("/update")
@@ -365,7 +360,7 @@ async def first_install(data: UpdateSuperuserPassword) -> JSONResponse:
account.hash_password(data.password) account.hash_password(data.password)
await update_account(account) await update_account(account)
settings.first_install = False settings.first_install = False
return _auth_success_response(username=account.username) return _auth_success_response(account.username, account.id, account.email)
async def _handle_sso_login(userinfo: OpenID, verified_user_id: Optional[str] = None): async def _handle_sso_login(userinfo: OpenID, verified_user_id: Optional[str] = None):
+10 -3
View File
@@ -95,6 +95,7 @@ async def test_login_alan_username_password_ok(
payload: dict = jwt.decode(access_token, settings.auth_secret_key, ["HS256"]) payload: dict = jwt.decode(access_token, settings.auth_secret_key, ["HS256"])
access_token_payload = AccessTokenPayload(**payload) access_token_payload = AccessTokenPayload(**payload)
assert access_token_payload.sub == "alan", "Subject is Alan." assert access_token_payload.sub == "alan", "Subject is Alan."
assert access_token_payload.email == "alan@lnbits.com" assert access_token_payload.email == "alan@lnbits.com"
assert access_token_payload.auth_time, "Auth time should be set by server." assert access_token_payload.auth_time, "Auth time should be set by server."
@@ -113,7 +114,9 @@ async def test_login_alan_username_password_ok(
assert not user.admin, "Not admin." assert not user.admin, "Not admin."
assert not user.super_user, "Not superuser." assert not user.super_user, "Not superuser."
assert user.has_password, "Password configured." assert user.has_password, "Password configured."
assert len(user.wallets) == 1, "One default wallet." assert (
len(user.wallets) == 1
), f"Expected 1 default wallet, not {len(user.wallets)}."
@pytest.mark.asyncio @pytest.mark.asyncio
@@ -221,7 +224,9 @@ async def test_register_ok(http_client: AsyncClient):
assert not user.admin, "Not admin." assert not user.admin, "Not admin."
assert not user.super_user, "Not superuser." assert not user.super_user, "Not superuser."
assert user.has_password, "Password configured." assert user.has_password, "Password configured."
assert len(user.wallets) == 1, "One default wallet." assert (
len(user.wallets) == 1
), f"Expected 1 default wallet, not {len(user.wallets)}."
@pytest.mark.asyncio @pytest.mark.asyncio
@@ -509,7 +514,9 @@ async def test_register_nostr_ok(http_client: AsyncClient):
assert not user.admin, "Not admin." assert not user.admin, "Not admin."
assert not user.super_user, "Not superuser." assert not user.super_user, "Not superuser."
assert not user.has_password, "Password configured." assert not user.has_password, "Password configured."
assert len(user.wallets) == 1, "One default wallet." assert (
len(user.wallets) == 1
), f"Expected 1 default wallet, not {len(user.wallets)}."
@pytest.mark.asyncio @pytest.mark.asyncio
+2 -1
View File
@@ -102,7 +102,7 @@ async def db():
yield Database("database") yield Database("database")
@pytest_asyncio.fixture(scope="package") @pytest_asyncio.fixture(scope="session")
async def user_alan(): async def user_alan():
account = await get_account_by_username("alan") account = await get_account_by_username("alan")
if not account: if not account:
@@ -112,6 +112,7 @@ async def user_alan():
username="alan", username="alan",
) )
account.hash_password("secret1234") account.hash_password("secret1234")
account = await create_account(account)
yield account yield account