27 lines
3.5 KiB
Python
27 lines
3.5 KiB
Python
"""Optional dev-only Chromium test with mock API; not a server dependency."""
|
|
from pathlib import Path
|
|
import os
|
|
from playwright.sync_api import sync_playwright
|
|
root=Path(__file__).resolve().parents[1]/'websetup'
|
|
screen=Path(os.environ.get('MARS_GATEWAY_SCREENSHOT',str(root.parent/'build/gateway.png')))
|
|
state=dict(defaultPassword=False,groups={k:[] for k in ['1','2','3','4','5','12','13','17','21','45','200']},text='',revision='a',csrf='b',validation={'errors':[]},devices=[{'name':'enp3s0','mac':'02:01:02:03:04:05','state':'up'},{'name':'enp4s0','mac':'02:01:02:03:04:06','state':'up'}],backups=[],path='/usr/local/etc/mars_nwe/nwserv.conf',services=True)
|
|
state['groups']['4']=['0x101 enp3s0 802.3 1','0x102 enp3s0 802.2 1']
|
|
gw=dict(enabled=False,network='0x101',gateway='10.250.0.1',dns='1.1.1.1',uplink='',web=True,ping=True,clients=[])
|
|
rev='r1'; actions=[]
|
|
def fixture(source,path,body):
|
|
global gw,rev
|
|
if path=='/api/state':return state
|
|
if path=='/api/status':return {'mars-nwe-pcap.service':'ActiveState=active','mars-pcapd.service':'ActiveState=active'}
|
|
if path=='/api/gateway':
|
|
if body:
|
|
assert body['revision']==rev and body['confirm'];gw=body['config'];rev='r2';actions.append('save');return {'ok':True}
|
|
return {'config':gw,'revision':rev,'status':'Disabled (simulierte API)'}
|
|
if path=='/api/service':assert body['action']=='restart' and body['revision']=='a';actions.append('restart');return {'ok':True}
|
|
raise AssertionError(path)
|
|
with sync_playwright() as p:
|
|
b=p.chromium.launch(executable_path='/usr/bin/chromium',args=['--no-sandbox']);page=b.new_page(viewport={'width':1280,'height':1050});errors=[];page.on('pageerror',lambda e:errors.append(str(e)));page.on('dialog',lambda d:d.accept());page.expose_binding('fixture',fixture);page.evaluate("window.fetch=async(path,opts)=>({ok:true,status:200,json:()=>window.fixture(path,opts?.body?JSON.parse(opts.body):null)})")
|
|
html=(root/'index.html').read_text().replace('<link rel="stylesheet" href="/style.css">','<style>'+(root/'style.css').read_text()+'</style>').replace('<script src="/app.js" defer></script>','');page.set_content(html);page.add_script_tag(content=(root/'app.js').read_text());page.wait_for_function("document.getElementById('serverStatus').textContent==='active'")
|
|
page.locator('#gatewayOpen').click();page.locator('#gwEnabled').check();page.locator('#gwNetwork').select_option('0x102');page.locator('#gwUplink').select_option('enp4s0');page.locator('#gwClients').fill('02:01:02:03:04:07 10.250.0.10');page.locator('#gwRestart').click();page.wait_for_function("document.getElementById('gwMessage').textContent.includes('zuerst speichern')");assert not actions
|
|
page.locator('#gatewayForm button[type=submit]').click();page.wait_for_function("document.getElementById('gwMessage').textContent.includes('Gespeichert')");assert gw['enabled'] and gw['network']=='0x102' and gw['clients']==[{'mac':'020102030407','ip':'10.250.0.10'}] and gw['uplink']=='enp4s0'
|
|
page.locator('#gwRestart').click();page.wait_for_function("document.getElementById('gwMessage').textContent.includes('Neustart angefordert')");assert actions==['save','restart'];page.locator('#gwRefresh').click();page.wait_for_function("!document.getElementById('gwRefresh').disabled");page.locator('#gatewayDialog').evaluate('(el)=>el.scrollTop=0');page.screenshot(path=str(screen));page.locator('#gatewayClose').click();assert not errors,errors;print('PASS: mock API Chromium Gateway modal, discovered interface selection, MAC/IP entries, save, dirty guard, restart, status');b.close()
|