70 lines
4.3 KiB
Python
70 lines
4.3 KiB
Python
"""Preview/live routing and authenticated editor/pagination contracts in a subprocess."""
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
|
|
|
|
def test_preview_and_live_feature_contract():
|
|
root = Path(__file__).resolve().parents[1]
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
env = {**os.environ, "XBNIU_PREVIEW": "1", "XBNIU_DB_PATH": str(Path(directory) / "preview.db"), "PYTHONDONTWRITEBYTECODE": "1"}
|
|
script = r'''
|
|
from unittest.mock import patch
|
|
import db
|
|
db.init_db()
|
|
import app
|
|
from preview_data import seed_preview
|
|
seed_preview()
|
|
c=app.app.test_client()
|
|
assert c.get('/healthz').json == {'status':'ok','mode':'preview'}
|
|
login=c.post('/api/auth/login',json={'email':'demo@preview.local','password':'preview123'}).json
|
|
h={'Authorization':'Bearer '+login['data']['token']}
|
|
with patch('wechat.request_with_user_auth',side_effect=AssertionError('Demo called upstream')):
|
|
bikes=c.get('/api/map?type=3',headers=h).json['data']['bicycleStation']
|
|
assert len(bikes)==28
|
|
assert c.post('/api/order/unlock',headers=h,json={'code':'51201'}).json['rtn_code']==0
|
|
assert c.get('/api/order/status',headers=h).json['has_order']
|
|
assert c.post('/api/order/return',headers=h,json={}).json['rtn_code']==0
|
|
assert not c.get('/api/order/status',headers=h).json['has_order']
|
|
assert c.post('/api/auth/save_token',headers=h,json={'token_id':'real'}).status_code==403
|
|
page1=c.get('/api/user/orders?limit=20',headers=h).json
|
|
page2=c.get('/api/user/orders?limit=20&offset=20',headers=h).json
|
|
assert page1['total']==68 and page1['has_more'] and len(page1['data'])==20
|
|
assert not ({x['id'] for x in page1['data']} & {x['id'] for x in page2['data']})
|
|
assert all(x['shared'] for x in c.get('/api/user/orders?source=shared',headers=h).json['data'])
|
|
assert c.get('/api/user/orders?from=2026-11-05&to=2026-11-01',headers=h).status_code==400
|
|
rules=c.get('/api/subscribe/rules',headers=h).json['data'];rule=rules[0]
|
|
parking=c.get('/api/campus/locations',headers=h).json['data']
|
|
location=next(p for p in parking if p['kind']=='parking' and p['id']!=rule['location_id'])
|
|
updated=c.put('/api/subscribe/rules/'+str(rule['id']),headers=h,json={'location_id':location['id'],'time':'12:34','weekday':3,'min_electric':40,'auto_unlock':False,'notify_enabled':True})
|
|
assert updated.json['rtn_code']==0,updated.json
|
|
saved=c.get('/api/subscribe/rules',headers=h).json['data'][0]
|
|
assert saved['minute_of_day']==754 and saved['location_id']==location['id'] and saved['min_electric']==40
|
|
assert saved['last_run_status']=='success'
|
|
assert c.put('/api/subscribe/rules/'+str(rule['id']),headers=h,json={'min_electric':90,'max_electric':20}).status_code==400
|
|
session=c.post('/api/preview/session').json
|
|
live={'Authorization':'Bearer '+session['data']['token']}
|
|
assert not c.get('/api/user/orders',headers=live).json['data']
|
|
assert not c.get('/api/subscribe/rules',headers=live).json['data']
|
|
with patch('wechat.request_with_user_auth',return_value={'rtn_code':0,'data':{'bicycleStation':[]}}) as upstream:
|
|
assert c.get('/api/map?type=3',headers=live).json['rtn_code']==0
|
|
assert c.get('/api/map?type=3',headers=live).json['rtn_code']==0
|
|
assert upstream.call_count==1, 'live map cache should coalesce requests'
|
|
assert upstream.call_args.args[0] != 1, 'live requests must use private test account credentials'
|
|
assert c.put('/api/subscribe/rules/'+str(rule['id']),headers=live,json={'enabled':False}).json['rtn_code']==404
|
|
with patch('wechat.request_with_user_auth',return_value={'rtn_code':0,'data':{'order_id':'REAL-TEST'}}) as upstream:
|
|
assert c.post('/api/bicycle',headers=live,json={'code':'51201'}).json['rtn_code']==0
|
|
assert upstream.call_count==1
|
|
with patch('email_utils.smtplib.SMTP',side_effect=AssertionError('Demo SMTP')):
|
|
from email_utils import send_email
|
|
assert send_email('demo@preview.local','test','body') is True
|
|
from bike_data import classify,battery
|
|
assert classify(1,0,10)=='low' and classify(1,1,10)=='used' and classify(4,0,10)=='locked'
|
|
assert battery(None) is None and battery(0)==0
|
|
print('PASS: demo isolation, real test account routing, pagination, subscription ownership and full edits')
|
|
'''
|
|
result = subprocess.run([sys.executable, "-X", "utf8", "-c", script], cwd=root, env=env, capture_output=True, text=True, timeout=60)
|
|
assert result.returncode == 0, result.stdout + result.stderr
|