refactor: make settings a fixture

This commit is contained in:
Vlad Stan
2024-10-17 10:39:57 +02:00
committed by dni ⚡
parent b33abddf3a
commit 7bad0552f6
7 changed files with 96 additions and 90 deletions
+3 -2
View File
@@ -1,6 +1,7 @@
import pytest
from lnbits.settings import settings
from lnbits.core.models import User
from lnbits.settings import Settings
@pytest.mark.asyncio
@@ -18,7 +19,7 @@ async def test_admin_get_settings(client, superuser):
@pytest.mark.asyncio
async def test_admin_update_settings(client, superuser):
async def test_admin_update_settings(client, superuser: User, settings: Settings):
new_site_title = "UPDATED SITETITLE"
response = await client.put(
f"/admin/api/v1/settings?usr={superuser.id}",
+4 -1
View File
@@ -18,7 +18,10 @@ from ..helpers import (
async def test_create_account(client, settings: Settings):
settings.lnbits_allow_new_accounts = False
response = await client.post("/api/v1/account", json={"name": "test"})
assert response.status_code == 403
assert response.status_code == 400
assert response.json().get("detail") == "Account creation is disabled."
settings.lnbits_allow_new_accounts = True
response = await client.post("/api/v1/account", json={"name": "test"})
assert response.status_code == 200
+37 -45
View File
@@ -62,17 +62,17 @@ async def test_login_alan_usr(user_alan: User, http_client: AsyncClient):
@pytest.mark.asyncio
async def test_login_usr_not_allowed(
user_alan: User, http_client: AsyncClient, lnbits_settings: Settings
user_alan: User, http_client: AsyncClient, settings: Settings
):
# exclude 'user_id_only'
lnbits_settings.auth_allowed_methods = [AuthMethods.username_and_password.value]
settings.auth_allowed_methods = [AuthMethods.username_and_password.value]
response = await http_client.post("/api/v1/auth/usr", json={"usr": user_alan.id})
assert response.status_code == 401, "Login method not allowed."
assert response.json().get("detail") == "Login by 'User ID' not allowed."
lnbits_settings.auth_allowed_methods = AuthMethods.all()
settings.auth_allowed_methods = AuthMethods.all()
response = await http_client.post("/api/v1/auth/usr", json={"usr": user_alan.id})
assert response.status_code == 200, "Login with 'usr' allowed."
@@ -83,7 +83,7 @@ async def test_login_usr_not_allowed(
@pytest.mark.asyncio
async def test_login_alan_username_password_ok(
user_alan: User, http_client: AsyncClient, lnbits_settings: Settings
user_alan: User, http_client: AsyncClient, settings: Settings
):
response = await http_client.post(
"/api/v1/auth", json={"username": user_alan.username, "password": "secret1234"}
@@ -93,7 +93,7 @@ async def test_login_alan_username_password_ok(
access_token = response.json().get("access_token")
assert access_token is not None
payload: dict = jwt.decode(access_token, lnbits_settings.auth_secret_key, ["HS256"])
payload: dict = jwt.decode(access_token, settings.auth_secret_key, ["HS256"])
access_token_payload = AccessTokenPayload(**payload)
assert access_token_payload.sub == "alan", "Subject is Alan."
@@ -142,10 +142,10 @@ async def test_login_alan_password_nok(user_alan: User, http_client: AsyncClient
@pytest.mark.asyncio
async def test_login_username_password_not_allowed(
user_alan: User, http_client: AsyncClient, lnbits_settings: Settings
user_alan: User, http_client: AsyncClient, settings: Settings
):
# exclude 'username_password'
lnbits_settings.auth_allowed_methods = [AuthMethods.user_id_only.value]
settings.auth_allowed_methods = [AuthMethods.user_id_only.value]
response = await http_client.post(
"/api/v1/auth", json={"username": user_alan.username, "password": "secret1234"}
@@ -156,7 +156,7 @@ async def test_login_username_password_not_allowed(
response.json().get("detail") == "Login by 'Username and Password' not allowed."
)
lnbits_settings.auth_allowed_methods = AuthMethods.all()
settings.auth_allowed_methods = AuthMethods.all()
response = await http_client.post(
"/api/v1/auth", json={"username": user_alan.username, "password": "secret1234"}
@@ -167,7 +167,7 @@ async def test_login_username_password_not_allowed(
@pytest.mark.asyncio
async def test_login_alan_change_auth_secret_key(
user_alan: User, http_client: AsyncClient, lnbits_settings: Settings
user_alan: User, http_client: AsyncClient, settings: Settings
):
response = await http_client.post(
"/api/v1/auth", json={"username": user_alan.username, "password": "secret1234"}
@@ -177,9 +177,9 @@ async def test_login_alan_change_auth_secret_key(
access_token = response.json().get("access_token")
assert access_token is not None
initial_auth_secret_key = lnbits_settings.auth_secret_key
initial_auth_secret_key = settings.auth_secret_key
lnbits_settings.auth_secret_key = shortuuid.uuid()
settings.auth_secret_key = shortuuid.uuid()
response = await http_client.get(
"/api/v1/auth", headers={"Authorization": f"Bearer {access_token}"}
@@ -187,7 +187,7 @@ async def test_login_alan_change_auth_secret_key(
assert response.status_code == 401, "Access token not valid anymore."
assert response.json().get("detail") == "Invalid access token."
lnbits_settings.auth_secret_key = initial_auth_secret_key
settings.auth_secret_key = initial_auth_secret_key
response = await http_client.get(
"/api/v1/auth", headers={"Authorization": f"Bearer {access_token}"}
@@ -326,7 +326,7 @@ async def test_register_bad_email(http_client: AsyncClient):
################################ CHANGE PASSWORD ################################
@pytest.mark.asyncio
async def test_change_password_ok(http_client: AsyncClient, lnbits_settings: Settings):
async def test_change_password_ok(http_client: AsyncClient, settings: Settings):
tiny_id = shortuuid.uuid()[:8]
response = await http_client.post(
"/api/v1/auth/register",
@@ -342,7 +342,7 @@ async def test_change_password_ok(http_client: AsyncClient, lnbits_settings: Set
access_token = response.json().get("access_token")
assert access_token is not None
payload: dict = jwt.decode(access_token, lnbits_settings.auth_secret_key, ["HS256"])
payload: dict = jwt.decode(access_token, settings.auth_secret_key, ["HS256"])
access_token_payload = AccessTokenPayload(**payload)
response = await http_client.put(
@@ -447,7 +447,7 @@ async def test_alan_change_password_different_user(
@pytest.mark.asyncio
async def test_alan_change_password_auth_threshold_expired(
user_alan: User, http_client: AsyncClient, lnbits_settings: Settings
user_alan: User, http_client: AsyncClient, settings: Settings
):
response = await http_client.post("/api/v1/auth/usr", json={"usr": user_alan.id})
@@ -456,7 +456,7 @@ async def test_alan_change_password_auth_threshold_expired(
access_token = response.json().get("access_token")
assert access_token is not None
lnbits_settings.auth_credetials_update_threshold = 1
settings.auth_credetials_update_threshold = 1
time.sleep(1.1)
response = await http_client.put(
"/api/v1/auth/password",
@@ -482,7 +482,7 @@ async def test_alan_change_password_auth_threshold_expired(
@pytest.mark.asyncio
async def test_register_nostr_ok(http_client: AsyncClient, lnbits_settings: Settings):
async def test_register_nostr_ok(http_client: AsyncClient, settings: Settings):
event = {**nostr_event}
event["created_at"] = int(time.time())
@@ -498,7 +498,7 @@ async def test_register_nostr_ok(http_client: AsyncClient, lnbits_settings: Sett
access_token = response.json().get("access_token")
assert access_token is not None
payload: dict = jwt.decode(access_token, lnbits_settings.auth_secret_key, ["HS256"])
payload: dict = jwt.decode(access_token, settings.auth_secret_key, ["HS256"])
access_token_payload = AccessTokenPayload(**payload)
assert access_token_payload.auth_time, "Auth time should be set by server."
assert (
@@ -522,11 +522,9 @@ async def test_register_nostr_ok(http_client: AsyncClient, lnbits_settings: Sett
@pytest.mark.asyncio
async def test_register_nostr_not_allowed(
http_client: AsyncClient, lnbits_settings: Settings
):
async def test_register_nostr_not_allowed(http_client: AsyncClient, settings: Settings):
# exclude 'nostr_auth_nip98'
lnbits_settings.auth_allowed_methods = [AuthMethods.username_and_password.value]
settings.auth_allowed_methods = [AuthMethods.username_and_password.value]
response = await http_client.post(
"/api/v1/auth/nostr",
json={},
@@ -535,7 +533,7 @@ async def test_register_nostr_not_allowed(
assert response.status_code == 401, "User not authenticated."
assert response.json().get("detail") == "Login with Nostr Auth not allowed."
lnbits_settings.auth_allowed_methods = AuthMethods.all()
settings.auth_allowed_methods = AuthMethods.all()
@pytest.mark.asyncio
@@ -562,10 +560,8 @@ async def test_register_nostr_bad_header(http_client: AsyncClient):
@pytest.mark.asyncio
async def test_register_nostr_bad_event(
http_client: AsyncClient, lnbits_settings: Settings
):
lnbits_settings.auth_allowed_methods = AuthMethods.all()
async def test_register_nostr_bad_event(http_client: AsyncClient, settings: Settings):
settings.auth_allowed_methods = AuthMethods.all()
base64_event = base64.b64encode(json.dumps(nostr_event).encode()).decode("ascii")
response = await http_client.post(
"/api/v1/auth/nostr",
@@ -574,7 +570,7 @@ async def test_register_nostr_bad_event(
assert response.status_code == 400, "Nostr event expired."
assert (
response.json().get("detail")
== f"More than {lnbits_settings.auth_credetials_update_threshold}"
== f"More than {settings.auth_credetials_update_threshold}"
" seconds have passed since the event was signed."
)
@@ -676,9 +672,7 @@ async def test_register_nostr_bad_event_tag_menthod(http_client: AsyncClient):
################################ CHANGE PUBLIC KEY ################################
async def test_change_pubkey_npub_ok(
http_client: AsyncClient, lnbits_settings: Settings
):
async def test_change_pubkey_npub_ok(http_client: AsyncClient, settings: Settings):
tiny_id = shortuuid.uuid()[:8]
response = await http_client.post(
"/api/v1/auth/register",
@@ -694,7 +688,7 @@ async def test_change_pubkey_npub_ok(
access_token = response.json().get("access_token")
assert access_token is not None
payload: dict = jwt.decode(access_token, lnbits_settings.auth_secret_key, ["HS256"])
payload: dict = jwt.decode(access_token, settings.auth_secret_key, ["HS256"])
access_token_payload = AccessTokenPayload(**payload)
private_key = secp256k1.PrivateKey(bytes.fromhex(os.urandom(32).hex()))
@@ -719,7 +713,7 @@ async def test_change_pubkey_npub_ok(
@pytest.mark.asyncio
async def test_change_pubkey_ok(
http_client: AsyncClient, user_alan: User, lnbits_settings: Settings
http_client: AsyncClient, user_alan: User, settings: Settings
):
tiny_id = shortuuid.uuid()[:8]
response = await http_client.post(
@@ -736,7 +730,7 @@ async def test_change_pubkey_ok(
access_token = response.json().get("access_token")
assert access_token is not None
payload: dict = jwt.decode(access_token, lnbits_settings.auth_secret_key, ["HS256"])
payload: dict = jwt.decode(access_token, settings.auth_secret_key, ["HS256"])
access_token_payload = AccessTokenPayload(**payload)
private_key = secp256k1.PrivateKey(bytes.fromhex(os.urandom(32).hex()))
@@ -842,7 +836,7 @@ async def test_change_pubkey_other_user(http_client: AsyncClient, user_alan: Use
@pytest.mark.asyncio
async def test_alan_change_pubkey_auth_threshold_expired(
user_alan: User, http_client: AsyncClient, lnbits_settings: Settings
user_alan: User, http_client: AsyncClient, settings: Settings
):
response = await http_client.post("/api/v1/auth/usr", json={"usr": user_alan.id})
@@ -851,7 +845,7 @@ async def test_alan_change_pubkey_auth_threshold_expired(
access_token = response.json().get("access_token")
assert access_token is not None
lnbits_settings.auth_credetials_update_threshold = 1
settings.auth_credetials_update_threshold = 1
time.sleep(2.1)
response = await http_client.put(
"/api/v1/auth/pubkey",
@@ -872,9 +866,7 @@ async def test_alan_change_pubkey_auth_threshold_expired(
################################ RESET PASSWORD ################################
@pytest.mark.asyncio
async def test_request_reset_key_ok(
http_client: AsyncClient, lnbits_settings: Settings
):
async def test_request_reset_key_ok(http_client: AsyncClient, settings: Settings):
tiny_id = shortuuid.uuid()[:8]
response = await http_client.post(
"/api/v1/auth/register",
@@ -890,7 +882,7 @@ async def test_request_reset_key_ok(
access_token = response.json().get("access_token")
assert access_token is not None
payload: dict = jwt.decode(access_token, lnbits_settings.auth_secret_key, ["HS256"])
payload: dict = jwt.decode(access_token, settings.auth_secret_key, ["HS256"])
access_token_payload = AccessTokenPayload(**payload)
assert access_token_payload.usr, "User id set."
@@ -947,10 +939,10 @@ async def test_request_reset_key_user_not_found(http_client: AsyncClient):
@pytest.mark.asyncio
async def test_reset_username_password_not_allowed(
http_client: AsyncClient, lnbits_settings: Settings
http_client: AsyncClient, settings: Settings
):
# exclude 'username_password'
lnbits_settings.auth_allowed_methods = [AuthMethods.user_id_only.value]
settings.auth_allowed_methods = [AuthMethods.user_id_only.value]
user_id = "926abb2ab59a48ebb2485bcceb58d05e"
reset_key = await api_users_reset_password(user_id)
@@ -964,7 +956,7 @@ async def test_reset_username_password_not_allowed(
"password_repeat": "secret0000",
},
)
lnbits_settings.auth_allowed_methods = AuthMethods.all()
settings.auth_allowed_methods = AuthMethods.all()
assert response.status_code == 401, "Login method not allowed."
assert (
@@ -1010,13 +1002,13 @@ async def test_reset_username_password_bad_key(http_client: AsyncClient):
@pytest.mark.asyncio
async def test_reset_password_auth_threshold_expired(
user_alan: User, http_client: AsyncClient, lnbits_settings: Settings
user_alan: User, http_client: AsyncClient, settings: Settings
):
reset_key = await api_users_reset_password(user_alan.id)
assert reset_key, "Reset key created."
lnbits_settings.auth_credetials_update_threshold = 1
settings.auth_credetials_update_threshold = 1
time.sleep(1.1)
response = await http_client.put(
"/api/v1/auth/reset",