Files
Nexus/tests/test_btpanel_ssh_login.py
T
r 5ea5c3a411 fix(btpanel): 一键登录 URL 去掉安全入口路径
tmp_token 登录在 :port/login,非 :port/{admin}/login,后者 nginx 404。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-21 10:46:22 +08:00

89 lines
2.8 KiB
Python

"""Tests for Baota SSH login helpers."""
from unittest.mock import AsyncMock, patch
import pytest
from server.domain.models import Server
from server.infrastructure.btpanel.ssh_login import (
detect_bt_panel_installed,
normalize_temp_login_url,
_extract_temp_login_line,
ssh_temp_login_url,
)
@pytest.mark.asyncio
async def test_detect_bt_panel_installed_uses_status_field():
server = Server(id=1, name="s", domain="1.2.3.4")
with patch(
"server.infrastructure.btpanel.ssh_login.exec_ssh_command",
new_callable=AsyncMock,
return_value={"status": "success", "stdout": "BT_OK\n", "stderr": "", "exit_code": 0},
):
assert await detect_bt_panel_installed(server) is True
@pytest.mark.asyncio
async def test_detect_bt_panel_installed_failed_ssh():
server = Server(id=1, name="s", domain="1.2.3.4")
with patch(
"server.infrastructure.btpanel.ssh_login.exec_ssh_command",
new_callable=AsyncMock,
return_value={"status": "connection_error", "stdout": "", "stderr": "refused", "exit_code": -1},
):
assert await detect_bt_panel_installed(server) is False
def test_extract_temp_login_line_multiline():
stdout = "https://1.2.3.4:8888\n/login?tmp_token=abc123\n"
assert _extract_temp_login_line(stdout) == "/login?tmp_token=abc123"
def test_normalize_temp_login_relative_with_base_url():
url = normalize_temp_login_url(
"/login?tmp_token=abc123",
base_url="https://1.2.3.4:8888/secret",
prefer_host="1.2.3.4",
)
assert url == "https://1.2.3.4:8888/login?tmp_token=abc123"
def test_normalize_temp_login_full_url_strips_admin_path():
url = normalize_temp_login_url(
"https://127.0.0.1:8888/secret/login?tmp_token=abc123",
base_url=None,
prefer_host="5.6.7.8",
)
assert url == "https://5.6.7.8:8888/login?tmp_token=abc123"
def test_normalize_temp_login_full_url_rewrites_host():
url = normalize_temp_login_url(
"https://127.0.0.1:8888/login?tmp_token=abc123",
base_url="https://1.2.3.4:8888/secret",
prefer_host="5.6.7.8",
)
assert url == "https://5.6.7.8:8888/login?tmp_token=abc123"
@pytest.mark.asyncio
async def test_ssh_temp_login_url_accepts_relative_path():
server = Server(id=1, name="s", domain="5.6.7.8")
with patch(
"server.infrastructure.btpanel.ssh_login.exec_ssh_command",
new_callable=AsyncMock,
return_value={
"status": "success",
"stdout": "/login?tmp_token=VHj687eKwrPKr5dn\n",
"stderr": "",
"exit_code": 0,
},
):
url = await ssh_temp_login_url(
server,
base_url="https://5.6.7.8:8888/abc123",
)
assert url == "https://5.6.7.8:8888/login?tmp_token=VHj687eKwrPKr5dn"