Upload test 1
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
import importlib.util
|
||||
import io
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
import unittest
|
||||
from contextlib import redirect_stdout
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
MODULE = Path(__file__).with_name("dragoncore_cli.py")
|
||||
spec = importlib.util.spec_from_file_location("dragoncore_cli", MODULE)
|
||||
cli = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(cli)
|
||||
|
||||
|
||||
class TokenManagementTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.temp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.temp.cleanup)
|
||||
env = Path(self.temp.name) / ".env"
|
||||
env.write_text("PG_DSN=test\nADMIN_TOKEN=" + "a" * 48 + "\nADMIN_HTTP_ADDR=127.0.0.1:9090\n")
|
||||
env.chmod(0o600)
|
||||
self.path = env
|
||||
patcher = patch.object(cli, "ENV_FILE", env)
|
||||
patcher.start()
|
||||
self.addCleanup(patcher.stop)
|
||||
|
||||
def test_rotation_preserves_other_env_and_never_prompts(self):
|
||||
with patch.object(cli, "service_action") as service, patch.object(cli, "request", return_value=[]):
|
||||
with patch("builtins.input", side_effect=AssertionError("prompted")):
|
||||
with redirect_stdout(io.StringIO()):
|
||||
cli.rotate_api_password()
|
||||
self.assertNotEqual(cli.api_password(), "a" * 48)
|
||||
self.assertIn("PG_DSN=test\n", self.path.read_text())
|
||||
self.assertEqual(self.path.stat().st_mode & 0o777, 0o600)
|
||||
service.assert_called_once_with("restart")
|
||||
|
||||
def test_failed_rotation_restores_previous_token(self):
|
||||
with patch.object(cli, "service_action", side_effect=[None, None]), \
|
||||
patch.object(cli, "request", side_effect=cli.CLIError("bad API")), \
|
||||
patch.object(cli.time, "sleep"):
|
||||
with self.assertRaises(cli.CLIError):
|
||||
cli.rotate_api_password()
|
||||
self.assertEqual(cli.api_password(), "a" * 48)
|
||||
|
||||
def test_live_vps_header_counts_users_and_rates(self):
|
||||
responses = [
|
||||
{"cpu_percent": 12.5, "mem_total_bytes": 8 * 1024**3,
|
||||
"mem_used_bytes": 2 * 1024**3,
|
||||
"interfaces": [{"rx_mbps": 18.5, "tx_mbps": 4.2}]},
|
||||
[{"username": "alice", "active_conns": 2, "expires_at": "2099-01-01T00:00:00Z"},
|
||||
{"username": "bob", "active_conns": 0, "expires_at": "2020-01-01T00:00:00Z"}],
|
||||
{"online_users": 3},
|
||||
]
|
||||
with patch.object(cli, "request", side_effect=responses), \
|
||||
patch.object(cli.shutil, "disk_usage", return_value=SimpleNamespace(used=50, total=100)), \
|
||||
patch.object(cli.subprocess, "run", return_value=SimpleNamespace(stdout="active\n")):
|
||||
status = cli.collect_vps_status()
|
||||
header = cli.render_vps_status(status)
|
||||
self.assertIn("12.5%", header)
|
||||
self.assertIn("1 online | 1 expired | 2 total", header)
|
||||
self.assertIn("Xray: 3 online", header)
|
||||
self.assertIn("18.50 Mbps", header)
|
||||
self.assertEqual(len({len(row) for row in header.splitlines()}), 1)
|
||||
menu_text = cli.render_menu("MAIN MENU", cli.main_menu_options(), status)
|
||||
self.assertIn("CONNECTION", menu_text.upper())
|
||||
self.assertNotIn("Resellers", menu_text)
|
||||
mobile = cli.render_menu("MAIN MENU", cli.main_menu_options(), status, columns=40)
|
||||
self.assertLessEqual(max(map(len, mobile.splitlines())), 40)
|
||||
self.assertIn("CPU: 12.5%", mobile)
|
||||
self.assertIn("SSH: 1 on / 2 total", mobile)
|
||||
|
||||
def test_user_list_fits_phone_terminal(self):
|
||||
users = [{"username": "account-with-a-long-name", "active_conns": 2,
|
||||
"max_connections": 5, "total_bytes": 1234567,
|
||||
"expires_at": "2099-05-01T12:00:00Z"}]
|
||||
with patch.object(cli, "request", return_value=users), \
|
||||
patch.object(cli, "terminal_columns", return_value=40), \
|
||||
redirect_stdout(io.StringIO()) as output:
|
||||
cli.list_users()
|
||||
self.assertLessEqual(max(map(len, output.getvalue().splitlines())), 40)
|
||||
|
||||
def test_shared_xray_generates_path_without_password_prompt(self):
|
||||
server = {"listen": "0.0.0.0:80", "tls_forwarders": [{"listen": "0.0.0.0:443"}],
|
||||
"xray": {"mode": "native"}}
|
||||
config = {"inbounds": [{"tag": "first", "protocol": "vless", "dragoncoreSharedPort": True,
|
||||
"streamSettings": {"network": "ws", "wsSettings": {"path": "/c1"}}}]}
|
||||
calls = []
|
||||
|
||||
def fake_request(method, path, data=None):
|
||||
calls.append((method, path, data))
|
||||
if path == "/api/server/config":
|
||||
return server
|
||||
if path == "/api/xray/config" and method == "GET":
|
||||
return config
|
||||
if path == "/api/xray/status":
|
||||
return {"running": False}
|
||||
return None
|
||||
|
||||
# The flow asks only for the tag, transport, path, protocol, and consent.
|
||||
with patch.object(cli, "request", side_effect=fake_request), \
|
||||
patch("builtins.input", side_effect=["second", "", "", "", "YES"]), \
|
||||
redirect_stdout(io.StringIO()):
|
||||
cli.xray_shared_port()
|
||||
new = config["inbounds"][1]
|
||||
self.assertEqual(new["streamSettings"]["wsSettings"]["path"], "/c2")
|
||||
self.assertEqual(new["streamSettings"]["security"], "none")
|
||||
self.assertTrue(new["dragoncoreSharedPort"])
|
||||
self.assertEqual([item[1] for item in calls[-3:]],
|
||||
["/api/xray/config", "/api/xray/status", "/api/xray/start"])
|
||||
|
||||
def test_shared_xray_rejects_external_mode(self):
|
||||
with patch.object(cli, "request", return_value={"xray": {"mode": "external"}}), \
|
||||
patch("builtins.input", side_effect=AssertionError("prompted")):
|
||||
with self.assertRaisesRegex(cli.CLIError, "native"):
|
||||
cli.xray_shared_port()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user