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 '