Initial Commit: Status Testing
This commit is contained in:
1 parent
1eff675f05
commit
42dd1f2d4a
286 files changed
+64800
-369
No files matched your search
@@ -0,0 +1,216 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""Passkeys und Gruppen - mit Software-Authenticator gegen PostgreSQL (CI)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
pytest.importorskip("fastapi")
|
||||
pytest.importorskip("webauthn")
|
||||
pytest.importorskip("cbor2")
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from helpers import (
|
||||
PASSWORD,
|
||||
SoftAuthenticator,
|
||||
csrf_from,
|
||||
login,
|
||||
make_user,
|
||||
new_client,
|
||||
post_json,
|
||||
token_for,
|
||||
)
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from surveybox import db
|
||||
from surveybox.models import AuditEvent, GroupMember, Role, UserGroup, WebAuthnCredential
|
||||
|
||||
pytestmark = pytest.mark.db
|
||||
|
||||
|
||||
def register_passkey(client: TestClient, auth: SoftAuthenticator, name: str = "Laptop") -> Any:
|
||||
csrf = token_for(client, "/account")
|
||||
options = post_json(client, "/account/passkeys/options", {"password": PASSWORD}, csrf)
|
||||
assert options.status_code == 200, options.text
|
||||
data = options.json()
|
||||
return post_json(
|
||||
client,
|
||||
"/account/passkeys/register",
|
||||
{"challengeId": data["challengeId"], "credential": auth.create(data["publicKey"]),
|
||||
"name": name},
|
||||
csrf,
|
||||
)
|
||||
|
||||
|
||||
def passkey_login(client: TestClient, auth: SoftAuthenticator, base: str = "/login/passkey",
|
||||
page: str = "/login") -> Any:
|
||||
csrf = token_for(client, page)
|
||||
options = post_json(client, f"{base}/options", {}, csrf)
|
||||
assert options.status_code == 200, options.text
|
||||
data = options.json()
|
||||
return post_json(
|
||||
client,
|
||||
f"{base}/verify",
|
||||
{"challengeId": data["challengeId"], "credential": auth.get(data["publicKey"]),
|
||||
"next": "/account"},
|
||||
csrf,
|
||||
)
|
||||
|
||||
|
||||
def test_register_passkey_and_sign_in_without_password( # type: ignore[no-untyped-def]
|
||||
client: TestClient, app
|
||||
) -> None:
|
||||
make_user()
|
||||
login(client, "anna@example.de")
|
||||
auth = SoftAuthenticator()
|
||||
resp = register_passkey(client, auth)
|
||||
assert resp.status_code == 200, resp.text
|
||||
assert resp.json()["redirect"] == "/account?notice=passkey_added"
|
||||
page = client.get("/account")
|
||||
assert "Laptop" in page.text
|
||||
|
||||
with new_client(app) as fresh:
|
||||
result = passkey_login(fresh, auth)
|
||||
assert result.status_code == 200, result.text
|
||||
assert result.json()["redirect"] == "/account"
|
||||
assert fresh.get("/account").status_code == 200
|
||||
|
||||
with Session(db.get_engine()) as s:
|
||||
row = s.scalar(select(WebAuthnCredential))
|
||||
assert row is not None and row.sign_count == 1 and row.last_used_at is not None
|
||||
assert row.transports == ["internal"]
|
||||
|
||||
|
||||
def test_wrong_password_blocks_registration(client: TestClient) -> None:
|
||||
make_user()
|
||||
login(client, "anna@example.de")
|
||||
csrf = token_for(client, "/account")
|
||||
resp = post_json(client, "/account/passkeys/options", {"password": "falsch"}, csrf)
|
||||
assert resp.status_code == 400
|
||||
assert "current password is wrong" in resp.json()["error"]
|
||||
|
||||
|
||||
def test_challenge_is_single_use(client: TestClient) -> None:
|
||||
make_user()
|
||||
login(client, "anna@example.de")
|
||||
auth = SoftAuthenticator()
|
||||
csrf = token_for(client, "/account")
|
||||
data = post_json(client, "/account/passkeys/options", {"password": PASSWORD}, csrf).json()
|
||||
body = {"challengeId": data["challengeId"], "credential": auth.create(data["publicKey"])}
|
||||
assert post_json(client, "/account/passkeys/register", body, csrf).status_code == 200
|
||||
again = post_json(client, "/account/passkeys/register", body, csrf)
|
||||
assert again.status_code == 400
|
||||
assert "expired" in again.json()["error"]
|
||||
|
||||
|
||||
def test_unknown_passkey_is_rejected(client: TestClient) -> None:
|
||||
make_user()
|
||||
stranger = SoftAuthenticator()
|
||||
stranger.user_handle = b"x" * 32
|
||||
resp = passkey_login(client, stranger)
|
||||
assert resp.status_code == 400
|
||||
assert "not registered" in resp.json()["error"]
|
||||
|
||||
|
||||
def test_json_endpoints_need_csrf_header(client: TestClient) -> None:
|
||||
client.get("/login")
|
||||
resp = client.post("/login/passkey/options", json={})
|
||||
assert resp.status_code == 403
|
||||
|
||||
|
||||
def test_passkey_as_second_factor_for_admin( # type: ignore[no-untyped-def]
|
||||
client: TestClient, app
|
||||
) -> None:
|
||||
make_user(Role.ADMIN, "admin@example.de")
|
||||
login(client, "admin@example.de")
|
||||
# Admin ohne zweiten Faktor kommt nur auf die Kontoseite; dort legt er einen Passkey an
|
||||
assert client.get("/admin/users").status_code == 303
|
||||
auth = SoftAuthenticator()
|
||||
assert register_passkey(client, auth).status_code == 200
|
||||
assert client.get("/admin/users").status_code == 200
|
||||
|
||||
with new_client(app) as fresh:
|
||||
resp = login(fresh, "admin@example.de")
|
||||
assert resp.headers["location"].startswith("/login/mfa")
|
||||
result = passkey_login(fresh, auth, base="/login/mfa/passkey", page="/login/mfa")
|
||||
assert result.status_code == 200, result.text
|
||||
assert fresh.get("/admin/users").status_code == 200
|
||||
|
||||
|
||||
def test_admin_cannot_remove_last_second_factor(client: TestClient) -> None:
|
||||
make_user(Role.ADMIN, "admin@example.de")
|
||||
login(client, "admin@example.de")
|
||||
register_passkey(client, SoftAuthenticator())
|
||||
with Session(db.get_engine()) as s:
|
||||
pk_id = s.scalar(select(WebAuthnCredential.id))
|
||||
page = client.get("/account")
|
||||
resp = client.post(
|
||||
f"/account/passkeys/{pk_id}/delete",
|
||||
data={"current_password": PASSWORD, "csrf_token": csrf_from(page.text)},
|
||||
)
|
||||
assert resp.status_code == 400
|
||||
with Session(db.get_engine()) as s:
|
||||
assert s.scalar(select(func.count()).select_from(WebAuthnCredential)) == 1
|
||||
|
||||
|
||||
def test_participant_can_remove_passkey(client: TestClient) -> None:
|
||||
make_user()
|
||||
login(client, "anna@example.de")
|
||||
register_passkey(client, SoftAuthenticator())
|
||||
with Session(db.get_engine()) as s:
|
||||
pk_id = s.scalar(select(WebAuthnCredential.id))
|
||||
page = client.get("/account")
|
||||
resp = client.post(
|
||||
f"/account/passkeys/{pk_id}/delete",
|
||||
data={"current_password": PASSWORD, "csrf_token": csrf_from(page.text)},
|
||||
)
|
||||
assert resp.headers["location"] == "/account?notice=passkey_removed"
|
||||
|
||||
|
||||
def test_groups_lifecycle(client: TestClient) -> None:
|
||||
make_user(Role.ADMIN, "admin@example.de")
|
||||
member, _ = make_user(Role.PARTICIPANT, "bob@example.de")
|
||||
login(client, "admin@example.de")
|
||||
register_passkey(client, SoftAuthenticator())
|
||||
|
||||
page = client.get("/admin/groups")
|
||||
created = client.post(
|
||||
"/admin/groups",
|
||||
data={"name": "Jugendfeuerwehr", "description": "Gruppenabende",
|
||||
"csrf_token": csrf_from(page.text)},
|
||||
)
|
||||
assert created.status_code == 303
|
||||
group_url = created.headers["location"].split("?")[0]
|
||||
|
||||
duplicate = client.post(
|
||||
"/admin/groups",
|
||||
data={"name": "jugendfeuerwehr", "csrf_token": csrf_from(page.text)},
|
||||
)
|
||||
assert duplicate.status_code == 400
|
||||
|
||||
edit = client.get(group_url)
|
||||
assert "Jugendfeuerwehr" in edit.text and "bob@example.de" in edit.text
|
||||
client.post(f"{group_url}/members",
|
||||
data={"user": str(member.public_id), "csrf_token": csrf_from(edit.text)})
|
||||
with Session(db.get_engine()) as s:
|
||||
assert s.scalar(select(func.count()).select_from(GroupMember)) == 1
|
||||
|
||||
client.post(f"{group_url}/members/{member.public_id}/remove",
|
||||
data={"csrf_token": csrf_from(edit.text)})
|
||||
no_confirm = client.post(f"{group_url}/delete", data={"csrf_token": csrf_from(edit.text)})
|
||||
assert no_confirm.status_code == 400
|
||||
deleted = client.post(f"{group_url}/delete",
|
||||
data={"confirm": "on", "csrf_token": csrf_from(edit.text)})
|
||||
assert deleted.headers["location"] == "/admin/groups?notice=group_deleted"
|
||||
|
||||
with Session(db.get_engine()) as s:
|
||||
assert s.scalar(select(func.count()).select_from(UserGroup)) == 0
|
||||
actions = set(s.scalars(select(AuditEvent.action)))
|
||||
assert {"admin.group_created", "admin.group_member_added", "admin.group_member_removed",
|
||||
"admin.group_deleted", "account.passkey_added"} <= actions
|
||||
audit = client.get("/admin/audit")
|
||||
assert re.search(r"Group created", audit.text)
|
||||
Reference in new issue
Block a user