100 lines
3.9 KiB
Python
100 lines
3.9 KiB
Python
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
"""Phase 7c gegen PostgreSQL: Datenexport für Betroffene (CI)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import re
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
pytest.importorskip("fastapi")
|
|
|
|
from fastapi.testclient import TestClient
|
|
from helpers import new_client, token_for
|
|
from sqlalchemy import select
|
|
from sqlalchemy.orm import Session
|
|
from test_forms import creator, fill, participant
|
|
from test_mail import admin, content, messages
|
|
from test_phase5 import slot_form
|
|
|
|
from surveybox import db
|
|
from surveybox.models import AuditEvent, UserAccount
|
|
from surveybox.settings import get_settings
|
|
|
|
pytestmark = pytest.mark.db
|
|
|
|
|
|
def download(client: TestClient, page: str, action: str, **data: Any) -> dict[str, Any]:
|
|
csrf = token_for(client, page)
|
|
resp = client.post(action, data={"csrf_token": csrf, **data})
|
|
assert resp.status_code == 200, resp.text
|
|
assert resp.headers["content-type"].startswith("application/json")
|
|
assert "attachment" in resp.headers["content-disposition"]
|
|
assert resp.headers["cache-control"] == "no-store"
|
|
return dict(json.loads(resp.content))
|
|
|
|
|
|
def actions() -> list[str]:
|
|
with Session(db.get_engine()) as s:
|
|
return list(s.scalars(select(AuditEvent.action)))
|
|
|
|
|
|
def test_user_downloads_own_data(app: Any) -> None:
|
|
carla = creator(app)
|
|
_, token, qid, (early, _) = slot_form(carla)
|
|
paul = participant(app)
|
|
fill(paul, token, {f"q{qid}": early})
|
|
data = download(paul, "/account", "/account/export")
|
|
assert data["format"] == "surveybox-subject-export"
|
|
assert data["account"]["email"] == "paul@example.de"
|
|
(response,) = data["responses"]
|
|
assert response["form"]["title"] == "Sommerfest"
|
|
assert response["answers"][0]["answer"]
|
|
assert response["places"][0]["status"] == "confirmed"
|
|
assert "sessions" in data and "email_log" in data
|
|
assert "account.exported" in actions()
|
|
|
|
|
|
def test_guest_downloads_data_by_mail_and_by_personal_link(app: Any) -> None:
|
|
carla = creator(app)
|
|
_, token, qid, (early, _) = slot_form(carla, capacity="", guest_email="optional")
|
|
fill(new_client(app), token, {f"q{qid}": early, "guest_email": "gast@example.de"})
|
|
guest = new_client(app)
|
|
csrf = token_for(guest, "/links")
|
|
resp = guest.post("/links/export", data={"csrf_token": csrf, "email": "gast@example.de"})
|
|
assert "sent a link to download it" in resp.text
|
|
(mail,) = messages("guest_export")
|
|
link = re.search(r"https?://\S+/g/export/\S+", content(mail)["text"]).group(0) # type: ignore[union-attr]
|
|
path = link.replace(get_settings().public_origin, "")
|
|
data = download(guest, path, path)
|
|
assert data["guest"]["email"] == "gast@example.de"
|
|
assert len(data["responses"]) == 1
|
|
assert "guest.exported" in actions()
|
|
# Gast ohne Adresse: über den persönlichen Link
|
|
other = new_client(app)
|
|
fill(other, token, {f"q{qid}": early})
|
|
personal = re.search(r"/r/[A-Za-z0-9_-]+", other.get(f"/f/{token}/thanks").text).group(0) # type: ignore[union-attr]
|
|
single = download(other, personal, f"{personal}/export")
|
|
assert len(single["responses"]) == 1 and "guest" not in single
|
|
# Falscher Link
|
|
assert new_client(app).get("/g/export/abc.123.def").status_code == 404
|
|
|
|
|
|
def test_admin_exports_user_and_guest(app: Any) -> None:
|
|
root = admin(app)
|
|
paul = participant(app)
|
|
del paul
|
|
with Session(db.get_engine()) as s:
|
|
target = s.scalar(select(UserAccount).where(UserAccount.email == "paul@example.de"))
|
|
assert target is not None
|
|
pid = target.public_id
|
|
data = download(root, f"/admin/users/{pid}", f"/admin/users/{pid}/export")
|
|
assert data["account"]["email"] == "paul@example.de"
|
|
csrf = token_for(root, "/admin/users")
|
|
resp = root.post("/admin/guests/export",
|
|
data={"csrf_token": csrf, "email": "niemand@example.de"})
|
|
assert resp.status_code == 303 and "guest_unknown" in resp.headers["location"]
|
|
assert "admin.user_exported" in actions()
|