Files
surveybox/tests/test_guests_units.py
T
marco.morath 42dd1f2d4a
CI / audit (push) Skipped
CI / checks (push) Failing after 37s
CI / image (push) Skipped
Initial Commit: Status Testing
2026-11-25 18:46:04 +01:00

124 lines
5.6 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
"""Gäste und Selbstregistrierung ohne Datenbank (braucht die Modelle)."""
from __future__ import annotations
import time
import uuid
from types import SimpleNamespace
import pytest
pytest.importorskip("sqlalchemy")
from surveybox.accounts import registration
from surveybox.forms import guests
from surveybox.mail import notify
SECRET = "s" * 48
def form(**kw: object) -> SimpleNamespace:
return SimpleNamespace(**{"anonymous": False, "guest_email": "optional", **kw})
def test_registration_token_roundtrip_and_expiry() -> None:
now = time.time()
token = registration.make_token(SECRET, "anna@example.de", "Anna Ä", "de", now)
pending = registration.parse_token(SECRET, token, now + 60)
assert pending == registration.Pending("anna@example.de", "Anna Ä", "de")
assert registration.parse_token(SECRET, token, now + 25 * 3600) is None
assert registration.parse_token("x" * 48, token, now) is None
raw, _, mac = token.partition(".")
other = registration.make_token(SECRET, "eve@example.de", "Eve", "de", now)
assert registration.parse_token(SECRET, other.partition(".")[0] + "." + mac, now) is None
assert registration.parse_token(SECRET, raw, now) is None
assert registration.parse_token(SECRET, "", now) is None
def test_parse_guest_email() -> None:
user = SimpleNamespace(id=1)
assert guests.parse_email(form(), user, "a@b.de").email is None # angemeldet
assert guests.parse_email(form(anonymous=True), None, "a@b.de").email is None
assert guests.parse_email(form(guest_email="none"), None, "a@b.de").email is None
assert guests.parse_email(form(), None, " Anna@Example.DE ").email == "anna@example.de"
assert guests.parse_email(form(), None, "").error is None
assert guests.parse_email(form(guest_email="required"), None, "").error == "required"
assert guests.parse_email(form(), None, "kein@").error == "email"
def test_wants_link() -> None:
assert guests.wants_link(form(), None)
assert not guests.wants_link(form(anonymous=True), None)
assert not guests.wants_link(form(), SimpleNamespace(id=1))
def test_guest_unsubscribe_token() -> None:
contact = SimpleNamespace(public_id=uuid.uuid4(), email="g@example.de", language="de",
confirmed_at=None, notify=True)
recipient = notify.for_guest(contact, SimpleNamespace(default_language="de")) # type: ignore[arg-type]
token = notify.unsubscribe_token(SECRET, recipient, "promoted")
found = notify.parse_unsubscribe(SECRET, token)
assert found is not None and found.kind == "guest" and found.public_id == contact.public_id
def test_guest_needs_confirmation_for_notifications() -> None:
contact = SimpleNamespace(public_id=uuid.uuid4(), email="g@example.de", language=None,
confirmed_at=None, notify=True)
settings = SimpleNamespace(default_language="fr")
recipient = notify.for_guest(contact, settings) # type: ignore[arg-type]
assert recipient.language == "fr"
assert not notify.allowed(None, recipient, "promoted", None) # type: ignore[arg-type]
contact.confirmed_at = 1
assert notify.allowed(None, recipient, "promoted", None) # type: ignore[arg-type]
assert not notify.allowed(None, recipient, "comment_new", None) # type: ignore[arg-type]
contact.notify = False
assert not notify.allowed(None, recipient, "promoted", None) # type: ignore[arg-type]
def test_email_from_answers() -> None:
questions = [SimpleNamespace(id=1, type="short_text"), SimpleNamespace(id=2, type="email"),
SimpleNamespace(id=3, type="email")]
stored = {2: {"value_text": " Anna@Example.DE "}, 3: {"value_text": "b@example.de"}}
assert guests.email_from_answers(
form(guest_email="question", questions=questions), stored, set()) == "anna@example.de"
# Ausgeblendete Frage zählt nicht, die nächste E-Mail-Frage schon
assert guests.email_from_answers(
form(guest_email="question", questions=questions), stored, {2}) == "b@example.de"
assert guests.email_from_answers(form(guest_email="optional", questions=questions),
stored, set()) is None
assert guests.email_from_answers(
form(guest_email="question", anonymous=True, questions=questions), stored, set()) is None
# Das eigene Feld gibt es dann nicht
assert guests.parse_email(form(guest_email="question"), None, "a@b.de").email is None
def test_followup_document_token() -> None:
from surveybox.forms import followups
doc = SimpleNamespace(public_id=uuid.uuid4())
now = time.time()
token = followups.document_token(SECRET, doc, "u:x", int(now + 60)) # type: ignore[arg-type]
assert followups.parse_document_token(SECRET, token, now) == doc.public_id
assert followups.parse_document_token(SECRET, token, now + 120) is None # abgelaufen
assert followups.parse_document_token("x" * 48, token, now) is None
raw, _, mac = token.partition(".")
assert followups.parse_document_token(SECRET, raw + "." + mac[::-1], now) is None
def test_followup_placeholders() -> None:
from surveybox.forms import followups
f = SimpleNamespace(title="Fest")
assert followups.personalize("Hallo {name}, zu {form}: {x} %s {}", "Anna", f) == ( # type: ignore[arg-type]
"Hallo Anna, zu Fest: {x} %s {}")
def test_removed_options() -> None:
from surveybox.forms import changes
before = [{"id": "a", "label": "A"}, {"id": "b", "label": "B"}]
after = [{"id": "b", "label": "B2"}]
assert changes.removed_options(before, after) == [{"id": "a", "label": "A"}]
assert changes.removed_options(before, before) == []