56 lines
3.5 KiB
Python
56 lines
3.5 KiB
Python
|
|
import httpx
|
||
|
|
from conftest import PASSWORD
|
||
|
|
|
||
|
|
|
||
|
|
async def test_login_cookie_csrf_and_revocation(system):
|
||
|
|
async with httpx.AsyncClient(transport=httpx.ASGITransport(app=system.app), base_url="http://test") as client:
|
||
|
|
assert (await client.get("/api/auth/me")).status_code == 401
|
||
|
|
denied = await client.post("/api/auth/login", json={"username": "admin", "password": "wrong"})
|
||
|
|
assert denied.status_code == 401
|
||
|
|
login = await client.post("/api/auth/login", json={"username": "admin", "password": PASSWORD})
|
||
|
|
assert login.status_code == 200 and "HttpOnly" in login.headers["set-cookie"]
|
||
|
|
assert (await client.post("/api/auth/logout")).status_code == 403
|
||
|
|
assert (await client.post("/api/auth/logout", headers={"X-CSRF-Token": b"\xc3\xa9"})).status_code == 403
|
||
|
|
csrf = login.json()["csrf_token"]
|
||
|
|
assert (await client.get("/api/auth/me")).json()["csrf_token"] == csrf
|
||
|
|
cookie = client.cookies.get("cmvr_session")
|
||
|
|
assert (await client.post("/api/auth/logout", headers={"X-CSRF-Token": csrf})).status_code == 204
|
||
|
|
client.cookies.set("cmvr_session", cookie)
|
||
|
|
assert (await client.get("/api/auth/me")).status_code == 401
|
||
|
|
|
||
|
|
|
||
|
|
async def test_non_admin_cannot_create_users_and_public_registration_is_absent(other_client):
|
||
|
|
assert (await other_client.post("/api/auth/users", json={"username": "bob", "password": PASSWORD})).status_code == 403
|
||
|
|
assert (await other_client.post("/api/auth/register", json={})).status_code == 404
|
||
|
|
|
||
|
|
|
||
|
|
async def test_requested_initial_admin_password_is_hashed_and_not_reset_on_restart(tmp_path):
|
||
|
|
from sqlalchemy import select
|
||
|
|
from app.auth import verify_password
|
||
|
|
from app.config import Settings
|
||
|
|
from app.main import create_app
|
||
|
|
from app.models import User
|
||
|
|
from conftest import FakeProvider
|
||
|
|
|
||
|
|
settings = Settings(mode="test", database_url=f"sqlite+aiosqlite:///{tmp_path / 'admin.sqlite3'}",
|
||
|
|
workspace_root=tmp_path, admin_username="admin", admin_password="123456",
|
||
|
|
run_coordinator=False, cookie_secure=False)
|
||
|
|
app = create_app(settings, provider=FakeProvider())
|
||
|
|
async with app.router.lifespan_context(app):
|
||
|
|
async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://test") as client:
|
||
|
|
login = await client.post("/api/auth/login", json={"username": "admin", "password": "123456"})
|
||
|
|
assert login.status_code == 200 and login.json()["user"]["role"] == "admin"
|
||
|
|
async with app.state.db.session() as session:
|
||
|
|
user = await session.scalar(select(User).where(User.username == "admin"))
|
||
|
|
assert user.password_hash != "123456" and verify_password("123456", user.password_hash)
|
||
|
|
client.headers["X-CSRF-Token"] = login.json()["csrf_token"]
|
||
|
|
denied = await client.post("/api/auth/users", json={"username": "ordinary", "password": "123456"})
|
||
|
|
assert denied.status_code == 422 # Ordinary accounts retain their stronger password policy.
|
||
|
|
|
||
|
|
settings.admin_password = PASSWORD
|
||
|
|
restarted = create_app(settings, provider=FakeProvider())
|
||
|
|
async with restarted.router.lifespan_context(restarted):
|
||
|
|
async with httpx.AsyncClient(transport=httpx.ASGITransport(app=restarted), base_url="http://test") as client:
|
||
|
|
assert (await client.post("/api/auth/login", json={"username": "admin", "password": "123456"})).status_code == 200
|
||
|
|
assert (await client.post("/api/auth/login", json={"username": "admin", "password": PASSWORD})).status_code == 401
|