fix(db): sync default_auth_strategy on platform reseed
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -360,6 +360,8 @@ def seed_platforms(conn) -> None:
|
|||||||
Upsert all platform definitions into the 'platforms' table.
|
Upsert all platform definitions into the 'platforms' table.
|
||||||
Must be called after init_db() in the same connection lifecycle.
|
Must be called after init_db() in the same connection lifecycle.
|
||||||
"""
|
"""
|
||||||
|
from db.platform_defaults import PLATFORM_DEFAULT_AUTH_STRATEGY
|
||||||
|
|
||||||
log = get_skill_logger()
|
log = get_skill_logger()
|
||||||
log.info("platform_seed_start")
|
log.info("platform_seed_start")
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
@@ -368,12 +370,13 @@ def seed_platforms(conn) -> None:
|
|||||||
for key, spec in PLATFORMS.items():
|
for key, spec in PLATFORMS.items():
|
||||||
aliases_json = json.dumps(spec.get("aliases", []), ensure_ascii=False)
|
aliases_json = json.dumps(spec.get("aliases", []), ensure_ascii=False)
|
||||||
capabilities_json = json.dumps(spec.get("capabilities", {}), ensure_ascii=False)
|
capabilities_json = json.dumps(spec.get("capabilities", {}), ensure_ascii=False)
|
||||||
|
default_auth_strategy = PLATFORM_DEFAULT_AUTH_STRATEGY.get(key)
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"""
|
"""
|
||||||
INSERT INTO platforms (platform_key, display_name, domain, provider_code,
|
INSERT INTO platforms (platform_key, display_name, domain, provider_code,
|
||||||
default_url, aliases_json, capabilities_json,
|
default_url, aliases_json, capabilities_json,
|
||||||
enabled, created_at, updated_at)
|
enabled, default_auth_strategy, created_at, updated_at)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, 1, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?, ?, 1, ?, ?, ?)
|
||||||
ON CONFLICT(platform_key) DO UPDATE SET
|
ON CONFLICT(platform_key) DO UPDATE SET
|
||||||
display_name = excluded.display_name,
|
display_name = excluded.display_name,
|
||||||
domain = excluded.domain,
|
domain = excluded.domain,
|
||||||
@@ -382,6 +385,7 @@ def seed_platforms(conn) -> None:
|
|||||||
aliases_json = excluded.aliases_json,
|
aliases_json = excluded.aliases_json,
|
||||||
capabilities_json = excluded.capabilities_json,
|
capabilities_json = excluded.capabilities_json,
|
||||||
enabled = 1,
|
enabled = 1,
|
||||||
|
default_auth_strategy = excluded.default_auth_strategy,
|
||||||
updated_at = excluded.updated_at
|
updated_at = excluded.updated_at
|
||||||
""",
|
""",
|
||||||
(
|
(
|
||||||
@@ -392,6 +396,7 @@ def seed_platforms(conn) -> None:
|
|||||||
spec.get("default_url", ""),
|
spec.get("default_url", ""),
|
||||||
aliases_json,
|
aliases_json,
|
||||||
capabilities_json,
|
capabilities_json,
|
||||||
|
default_auth_strategy,
|
||||||
now,
|
now,
|
||||||
now,
|
now,
|
||||||
),
|
),
|
||||||
|
|||||||
68
tests/test_seed_platforms_upgrade.py
Normal file
68
tests/test_seed_platforms_upgrade.py
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import sys
|
||||||
|
import tempfile
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
_scripts = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "scripts")
|
||||||
|
if _scripts not in sys.path:
|
||||||
|
sys.path.insert(0, _scripts)
|
||||||
|
|
||||||
|
from db.connection import get_conn, init_db # noqa: E402
|
||||||
|
from util.platforms import seed_platforms # noqa: E402
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def iso_seed_env(monkeypatch):
|
||||||
|
tmp = tempfile.mkdtemp(prefix="seed_plat_")
|
||||||
|
data_root = os.path.join(tmp, "claw-data")
|
||||||
|
os.makedirs(data_root)
|
||||||
|
uid = "seed_user_1"
|
||||||
|
monkeypatch.setenv("JIANGCHANG_DATA_ROOT", data_root)
|
||||||
|
monkeypatch.setenv("JIANGCHANG_USER_ID", uid)
|
||||||
|
monkeypatch.setenv("CLAW_DATA_ROOT", data_root)
|
||||||
|
monkeypatch.setenv("CLAW_USER_ID", uid)
|
||||||
|
yield
|
||||||
|
shutil.rmtree(tmp, ignore_errors=True)
|
||||||
|
|
||||||
|
|
||||||
|
def _default_auth_maersk() -> str | None:
|
||||||
|
conn = get_conn()
|
||||||
|
try:
|
||||||
|
row = conn.execute(
|
||||||
|
"SELECT default_auth_strategy FROM platforms WHERE platform_key = ?",
|
||||||
|
("maersk",),
|
||||||
|
).fetchone()
|
||||||
|
return row[0] if row else None
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_default_auth_strategy_filled_on_first_seed(iso_seed_env):
|
||||||
|
init_db()
|
||||||
|
conn = get_conn()
|
||||||
|
try:
|
||||||
|
seed_platforms(conn)
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
assert _default_auth_maersk() == "qr_code_manual"
|
||||||
|
|
||||||
|
|
||||||
|
def test_default_auth_strategy_updated_on_reseed(iso_seed_env):
|
||||||
|
init_db()
|
||||||
|
conn = get_conn()
|
||||||
|
try:
|
||||||
|
seed_platforms(conn)
|
||||||
|
conn.execute(
|
||||||
|
"UPDATE platforms SET default_auth_strategy = ? WHERE platform_key = ?",
|
||||||
|
("password_auto", "maersk"),
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
seed_platforms(conn)
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
assert _default_auth_maersk() == "qr_code_manual"
|
||||||
Reference in New Issue
Block a user