b5cba83df4
Co-authored-by: Cursor <cursoragent@cursor.com>
50 lines
1.1 KiB
Python
50 lines
1.1 KiB
Python
import os
|
|
|
|
from packages.config.settings import get_settings
|
|
|
|
|
|
def _with_env(**kwargs):
|
|
old = {}
|
|
for k, v in kwargs.items():
|
|
old[k] = os.environ.get(k)
|
|
os.environ[k] = v
|
|
get_settings.cache_clear()
|
|
return old
|
|
|
|
|
|
def _restore(old: dict):
|
|
for k, v in old.items():
|
|
if v is None:
|
|
os.environ.pop(k, None)
|
|
else:
|
|
os.environ[k] = v
|
|
get_settings.cache_clear()
|
|
|
|
|
|
def test_issue_token_ok():
|
|
from apps.api.auth import expected_token, issue_token
|
|
|
|
old = _with_env(
|
|
AUTH_SECRET="unit-secret",
|
|
ADMIN_USERNAME="admin",
|
|
ADMIN_PASSWORD="pass123",
|
|
)
|
|
try:
|
|
tok = issue_token("admin", "pass123")
|
|
assert tok is not None
|
|
assert tok == expected_token()
|
|
assert issue_token("admin", "wrong") is None
|
|
assert issue_token("wrong", "pass123") is None
|
|
finally:
|
|
_restore(old)
|
|
|
|
|
|
def test_auth_disabled():
|
|
from apps.api.auth import auth_disabled
|
|
|
|
old = _with_env(AUTH_SECRET="disabled")
|
|
try:
|
|
assert auth_disabled() is True
|
|
finally:
|
|
_restore(old)
|