528a13ca7c
Adds an owner-authenticated weekly agenda (grouped by day, today highlighted) with manual walk-in/phone booking creation, cancel, and reschedule -- all routed through booking_api.py's create/cancel/reschedule logic (refactored into shared helpers) so the EXCLUDE overlap constraint and confirmation email stay on the single existing code path. Manual creation can skip the opening-hours/min-notice/max-advance/buffer checks via an explicit override, but never the overlap constraint itself. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
210 lines
9.7 KiB
Python
210 lines
9.7 KiB
Python
"""Flask test client / real-DB integration tests for the owner agenda,
|
|
manual booking, and owner-initiated cancel/reschedule (#20), per #14's
|
|
testing decision: assert on HTTP response + resulting DB state.
|
|
"""
|
|
from datetime import date, datetime, time, timedelta, timezone
|
|
|
|
import pytest
|
|
|
|
import booking_db as bdb
|
|
from app import app as flask_app
|
|
|
|
CLIENT_A = "C-TEST-OWNER-BOOKING-A"
|
|
CLIENT_B = "C-TEST-OWNER-BOOKING-B"
|
|
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
flask_app.config["TESTING"] = True
|
|
flask_app.secret_key = "test-secret"
|
|
return flask_app.test_client()
|
|
|
|
|
|
def _next_monday(after):
|
|
d = after + timedelta(days=1)
|
|
while d.weekday() != 0:
|
|
d += timedelta(days=1)
|
|
return d
|
|
|
|
|
|
def _setup(client_id=CLIENT_A, **resource_kwargs):
|
|
with bdb.db.connect() as conn, conn.cursor() as cur:
|
|
cur.execute(
|
|
"INSERT INTO clients (client_id, timezone, auto_confirm) VALUES (%s, %s, %s) "
|
|
"ON CONFLICT (client_id) DO UPDATE SET timezone = EXCLUDED.timezone, "
|
|
"auto_confirm = EXCLUDED.auto_confirm",
|
|
(client_id, "Europe/Berlin", True))
|
|
conn.commit()
|
|
resource = bdb.create_resource(client_id, "Chair 1", **resource_kwargs)
|
|
bdb.set_resource_hours(client_id, resource["resource_id"], 0, time(9, 0), time(17, 0))
|
|
service = bdb.create_service(client_id, "Haircut", 60, price=25)
|
|
return resource, service
|
|
|
|
|
|
# ---- agenda view ----
|
|
|
|
def test_agenda_requires_login(client):
|
|
resp = client.get("/owner/agenda")
|
|
assert resp.status_code == 302
|
|
assert "/owner/login" in resp.headers["Location"]
|
|
|
|
|
|
def test_agenda_shows_only_own_clients_bookings(client):
|
|
resource_a, service_a = _setup(CLIENT_A, min_notice_minutes=0, max_advance_days=365)
|
|
resource_b, service_b = _setup(CLIENT_B, min_notice_minutes=0, max_advance_days=365)
|
|
day = _next_monday(date.today())
|
|
start = datetime.combine(day, time(10, 0), tzinfo=timezone(timedelta(hours=2)))
|
|
bdb.create_booking(CLIENT_A, resource_a["resource_id"], "Alice", "a@example.com",
|
|
"Haircut", start, start + timedelta(hours=1), source="public")
|
|
bdb.create_booking(CLIENT_B, resource_b["resource_id"], "Bob", "b@example.com",
|
|
"Haircut", start, start + timedelta(hours=1), source="public")
|
|
|
|
bdb.create_user(CLIENT_A, "owner@example.com", "correct horse")
|
|
client.post("/owner/login", data={"email": "owner@example.com", "password": "correct horse"})
|
|
|
|
resp = client.get("/owner/agenda", query_string={"week": day.isoformat()})
|
|
assert resp.status_code == 200
|
|
body = resp.get_data(as_text=True)
|
|
assert "Alice" in body
|
|
assert "Bob" not in body
|
|
|
|
|
|
# ---- manual booking ----
|
|
|
|
def test_owner_can_create_booking_outside_opening_hours(client):
|
|
resource, service = _setup(CLIENT_A)
|
|
bdb.create_user(CLIENT_A, "owner@example.com", "correct horse")
|
|
client.post("/owner/login", data={"email": "owner@example.com", "password": "correct horse"})
|
|
|
|
day = _next_monday(date.today())
|
|
# 20:00 local is outside the 09:00-17:00 hours configured by _setup.
|
|
outside_local = datetime.combine(day, time(20, 0)).strftime("%Y-%m-%dT%H:%M")
|
|
resp = client.post("/owner/bookings", data={
|
|
"resource_id": resource["resource_id"], "service_id": service["service_id"],
|
|
"start_time": outside_local, "customer_name": "Walk-in", "customer_contact": "n/a",
|
|
"week": day.isoformat()})
|
|
assert resp.status_code == 302
|
|
assert "error" not in resp.headers["Location"]
|
|
|
|
bookings = bdb.list_bookings(CLIENT_A)
|
|
assert len(bookings) == 1
|
|
assert bookings[0]["customer_name"] == "Walk-in"
|
|
assert bookings[0]["source"] == "owner"
|
|
|
|
|
|
def test_owner_cannot_double_book_the_same_resource(client):
|
|
resource, service = _setup(CLIENT_A)
|
|
bdb.create_user(CLIENT_A, "owner@example.com", "correct horse")
|
|
client.post("/owner/login", data={"email": "owner@example.com", "password": "correct horse"})
|
|
|
|
day = _next_monday(date.today())
|
|
local_time = datetime.combine(day, time(10, 0)).strftime("%Y-%m-%dT%H:%M")
|
|
first = client.post("/owner/bookings", data={
|
|
"resource_id": resource["resource_id"], "service_id": service["service_id"],
|
|
"start_time": local_time, "customer_name": "First", "customer_contact": "n/a",
|
|
"week": day.isoformat()})
|
|
assert "error" not in first.headers["Location"]
|
|
|
|
second = client.post("/owner/bookings", data={
|
|
"resource_id": resource["resource_id"], "service_id": service["service_id"],
|
|
"start_time": local_time, "customer_name": "Second", "customer_contact": "n/a",
|
|
"week": day.isoformat()})
|
|
assert "error=slot_taken" in second.headers["Location"]
|
|
|
|
bookings = bdb.list_bookings(CLIENT_A)
|
|
assert len(bookings) == 1
|
|
|
|
|
|
def test_owner_cannot_create_booking_for_another_tenants_resource(client):
|
|
resource_b, service_b = _setup(CLIENT_B)
|
|
bdb.create_user(CLIENT_A, "owner@example.com", "correct horse")
|
|
with bdb.db.connect() as conn, conn.cursor() as cur:
|
|
cur.execute(
|
|
"INSERT INTO clients (client_id, timezone, auto_confirm) VALUES (%s, %s, %s) "
|
|
"ON CONFLICT (client_id) DO NOTHING", (CLIENT_A, "Europe/Berlin", True))
|
|
conn.commit()
|
|
client.post("/owner/login", data={"email": "owner@example.com", "password": "correct horse"})
|
|
|
|
day = _next_monday(date.today())
|
|
local_time = datetime.combine(day, time(10, 0)).strftime("%Y-%m-%dT%H:%M")
|
|
resp = client.post("/owner/bookings", data={
|
|
"resource_id": resource_b["resource_id"], "service_id": service_b["service_id"],
|
|
"start_time": local_time, "customer_name": "Sneaky", "customer_contact": "n/a",
|
|
"week": day.isoformat()})
|
|
assert "error=not_found" in resp.headers["Location"]
|
|
assert bdb.list_bookings(CLIENT_A) == []
|
|
|
|
|
|
# ---- owner cancel/reschedule ----
|
|
|
|
def test_owner_can_cancel_own_booking(client):
|
|
resource, service = _setup(CLIENT_A)
|
|
booking = bdb.create_booking(
|
|
CLIENT_A, resource["resource_id"], "Dana", "d@example.com", "Haircut",
|
|
datetime.now(timezone.utc) + timedelta(days=2),
|
|
datetime.now(timezone.utc) + timedelta(days=2, hours=1))
|
|
bdb.create_user(CLIENT_A, "owner@example.com", "correct horse")
|
|
client.post("/owner/login", data={"email": "owner@example.com", "password": "correct horse"})
|
|
|
|
resp = client.post(f"/owner/bookings/{booking['booking_id']}/cancel", data={"week": ""})
|
|
assert resp.status_code == 302
|
|
assert "error" not in resp.headers["Location"]
|
|
assert bdb.get_booking(CLIENT_A, booking["booking_id"])["status"] == "cancelled"
|
|
|
|
|
|
def test_owner_cannot_cancel_another_tenants_booking(client):
|
|
resource_b, service_b = _setup(CLIENT_B)
|
|
booking = bdb.create_booking(
|
|
CLIENT_B, resource_b["resource_id"], "Eve", "e@example.com", "Haircut",
|
|
datetime.now(timezone.utc) + timedelta(days=2),
|
|
datetime.now(timezone.utc) + timedelta(days=2, hours=1))
|
|
with bdb.db.connect() as conn, conn.cursor() as cur:
|
|
cur.execute(
|
|
"INSERT INTO clients (client_id, timezone, auto_confirm) VALUES (%s, %s, %s) "
|
|
"ON CONFLICT (client_id) DO NOTHING", (CLIENT_A, "Europe/Berlin", True))
|
|
conn.commit()
|
|
bdb.create_user(CLIENT_A, "owner@example.com", "correct horse")
|
|
client.post("/owner/login", data={"email": "owner@example.com", "password": "correct horse"})
|
|
|
|
resp = client.post(f"/owner/bookings/{booking['booking_id']}/cancel", data={"week": ""})
|
|
assert "error=not_found" in resp.headers["Location"]
|
|
assert bdb.get_booking(CLIENT_B, booking["booking_id"])["status"] != "cancelled"
|
|
|
|
|
|
def test_owner_can_reschedule_own_booking(client):
|
|
resource, service = _setup(CLIENT_A, min_notice_minutes=0, max_advance_days=365)
|
|
day = _next_monday(date.today())
|
|
start = datetime.combine(day, time(9, 0), tzinfo=timezone(timedelta(hours=2)))
|
|
booking = bdb.create_booking(
|
|
CLIENT_A, resource["resource_id"], "Fay", "f@example.com", "Haircut",
|
|
start, start + timedelta(hours=1))
|
|
bdb.create_user(CLIENT_A, "owner@example.com", "correct horse")
|
|
client.post("/owner/login", data={"email": "owner@example.com", "password": "correct horse"})
|
|
|
|
new_local = datetime.combine(day, time(11, 0)).strftime("%Y-%m-%dT%H:%M")
|
|
resp = client.post(f"/owner/bookings/{booking['booking_id']}/reschedule",
|
|
data={"start_time": new_local, "week": day.isoformat()})
|
|
assert resp.status_code == 302
|
|
assert "error" not in resp.headers["Location"]
|
|
updated = bdb.get_booking(CLIENT_A, booking["booking_id"])
|
|
assert updated["start_time"].hour in (9, 10) # 11:00 Europe/Berlin -> 09:00/10:00 UTC
|
|
|
|
|
|
def test_owner_reschedule_into_occupied_slot_fails(client):
|
|
resource, service = _setup(CLIENT_A, min_notice_minutes=0, max_advance_days=365)
|
|
day = _next_monday(date.today())
|
|
slot_1 = datetime.combine(day, time(9, 0), tzinfo=timezone(timedelta(hours=2)))
|
|
slot_2 = datetime.combine(day, time(10, 0), tzinfo=timezone(timedelta(hours=2)))
|
|
bdb.create_booking(CLIENT_A, resource["resource_id"], "Gus", "g@example.com",
|
|
"Haircut", slot_1, slot_1 + timedelta(hours=1))
|
|
movable = bdb.create_booking(CLIENT_A, resource["resource_id"], "Hana", "h@example.com",
|
|
"Haircut", slot_2, slot_2 + timedelta(hours=1))
|
|
bdb.create_user(CLIENT_A, "owner@example.com", "correct horse")
|
|
client.post("/owner/login", data={"email": "owner@example.com", "password": "correct horse"})
|
|
|
|
new_local = datetime.combine(day, time(9, 0)).strftime("%Y-%m-%dT%H:%M")
|
|
resp = client.post(f"/owner/bookings/{movable['booking_id']}/reschedule",
|
|
data={"start_time": new_local, "week": day.isoformat()})
|
|
assert "error=unavailable" in resp.headers["Location"]
|
|
assert bdb.get_booking(CLIENT_A, movable["booking_id"])["start_time"] == slot_2
|