135 lines
5.3 KiB
Python
135 lines
5.3 KiB
Python
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
"""Fragengruppen gegen PostgreSQL: Einfügen, Tabelle, Höchstzahl, Buchung (CI)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
pytest.importorskip("fastapi")
|
|
|
|
from fastapi.testclient import TestClient
|
|
from helpers import new_client, token_for
|
|
from sqlalchemy.orm import Session
|
|
from test_forms import (
|
|
add_question,
|
|
create_form,
|
|
creator,
|
|
fill,
|
|
form_row,
|
|
question_ids,
|
|
save_settings,
|
|
set_status,
|
|
)
|
|
from test_phase5 import bookings
|
|
|
|
from surveybox import db
|
|
from surveybox.models import Question
|
|
|
|
pytestmark = pytest.mark.db
|
|
|
|
|
|
def add_to_group(client: TestClient, pid: str, group: int, type_key: str, **fields: Any) -> None:
|
|
url = f"/forms/{pid}/questions/new?type={type_key}&group={group}"
|
|
csrf = token_for(client, url)
|
|
resp = client.post(url, data={"csrf_token": csrf, **fields})
|
|
assert resp.status_code == 303, resp.text
|
|
|
|
|
|
def types_in_order(pid: str) -> list[str]:
|
|
with Session(db.get_engine()) as s:
|
|
return [s.get(Question, qid).type for qid in question_ids(pid)] # type: ignore[union-attr]
|
|
|
|
|
|
def shift_form(owner: TestClient, max_select: str = "1") -> tuple[str, str, list[int]]:
|
|
pid = create_form(owner)
|
|
add_question(owner, pid, "question_group", label="Schichten", member_type="slots",
|
|
max_select=max_select)
|
|
add_question(owner, pid, "short_text", label="Bemerkung")
|
|
(group, _) = question_ids(pid)
|
|
# In die Gruppe einfügen: landet vor "Bemerkung", nicht am Ende
|
|
add_to_group(owner, pid, group, "slots", label="Freitag", capacity="1")
|
|
add_to_group(owner, pid, group, "slots", label="Samstag", capacity="5")
|
|
assert types_in_order(pid) == ["question_group", "slots", "slots", "short_text"]
|
|
assert save_settings(owner, pid, access="link").status_code == 303
|
|
assert set_status(owner, pid, "open").status_code == 303
|
|
return pid, form_row(pid).share_token, question_ids(pid)
|
|
|
|
|
|
def test_group_is_shown_as_table_and_indented(app: Any) -> None:
|
|
owner = creator(app)
|
|
pid, token, (group, fri, _sat, _) = shift_form(owner)
|
|
designer = owner.get(f"/forms/{pid}").text
|
|
assert designer.count("group-member") == 2
|
|
assert f"group={group}" in designer
|
|
page = new_client(app).get(f"/f/{token}").text
|
|
assert 'class="group-table group-slots"' in page
|
|
assert f'data-qid="{fri}" data-type="slots"' in page
|
|
assert 'value="single"' in page
|
|
assert 'data-max="1"' in page
|
|
|
|
|
|
def test_group_limit_and_booking(app: Any) -> None:
|
|
owner = creator(app)
|
|
_, token, (_group, fri, sat, _) = shift_form(owner)
|
|
guest = new_client(app)
|
|
resp = fill(guest, token, {f"q{fri}": "single", f"q{sat}": "single"})
|
|
assert resp.status_code == 400
|
|
assert "more entries than allowed" in resp.text
|
|
assert bookings(fri) == [] and bookings(sat) == []
|
|
|
|
assert fill(guest, token, {f"q{fri}": "single"}).status_code == 303
|
|
assert bookings(fri) == [("single", "confirmed", False)]
|
|
# Freitag hat nur einen Platz
|
|
other = new_client(app)
|
|
resp = fill(other, token, {f"q{fri}": "single"})
|
|
assert resp.status_code == 409
|
|
places = other.get(f"/f/{token}/places").json()
|
|
assert places[str(fri)]["single"] == {"free": 0, "full": True}
|
|
|
|
|
|
def test_required_rows_do_not_block_the_form(app: Any) -> None:
|
|
"""Drei Pflicht-Schichten bei höchstens einer: Die Gruppe regelt die Pflicht."""
|
|
owner = creator(app)
|
|
pid = create_form(owner)
|
|
add_question(owner, pid, "question_group", label="Schichten", member_type="slots",
|
|
min_select="1", max_select="1")
|
|
(group,) = question_ids(pid)
|
|
for label in ("Fr", "Sa", "So"):
|
|
add_to_group(owner, pid, group, "slots", label=label, required="on")
|
|
assert "A single entry cannot be required" in owner.get(
|
|
f"/forms/{pid}/questions/new?type=slots&group={group}").text
|
|
assert save_settings(owner, pid, access="link").status_code == 303
|
|
assert set_status(owner, pid, "open").status_code == 303
|
|
token = form_row(pid).share_token
|
|
(_, fri, _, _) = question_ids(pid)
|
|
guest = new_client(app)
|
|
resp = fill(guest, token, {})
|
|
assert resp.status_code == 400
|
|
assert "Please choose more entries in this group" in resp.text
|
|
assert "Please answer this question" not in resp.text
|
|
assert fill(guest, token, {f"q{fri}": "single"}).status_code == 303
|
|
|
|
|
|
def test_unlimited_group(app: Any) -> None:
|
|
owner = creator(app)
|
|
_, token, (_, fri, sat, _) = shift_form(owner, max_select="0")
|
|
resp = fill(new_client(app), token, {f"q{fri}": "single", f"q{sat}": "single"})
|
|
assert resp.status_code == 303
|
|
|
|
|
|
def test_capacity_edit_keeps_bookings(app: Any) -> None:
|
|
owner = creator(app)
|
|
pid, token, (_, fri, _, _) = shift_form(owner)
|
|
assert fill(new_client(app), token, {f"q{fri}": "single"}).status_code == 303
|
|
csrf = token_for(owner, f"/forms/{pid}/questions/{fri}")
|
|
resp = owner.post(f"/forms/{pid}/questions/{fri}", data={
|
|
"csrf_token": csrf, "label": "Freitag abend", "slot_mode": "single", "capacity": "3"})
|
|
assert resp.status_code == 303
|
|
assert bookings(fri) == [("single", "confirmed", False)]
|
|
with Session(db.get_engine()) as s:
|
|
q = s.get(Question, fri)
|
|
assert q is not None
|
|
assert q.config["options"] == [{"id": "single", "label": "Freitag abend", "capacity": 3}]
|