36 lines
4.1 KiB
Python
36 lines
4.1 KiB
Python
"""Offline browser contract: the production hostname is intercepted; never send test credentials upstream."""
|
|
from argparse import ArgumentParser
|
|
from pathlib import Path
|
|
from urllib.parse import urlsplit
|
|
from playwright.sync_api import expect,sync_playwright
|
|
import json
|
|
p=ArgumentParser();p.add_argument('--chrome',required=True);p.add_argument('--out',type=Path,required=True);arg=p.parse_args();arg.out.mkdir(parents=True,exist_ok=True)
|
|
base='http://127.0.0.1:5179'; official='https://admin.zhenyangtang.com.cn'; calls=[]; unexpected=[]; errors=[]
|
|
with sync_playwright() as pw:
|
|
b=pw.chromium.launch(executable_path=arg.chrome,args=['--disable-background-networking','--no-proxy-server','--host-resolver-rules=MAP * ~NOTFOUND, EXCLUDE 127.0.0.1'])
|
|
c=b.new_context(service_workers='block',viewport={'width':1320,'height':850});page=c.new_page();page.on('pageerror',lambda e:errors.append(str(e)))
|
|
def route(r):
|
|
u=urlsplit(r.request.url); path=u.path.removeprefix('/adminapi');m=r.request.method
|
|
if u.scheme=='http' and u.netloc=='127.0.0.1:5179' and m=='GET': return r.continue_()
|
|
if u.scheme!='https' or u.netloc!='admin.zhenyangtang.com.cn' or not u.path.startswith('/adminapi/'):
|
|
unexpected.append([m,u.path]);return r.abort()
|
|
cors={'Access-Control-Allow-Origin':base,'Access-Control-Allow-Methods':'GET,POST,OPTIONS','Access-Control-Allow-Headers':'token,version,content-type,accept','Cache-Control':'no-store'}
|
|
if m=='OPTIONS':return r.fulfill(status=204,headers=cors)
|
|
calls.append([m,path]);assert r.request.headers.get('cookie') is None
|
|
def reply(data):return r.fulfill(headers=cors,json={'code':1,'show':0,'msg':'','data':data})
|
|
if (m,path)==('POST','/login/account'):
|
|
assert r.request.post_data_json=={'account':'offline-qa','password':'offline-test-only','terminal':1};return reply({'token':'offline-session-example','is_paw':1,'need_bind_work_wechat':False})
|
|
assert r.request.headers.get('token')=='offline-session-example'
|
|
if (m,path)==('GET','/auth.admin/mySelf'):return reply({'user':{'id':999,'name':'离线验收角色','disable':0,'is_paw':1,'root':0,'need_bind_work_wechat':False},'permissions':['order.order/lists'],'menu':[{'type':'C','paths':'orders','component':'order/index','is_disable':0,'is_show':1,'perms':'order.order/lists'}]})
|
|
if (m,path)==('GET','/order.order/lists'):return reply({'lists':[{'id':1,'order_no':'OFFLINE-VIEW-001','order_type':1,'status':2,'payment_time':'2026-12-01','create_time':'2026-12-01','amount':'2.00','is_exempt':0,'creator':{'name':'演示'},'patient':None,'prescription_orders':[]}],'count':1,'extend':{}})
|
|
unexpected.append([m,path]);return r.abort()
|
|
c.route('**/*',route); page.goto(base+'/admin/order',wait_until='networkidle'); expect(page.get_by_placeholder('请输入账号')).to_be_visible(); assert not calls
|
|
page.screenshot(path=str(arg.out/'login.png'))
|
|
page.get_by_placeholder('请输入账号').fill('offline-qa');page.get_by_placeholder('请输入密码').fill('offline-test-only');page.get_by_role('button',name='登录',exact=True).click()
|
|
expect(page.get_by_text('OFFLINE-VIEW-001',exact=True)).to_be_visible(timeout=10000);expect(page.get_by_text('正式数据 · 只读',exact=True)).to_be_visible(); expect(page.get_by_role('menuitem')).to_have_count(1)
|
|
assert not page.get_by_role('button',name='详情',exact=True).count() and not page.get_by_role('button',name='创建支付单').count()
|
|
saved=page.evaluate('JSON.stringify([Object.entries(localStorage),Object.entries(sessionStorage),document.cookie])');assert all(x not in saved for x in ['offline-test-only','offline-session-example','offline-qa','OFFLINE-VIEW-001'])
|
|
page.get_by_role('button',name='退出预览').click();expect(page.get_by_placeholder('请输入账号')).to_be_visible();page.goto(base+'/admin/order',wait_until='networkidle');expect(page.get_by_placeholder('请输入账号')).to_be_visible();assert not unexpected and not errors,(unexpected,errors)
|
|
b.close()
|
|
print(json.dumps({'offline_browser_pass':True,'intercepted_paths':calls,'real_auth_requests':0,'unexpected':unexpected,'page_errors':errors},ensure_ascii=False))
|