Files
devplacepy/tests/api/auth/token.py
T
retoor dbdd0d29dd Auth: username+password, email+password and API key everywhere; key returns carry username and email
POST /auth/login (form and JSON) now accepts the username in the email field, like HTTP Basic and POST /auth/token always did; the token endpoint no longer lowercases the identifier before the username lookup, fixing mixed-case usernames. The login field is relabelled and type=text so browsers accept a username.

Every JSON return of the personal API key now also returns the account username and email: the profile JSON gains a can_view_api_key-gated email beside api_key (username already rides profile_user), and POST /profile/regenerate-api-key returns {api_key, username, email}. A client holding only its DEVPLACE_API_KEY value can now fetch its full account via GET /profile in one call.

Documented fully: authentication prose page (three credentials, precedence, DEVPLACE_API_KEY, account fetch, separate DevRant/WebSocket auth domains), new auth-token docs_api card, regenerated-key sample, conventions, README, CLAUDE.md, OpenAPI description, corrected profile key hint. Summary in changes.md (left unversioned per request).

Tests: username/mixed-case/uppercase-email login and token cases, API-key account fetch via X-API-KEY and Bearer, owner/admin vs stranger/guest email gating, regenerate shape, browser login by username. Failing-first observed red, green after.
2026-11-11 16:07:43 +01:00

162 lines
4.9 KiB
Python

# retoor <retoor@molodetz.nl>
import time
import pytest
import requests
from tests.conftest import BASE_URL
from devplacepy.database import get_table, refresh_snapshot, set_setting
_counter_token = [0]
@pytest.fixture(scope="module", autouse=True)
def _token_settings(app_server):
for key, value in {
"rate_limit_per_minute": "1000000",
"rate_limit_window_seconds": "60",
"registration_open": "1",
"maintenance_mode": "0",
"session_max_age_days": "7",
}.items():
set_setting(key, value)
yield
def _signup(password="secret123"):
_counter_token[0] += 1
name = f"tok{int(time.time() * 1000)}{_counter_token[0]}"
email = f"{name}@t.dev"
requests.post(
f"{BASE_URL}/auth/signup",
data={
"username": name,
"email": email,
"password": password,
"confirm_password": password,
"birth_date": "1990-01-01",
"accept_terms": "1",
},
allow_redirects=True,
)
return name, email, password
def test_token_happy_path_with_email(app_server):
name, email, password = _signup()
r = requests.post(f"{BASE_URL}/auth/token", data={"email": email, "password": password})
assert r.status_code == 200, r.text[:300]
body = r.json()
assert body["access_token"] and len(body["access_token"]) >= 32
assert body["token_type"]
assert isinstance(body["expires_in"], int) and body["expires_in"] > 0
def test_token_accepts_username_as_identifier(app_server):
name, email, password = _signup()
r = requests.post(f"{BASE_URL}/auth/token", data={"email": name, "password": password})
assert r.status_code == 200, r.text[:300]
assert r.json()["access_token"]
def _signup_mixed_case(password="secret123"):
_counter_token[0] += 1
name = f"TokMix{int(time.time() * 1000)}{_counter_token[0]}"
email = f"{name.lower()}@t.dev"
requests.post(
f"{BASE_URL}/auth/signup",
data={
"username": name,
"email": email,
"password": password,
"confirm_password": password,
"birth_date": "1990-01-01",
"accept_terms": "1",
},
allow_redirects=True,
)
return name, email, password
def test_token_accepts_mixed_case_username(app_server):
name, email, password = _signup_mixed_case()
r = requests.post(f"{BASE_URL}/auth/token", data={"email": name, "password": password})
assert r.status_code == 200, r.text[:300]
assert r.json()["access_token"]
def test_token_accepts_uppercase_email(app_server):
name, email, password = _signup()
r = requests.post(
f"{BASE_URL}/auth/token", data={"email": email.upper(), "password": password}
)
assert r.status_code == 200, r.text[:300]
assert r.json()["access_token"]
def test_token_via_json_body(app_server):
name, email, password = _signup()
r = requests.post(
f"{BASE_URL}/auth/token",
json={"email": email, "password": password},
headers={"Content-Type": "application/json"},
)
assert r.status_code == 200, r.text[:300]
assert r.json()["access_token"]
def test_token_wrong_password_401(app_server):
name, email, password = _signup()
r = requests.post(f"{BASE_URL}/auth/token", data={"email": email, "password": "nope!!"})
assert r.status_code == 401
assert "access_token" not in r.json()
def test_token_unknown_identifier_401(app_server):
r = requests.post(
f"{BASE_URL}/auth/token",
data={"email": "no_such_user_zzz@t.dev", "password": "whatever"},
)
assert r.status_code == 401
def test_token_empty_password_rejected(app_server):
name, email, password = _signup()
r = requests.post(
f"{BASE_URL}/auth/token",
headers={"Accept": "application/json"},
data={"email": email, "password": ""},
)
assert r.status_code in (400, 401, 422)
assert "access_token" not in r.json()
def test_token_inactive_account_403(app_server):
name, email, password = _signup()
users = get_table("users")
row = users.find_one(username=name)
users.update({"uid": row["uid"], "is_active": False}, ["uid"])
refresh_snapshot()
try:
r = requests.post(
f"{BASE_URL}/auth/token", data={"email": email, "password": password}
)
assert r.status_code == 403, r.text[:300]
assert "access_token" not in r.json()
finally:
users.update({"uid": row["uid"], "is_active": True}, ["uid"])
refresh_snapshot()
def test_token_is_usable_as_bearer_credential(app_server):
name, email, password = _signup()
token = requests.post(
f"{BASE_URL}/auth/token", data={"email": email, "password": password}
).json()["access_token"]
target, _, _ = _signup()
r = requests.post(
f"{BASE_URL}/follow/{target}",
headers={"Authorization": f"Bearer {token}"},
allow_redirects=False,
)
assert r.status_code in (200, 302), r.text[:200]