feat: new buttons to assign to schedule
This commit is contained in:
1 parent
048e80ddba
commit
ec3e9e46fe
7 files changed
+394
-44
No files matched your search
@@ -23,7 +23,7 @@ MONTH = 10
|
||||
def db():
|
||||
engine = create_engine("sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool)
|
||||
Base.metadata.create_all(engine)
|
||||
session = sessionmaker(bind=engine, expire_on_commit=False)()
|
||||
session = sessionmaker(bind=engine, expire_on_commit=False, autoflush=False)()
|
||||
try:
|
||||
yield session
|
||||
finally:
|
||||
@@ -305,7 +305,9 @@ def test_apply_rotation_updates_only_automatic_rows_in_selected_schedule_and_mon
|
||||
db.expire_all()
|
||||
assert db.get(Assignment, first.id).user_id == ana.id
|
||||
assert db.get(Assignment, first.id).note == "Keep this note"
|
||||
assert db.get(Assignment, unassigned.id).user_id == bruno.id
|
||||
# The preserved swap on the 17th supplies Bruno as the reference;
|
||||
# in the reordered Bruno -> Ana rotation, the 24th goes to Ana.
|
||||
assert db.get(Assignment, unassigned.id).user_id == ana.id
|
||||
for assignment_id, expected in untouched.items():
|
||||
row = db.get(Assignment, assignment_id)
|
||||
assert (row.user_id, row.source) == expected
|
||||
@@ -392,18 +394,25 @@ def test_reset_unassigned_manual_row_and_apply_later_when_members_become_active(
|
||||
assert (row.user_id, row.source) == (ana.id, "generated")
|
||||
|
||||
|
||||
def test_rotation_controls_render_manual_rows_as_separate_forms(client, db):
|
||||
def test_rotation_controls_render_regenerate_for_every_row_as_separate_forms(client, db):
|
||||
night, sat, _, _, _, _ = seed(db)
|
||||
sign_in(client)
|
||||
ensure_month_assignments(db, YEAR, MONTH)
|
||||
row = assignment_on(db, sat, date(YEAR, MONTH, 3))
|
||||
row.source = "manual"
|
||||
swapped = assignment_on(db, sat, date(YEAR, MONTH, 10))
|
||||
swapped.source = "swap"
|
||||
db.commit()
|
||||
page = client.get(f"/admin/schedules/{sat.id}?year={YEAR}&month={MONTH}").text
|
||||
assert "Apply rotation to October" in page
|
||||
assert f'form="use-rotation-{row.id}"' in page
|
||||
assert f'action="/admin/schedules/{sat.id}/assignments/{row.id}/use-rotation"' in page
|
||||
assert page.index(f'id="use-rotation-{row.id}"') > page.index("Save October")
|
||||
assert page.count(">Regenerate</button>") == 5
|
||||
assert f'form="regenerate-{row.id}"' in page
|
||||
assert f'form="regenerate-{swapped.id}"' in page
|
||||
assert f'action="/admin/schedules/{sat.id}/assignments/{row.id}/regenerate"' in page
|
||||
assert page.index(f'id="regenerate-{row.id}"') > page.index("Save October")
|
||||
assert "Generate after date" in page
|
||||
assert 'name="after_date" value="2026-09-30"' in page
|
||||
assert 'name="through_date" value="2026-10-31"' in page
|
||||
assert "Apply rotation" not in client.get(f"/admin/schedules/{night.id}").text
|
||||
|
||||
|
||||
@@ -416,10 +425,12 @@ def test_rotation_actions_reject_invalid_schedule_row_and_month(client, db):
|
||||
assert client.post(f"/admin/schedules/{night.id}/apply-rotation", data=base).status_code == 400
|
||||
assert client.post(f"/admin/schedules/{sat.id}/apply-rotation", data={**base, "month": 13}).status_code == 400
|
||||
assert client.post(f"/admin/schedules/{sun.id}/assignments/{row.id}/use-rotation", data=base).status_code == 404
|
||||
assert client.post(f"/admin/schedules/{sat.id}/assignments/{row.id}/use-rotation", data=base).status_code == 400
|
||||
row.source = "swap"
|
||||
wrong_day = Assignment(schedule_id=sat.id, date=date(YEAR, MONTH, 4), user_id=None, source="manual")
|
||||
db.add(wrong_day)
|
||||
db.commit()
|
||||
assert client.post(f"/admin/schedules/{sat.id}/assignments/{row.id}/use-rotation", data=base).status_code == 400
|
||||
assert client.post(f"/admin/schedules/{sat.id}/assignments/{wrong_day.id}/regenerate", data=base).status_code == 400
|
||||
night_row = assignment_on(db, night, date(YEAR, MONTH, 3))
|
||||
assert client.post(f"/admin/schedules/{night.id}/assignments/{night_row.id}/regenerate", data=base).status_code == 400
|
||||
|
||||
|
||||
@pytest.mark.parametrize("action", ["apply-rotation", "use-rotation"])
|
||||
@@ -442,3 +453,181 @@ def test_rotation_actions_require_admin_and_csrf(client, db, action):
|
||||
token = CSRF_RE.search(client.get("/login").text).group(1)
|
||||
client.post("/login", data={"username": "member", "password": "member-password", "csrf_token": token})
|
||||
assert client.post(url, data={**data, "csrf_token": token}).status_code == 403
|
||||
|
||||
|
||||
def test_use_rotation_selects_next_person_after_previous_actual_assignee(client, db):
|
||||
_, sat, _, ana, bruno, _ = seed(db)
|
||||
token = sign_in(client)
|
||||
ensure_month_assignments(db, YEAR, MONTH)
|
||||
carla = User(name="Carla", is_active=True, notify_email=False)
|
||||
db.add(carla)
|
||||
db.flush()
|
||||
db.add(RotationMember(schedule_id=sat.id, user_id=carla.id, position=2))
|
||||
previous = assignment_on(db, sat, date(YEAR, MONTH, 3))
|
||||
previous.user_id, previous.source = bruno.id, "manual"
|
||||
target = assignment_on(db, sat, date(YEAR, MONTH, 10))
|
||||
target.user_id, target.source = ana.id, "manual"
|
||||
db.commit()
|
||||
other_rows = {row.id: (row.user_id, row.source) for row in db.scalars(select(Assignment)).all() if row.id != target.id}
|
||||
|
||||
response = client.post(f"/admin/schedules/{sat.id}/assignments/{target.id}/use-rotation", data={
|
||||
"csrf_token": token,
|
||||
}, follow_redirects=False)
|
||||
assert response.status_code == 303
|
||||
db.expire_all()
|
||||
assert (target.user_id, target.source) == (carla.id, "generated")
|
||||
for assignment_id, expected in other_rows.items():
|
||||
row = db.get(Assignment, assignment_id)
|
||||
assert (row.user_id, row.source) == expected
|
||||
|
||||
|
||||
def test_apply_rotation_continues_chronologically_from_previous_month_and_preserved_override(client, db):
|
||||
_, sat, _, ana, bruno, _ = seed(db)
|
||||
token = sign_in(client)
|
||||
ensure_month_assignments(db, YEAR, MONTH)
|
||||
carla = User(name="Carla", is_active=True, notify_email=False)
|
||||
db.add(carla)
|
||||
db.flush()
|
||||
db.add(RotationMember(schedule_id=sat.id, user_id=carla.id, position=2))
|
||||
db.add(Assignment(schedule_id=sat.id, date=date(YEAR, 9, 26), user_id=bruno.id, source="manual"))
|
||||
preserved = assignment_on(db, sat, date(YEAR, MONTH, 17))
|
||||
preserved.user_id, preserved.source = carla.id, "swap"
|
||||
db.commit()
|
||||
|
||||
response = client.post(f"/admin/schedules/{sat.id}/apply-rotation", data={
|
||||
"csrf_token": token, "year": YEAR, "month": MONTH,
|
||||
}, follow_redirects=False)
|
||||
assert response.status_code == 303
|
||||
db.expire_all()
|
||||
rows = [assignment_on(db, sat, date(YEAR, MONTH, day)) for day in (3, 10, 17, 24, 31)]
|
||||
assert [row.user_id for row in rows] == [carla.id, ana.id, carla.id, ana.id, bruno.id]
|
||||
assert preserved.source == "swap"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("source", ["generated", "manual", "swap"])
|
||||
def test_regenerate_replaces_any_assignment_source_and_retains_history(client, db, source):
|
||||
_, sat, _, ana, bruno, _ = seed(db)
|
||||
token = sign_in(client)
|
||||
ensure_month_assignments(db, YEAR, MONTH)
|
||||
row = assignment_on(db, sat, date(YEAR, MONTH, 3))
|
||||
row.user_id, row.source, row.note = ana.id, source, "Keep date history"
|
||||
completed = SwapRequest(assignment_id=row.id, requester_id=bruno.id, accepted_by_id=ana.id, status="approved")
|
||||
stale = SwapRequest(assignment_id=row.id, requester_id=ana.id, target_user_id=bruno.id, status="open")
|
||||
db.add_all([completed, stale])
|
||||
db.commit()
|
||||
other_rows = {r.id: (r.user_id, r.source) for r in db.scalars(select(Assignment)).all() if r.id != row.id}
|
||||
|
||||
response = client.post(f"/admin/schedules/{sat.id}/assignments/{row.id}/regenerate", data={
|
||||
"csrf_token": token,
|
||||
}, follow_redirects=False)
|
||||
assert response.status_code == 303
|
||||
db.expire_all()
|
||||
assert (row.user_id, row.source, row.note) == (bruno.id, "generated", "Keep date history")
|
||||
assert completed.status == "approved"
|
||||
assert stale.status == "cancelled"
|
||||
for assignment_id, expected in other_rows.items():
|
||||
current = db.get(Assignment, assignment_id)
|
||||
assert (current.user_id, current.source) == expected
|
||||
|
||||
|
||||
def test_generate_after_date_spans_months_with_exclusive_start_and_inclusive_end(client, db):
|
||||
_, sat, _, ana, bruno, admin = seed(db)
|
||||
token = sign_in(client)
|
||||
ensure_month_assignments(db, YEAR, MONTH)
|
||||
reference = assignment_on(db, sat, date(YEAR, MONTH, 31))
|
||||
reference.user_id, reference.source = ana.id, "manual"
|
||||
outdated = Assignment(schedule_id=sat.id, date=date(YEAR, 11, 7), user_id=ana.id, source="generated")
|
||||
swapped = Assignment(schedule_id=sat.id, date=date(YEAR, 11, 14), user_id=bruno.id, source="swap")
|
||||
later = Assignment(schedule_id=sat.id, date=date(YEAR, 11, 28), user_id=admin.id, source="generated")
|
||||
db.add_all([outdated, swapped, later])
|
||||
db.flush()
|
||||
stale = SwapRequest(assignment_id=outdated.id, requester_id=ana.id, status="pending_approval")
|
||||
db.add(stale)
|
||||
db.commit()
|
||||
untouched = {row.id: (row.user_id, row.source) for row in db.scalars(select(Assignment)).all()
|
||||
if row.id != outdated.id}
|
||||
|
||||
response = client.post(f"/admin/schedules/{sat.id}/generate-after", data={
|
||||
"csrf_token": token, "after_date": "2026-10-31", "through_date": "2026-11-21",
|
||||
}, follow_redirects=False)
|
||||
assert response.status_code == 303
|
||||
assert response.headers["location"] == f"/admin/schedules/{sat.id}?year=2026&month=11"
|
||||
db.expire_all()
|
||||
assert (outdated.user_id, outdated.source) == (bruno.id, "generated")
|
||||
generated = assignment_on(db, sat, date(YEAR, 11, 21))
|
||||
assert (generated.user_id, generated.source) == (ana.id, "generated")
|
||||
assert stale.status == "cancelled"
|
||||
for assignment_id, expected in untouched.items():
|
||||
row = db.get(Assignment, assignment_id)
|
||||
assert (row.user_id, row.source) == expected
|
||||
november = db.scalars(select(Assignment).where(
|
||||
Assignment.date >= date(YEAR, 11, 1), Assignment.date <= date(YEAR, 11, 30)
|
||||
)).all()
|
||||
assert len(november) == 4
|
||||
assert all(row.schedule_id == sat.id for row in november)
|
||||
before_repeat = [(r.id, r.user_id, r.source) for r in november]
|
||||
log_count = len(db.scalars(select(NotificationLog)).all())
|
||||
client.post(f"/admin/schedules/{sat.id}/generate-after", data={
|
||||
"csrf_token": token, "after_date": "2026-10-31", "through_date": "2026-11-21",
|
||||
}, follow_redirects=False)
|
||||
db.expire_all()
|
||||
assert [(r.id, r.user_id, r.source) for r in november] == before_repeat
|
||||
assert len(db.scalars(select(NotificationLog)).all()) == log_count
|
||||
|
||||
|
||||
def test_generate_after_date_fills_multiple_months_in_order(client, db):
|
||||
_, sat, _, ana, bruno, _ = seed(db)
|
||||
token = sign_in(client)
|
||||
ensure_month_assignments(db, YEAR, MONTH)
|
||||
reference = assignment_on(db, sat, date(YEAR, MONTH, 31))
|
||||
reference.user_id = bruno.id
|
||||
db.commit()
|
||||
response = client.post(f"/admin/schedules/{sat.id}/generate-after", data={
|
||||
"csrf_token": token, "after_date": "2026-10-31", "through_date": "2026-12-05",
|
||||
}, follow_redirects=False)
|
||||
assert response.status_code == 303
|
||||
rows = db.scalars(select(Assignment).where(Assignment.date > date(YEAR, MONTH, 31)).order_by(Assignment.date)).all()
|
||||
assert [r.date for r in rows] == [date(YEAR, 11, day) for day in (7, 14, 21, 28)] + [date(YEAR, 12, 5)]
|
||||
assert [r.user_id for r in rows] == [ana.id, bruno.id, ana.id, bruno.id, ana.id]
|
||||
assert all(r.schedule_id == sat.id and r.source == "generated" for r in rows)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("after_date,through_date,status", [
|
||||
("2026-10-31", "2026-10-31", 400),
|
||||
("2026-10-31", "2026-10-01", 400),
|
||||
("1999-12-30", "2026-10-31", 400),
|
||||
("2026-10-31", "2101-01-01", 400),
|
||||
("not-a-date", "2026-10-31", 422),
|
||||
("2026-10-31", "2026-11-31", 422),
|
||||
])
|
||||
def test_generate_after_rejects_invalid_ranges_without_editing_data(client, db, after_date, through_date, status):
|
||||
_, sat, _, _, _, _ = seed(db)
|
||||
token = sign_in(client)
|
||||
snapshot = [(r.id, r.user_id, r.source) for r in db.scalars(select(Assignment)).all()]
|
||||
assert client.post(f"/admin/schedules/{sat.id}/generate-after", data={
|
||||
"csrf_token": token, "after_date": after_date, "through_date": through_date,
|
||||
}).status_code == status
|
||||
assert [(r.id, r.user_id, r.source) for r in db.scalars(select(Assignment)).all()] == snapshot
|
||||
|
||||
|
||||
@pytest.mark.parametrize("action", ["regenerate", "generate-after"])
|
||||
def test_new_generation_controls_require_admin_and_csrf(client, db, action):
|
||||
night, sat, _, _, _, _ = seed(db)
|
||||
ensure_month_assignments(db, YEAR, MONTH)
|
||||
row = assignment_on(db, sat, date(YEAR, MONTH, 3))
|
||||
member = User(name="Member", username="member", password_hash=hash_password("member-password"), is_active=True)
|
||||
db.add(member)
|
||||
db.commit()
|
||||
url = f"/admin/schedules/{sat.id}/assignments/{row.id}/regenerate" if action == "regenerate" else (
|
||||
f"/admin/schedules/{sat.id}/generate-after"
|
||||
)
|
||||
data = {"csrf_token": "invalid", "after_date": "2026-10-31", "through_date": "2026-11-21"}
|
||||
assert client.post(url, data=data).status_code == 401
|
||||
token = sign_in(client)
|
||||
assert client.post(url, data=data).status_code == 400
|
||||
if action == "generate-after":
|
||||
assert client.post(f"/admin/schedules/{night.id}/generate-after", data={**data, "csrf_token": token}).status_code == 400
|
||||
client.post("/logout", data={"csrf_token": token})
|
||||
token = CSRF_RE.search(client.get("/login").text).group(1)
|
||||
client.post("/login", data={"username": "member", "password": "member-password", "csrf_token": token})
|
||||
assert client.post(url, data={**data, "csrf_token": token}).status_code == 403
|
||||
@@ -1,12 +1,13 @@
|
||||
from datetime import date
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import create_engine, select
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy.pool import StaticPool
|
||||
|
||||
from app.database import Base
|
||||
from app.models import Assignment, RotationMember, Schedule, User
|
||||
from app.scheduling import ensure_month_assignments
|
||||
from app.scheduling import ensure_month_assignments, rotation_user_for_date
|
||||
from app.security import hash_password
|
||||
|
||||
|
||||
@@ -17,7 +18,7 @@ def make_db() -> Session:
|
||||
poolclass=StaticPool,
|
||||
)
|
||||
Base.metadata.create_all(engine)
|
||||
return Session(engine)
|
||||
return Session(engine, autoflush=False)
|
||||
|
||||
|
||||
def add_user(db: Session, name: str, pos: int) -> User:
|
||||
@@ -124,3 +125,87 @@ def test_existing_assignment_is_not_rewritten_when_rotation_changes():
|
||||
ensure_month_assignments(db, 2026, 5)
|
||||
refreshed = db.get(Assignment, first_sat.id)
|
||||
assert refreshed.user_id == original_user
|
||||
|
||||
|
||||
def test_generation_continues_from_previous_people_across_months_independently():
|
||||
with make_db() as db:
|
||||
alice, bruno, carla = [add_user(db, name, pos) for pos, name in enumerate(["Alice", "Bruno", "Carla"], 1)]
|
||||
_, sat, sun = seed_schedules(db)
|
||||
for schedule in (sat, sun):
|
||||
for pos, person in enumerate((alice, bruno, carla), 1):
|
||||
db.add(RotationMember(schedule_id=schedule.id, user_id=person.id, position=pos))
|
||||
db.add_all([
|
||||
Assignment(schedule_id=sat.id, date=date(2026, 9, 26), user_id=bruno.id, source="manual"),
|
||||
Assignment(schedule_id=sun.id, date=date(2026, 9, 27), user_id=carla.id, source="swap"),
|
||||
])
|
||||
db.commit()
|
||||
|
||||
assignments = ensure_month_assignments(db, 2026, 10)
|
||||
assert [row.user_id for row in assignments if row.schedule_id == sat.id] == [
|
||||
carla.id, alice.id, bruno.id, carla.id, alice.id,
|
||||
]
|
||||
assert [row.user_id for row in assignments if row.schedule_id == sun.id] == [
|
||||
alice.id, bruno.id, carla.id, alice.id,
|
||||
]
|
||||
november = ensure_month_assignments(db, 2026, 11)
|
||||
assert next(row for row in november if row.schedule_id == sat.id).user_id == bruno.id
|
||||
assert next(row for row in november if row.schedule_id == sun.id).user_id == bruno.id
|
||||
|
||||
|
||||
def test_generation_uses_preserved_manual_and_swapped_shifts_as_references():
|
||||
with make_db() as db:
|
||||
alice, bruno, carla = [add_user(db, name, pos) for pos, name in enumerate(["Alice", "Bruno", "Carla"], 1)]
|
||||
_, sat, _ = seed_schedules(db)
|
||||
for pos, person in enumerate((alice, bruno, carla), 1):
|
||||
db.add(RotationMember(schedule_id=sat.id, user_id=person.id, position=pos))
|
||||
manual = Assignment(schedule_id=sat.id, date=date(2026, 10, 10), user_id=bruno.id, source="manual")
|
||||
swapped = Assignment(schedule_id=sat.id, date=date(2026, 10, 24), user_id=alice.id, source="swap")
|
||||
db.add_all([manual, swapped])
|
||||
db.commit()
|
||||
original_ids = manual.id, swapped.id
|
||||
|
||||
rows = ensure_month_assignments(db, 2026, 10)
|
||||
assert [row.user_id for row in rows if row.schedule_id == sat.id] == [
|
||||
alice.id, bruno.id, carla.id, alice.id, bruno.id,
|
||||
]
|
||||
assert (manual.id, swapped.id) == original_ids
|
||||
assert (manual.source, swapped.source) == ("manual", "swap")
|
||||
snapshot = [(row.id, row.user_id, row.source) for row in rows]
|
||||
assert [(row.id, row.user_id, row.source) for row in ensure_month_assignments(db, 2026, 10)] == snapshot
|
||||
|
||||
|
||||
@pytest.mark.parametrize("previous, inactive, expected", [
|
||||
("Alice", ["Alice"], "Bruno"), # inactive reference still has its position
|
||||
("Alice", ["Bruno"], "Carla"), # skip inactive next person
|
||||
("Carla", [], "Alice"), # wrap at the end
|
||||
("Alice", ["Alice", "Bruno", "Carla"], None),
|
||||
("Outside", [], "Alice"), # removed person: fallback to the anchor
|
||||
])
|
||||
def test_reference_selection_handles_inactive_members_wraparound_and_missing_members(previous, inactive, expected):
|
||||
with make_db() as db:
|
||||
users = {name: add_user(db, name, pos) for pos, name in enumerate(["Alice", "Bruno", "Carla", "Outside"], 1)}
|
||||
_, sat, _ = seed_schedules(db)
|
||||
for pos, name in enumerate(("Alice", "Bruno", "Carla"), 1):
|
||||
users[name].is_active = name not in inactive
|
||||
db.add(RotationMember(schedule_id=sat.id, user_id=users[name].id, position=pos))
|
||||
db.add(Assignment(schedule_id=sat.id, date=date(2026, 9, 26), user_id=users[previous].id, source="manual"))
|
||||
db.commit()
|
||||
selected = rotation_user_for_date(db, sat, date(2026, 10, 3))
|
||||
assert (selected.name if selected else None) == expected
|
||||
|
||||
|
||||
def test_reference_ignores_unassigned_dates_current_date_and_future_shifts():
|
||||
with make_db() as db:
|
||||
alice, bruno, carla = [add_user(db, name, pos) for pos, name in enumerate(["Alice", "Bruno", "Carla"], 1)]
|
||||
_, sat, sun = seed_schedules(db)
|
||||
for pos, person in enumerate((alice, bruno, carla), 1):
|
||||
db.add(RotationMember(schedule_id=sat.id, user_id=person.id, position=pos))
|
||||
db.add_all([
|
||||
Assignment(schedule_id=sat.id, date=date(2026, 9, 19), user_id=bruno.id, source="manual"),
|
||||
Assignment(schedule_id=sat.id, date=date(2026, 9, 26), user_id=None, source="manual"),
|
||||
Assignment(schedule_id=sun.id, date=date(2026, 9, 27), user_id=carla.id, source="manual"),
|
||||
Assignment(schedule_id=sat.id, date=date(2026, 10, 3), user_id=alice.id, source="manual"),
|
||||
Assignment(schedule_id=sat.id, date=date(2026, 10, 10), user_id=carla.id, source="manual"),
|
||||
])
|
||||
db.commit()
|
||||
assert rotation_user_for_date(db, sat, date(2026, 10, 3)).id == carla.id
|
||||
Reference in new issue
Block a user