Files
crr-shifts/tests/test_calendar.py
T

145 lines
5.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from datetime import date, time, timedelta
from sqlalchemy import select
from app.calendar import build_feed
from app.models import Assignment, Schedule, User
# the fixtures live next door and are shared by importing them here
from app.security import hash_password
from test_admin_schedule_assignments import CSRF_RE, client, db, seed, sign_in # noqa: F401
def unfold(feed: str) -> str:
"""Undo RFC 5545 line folding so assertions can read whole lines."""
return feed.replace("\r\n ", "")
def _hours(db, schedule: Schedule, start: str, end: str) -> Schedule:
schedule.start_time = time.fromisoformat(start)
schedule.end_time = time.fromisoformat(end)
db.commit()
return schedule
def _shift(db, schedule: Schedule, day: date, user: User) -> Assignment:
assignment = Assignment(schedule_id=schedule.id, date=day, user_id=user.id, source="manual")
db.add(assignment)
db.commit()
db.refresh(assignment)
return assignment
def test_the_personal_feed_only_carries_that_person_shifts(client, db):
night, sat, sun, ana, bruno, admin = seed(db)
_hours(db, night, "20:30", "00:00")
_hours(db, sat, "13:00", "16:00")
today = date.today()
_shift(db, night, today, ana)
_shift(db, sat, today, bruno)
feed = build_feed(db, ana, "Rota — Ana")
flat = unfold(feed)
assert "BEGIN:VCALENDAR" in feed and feed.endswith("END:VCALENDAR\r\n")
assert "SUMMARY:Noite · 20:30–00:00" in flat
assert "SUMMARY:Almoço - Sábados · 13:00–16:00" not in flat # Bruno's shift
assert "Pessoa: Ana" in flat
assert "Bruno" not in flat
assert f"DTSTART:{today:%Y%m%d}T203000" in flat
# 20:30–00:00 ends after midnight, so the night ends the next morning
assert f"DTEND:{(today + timedelta(days=1)):%Y%m%d}T000000" in flat
def test_a_shift_past_midnight_ends_on_the_next_day(client, db):
night, _, _, ana, _, _ = seed(db)
night.start_time = time(20, 30)
night.end_time = time(0, 0)
db.commit()
_shift(db, night, date(2026, 11, 7), ana)
feed = unfold(build_feed(db, ana, "Rota — Ana"))
assert "DTSTART:20261107T203000" in feed
assert "DTEND:20261108T000000" in feed
def test_the_feed_reaches_the_right_person_by_token(client, db):
night, _, _, ana, bruno, admin = seed(db)
_shift(db, night, date.today(), ana)
ana.calendar_token = "ana-token"
db.commit()
sign_in(client)
page = client.get("/calendar").text
assert f"/calendar/{admin.calendar_token}.ics" in page
mine = unfold(client.get("/calendar/ana-token.ics").text)
assert client.get("/calendar/ana-token.ics").headers["content-type"].startswith("text/calendar")
assert "Pessoa: Ana" in mine
assert "X-WR-CALNAME:Contínuos CRR — Ana" in mine
assert client.get("/calendar/nao-existe.ics").status_code == 404
def test_admins_share_one_feed_with_the_whole_rota(client, db):
from app.config import settings
night, sat, _, ana, bruno, admin = seed(db)
_shift(db, night, date.today(), ana)
_shift(db, sat, date.today(), bruno)
sign_in(client)
hub = client.get("/admin/schedules").text
assert "/calendar/" in hub
token = settings.calendar_token
assert token, "the team token is created on first use"
response = client.get(f"/calendar/{token}.ics")
feed = unfold(response.text)
assert response.status_code == 200
assert "Pessoa: Ana" in feed
assert "Pessoa: Bruno" in feed
assert "X-WR-CALNAME:Contínuos CRR" in feed
assert settings.calendar_token == token # stable between visits
def test_inactive_people_lose_their_feed(client, db):
_, _, _, ana, _, _ = seed(db)
ana.calendar_token = "ana-token"
ana.is_active = False
db.commit()
assert client.get("/calendar/ana-token.ics").status_code == 404
def test_the_calendar_page_holds_the_links_and_the_rota_only_links_to_it(client, db):
night, sat, _, ana, bruno, admin = seed(db)
sign_in(client)
rota = client.get("/").text
assert 'href="/calendar">Calendário</a>' in rota
assert "O meu calendário" not in rota # the links live on their own page
page = client.get("/calendar").text
assert "Os meus turnos" in page
assert "Rota completa" in page # admins also get the team feed
assert "/calendar/" in page
assert 'data-copy="http://testserver/calendar/' in page
assert ">Copiar</button>" in page
assert "webcal://testserver/calendar/" in page
# members only get their own feed
client.post("/logout", data={"csrf_token": CSRF_RE.search(client.get("/").text).group(1)})
ana.username = "ana"
ana.password_hash = hash_password("ana-password")
db.commit()
token = CSRF_RE.search(client.get("/login").text).group(1)
client.post("/login", data={"username": "ana", "password": "ana-password", "csrf_token": token})
member_page = client.get("/calendar").text
assert "Os meus turnos" in member_page
assert "Rota completa" not in member_page
def test_the_calendar_page_needs_a_signed_in_person(client, db):
seed(db)
assert client.get("/calendar", follow_redirects=False).status_code == 401