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,226 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""Phase 8c gegen PostgreSQL: Antworten ändern mit Vermerk, Ergebnisse für Teilnehmende,
|
||||
Teilnehmerlisten als PDF (CI)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import zipfile
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
import pytest
|
||||
|
||||
pytest.importorskip("fastapi")
|
||||
|
||||
from helpers import new_client, token_for
|
||||
from sqlalchemy import select, update
|
||||
from sqlalchemy.orm import Session
|
||||
from test_evaluation import prepare
|
||||
from test_forms import (
|
||||
add_question,
|
||||
create_form,
|
||||
creator,
|
||||
fill,
|
||||
form_row,
|
||||
participant,
|
||||
question_ids,
|
||||
)
|
||||
from test_phase5 import slot_form
|
||||
|
||||
from surveybox import db
|
||||
from surveybox.models import AuditEvent, Form, Response, ResponseNote
|
||||
|
||||
pytestmark = pytest.mark.db
|
||||
|
||||
|
||||
def first_response(pid: str) -> Response:
|
||||
with Session(db.get_engine()) as s:
|
||||
found = s.scalar(select(Response).where(Response.form_id == form_row(pid).id)
|
||||
.order_by(Response.submitted_at, Response.id))
|
||||
assert found is not None
|
||||
s.expunge(found)
|
||||
return found
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# Antwort ändern mit Vermerk
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_manager_changes_answer_with_note(app: Any) -> None:
|
||||
carla, pid, _, slot_q, name_q, options = prepare(app)
|
||||
rid = first_response(pid).public_id # Bernd Krause, Früh
|
||||
url = f"/forms/{pid}/responses/{rid}/edit"
|
||||
page = carla.get(url).text
|
||||
assert 'name="edit_note"' in page and "Bernd Krause" in page
|
||||
data = {f"q{slot_q}": options[1], f"q{name_q}_first": "Bernd",
|
||||
f"q{name_q}_last": "Krause-Neu", f"q{name_q}_email": "b@x.de"}
|
||||
# Ohne Vermerk wird nichts gespeichert
|
||||
resp = carla.post(url, data={"csrf_token": token_for(carla, url), **data})
|
||||
assert resp.status_code == 400 and "Please describe why" in resp.text
|
||||
assert "Krause-Neu" not in carla.get(f"/forms/{pid}/responses/{rid}").text
|
||||
resp = carla.post(url, data={"csrf_token": token_for(carla, url), **data,
|
||||
"edit_note": "Anruf am Montag: <b>Schicht tauschen</b>"})
|
||||
assert resp.status_code == 303 and "response_edited" in resp.headers["location"]
|
||||
view = carla.get(f"/forms/{pid}/responses/{rid}").text
|
||||
assert "Krause-Neu" in view and "Anruf am Montag" in view and "<b>" in view
|
||||
with Session(db.get_engine()) as s:
|
||||
note = s.scalar(select(ResponseNote))
|
||||
assert note is not None and set(note.changed) == {"Schicht", "Name"}
|
||||
actions = list(s.scalars(select(AuditEvent.action)))
|
||||
assert "response.edited" in actions
|
||||
|
||||
|
||||
def test_note_in_subject_export_and_removed_when_anonymized(app: Any) -> None:
|
||||
from surveybox.forms import retention, subject_export
|
||||
|
||||
carla, pid, _, slot_q, name_q, options = prepare(app)
|
||||
response = first_response(pid)
|
||||
url = f"/forms/{pid}/responses/{response.public_id}/edit"
|
||||
carla.post(url, data={"csrf_token": token_for(carla, url), f"q{slot_q}": options[0],
|
||||
f"q{name_q}_first": "Bernd", f"q{name_q}_last": "Krause",
|
||||
"edit_note": "Nur geprüft"})
|
||||
with Session(db.get_engine()) as s:
|
||||
own = s.get(Response, response.id)
|
||||
assert own is not None
|
||||
data = subject_export.for_response(s, own, ZoneInfo("UTC"))
|
||||
assert any(n["note"] == "Nur geprüft" for n in str_notes(data))
|
||||
form = s.get(Form, own.form_id)
|
||||
assert form is not None
|
||||
retention.anonymize_responses(s, form, [own.id], datetime.now(UTC))
|
||||
s.commit()
|
||||
assert s.scalar(select(ResponseNote)) is None
|
||||
|
||||
|
||||
def str_notes(data: Any) -> list[dict[str, Any]]:
|
||||
"""Vermerke irgendwo in der Auskunft finden (Aufbau des Umschlags egal)."""
|
||||
found: list[dict[str, Any]] = []
|
||||
|
||||
def walk(node: Any) -> None:
|
||||
if isinstance(node, dict):
|
||||
if isinstance(node.get("notes"), list):
|
||||
found.extend(node["notes"])
|
||||
for value in node.values():
|
||||
walk(value)
|
||||
elif isinstance(node, list):
|
||||
for value in node:
|
||||
walk(value)
|
||||
|
||||
walk(data)
|
||||
return found
|
||||
|
||||
|
||||
def test_evaluators_cannot_change_answers(app: Any) -> None:
|
||||
_, pid, _, _, _, _ = prepare(app)
|
||||
rid = first_response(pid).public_id
|
||||
assert new_client(app).get(f"/forms/{pid}/responses/{rid}/edit").status_code == 303
|
||||
olga = participant(app, "olga@example.de")
|
||||
assert olga.get(f"/forms/{pid}/responses/{rid}/edit").status_code in (403, 404)
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# Ergebnisse für Teilnehmende
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_results_after_own_participation(app: Any) -> None:
|
||||
owner = creator(app)
|
||||
pid, token, slot_q, options = slot_form(owner, capacity="2",
|
||||
results_visibility="after_submit")
|
||||
add_question(owner, pid, "short_text", label="Bemerkung")
|
||||
note_q = question_ids(pid)[-1]
|
||||
other = new_client(app)
|
||||
page = other.get(f"/f/{token}/results").text
|
||||
assert "after you have taken part" in page
|
||||
guest = new_client(app)
|
||||
assert fill(guest, token, {f"q{slot_q}": options[0],
|
||||
f"q{note_q}": "geheimer Freitext"}).status_code == 303
|
||||
assert "/results" in guest.get(f"/f/{token}/thanks").text
|
||||
page = guest.get(f"/f/{token}/results").text
|
||||
assert "Results from 1 answer" in page and "Schicht" in page
|
||||
assert "geheimer Freitext" not in page and "Bemerkung" not in page
|
||||
# Wer nicht teilgenommen hat, sieht weiterhin nichts
|
||||
assert "after you have taken part" in other.get(f"/f/{token}/results").text
|
||||
|
||||
|
||||
def test_results_after_close_and_never(app: Any) -> None:
|
||||
owner = creator(app)
|
||||
pid, token, slot_q, options = slot_form(owner, capacity="2",
|
||||
results_visibility="after_close")
|
||||
assert fill(new_client(app), token, {f"q{slot_q}": options[0]}).status_code == 303
|
||||
visitor = new_client(app)
|
||||
assert "once the form has closed" in visitor.get(f"/f/{token}/results").text
|
||||
with Session(db.get_engine()) as s:
|
||||
s.execute(update(Form).where(Form.id == form_row(pid).id).values(
|
||||
status="closed", closed_at=datetime.now(UTC)))
|
||||
s.commit()
|
||||
closed = visitor.get(f"/f/{token}").text
|
||||
assert f"/f/{token}/results" in closed
|
||||
assert "Results from 1 answer" in visitor.get(f"/f/{token}/results").text
|
||||
with Session(db.get_engine()) as s:
|
||||
s.execute(update(Form).where(Form.id == form_row(pid).id).values(
|
||||
results_visibility="never"))
|
||||
s.commit()
|
||||
assert visitor.get(f"/f/{token}/results").status_code == 404
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# Teilnehmerlisten als PDF
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_participant_lists_pdf(app: Any) -> None:
|
||||
pytest.importorskip("reportlab")
|
||||
carla, pid, _, _, _, _ = prepare(app)
|
||||
csrf = token_for(carla, f"/forms/{pid}/responses")
|
||||
resp = carla.post(f"/forms/{pid}/responses/lists",
|
||||
data={"csrf_token": csrf, "signature": "on"})
|
||||
assert resp.status_code == 200 and resp.headers["content-type"] == "application/pdf"
|
||||
assert resp.content.startswith(b"%PDF") and "attachment" in resp.headers[
|
||||
"content-disposition"]
|
||||
with Session(db.get_engine()) as s:
|
||||
details = [e.details for e in s.scalars(
|
||||
select(AuditEvent).where(AuditEvent.action == "form.exported"))]
|
||||
assert details[-1]["format"] == "lists" # type: ignore[index]
|
||||
|
||||
|
||||
def test_answers_as_pdf_and_ods(app: Any) -> None:
|
||||
pytest.importorskip("reportlab")
|
||||
carla, pid, _, _, _, _ = prepare(app)
|
||||
for kind, start in (("pdf", b"%PDF"), ("ods", b"PK")):
|
||||
csrf = token_for(carla, f"/forms/{pid}/responses")
|
||||
resp = carla.post(f"/forms/{pid}/responses/export",
|
||||
data={"csrf_token": csrf, "format": kind})
|
||||
assert resp.status_code == 200 and resp.content.startswith(start)
|
||||
assert resp.headers["content-disposition"].endswith(f'.{kind}"')
|
||||
with zipfile.ZipFile(io.BytesIO(resp.content)) as z:
|
||||
assert "Krause" in z.read("content.xml").decode()
|
||||
csrf = token_for(carla, f"/forms/{pid}/responses")
|
||||
assert carla.post(f"/forms/{pid}/responses/export",
|
||||
data={"csrf_token": csrf, "format": "docx"}).status_code == 404
|
||||
|
||||
|
||||
def test_no_participant_lists_without_slots(app: Any) -> None:
|
||||
pytest.importorskip("reportlab")
|
||||
owner = creator(app)
|
||||
pid = create_form(owner)
|
||||
add_question(owner, pid, "short_text", label="Was bringst du mit?")
|
||||
page = owner.get(f"/forms/{pid}/responses").text
|
||||
assert "Participant lists (PDF)" not in page and 'value="pdf"' in page
|
||||
csrf = token_for(owner, f"/forms/{pid}/responses")
|
||||
assert owner.post(f"/forms/{pid}/responses/lists",
|
||||
data={"csrf_token": csrf}).status_code == 404
|
||||
|
||||
|
||||
def test_no_participant_lists_for_anonymous_forms(app: Any) -> None:
|
||||
pytest.importorskip("reportlab")
|
||||
carla, pid, _, _, _, _ = prepare(app)
|
||||
with Session(db.get_engine()) as s:
|
||||
s.execute(update(Form).where(Form.id == form_row(pid).id).values(anonymous=True))
|
||||
s.commit()
|
||||
csrf = token_for(carla, f"/forms/{pid}/responses")
|
||||
assert "Participant lists (PDF)" not in carla.get(f"/forms/{pid}/responses").text
|
||||
resp = carla.post(f"/forms/{pid}/responses/lists", data={"csrf_token": csrf})
|
||||
assert resp.status_code == 404
|
||||
Reference in new issue
Block a user