Ship Operations console polish, PocketBase social fixtures, and portal join-leads without local DBs or probe scripts. Co-authored-by: Cursor <cursoragent@cursor.com>
71 lines
3.3 KiB
Python
71 lines
3.3 KiB
Python
"""Admin health/schema regression checks without app startup or production data."""
|
|
import asyncio
|
|
import importlib.util
|
|
import json
|
|
import sys
|
|
import unittest
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
from unittest.mock import patch
|
|
|
|
import httpx
|
|
from fastapi import FastAPI
|
|
from fastapi.testclient import TestClient
|
|
from pydantic import ValidationError
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'services'))
|
|
from ebook_svc.routers import ops_admin
|
|
|
|
|
|
class OpsUpgradeTests(unittest.TestCase):
|
|
def test_health_requires_admin(self):
|
|
app = FastAPI()
|
|
app.include_router(ops_admin.router)
|
|
with TestClient(app) as client:
|
|
self.assertEqual(client.get('/api/admin/ops/health').status_code, 401)
|
|
|
|
def test_health_checks_payload_and_handles_downstream_failure(self):
|
|
def respond(request):
|
|
if request.url.port == 4003:
|
|
return httpx.Response(200, json={'ok': True})
|
|
if request.url.port == 8010:
|
|
return httpx.Response(200, json={'ok': False})
|
|
raise httpx.ConnectError('unavailable')
|
|
async def run():
|
|
async with httpx.AsyncClient(transport=httpx.MockTransport(respond)) as client:
|
|
return await asyncio.gather(*[ops_admin._probe(client, str(port), f'http://127.0.0.1:{port}/health') for port in (4003, 8010, 8020)])
|
|
checks = asyncio.run(run())
|
|
self.assertEqual([row['ok'] for row in checks], [True, False, False])
|
|
self.assertIsNone(checks[-1]['status'])
|
|
|
|
def load_switches(self):
|
|
target = Path(__file__).resolve().parents[1] / 'services/ebook_svc/routers/weapp_switches_admin.py'
|
|
spec = importlib.util.spec_from_file_location('isolated_switches', target)
|
|
module = importlib.util.module_from_spec(spec)
|
|
sys.modules[spec.name] = module
|
|
with patch.dict(sys.modules, {
|
|
'ebook_svc.services.store': SimpleNamespace(store=SimpleNamespace()),
|
|
'ebook_svc.services.weapp_switches': SimpleNamespace(read_weapp_switches=lambda: {'version': 10}, write_weapp_switches=lambda patch: patch),
|
|
}):
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
def test_adsense_fields_are_preserved_and_validated(self):
|
|
module = self.load_switches()
|
|
body = module.SwitchesBody.model_validate({'flags': {'adsenseEnabled': False, 'adsenseClientId': 'ca-pub-123', 'adsenseSlotBlogMid': '456'}})
|
|
self.assertEqual(body.flags.adsenseClientId, 'ca-pub-123')
|
|
self.assertEqual(body.flags.adsenseSlotBlogMid, '456')
|
|
for value in ['javascript:alert(1)', 'ca-pub-no-digits']:
|
|
with self.assertRaises(ValidationError):
|
|
module.SwitchesBody.model_validate({'flags': {'adsenseClientId': value}})
|
|
|
|
def test_stale_config_write_is_rejected(self):
|
|
module = self.load_switches()
|
|
app = FastAPI()
|
|
app.include_router(module.router)
|
|
app.dependency_overrides[module.require_admin] = lambda: 'isolated-admin'
|
|
with TestClient(app) as client:
|
|
response = client.put('/api/admin/weapp/switches', json={'expectedVersion': 9, 'flags': {'payEnabled': False}})
|
|
self.assertEqual(response.status_code, 409)
|
|
self.assertEqual(client.put('/api/admin/weapp/switches', json={'expectedVersion': 10, 'flags': {'payEnabled': False}}).status_code, 200)
|