Files
diary-news/scripts/_smoke_test.py

61 lines
2.4 KiB
Python

import os, paramiko
PW = os.environ["REMOTE_PASS"]
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect("207.57.129.228", port=19717, username="root", password=PW, timeout=15, allow_agent=False, look_for_keys=False)
def run(cmd, t=15):
si, so, se = c.exec_command(cmd, timeout=t)
out = so.read().decode("utf-8", "replace")
err = se.read().decode("utf-8", "replace")
rc = so.channel.recv_exit_status()
print(f"$ {cmd}")
if out: print(out, end="")
if err: print("[err]", err, end="", file=__import__("sys").stderr)
print(f" rc={rc}")
return out
# 1) 看 users
run("cd /srv/news && sg docker -c \"docker compose exec -T postgres psql -U news -d news -c 'SELECT id, username, role, length(password_hash) AS pwlen, created_at FROM users;'\" 2>&1 | tail -10")
# 2) 试 owner_pass 文件
run("echo '---owner_pass file---'; cat /root/.owner_pass; echo")
# 3) 重新生成 owner 密码
new_pw = "owner_pass_2026"
print(f"\n=== 重设 owner 密码为: {new_pw} ===")
run(f"cd /srv/news && sg docker -c \"docker compose exec -T api python -m app.scripts.create_user --username owner --password {new_pw}\" 2>&1 | tail -10")
# 4) 重试登录
import urllib.request, json
req = urllib.request.Request(
"http://localhost/api/v1/auth/login",
data=json.dumps({"username": "owner", "password": new_pw}).encode(),
headers={"Content-Type": "application/json"},
)
try:
resp = urllib.request.urlopen(req, timeout=10)
data = json.loads(resp.read())
print(f"\n=== 登录成功! token 前 40: {data['access_token'][:40]}... ===")
print(f" expires_in: {data['expires_in']}")
# 试拉 articles
req2 = urllib.request.Request(
"http://localhost/api/v1/articles?limit=3",
headers={"Authorization": f"Bearer {data['access_token']}"},
)
resp2 = urllib.request.urlopen(req2, timeout=10)
ad = json.loads(resp2.read())
print(f" articles: {len(ad.get('items', []))}")
if ad.get("items"):
a = ad["items"][0]
print(f" sample: id={a['id']} src={a['source']['name']} status={a['translation_status']}")
print(f" title: {a['title'][:60]}")
if a.get("title_zh"):
print(f" title_zh: {a['title_zh'][:60]}")
except urllib.error.HTTPError as e:
print(f"\n[FAIL] {e.code} {e.reason}")
print(e.read().decode())
except Exception as e:
print(f"\n[ERR] {e}")
c.close()