101 lines
4.6 KiB
Python
101 lines
4.6 KiB
Python
import os
|
|
import tempfile
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
root = Path('/mnt/data/appcheck/backend')
|
|
work = Path(tempfile.mkdtemp(prefix='rbmnext_'))
|
|
os.environ['DATABASE_URL'] = f"sqlite:///{work/'test.db'}"
|
|
os.environ['DATA_DIR'] = str(work/'data')
|
|
os.environ['SECRET_KEY'] = 'test-secret'
|
|
os.environ['DEFAULT_ADMIN_USERNAME'] = 'admin'
|
|
os.environ['DEFAULT_ADMIN_PASSWORD'] = 'admin'
|
|
os.environ['ALLOW_REGISTRATION'] = 'true'
|
|
os.environ['CORS_ORIGINS'] = '["http://localhost:4200"]'
|
|
os.chdir(root)
|
|
import sys
|
|
sys.path.insert(0, str(root))
|
|
|
|
from fastapi.testclient import TestClient
|
|
from app.main import app
|
|
|
|
client = TestClient(app)
|
|
|
|
results = []
|
|
|
|
def record(name, ok, detail=''):
|
|
results.append((name, ok, detail))
|
|
|
|
# health
|
|
r = client.get('/api/health')
|
|
record('health', r.status_code == 200 and r.json().get('status') == 'ok', str(r.status_code))
|
|
|
|
# login as default admin
|
|
r = client.post('/api/auth/login', data={'username':'admin','password':'admin'})
|
|
record('login_form', r.status_code == 200 and 'access_token' in r.json(), str(r.text))
|
|
token = r.json()['access_token']
|
|
headers = {'Authorization': f'Bearer {token}'}
|
|
|
|
# me
|
|
r = client.get('/api/auth/me', headers=headers)
|
|
record('auth_me', r.status_code == 200 and r.json()['username'] == 'admin', str(r.text))
|
|
|
|
# register new user and login json should fail (endpoint only form in current archive)
|
|
r = client.post('/api/auth/register', json={'username':'u1','password':'p1234'})
|
|
record('register', r.status_code == 200 and r.json()['username'] == 'u1', str(r.text))
|
|
|
|
# create router
|
|
router_payload = {
|
|
'name':'R1','host':'192.0.2.1','port':22,'ssh_user':'admin','ssh_password':'pass','ssh_key':''
|
|
}
|
|
r = client.post('/api/routers', json=router_payload, headers=headers)
|
|
record('create_router', r.status_code == 200 and r.json()['name'] == 'R1', str(r.text))
|
|
router_id = r.json()['id']
|
|
|
|
# list routers
|
|
r = client.get('/api/routers', headers=headers)
|
|
record('list_routers', r.status_code == 200 and len(r.json()) == 1, str(r.text))
|
|
|
|
# dashboard
|
|
r = client.get('/api/dashboard', headers=headers)
|
|
record('dashboard', r.status_code == 200 and r.json()['routers_count'] == 1, str(r.text))
|
|
|
|
# fake router tests and backups
|
|
with patch('app.services.router_service.router_service.test_connection', return_value={'model':'hAP ax3','uptime':'1d','hostname':'r1'}):
|
|
r = client.get(f'/api/routers/{router_id}/test-connection', headers=headers)
|
|
record('test_connection_route', r.status_code == 200 and r.json()['hostname'] == 'r1', str(r.text))
|
|
|
|
with patch('app.services.router_service.router_service.export', return_value='/system identity set name=r1\n'):
|
|
r = client.post(f'/api/backups/router/{router_id}/export', headers=headers, json={})
|
|
record('export_router', r.status_code == 200 and r.json()['backup_type'] == 'export', str(r.text))
|
|
export_id = r.json()['id']
|
|
|
|
with patch('app.services.router_service.router_service.binary_backup', side_effect=lambda router, base_name, path, key: Path(path).write_bytes(b'binary')):
|
|
r = client.post(f'/api/backups/router/{router_id}/binary', headers=headers, json={})
|
|
record('binary_backup', r.status_code == 200 and r.json()['backup_type'] == 'binary', str(r.text))
|
|
binary_id = r.json()['id']
|
|
|
|
# view export
|
|
r = client.get(f'/api/backups/{export_id}/view', headers=headers)
|
|
record('view_export', r.status_code == 200 and 'content' in r.json(), str(r.text))
|
|
|
|
# download requires auth header at HTTP level; endpoint itself works with auth header
|
|
r = client.get(f'/api/backups/{binary_id}/download', headers=headers)
|
|
record('download_with_auth', r.status_code == 200 and r.content == b'binary', str(r.status_code))
|
|
|
|
# diff html endpoint works with auth header
|
|
r = client.get(f'/api/backups/{export_id}/diff/{export_id}/html', headers=headers)
|
|
record('diff_html_with_auth', r.status_code == 200 and '<html' in r.text.lower(), str(r.status_code))
|
|
|
|
# settings roundtrip
|
|
r = client.get('/api/settings', headers=headers)
|
|
record('get_settings', r.status_code == 200 and 'backup_retention_days' in r.json(), str(r.text))
|
|
r = client.put('/api/settings', headers=headers, json={'backup_retention_days':10,'log_retention_days':5,'export_cron':'','binary_cron':'','retention_cron':'','enable_auto_export':False,'global_ssh_key':'','pushover_token':'','pushover_userkey':'','notify_failures_only':True,'smtp_host':'','smtp_port':587,'smtp_login':'','smtp_password':'','smtp_notifications_enabled':False,'recipient_email':None})
|
|
record('put_settings', r.status_code == 200 and r.json()['backup_retention_days'] == 10, str(r.text))
|
|
|
|
for name, ok, detail in results:
|
|
print(f"{name}: {'OK' if ok else 'FAIL'} {detail}")
|
|
|
|
if not all(ok for _,ok,_ in results):
|
|
raise SystemExit(1)
|