修复--auth-stdin读取失败的bug
This commit is contained in:
@@ -3,7 +3,7 @@ from pathlib import Path
|
|||||||
|
|
||||||
from typer.testing import CliRunner
|
from typer.testing import CliRunner
|
||||||
|
|
||||||
from tjwater_cli import core
|
from tjwater_cli import common, core
|
||||||
from tjwater_cli.main import app, main
|
from tjwater_cli.main import app, main
|
||||||
|
|
||||||
|
|
||||||
@@ -65,6 +65,38 @@ def test_build_runtime_context_uses_default_server(monkeypatch):
|
|||||||
assert runtime.server == core.DEFAULT_SERVER
|
assert runtime.server == core.DEFAULT_SERVER
|
||||||
|
|
||||||
|
|
||||||
|
def test_auth_stdin_can_be_reused_with_runtime_context_cache(monkeypatch):
|
||||||
|
observed_runtime_ids: list[int] = []
|
||||||
|
|
||||||
|
def fake_request_json(ctx, **kwargs):
|
||||||
|
observed_runtime_ids.append(id(ctx))
|
||||||
|
assert ctx.auth.access_token == "token-1"
|
||||||
|
assert kwargs["params"] == {"network": "tjwater", "node": "11"}
|
||||||
|
return {"node": "11"}, 5
|
||||||
|
|
||||||
|
monkeypatch.setattr(common, "request_json", fake_request_json)
|
||||||
|
|
||||||
|
result = runner.invoke(
|
||||||
|
app,
|
||||||
|
["--auth-stdin", "network", "get-node-properties", "--node", "11"],
|
||||||
|
input=json.dumps(
|
||||||
|
{
|
||||||
|
"server": "http://server",
|
||||||
|
"access_token": "token-1",
|
||||||
|
"project_id": "project-1",
|
||||||
|
"network": "tjwater",
|
||||||
|
}
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
payload = json.loads(result.stdout)
|
||||||
|
|
||||||
|
assert result.exit_code == 0
|
||||||
|
assert payload["ok"] is True
|
||||||
|
assert payload["data"] == {"node": "11"}
|
||||||
|
assert len(observed_runtime_ids) == 1
|
||||||
|
|
||||||
|
|
||||||
def test_help_outputs_json_lists_commands():
|
def test_help_outputs_json_lists_commands():
|
||||||
result = runner.invoke(app, ["help"])
|
result = runner.invoke(app, ["help"])
|
||||||
payload = json.loads(result.stdout)
|
payload = json.loads(result.stdout)
|
||||||
|
|||||||
@@ -8,14 +8,24 @@ from .core import DEFAULT_TIMEOUT, build_runtime_context, emit_success, request_
|
|||||||
|
|
||||||
|
|
||||||
def runtime_context(ctx: typer.Context):
|
def runtime_context(ctx: typer.Context):
|
||||||
obj = ctx.obj or {}
|
obj = ctx.obj
|
||||||
return build_runtime_context(
|
if not isinstance(obj, dict):
|
||||||
|
obj = {}
|
||||||
|
ctx.obj = obj
|
||||||
|
|
||||||
|
cached_runtime = obj.get("_runtime_context")
|
||||||
|
if cached_runtime is not None:
|
||||||
|
return cached_runtime
|
||||||
|
|
||||||
|
runtime = build_runtime_context(
|
||||||
server=obj.get("server"),
|
server=obj.get("server"),
|
||||||
auth_stdin=obj.get("auth_stdin", False),
|
auth_stdin=obj.get("auth_stdin", False),
|
||||||
scheme=obj.get("scheme"),
|
scheme=obj.get("scheme"),
|
||||||
timeout=obj.get("timeout", DEFAULT_TIMEOUT),
|
timeout=obj.get("timeout", DEFAULT_TIMEOUT),
|
||||||
request_id=obj.get("request_id"),
|
request_id=obj.get("request_id"),
|
||||||
)
|
)
|
||||||
|
obj["_runtime_context"] = runtime
|
||||||
|
return runtime
|
||||||
|
|
||||||
|
|
||||||
def emit_api(
|
def emit_api(
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ def load_auth_context(auth_stdin: bool = False) -> AuthContext:
|
|||||||
project_id=_pick(raw, "project_id", "projectId", "x_project_id"),
|
project_id=_pick(raw, "project_id", "projectId", "x_project_id"),
|
||||||
user_id=_pick(raw, "user_id", "userId", "x_user_id"),
|
user_id=_pick(raw, "user_id", "userId", "x_user_id"),
|
||||||
username=_pick(raw, "username", "preferred_username"),
|
username=_pick(raw, "username", "preferred_username"),
|
||||||
network=_pick(raw, "network", "project_code", "projectCode", "project"),
|
network="tjwater",
|
||||||
headers={str(key): str(value) for key, value in headers.items()},
|
headers={str(key): str(value) for key, value in headers.items()},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user