197 lines
8.4 KiB
Python
197 lines
8.4 KiB
Python
# 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
|