51b6d0cdff
与面板 common.py 一致;原双重 md5 导致密钥校验失败。 Co-authored-by: Cursor <cursoragent@cursor.com>
55 lines
1.6 KiB
Python
55 lines
1.6 KiB
Python
"""Tests for Baota panel client signing and credentials."""
|
|
|
|
import hashlib
|
|
import time
|
|
|
|
import pytest
|
|
|
|
from server.infrastructure.btpanel.client import BtPanelClient
|
|
from server.infrastructure.btpanel.credentials import (
|
|
BT_PANEL_ATTR,
|
|
BtPanelCredentials,
|
|
merge_bt_panel_extra,
|
|
public_bt_panel_status,
|
|
)
|
|
from server.domain.models import Server
|
|
|
|
|
|
def test_sign_algorithm_matches_panel_common():
|
|
api_key = "test-api-key"
|
|
now = 1700000000
|
|
expected = hashlib.md5(f"{now}{api_key}".encode()).hexdigest()
|
|
|
|
creds = BtPanelCredentials(base_url="http://127.0.0.1:8888", api_key=api_key)
|
|
client = BtPanelClient(creds, server_id=1)
|
|
# patch time
|
|
orig = time.time
|
|
try:
|
|
time.time = lambda: now # type: ignore[method-assign]
|
|
signed = client._sign()
|
|
finally:
|
|
time.time = orig # type: ignore[method-assign]
|
|
|
|
assert signed["request_time"] == now
|
|
assert signed["request_token"] == expected
|
|
|
|
|
|
def test_merge_bt_panel_extra_encrypts_key(monkeypatch):
|
|
monkeypatch.setattr(
|
|
"server.infrastructure.btpanel.credentials.encrypt_value",
|
|
lambda s: f"enc:{s}",
|
|
)
|
|
extra = merge_bt_panel_extra({}, base_url="https://1.2.3.4:8888/abc", api_key="secret")
|
|
block = extra[BT_PANEL_ATTR]
|
|
assert block["base_url"] == "https://1.2.3.4:8888/abc"
|
|
assert block["api_key_enc"] == "enc:secret"
|
|
|
|
|
|
def test_public_bt_panel_status():
|
|
server = Server(id=1, name="s", domain="1.2.3.4", extra_attrs={
|
|
BT_PANEL_ATTR: {"base_url": "https://x:8888", "api_key_enc": "x"},
|
|
})
|
|
st = public_bt_panel_status(server)
|
|
assert st["configured"] is True
|
|
assert st["api_key_set"] is True
|