Files
smb-online/backoffice/app/tests/test_owner_settings.py
T
mivanchenko 456ca3872f
Test backoffice (smb-crm) / test (push) Successful in 1m46s
Add locations (Filialen) as a grouping layer above resources
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>
2026-09-12 03:02:56 +02:00

320 lines
13 KiB
Python

"""Flask test client / real-DB integration tests for the owner settings
page: services CRUD, per-resource hours/notice/buffer, auto_confirm, and
notify_channel (#21). Same testing decision as #20: assert on HTTP response
+ resulting DB state, real Postgres.
"""
from datetime import date, time, timedelta
import pytest
import booking_db as bdb
from app import app as flask_app
CLIENT_A = "C-TEST-OWNER-SETTINGS-A"
CLIENT_B = "C-TEST-OWNER-SETTINGS-B"
@pytest.fixture
def client():
flask_app.config["TESTING"] = True
flask_app.secret_key = "test-secret"
return flask_app.test_client()
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, notify_channel) "
"VALUES (%s, %s, %s, %s) ON CONFLICT (client_id) DO UPDATE SET "
"timezone = EXCLUDED.timezone, auto_confirm = EXCLUDED.auto_confirm, "
"notify_channel = EXCLUDED.notify_channel",
(client_id, "Europe/Berlin", True, None))
conn.commit()
location = bdb.create_location(client_id, "Main")
resource = bdb.create_resource(client_id, location["location_id"], "Chair 1", **resource_kwargs)
service = bdb.create_service(client_id, "Haircut", 60, price=25)
return resource, service
def _login(client, client_id, email="owner@example.com", password="correct horse"):
bdb.create_user(client_id, email, password)
client.post("/owner/login", data={"email": email, "password": password})
# ---- auth gate ----
def test_settings_requires_login(client):
resp = client.get("/owner/settings")
assert resp.status_code == 302
assert "/owner/login" in resp.headers["Location"]
# ---- services ----
def test_owner_can_create_service(client):
_setup(CLIENT_A)
_login(client, CLIENT_A)
resp = client.post("/owner/settings/services", data={
"name": "Coloring", "duration_minutes": "90", "price": "60"})
assert resp.status_code == 302
assert "error" not in resp.headers["Location"]
names = {s["name"] for s in bdb.list_services(CLIENT_A)}
assert "Coloring" in names
def test_create_service_rejects_missing_fields(client):
_setup(CLIENT_A)
_login(client, CLIENT_A)
resp = client.post("/owner/settings/services", data={"name": "", "duration_minutes": "90"})
assert "error=invalid_service" in resp.headers["Location"]
assert len(bdb.list_services(CLIENT_A)) == 1 # only the seed service from _setup
def test_owner_can_update_and_deactivate_service(client):
resource, service = _setup(CLIENT_A)
_login(client, CLIENT_A)
resp = client.post(f"/owner/settings/services/{service['service_id']}", data={
"name": "Haircut Deluxe", "duration_minutes": "45", "price": "30"})
assert "error" not in resp.headers["Location"]
updated = bdb.get_service(CLIENT_A, service["service_id"])
assert updated["name"] == "Haircut Deluxe"
assert updated["duration_minutes"] == 45
assert updated["active"] is False # checkbox omitted from form data == unchecked
resp = client.post(f"/owner/settings/services/{service['service_id']}", data={
"name": "Haircut Deluxe", "duration_minutes": "45", "price": "30", "active": "on"})
assert bdb.get_service(CLIENT_A, service["service_id"])["active"] is True
def test_owner_cannot_update_another_tenants_service(client):
resource_b, service_b = _setup(CLIENT_B)
_setup(CLIENT_A)
_login(client, CLIENT_A)
resp = client.post(f"/owner/settings/services/{service_b['service_id']}", data={
"name": "Hijacked", "duration_minutes": "30", "active": "on"})
assert "error=not_found" in resp.headers["Location"]
assert bdb.get_service(CLIENT_B, service_b["service_id"])["name"] == "Haircut"
def test_deactivated_service_disappears_from_public_picker(client):
resource, service = _setup(CLIENT_A)
_login(client, CLIENT_A)
client.post(f"/owner/settings/services/{service['service_id']}", data={
"name": "Haircut", "duration_minutes": "60", "price": "25"})
assert bdb.list_active_services(CLIENT_A) == []
assert len(bdb.list_services(CLIENT_A)) == 1
# ---- resource availability config ----
def test_owner_can_update_resource_notice_and_buffer(client):
resource, service = _setup(CLIENT_A)
_login(client, CLIENT_A)
resp = client.post(f"/owner/settings/resources/{resource['resource_id']}", data={
"name": "Chair 1", "min_notice_minutes": "120", "max_advance_days": "14",
"buffer_minutes": "15"})
assert "error" not in resp.headers["Location"]
updated = bdb.get_resource(CLIENT_A, resource["resource_id"])
assert updated["min_notice_minutes"] == 120
assert updated["max_advance_days"] == 14
assert updated["buffer_minutes"] == 15
def test_owner_can_rename_resource_and_toggle_active(client):
resource, service = _setup(CLIENT_A)
_login(client, CLIENT_A)
resp = client.post(f"/owner/settings/resources/{resource['resource_id']}", data={
"name": "Anna", "min_notice_minutes": "60", "max_advance_days": "30",
"buffer_minutes": "0"})
assert "error" not in resp.headers["Location"]
updated = bdb.get_resource(CLIENT_A, resource["resource_id"])
assert updated["name"] == "Anna"
assert updated["active"] is False # checkbox omitted == unchecked
resp = client.post(f"/owner/settings/resources/{resource['resource_id']}", data={
"name": "Anna", "active": "on", "min_notice_minutes": "60",
"max_advance_days": "30", "buffer_minutes": "0"})
assert bdb.get_resource(CLIENT_A, resource["resource_id"])["active"] is True
def test_update_resource_rejects_missing_name(client):
resource, service = _setup(CLIENT_A)
_login(client, CLIENT_A)
resp = client.post(f"/owner/settings/resources/{resource['resource_id']}", data={
"name": "", "min_notice_minutes": "0", "max_advance_days": "14",
"buffer_minutes": "15"})
assert "error=invalid_resource" in resp.headers["Location"]
def test_update_resource_rejects_negative_values(client):
resource, service = _setup(CLIENT_A)
_login(client, CLIENT_A)
resp = client.post(f"/owner/settings/resources/{resource['resource_id']}", data={
"name": "Chair 1", "min_notice_minutes": "-1", "max_advance_days": "14",
"buffer_minutes": "15"})
assert "error=invalid_resource" in resp.headers["Location"]
def test_owner_cannot_update_another_tenants_resource(client):
resource_b, service_b = _setup(CLIENT_B)
_setup(CLIENT_A)
_login(client, CLIENT_A)
resp = client.post(f"/owner/settings/resources/{resource_b['resource_id']}", data={
"name": "Hijacked", "min_notice_minutes": "0", "max_advance_days": "1",
"buffer_minutes": "0"})
assert "error=not_found" in resp.headers["Location"]
assert bdb.get_resource(CLIENT_B, resource_b["resource_id"])["max_advance_days"] != 1
# ---- locations / resources self-service ----
def test_owner_can_create_location(client):
_setup(CLIENT_A)
_login(client, CLIENT_A)
resp = client.post("/owner/settings/locations", data={"name": "Zweite Filiale"})
assert "error" not in resp.headers["Location"]
names = {l["name"] for l in bdb.list_locations(CLIENT_A)}
assert "Zweite Filiale" in names
def test_create_location_rejects_missing_name(client):
_setup(CLIENT_A)
_login(client, CLIENT_A)
resp = client.post("/owner/settings/locations", data={"name": ""})
assert "error=invalid_location" in resp.headers["Location"]
def test_owner_can_add_a_second_barber_to_a_location(client):
resource, service = _setup(CLIENT_A)
_login(client, CLIENT_A)
location_id = resource["location_id"]
resp = client.post("/owner/settings/resources", data={
"location_id": location_id, "name": "Jonas"})
assert "error" not in resp.headers["Location"]
names = {r["name"] for r in bdb.list_resources(CLIENT_A)}
assert {"Chair 1", "Jonas"} <= names
jonas = [r for r in bdb.list_resources(CLIENT_A) if r["name"] == "Jonas"][0]
assert jonas["location_id"] == location_id
def test_create_resource_rejects_another_tenants_location(client):
resource_b, service_b = _setup(CLIENT_B)
_setup(CLIENT_A)
_login(client, CLIENT_A)
resp = client.post("/owner/settings/resources", data={
"location_id": resource_b["location_id"], "name": "Hijacked"})
assert "error=invalid_resource" in resp.headers["Location"]
names = {r["name"] for r in bdb.list_resources(CLIENT_B)}
assert "Hijacked" not in names
def test_owner_can_set_opening_hours(client):
resource, service = _setup(CLIENT_A)
_login(client, CLIENT_A)
form = {"opens_at_0": "09:00", "closes_at_0": "17:00", "closed_1": "on"}
for weekday in range(2, 7):
form[f"closed_{weekday}"] = "on"
resp = client.post(
f"/owner/settings/resources/{resource['resource_id']}/hours", data=form)
assert "error" not in resp.headers["Location"]
hours = bdb.get_resource_hours(CLIENT_A, resource["resource_id"])
assert hours[0] == (time(9, 0), time(17, 0))
assert 1 not in hours
def test_set_opening_hours_rejects_end_before_start(client):
resource, service = _setup(CLIENT_A)
_login(client, CLIENT_A)
form = {"opens_at_0": "17:00", "closes_at_0": "09:00"}
for weekday in range(1, 7):
form[f"closed_{weekday}"] = "on"
resp = client.post(
f"/owner/settings/resources/{resource['resource_id']}/hours", data=form)
assert "error=invalid_hours" in resp.headers["Location"]
def test_owner_cannot_set_hours_for_another_tenants_resource(client):
resource_b, service_b = _setup(CLIENT_B)
_setup(CLIENT_A)
_login(client, CLIENT_A)
form = {"opens_at_0": "09:00", "closes_at_0": "17:00"}
for weekday in range(1, 7):
form[f"closed_{weekday}"] = "on"
resp = client.post(
f"/owner/settings/resources/{resource_b['resource_id']}/hours", data=form)
assert "error=not_found" in resp.headers["Location"]
assert bdb.get_resource_hours(CLIENT_B, resource_b["resource_id"]) == {}
def test_availability_change_reflected_in_slot_generation(client):
resource, service = _setup(CLIENT_A, min_notice_minutes=0, max_advance_days=365)
_login(client, CLIENT_A)
form = {"opens_at_0": "09:00", "closes_at_0": "10:00"}
for weekday in range(1, 7):
form[f"closed_{weekday}"] = "on"
client.post(f"/owner/settings/resources/{resource['resource_id']}/hours", data=form)
updated_hours = bdb.get_resource_hours(CLIENT_A, resource["resource_id"])
assert updated_hours[0] == (time(9, 0), time(10, 0))
# ---- client-level booking settings ----
def test_owner_can_toggle_auto_confirm(client):
_setup(CLIENT_A)
_login(client, CLIENT_A)
resp = client.post("/owner/settings/client", data={"notify_channel": ""})
assert "error" not in resp.headers["Location"]
assert bdb.get_client(CLIENT_A)["auto_confirm"] is False
resp = client.post("/owner/settings/client", data={
"auto_confirm": "on", "notify_channel": ""})
assert bdb.get_client(CLIENT_A)["auto_confirm"] is True
def test_owner_can_set_notify_channel_to_telegram(client):
_setup(CLIENT_A)
_login(client, CLIENT_A)
resp = client.post("/owner/settings/client", data={
"auto_confirm": "on", "notify_channel": "telegram"})
assert "error" not in resp.headers["Location"]
assert bdb.get_client(CLIENT_A)["notify_channel"] == "telegram"
def test_invalid_notify_channel_is_rejected(client):
_setup(CLIENT_A)
_login(client, CLIENT_A)
resp = client.post("/owner/settings/client", data={
"auto_confirm": "on", "notify_channel": "sms"})
assert "error=invalid_notify_channel" in resp.headers["Location"]
assert bdb.get_client(CLIENT_A)["notify_channel"] is None
def _next_monday(after):
d = after + timedelta(days=1)
while d.weekday() != 0:
d += timedelta(days=1)
return d
def test_auto_confirm_toggle_drives_new_public_booking_status(client):
resource, service = _setup(CLIENT_A, min_notice_minutes=0, max_advance_days=365)
_login(client, CLIENT_A)
day = _next_monday(date.today())
form = {"opens_at_0": "09:00", "closes_at_0": "17:00"}
for weekday in range(1, 7):
form[f"closed_{weekday}"] = "on"
client.post(f"/owner/settings/resources/{resource['resource_id']}/hours", data=form)
client.post("/owner/settings/client", data={"notify_channel": ""}) # auto_confirm off
assert bdb.get_client(CLIENT_A)["auto_confirm"] is False
slots = client.get("/api/booking/slots", query_string={
"client_id": CLIENT_A, "resource_id": resource["resource_id"],
"service_id": service["service_id"],
"date_from": day.isoformat(), "date_to": day.isoformat()}).get_json()["slots"]
resp = client.post("/api/booking", json={
"client_id": CLIENT_A, "resource_id": resource["resource_id"],
"service_id": service["service_id"], "start_time": slots[0],
"customer_name": "Ivy", "customer_contact": "ivy@example.com"})
assert resp.status_code == 201
assert resp.get_json()["status"] == "pending"