0ec0aaf174
API set_temp_login 传 expire_time=86400;SSH 回退用自定义脚本替代 tools.py 3 小时默认。
54 lines
1.9 KiB
Python
54 lines
1.9 KiB
Python
"""Tests for 24h Baota temp login TTL."""
|
|
|
|
import base64
|
|
import re
|
|
import time
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
import pytest
|
|
|
|
from server.application.services.btpanel_service import BtPanelService
|
|
from server.domain.models import Server
|
|
from server.infrastructure.btpanel.constants import BT_TEMP_LOGIN_TTL_SECONDS
|
|
from server.infrastructure.btpanel.credentials import BtPanelCredentials
|
|
from server.infrastructure.btpanel.ssh_login import build_ssh_temp_login_command
|
|
|
|
|
|
def test_build_ssh_temp_login_command_uses_configured_ttl():
|
|
server = Server(id=1, name="s", domain="5.6.7.8")
|
|
cmd = build_ssh_temp_login_command(server, ttl_seconds=BT_TEMP_LOGIN_TTL_SECONDS)
|
|
assert "get_temp_login_ipv4" not in cmd
|
|
match = re.search(r"echo ([^ |]+) \| base64", cmd)
|
|
assert match, cmd
|
|
script = base64.b64decode(match.group(1)).decode()
|
|
assert f"ttl = int({BT_TEMP_LOGIN_TTL_SECONDS})" in script
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_login_url_via_api_passes_expire_time_and_builds_url_from_token():
|
|
server = Server(id=2, name="s", domain="5.6.7.8")
|
|
creds = BtPanelCredentials(
|
|
base_url="https://5.6.7.8:33994",
|
|
api_key="key",
|
|
verify_ssl=False,
|
|
)
|
|
svc = BtPanelService(db=None) # type: ignore[arg-type]
|
|
client = AsyncMock()
|
|
client.post = AsyncMock(
|
|
return_value={
|
|
"status": True,
|
|
"token": "a" * 48,
|
|
"expire": int(time.time()) + BT_TEMP_LOGIN_TTL_SECONDS,
|
|
},
|
|
)
|
|
|
|
before = int(time.time())
|
|
with patch("server.application.services.btpanel_service.BtPanelClient", return_value=client):
|
|
url = await svc._login_url_via_api(creds, server)
|
|
after = int(time.time())
|
|
|
|
payload = client.post.await_args.args[1]
|
|
assert payload["expire_time"] >= before + BT_TEMP_LOGIN_TTL_SECONDS
|
|
assert payload["expire_time"] <= after + BT_TEMP_LOGIN_TTL_SECONDS
|
|
assert url == f"https://5.6.7.8:33994/login?tmp_token={'a' * 48}"
|