456ca3872f
Test backoffice (smb-crm) / test (push) Successful in 1m46s
Enables multiple barbers/staff bookable at the same location and time -- previously "resource" conflated "location" and "the thing that can't double-book itself" into one row, so a Filiale could only ever have exactly one bookable slot at once. - New `locations` table; `resources.location_id` with a generic, idempotent backfill migration (any resource without a location gets one auto-created matching its name -- not a one-off for any single client, protects any future resource stuck in the old flat shape too) - `resources`/`resource_hours`/services keep everything they already had (hours, min-notice, max-advance, buffer, the no-overlap constraint) scoped to resource_id, not location_id -- two barbers at one location must stay independently bookable at the same time - booking_db.py: new locations CRUD mirroring the existing resources/services pattern; create_resource now requires a location_id, guarded the same way every other tenant check here is (get_location existence check, no real FK -- matches this schema's existing no-FK convention throughout) - app.py: new POST /api/locations provisioning route; POST /api/resources now requires location_id - owner_settings.py + settings.html: new self-service "add a Filiale" / "add a barber" UI -- there was previously no way to create a resource at all outside the CRM/n8n provisioning API - public_booking.py + book.html: new Filiale picker (reuses the existing wireOptionGroup button-group pattern), filtering the Mitarbeiter picker to the selected location -- a single-location client sees no extra click, same as before Filialen existed - owner_booking.py + agenda.html: the Filiale show/hide toggle and hide-cancelled toggle (shipped earlier this session) now key off location_id instead of resource_id, so hiding a Filiale hides every barber's bookings at it; manual-booking dropdown grouped by Filiale - n8n/onboarding.json: default provisioning now creates a "Hauptfiliale" location before its resource (inert until re-imported into the live n8n instance) Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
211 lines
9.8 KiB
Python
211 lines
9.8 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()
|
|
location = bdb.create_location(client_id, "Main")
|
|
resource = bdb.create_resource(client_id, location["location_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
|