Document the 2026-11-05 UI/UX upgrade and keep roster/live gates aligned with face-check and presence. Co-authored-by: Cursor <cursoragent@cursor.com>
118 lines
7.9 KiB
Python
118 lines
7.9 KiB
Python
"""Real router/store behavior with isolated persistence and no external sends."""
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
from datetime import datetime, timedelta, timezone
|
|
from pathlib import Path
|
|
import sys
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'services'))
|
|
from fastapi import FastAPI
|
|
from fastapi.testclient import TestClient
|
|
from pb_fixture import MemoryPocketBase
|
|
from coffee_svc.routers.api import router as coffee_router
|
|
from notify_svc.routers.api import router as notify_router
|
|
from notify_svc import pb_store as notifications
|
|
|
|
|
|
class CoffeeNotifyTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.pb = MemoryPocketBase()
|
|
for target, value in [
|
|
('coffee_svc.pb_store.get_pb', lambda: self.pb),
|
|
('coffee_svc.routers.api.get_pb', lambda: self.pb),
|
|
('coffee_svc.pb_store.ensure_selected', lambda names: True),
|
|
('notify_svc.pb_store.get_pb', lambda: self.pb),
|
|
('notify_svc.pb_store.ensure_selected', lambda names: True),
|
|
('notify_svc.pb_store._seeded', False),
|
|
('social_svc.notify_bridge.notify_coffee_invite', lambda *args: None),
|
|
]:
|
|
override = patch(target, value); override.start(); self.addCleanup(override.stop)
|
|
env = patch.dict('os.environ', {'COFFEE_SEED_DEMO': '0', 'NOTIFY_INTERNAL_TOKEN': 'isolated-internal-secret'})
|
|
env.start(); self.addCleanup(env.stop)
|
|
app = FastAPI()
|
|
app.include_router(coffee_router, prefix='/api/coffee/v1')
|
|
app.include_router(notify_router, prefix='/api/notify/v1')
|
|
self.client = TestClient(app); self.addCleanup(self.client.close)
|
|
self.host = {'X-Social-Guest-Id': 'host-test', 'X-Coffee-Key': 'a' * 64}
|
|
self.guest = {'X-Social-Guest-Id': 'visitor-test', 'X-Coffee-Key': 'b' * 64}
|
|
|
|
def prepare_host(self):
|
|
res = self.client.put('/api/coffee/v1/me', headers=self.host, json={'name': 'Test host', 'timezone': 'America/New_York'})
|
|
self.assertEqual(res.status_code, 200, res.text)
|
|
res = self.client.patch('/api/coffee/v1/hosts/host-test/schedule', headers=self.host, json={'availability': [{'days': ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'], 'startTime': 600, 'endTime': 660}]})
|
|
self.assertEqual(res.status_code, 200, res.text)
|
|
day = (datetime.now(timezone.utc) + timedelta(days=2)).date().isoformat()
|
|
result = self.client.get('/api/coffee/v1/hosts/host-test/event-types/30min/slots', params={'day': day})
|
|
return result.json()['slots'][0]['start']
|
|
|
|
def test_host_onboarding_empty_schedule_then_usable_slots(self):
|
|
self.assertEqual(self.client.get('/api/coffee/v1/hosts').json()['hosts'], [])
|
|
self.prepare_host()
|
|
public = self.client.get('/api/coffee/v1/hosts/host-test').json()
|
|
self.assertNotIn('owner_key_hash', str(public))
|
|
self.assertEqual(public['host']['event_count'], 1)
|
|
|
|
def test_public_id_does_not_allow_host_takeover(self):
|
|
self.prepare_host()
|
|
attacker = {**self.host, 'X-Coffee-Key': 'c' * 64}
|
|
for method, url, body in [('GET', '/me', None), ('PUT', '/me', {'name': 'Hijacked', 'timezone': 'UTC'}), ('PATCH', '/hosts/host-test/schedule', {'availability': []})]:
|
|
result = self.client.request(method, '/api/coffee/v1' + url, headers=attacker, json=body)
|
|
self.assertEqual(result.status_code, 403, result.text)
|
|
|
|
def test_duplicate_parallel_booking_conflicts_and_cancellation_reopens_slot(self):
|
|
start = self.prepare_host()
|
|
body = {'username': 'host-test', 'event_slug': '30min', 'start': start, 'attendee_name': 'Visitor'}
|
|
def reserve(_): return self.client.post('/api/coffee/v1/bookings', headers=self.guest, json=body)
|
|
with ThreadPoolExecutor(max_workers=6) as pool: results = list(pool.map(reserve, range(6)))
|
|
self.assertEqual(sorted(r.status_code for r in results), [200, 409, 409, 409, 409, 409])
|
|
booking = next(r.json()['booking'] for r in results if r.status_code == 200)
|
|
self.assertNotIn('attendee_key_hash', booking)
|
|
url = '/api/coffee/v1/bookings/' + booking['id']
|
|
attacker = {**self.guest, 'X-Coffee-Key': 'c' * 64}
|
|
self.assertEqual(self.client.get(url, headers=attacker).status_code, 403)
|
|
self.assertEqual(self.client.post(url + '/cancel', headers=attacker).status_code, 403)
|
|
self.assertEqual(self.client.get('/api/coffee/v1/bookings', headers=attacker).json()['bookings'], [])
|
|
self.assertEqual(self.client.get(url, headers=self.host).status_code, 200)
|
|
self.assertEqual(self.client.post(url + '/cancel', headers=self.guest).status_code, 200)
|
|
self.assertEqual(reserve(0).status_code, 200)
|
|
|
|
def test_invalid_booking_and_schedule_rejected_without_writes(self):
|
|
self.prepare_host()
|
|
res = self.client.post('/api/coffee/v1/bookings', headers=self.guest, json={'username': 'host-test', 'event_slug': '30min', 'start': '2000-01-01T10:00:00Z', 'attendee_name': 'Visitor'})
|
|
self.assertEqual(res.status_code, 409)
|
|
res = self.client.patch('/api/coffee/v1/hosts/host-test/schedule', headers=self.host, json={'availability': [{'days': ['Monday'], 'startTime': 600, 'endTime': 500}]})
|
|
self.assertEqual(res.status_code, 422)
|
|
self.assertEqual(len(self.pb.rows.get('coffee_bookings', {})), 0)
|
|
|
|
def test_notify_management_cannot_be_triggered_by_a_visitor(self):
|
|
for method, path, body in [('GET', '/subscribers', None), ('GET', '/workflows', None), ('POST', '/events/trigger', {'workflow_id': 'welcome', 'subscriber_id': 'visitor-test'})]:
|
|
self.assertEqual(self.client.request(method, '/api/notify/v1' + path, headers=self.guest, json=body).status_code, 403)
|
|
self.assertEqual(self.client.get('/api/notify/v1/workflows', headers={'X-Notify-Internal-Token': 'isolated-internal-secret'}).status_code, 200)
|
|
|
|
def test_notify_inbox_filters_before_limit_and_owns_mutations(self):
|
|
notifications.init_db()
|
|
for index in range(60):
|
|
self.pb.create('notify_messages', {'subscriber_id': 'visitor-test', 'created_at': str(index).zfill(3), 'category': 'social', 'deleted': False, 'archived': False, 'read': False})
|
|
specific = self.pb.create('notify_messages', {'subscriber_id': 'visitor-test', 'created_at': '000', 'category': 'system', 'deleted': False, 'archived': False, 'read': False})
|
|
self.pb.create('notify_messages', {'subscriber_id': 'visitor-test', 'created_at': '999', 'category': 'system', 'deleted': False, 'archived': True, 'read': False})
|
|
res = self.client.get('/api/notify/v1/inbox?subscriber_id=visitor-test&category=system', headers=self.guest)
|
|
self.assertEqual(res.status_code, 200, res.text)
|
|
self.assertEqual([m['id'] for m in res.json()['messages']], [specific['id']])
|
|
self.assertEqual(self.client.get('/api/notify/v1/inbox?subscriber_id=host-test', headers=self.guest).status_code, 403)
|
|
url = '/api/notify/v1/inbox/' + specific['id']
|
|
self.assertEqual(self.client.patch(url, headers=self.host, json={'read': True}).status_code, 403)
|
|
self.assertEqual(self.client.patch(url, headers=self.guest, json={'read': True}).status_code, 200)
|
|
|
|
def test_notify_preference_read_preserves_profile_and_partial_update(self):
|
|
notifications.upsert_subscriber({'subscriber_id': 'visitor-test', 'email': 'owner@example.test', 'first_name': 'Owner'})
|
|
url = '/api/notify/v1/subscribers/visitor-test/preferences'
|
|
self.assertEqual(self.client.get(url, headers=self.guest).status_code, 200)
|
|
self.client.put(url, headers=self.guest, json={'channels': {'timezone': 'UTC'}})
|
|
res = self.client.put(url, headers=self.guest, json={'channels': {'dnd': True}})
|
|
self.assertEqual(res.json()['preferences']['channels']['timezone'], 'UTC')
|
|
self.assertEqual(notifications.get_subscriber('visitor-test')['email'], 'owner@example.test')
|
|
|
|
|
|
if __name__ == '__main__': unittest.main()
|