feature:Mail-Einladung
This commit is contained in:
1 parent
675a8cd6f2
commit
639f14fc2e
43 files changed
+3017
-486
No files matched your search
@@ -22,7 +22,7 @@ from test_mail import content, messages
|
||||
from test_phase5 import bookings, options_of, slot_form
|
||||
|
||||
from surveybox import db
|
||||
from surveybox.forms import digest, followups, reminders
|
||||
from surveybox.forms import digest, followups, invitations
|
||||
from surveybox.models import (
|
||||
Followup,
|
||||
FollowupDocument,
|
||||
@@ -179,7 +179,7 @@ def test_reminder_for_invited_people(app: Any) -> None:
|
||||
s.add(FormAccessEntry(form_id=form.id, user_id=users[email]))
|
||||
s.execute(update(Form).where(Form.id == form.id).values(
|
||||
access=FormAccess.RESTRICTED, closes_at=datetime.now(UTC) + timedelta(days=2),
|
||||
reminder_days=3))
|
||||
remind_first_at=datetime.now(UTC) - timedelta(minutes=1), invite_accounts=False))
|
||||
s.commit()
|
||||
olga = new_client(app)
|
||||
from helpers import login
|
||||
@@ -188,9 +188,9 @@ def test_reminder_for_invited_people(app: Any) -> None:
|
||||
assert fill(olga, token, {f"q{qid}": early}).status_code == 303
|
||||
settings = get_settings()
|
||||
with Session(db.get_engine()) as s:
|
||||
assert reminders.run_due(s, settings, datetime.now(UTC)) == 1
|
||||
assert invitations.run_due(s, settings, datetime.now(UTC)) == 1
|
||||
s.commit()
|
||||
assert reminders.run_due(s, settings, datetime.now(UTC)) == 0 # nur einmal
|
||||
assert invitations.run_due(s, settings, datetime.now(UTC)) == 0 # nur einmal
|
||||
s.commit()
|
||||
assert [m.recipient for m in messages("reminder")] == ["paul@example.de"]
|
||||
|
||||
|
||||
@@ -0,0 +1,196 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""Einladungen per E-Mail gegen PostgreSQL: Gäste mit persönlichem Link, Konten,
|
||||
Erinnerungen, Abmelden, Termine, Löschen (CI)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
pytest.importorskip("fastapi")
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from helpers import new_client, token_for
|
||||
from sqlalchemy import func, select, update
|
||||
from sqlalchemy.orm import Session
|
||||
from test_forms import creator, fill, form_row, participant, save_settings, simple_form
|
||||
from test_guests import links_in
|
||||
from test_mail import content, messages
|
||||
|
||||
from surveybox import db
|
||||
from surveybox.forms import invitations
|
||||
from surveybox.models import Form, FormAccessEntry, FormInvitation, Response
|
||||
from surveybox.settings import get_settings
|
||||
|
||||
pytestmark = pytest.mark.db
|
||||
|
||||
|
||||
def invite(client: TestClient, pid: str, addresses: str) -> Any:
|
||||
url = f"/forms/{pid}/invitations"
|
||||
return client.post(url, data={"csrf_token": token_for(client, url), "addresses": addresses})
|
||||
|
||||
|
||||
def run(now: datetime | None = None) -> int:
|
||||
with Session(db.get_engine()) as s:
|
||||
sent = invitations.run_due(s, get_settings(), now or datetime.now(UTC))
|
||||
s.commit()
|
||||
return sent
|
||||
|
||||
|
||||
def set_form(pid: str, **values: Any) -> None:
|
||||
with Session(db.get_engine()) as s:
|
||||
s.execute(update(Form).where(Form.id == form_row(pid).id).values(**values))
|
||||
s.commit()
|
||||
|
||||
|
||||
def rows(pid: str) -> list[FormInvitation]:
|
||||
with Session(db.get_engine()) as s:
|
||||
found = list(s.scalars(select(FormInvitation).where(
|
||||
FormInvitation.form_id == form_row(pid).id).order_by(FormInvitation.id)))
|
||||
for row in found:
|
||||
s.expunge(row)
|
||||
return found
|
||||
|
||||
|
||||
def test_guest_and_account_invitations(app: Any) -> None:
|
||||
owner = creator(app)
|
||||
participant(app) # paul@example.de mit Konto
|
||||
pid, token, (q_name, _) = simple_form(owner, access="restricted", guest_email="optional")
|
||||
page = invite(owner, pid, "Erika Mustermann <erika@example.org>, paul@example.de; kaputt@")
|
||||
assert page.status_code == 200
|
||||
assert "1 guest invited" in page.text and "1 person with an account invited" in page.text
|
||||
assert "kaputt@" in page.text
|
||||
# Das Konto steht jetzt in der Zugriffsliste
|
||||
with Session(db.get_engine()) as s:
|
||||
assert s.scalar(select(func.count()).select_from(FormAccessEntry).where(
|
||||
FormAccessEntry.form_id == form_row(pid).id)) == 1
|
||||
# Noch einmal dieselben Adressen: nichts doppelt
|
||||
assert "2 addresses were already invited" in invite(
|
||||
owner, pid, "erika@example.org paul@example.de").text
|
||||
|
||||
assert run() == 2
|
||||
mails = {m.recipient: content(m)["text"] for m in messages("invitation")}
|
||||
assert set(mails) == {"erika@example.org", "paul@example.de"}
|
||||
assert "/i/" in mails["erika@example.org"] and "/mail/invitation/" in mails["erika@example.org"]
|
||||
assert f"/f/{token}" in mails["paul@example.de"] and "/i/" not in mails["paul@example.de"]
|
||||
assert run() == 0 # nur einmal
|
||||
|
||||
# Gast öffnet den Link: Zugang ohne Anmeldung, obwohl nur Eingeladene dürfen
|
||||
link = next(u for u in links_in(mails["erika@example.org"], "/i/"))
|
||||
guest = new_client(app)
|
||||
assert new_client(app).get(f"/f/{token}").status_code == 303 # ohne Einladung: Anmeldung
|
||||
resp = guest.get(link)
|
||||
assert resp.status_code == 303 and resp.headers["location"] == f"/f/{token}"
|
||||
page = guest.get(f"/f/{token}")
|
||||
assert page.status_code == 200 and 'value="erika@example.org"' in page.text
|
||||
assert fill(guest, token, {f"q{q_name}": "Erika"}).status_code == 303
|
||||
(erika,) = [r for r in rows(pid) if r.email]
|
||||
assert erika.participated_at is not None and erika.response_id is not None
|
||||
with Session(db.get_engine()) as s:
|
||||
response = s.get(Response, erika.response_id)
|
||||
assert response is not None and response.guest_contact_id is not None
|
||||
# Derselbe Link führt jetzt zur eigenen Antwort
|
||||
again = new_client(app).get(link)
|
||||
assert again.status_code == 303 and again.headers["location"].startswith("/r/")
|
||||
|
||||
overview = owner.get(f"/forms/{pid}/invitations").text
|
||||
assert "erika@example.org" in overview and "Anna" in overview # Paul heißt "Anna"
|
||||
|
||||
# Erinnerung: nur wer noch nicht teilgenommen hat (Paul)
|
||||
set_form(pid, remind_first_at=datetime.now(UTC) + timedelta(seconds=1))
|
||||
assert run(datetime.now(UTC) + timedelta(minutes=1)) == 1
|
||||
assert [m.recipient for m in messages("reminder")] == ["paul@example.de"]
|
||||
|
||||
|
||||
def test_reminder_only_for_people_invited_before(app: Any) -> None:
|
||||
owner = creator(app)
|
||||
pid, _, _ = simple_form(owner, access="link")
|
||||
invite(owner, pid, "a@example.org")
|
||||
assert run() == 1
|
||||
later = datetime.now(UTC) + timedelta(hours=1)
|
||||
set_form(pid, remind_first_at=later)
|
||||
invite(owner, pid, "b@example.org")
|
||||
# b wird erst nach dem Termin eingeladen: Einladung für b, Erinnerung nur für a
|
||||
assert run(later + timedelta(minutes=1)) == 2
|
||||
assert [m.recipient for m in messages("reminder")] == ["a@example.org"]
|
||||
assert sorted(m.recipient for m in messages("invitation")) == ["a@example.org",
|
||||
"b@example.org"]
|
||||
|
||||
|
||||
def test_invitation_waits_for_date_and_open_form(app: Any) -> None:
|
||||
owner = creator(app)
|
||||
pid, _, _ = simple_form(owner, access="link")
|
||||
start = datetime.now(UTC) + timedelta(days=1)
|
||||
set_form(pid, invite_at=start)
|
||||
invite(owner, pid, "c@example.org")
|
||||
assert run() == 0
|
||||
assert run(start + timedelta(minutes=1)) == 1
|
||||
set_form(pid, status="closed")
|
||||
invite(owner, pid, "d@example.org")
|
||||
assert run(start + timedelta(minutes=2)) == 0 # geschlossen: nichts mehr
|
||||
|
||||
|
||||
def test_guest_can_stop_and_delete(app: Any) -> None:
|
||||
owner = creator(app)
|
||||
pid, _, _ = simple_form(owner, access="link")
|
||||
invite(owner, pid, "e@example.org")
|
||||
run()
|
||||
(mail,) = messages("invitation")
|
||||
stop = links_in(content(mail)["text"], "/mail/invitation/")[0]
|
||||
client = new_client(app)
|
||||
assert "No further emails" in client.get(stop).text
|
||||
assert client.post(stop, data={"action": "stop"}).status_code == 200
|
||||
(row,) = rows(pid)
|
||||
assert row.unsubscribed_at is not None
|
||||
set_form(pid, remind_first_at=datetime.now(UTC))
|
||||
assert run(datetime.now(UTC) + timedelta(minutes=1)) == 0
|
||||
assert client.post(stop, data={"action": "delete"}).status_code == 200
|
||||
assert rows(pid) == []
|
||||
assert client.get(stop).status_code == 404
|
||||
|
||||
|
||||
def test_dates_checked_against_participation(app: Any) -> None:
|
||||
owner = creator(app)
|
||||
pid, _, _ = simple_form(owner, access="link")
|
||||
resp = save_settings(owner, pid, access="link", opens_at="2030-01-01T10:00",
|
||||
closes_at="2030-01-10T10:00", remind_first_at="2030-01-12T10:00")
|
||||
assert resp.status_code == 400 and "before the end of participation" in resp.text
|
||||
resp = save_settings(owner, pid, access="link", opens_at="2030-01-01T10:00",
|
||||
closes_at="2030-01-10T10:00", invite_at="2030-01-03T10:00",
|
||||
remind_first_at="2030-01-02T10:00")
|
||||
assert resp.status_code == 400 and "after the invitations" in resp.text
|
||||
resp = save_settings(owner, pid, access="link", opens_at="2030-01-01T10:00",
|
||||
closes_at="2030-01-10T10:00", invite_at="2030-01-02T10:00",
|
||||
remind_first_at="2030-01-05T10:00", remind_last_at="2030-01-08T10:00")
|
||||
assert resp.status_code == 303
|
||||
row = form_row(pid)
|
||||
assert row.invite_at is not None and row.remind_last_at is not None
|
||||
|
||||
|
||||
def test_retention_and_erasure_remove_invitations(app: Any) -> None:
|
||||
from surveybox.forms import retention
|
||||
|
||||
owner = creator(app)
|
||||
pid, _, _ = simple_form(owner, access="link")
|
||||
invite(owner, pid, "f@example.org, g@example.org")
|
||||
assert len(rows(pid)) == 2
|
||||
with Session(db.get_engine()) as s:
|
||||
invitations.delete_for_email(s, "F@example.org")
|
||||
s.commit()
|
||||
assert [r.email for r in rows(pid)] == ["g@example.org"]
|
||||
with Session(db.get_engine()) as s:
|
||||
form = s.get(Form, form_row(pid).id)
|
||||
assert form is not None
|
||||
retention.apply(s, form, datetime.now(UTC))
|
||||
s.commit()
|
||||
assert rows(pid) == []
|
||||
|
||||
|
||||
def test_invitations_need_edit_rights(app: Any) -> None:
|
||||
owner = creator(app)
|
||||
pid, _, _ = simple_form(owner, access="link")
|
||||
other = participant(app)
|
||||
assert other.get(f"/forms/{pid}/invitations").status_code in (403, 404)
|
||||
assert owner.get(f"/forms/{pid}/invitations").status_code == 200
|
||||
@@ -0,0 +1,37 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""Einladungen ohne Datenbank: Adresslisten und Termine."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
import pytest
|
||||
|
||||
pytest.importorskip("sqlalchemy")
|
||||
|
||||
from surveybox.forms import invitations
|
||||
|
||||
NOW = datetime(2026, 12, 1, 9, 0, tzinfo=UTC)
|
||||
|
||||
|
||||
def test_addresses_from_pasted_text() -> None:
|
||||
raw = ("Erika Mustermann <Erika@Example.org>, max@example.org; max@example.org\n"
|
||||
"'kaputt@' ohne-at (anna@example.de)")
|
||||
valid, invalid = invitations.parse_addresses(raw)
|
||||
assert valid == ["erika@example.org", "max@example.org", "anna@example.de"]
|
||||
assert invalid == ["kaputt@"]
|
||||
assert invitations.parse_addresses("") == ([], [])
|
||||
|
||||
|
||||
def test_schedule_inside_participation() -> None:
|
||||
opens, closes = NOW, NOW + timedelta(days=10)
|
||||
day = timedelta(days=1)
|
||||
check = invitations.check_schedule
|
||||
assert check(opens, closes, None, None, None) is None
|
||||
assert check(opens, closes, NOW + day, NOW + 3 * day, NOW + 5 * day) is None
|
||||
assert check(None, None, None, NOW, NOW + day) is None # ohne Zeitraum: nur Reihenfolge
|
||||
assert check(opens, closes, NOW - day, None, None) == "schedule_before_open"
|
||||
assert check(opens, closes, None, None, closes) == "schedule_after_close"
|
||||
assert check(opens, closes, None, NOW + 5 * day, NOW + 3 * day) == "schedule_order"
|
||||
assert check(opens, closes, NOW + 4 * day, NOW + 3 * day, None) == (
|
||||
"schedule_reminder_before_invite")
|
||||
@@ -120,6 +120,9 @@ CONTEXTS: dict[str, dict[str, Any]] = {
|
||||
"documents": [("Programm <pdf>", "https://umfrage.example/d/abc.def")],
|
||||
"expires": "24. Februar 2027", "link": ""},
|
||||
"reminder": {"link": "https://umfrage.example/f/t", "closes": "1. Juli 2026, 18:00"},
|
||||
"invitation": {"link": "https://umfrage.example/i/abc.def", "closes": "1. Juli 2026, 18:00",
|
||||
"invited_guest": True,
|
||||
"stop_link": "https://umfrage.example/mail/invitation/abc.def"},
|
||||
"retention_notice": {"mode": "anonymize", "date": "1. Juli 2026", "responses": 3,
|
||||
"link": "https://umfrage.example/forms/x/responses",
|
||||
"settings_link": "https://umfrage.example/forms/x/settings"},
|
||||
|
||||
@@ -29,6 +29,7 @@ CSRF_EXEMPT = {
|
||||
# Abmelden per Klick im Mailprogramm (RFC 8058): kommt ohne Cookies; der
|
||||
# signierte Schlüssel im Link ist die Berechtigung
|
||||
"/mail/unsubscribe/{token}",
|
||||
"/mail/invitation/{token}",
|
||||
}
|
||||
PROTECTED_PREFIXES = ("/admin", "/forms", "/account")
|
||||
DUMMY_ID = "00000000-0000-0000-0000-000000000000"
|
||||
|
||||
Reference in new issue
Block a user