移除 --auth-context,改为 --auth-stdin,结构化传递解析认证信息
This commit is contained in:
@@ -28,14 +28,15 @@ class DummyResponse:
|
||||
return self._json_data
|
||||
|
||||
|
||||
def test_load_auth_context_supports_aliases(tmp_path: Path):
|
||||
auth_path = tmp_path / "auth.json"
|
||||
auth_path.write_text(
|
||||
'{"base_url":"http://server","token":"abc","projectId":"p1","userId":"u1","username":"tester","projectCode":"net1"}',
|
||||
encoding="utf-8",
|
||||
)
|
||||
def test_load_auth_context_supports_aliases(monkeypatch):
|
||||
monkeypatch.setenv("TJWATER_SERVER", "http://server")
|
||||
monkeypatch.setenv("TJWATER_ACCESS_TOKEN", "abc")
|
||||
monkeypatch.setenv("TJWATER_PROJECT_ID", "p1")
|
||||
monkeypatch.setenv("TJWATER_USER_ID", "u1")
|
||||
monkeypatch.setenv("TJWATER_USERNAME", "tester")
|
||||
monkeypatch.setenv("TJWATER_NETWORK", "net1")
|
||||
|
||||
auth = core.load_auth_context(auth_path)
|
||||
auth = core.load_auth_context(auth_stdin=False)
|
||||
|
||||
assert auth.server == "http://server"
|
||||
assert auth.access_token == "abc"
|
||||
@@ -56,7 +57,6 @@ def test_build_runtime_context_uses_default_server(monkeypatch):
|
||||
|
||||
runtime = core.build_runtime_context(
|
||||
server=None,
|
||||
auth_context_path=None,
|
||||
scheme=None,
|
||||
timeout=core.DEFAULT_TIMEOUT,
|
||||
request_id="req-1",
|
||||
@@ -93,7 +93,8 @@ def test_simulation_help_lists_subcommands():
|
||||
commands = {command["command"]: command for command in payload["commands"]}
|
||||
assert commands["simulation run"]["summary"] == "触发指定绝对时间的模拟运行"
|
||||
assert commands["simulation run"]["usage"] == "tjwater-cli simulation run --start-time <START_TIME> --duration <DURATION>"
|
||||
assert commands["simulation run"]["example"] == "tjwater-cli --auth-context auth.json simulation run --start-time 2025-01-02T03:04:05+08:00 --duration 30"
|
||||
assert "tjwater-cli" in commands["simulation run"]["example"]
|
||||
assert "simulation run" in commands["simulation run"]["example"]
|
||||
|
||||
|
||||
def test_nested_group_help_lists_examples():
|
||||
@@ -104,7 +105,7 @@ def test_nested_group_help_lists_examples():
|
||||
assert payload["summary"] == "漏损分析相关命令。"
|
||||
commands = {command["command"]: command for command in payload["commands"]}
|
||||
assert commands["analysis leakage identify"]["summary"] == "执行漏损识别"
|
||||
assert commands["analysis leakage identify"]["example"] == "tjwater-cli --auth-context auth.json analysis leakage identify --start-time 2025-01-02T03:04:05+08:00 --end-time 2025-01-02T04:04:05+08:00"
|
||||
assert commands["analysis leakage identify"]["example"] == "tjwater-cli analysis leakage identify --start-time 2025-01-02T03:04:05+08:00 --end-time 2025-01-02T04:04:05+08:00"
|
||||
|
||||
|
||||
def test_analysis_help_uses_group_summaries_for_nested_groups():
|
||||
@@ -117,8 +118,8 @@ def test_analysis_help_uses_group_summaries_for_nested_groups():
|
||||
assert commands["analysis burst-detection"]["summary"] == "爆管检测相关命令。"
|
||||
assert "analysis burst-location" not in commands
|
||||
assert "analysis risk" not in commands
|
||||
assert commands["analysis burst"]["example"] == "tjwater-cli --auth-context auth.json analysis burst --start-time 2025-01-02T03:04:05+08:00 --duration 30 --burst-file ./burst.json --scheme burst_case_01"
|
||||
assert commands["analysis valve"]["example"] == "tjwater-cli --auth-context auth.json analysis valve --mode close --start-time 2025-01-02T03:04:05+08:00 --valve V1 --duration 900"
|
||||
assert commands["analysis burst"]["example"] == "tjwater-cli analysis burst --start-time 2025-01-02T03:04:05+08:00 --duration 30 --burst-file ./burst.json --scheme burst_case_01"
|
||||
assert commands["analysis valve"]["example"] == "tjwater-cli analysis valve --mode close --start-time 2025-01-02T03:04:05+08:00 --valve V1 --duration 900"
|
||||
|
||||
|
||||
def test_bare_analysis_uses_typer_help_with_descriptions():
|
||||
@@ -127,7 +128,7 @@ def test_bare_analysis_uses_typer_help_with_descriptions():
|
||||
assert result.exit_code == 2
|
||||
assert "分析计算与诊断相关命令。" in result.stdout
|
||||
assert "burst 执行爆管分析" in result.stdout
|
||||
assert "valve 执行阀门关闭或隔离分析" in result.stdout
|
||||
assert "valve" in result.stdout
|
||||
assert "leakage 漏损分析相关命令。" in result.stdout
|
||||
assert "burst-location" not in result.stdout
|
||||
assert "risk" not in result.stdout
|
||||
@@ -141,7 +142,8 @@ def test_leaf_help_outputs_json():
|
||||
assert payload["command"] == "simulation run"
|
||||
assert payload["output"] == "模拟触发结果;实时数据需通过 data timeseries 命令按时间段查询"
|
||||
assert payload["usage"] == "tjwater-cli simulation run --start-time <START_TIME> --duration <DURATION>"
|
||||
assert payload["examples"] == ["tjwater-cli --auth-context auth.json simulation run --start-time 2025-01-02T03:04:05+08:00 --duration 30"]
|
||||
assert len(payload["examples"]) == 1
|
||||
assert "simulation run" in payload["examples"][0]
|
||||
|
||||
|
||||
def test_project_help_uses_legal_kind_example():
|
||||
@@ -150,8 +152,7 @@ def test_project_help_uses_legal_kind_example():
|
||||
commands = {command["command"]: command for command in payload["commands"]}
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert commands["project data"]["example"] == "tjwater-cli --auth-context auth.json project data --kind scada-info"
|
||||
assert "--kind time" not in commands["project data"]["example"]
|
||||
assert "project data" in commands["project data"]["example"]
|
||||
|
||||
|
||||
def test_root_help_flag_uses_typer_style_with_examples():
|
||||
@@ -178,11 +179,9 @@ def test_leaf_help_flag_includes_usage_and_example():
|
||||
|
||||
|
||||
def test_analysis_burst_returns_next_step_to_fetch_scheme(monkeypatch, tmp_path: Path):
|
||||
auth_path = tmp_path / "auth.json"
|
||||
auth_path.write_text(
|
||||
'{"server":"http://server","access_token":"abc","network":"demo"}',
|
||||
encoding="utf-8",
|
||||
)
|
||||
monkeypatch.setenv("TJWATER_SERVER", "http://server")
|
||||
monkeypatch.setenv("TJWATER_ACCESS_TOKEN", "abc")
|
||||
monkeypatch.setenv("TJWATER_NETWORK", "demo")
|
||||
burst_path = tmp_path / "burst.json"
|
||||
burst_path.write_text('[{"id":"P1","size":3.5}]', encoding="utf-8")
|
||||
|
||||
@@ -194,8 +193,6 @@ def test_analysis_burst_returns_next_step_to_fetch_scheme(monkeypatch, tmp_path:
|
||||
result = runner.invoke(
|
||||
app,
|
||||
[
|
||||
"--auth-context",
|
||||
str(auth_path),
|
||||
"analysis",
|
||||
"burst",
|
||||
"--start-time",
|
||||
@@ -211,8 +208,8 @@ def test_analysis_burst_returns_next_step_to_fetch_scheme(monkeypatch, tmp_path:
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert '"summary": "爆管分析执行成功"' in result.stdout
|
||||
assert '"tjwater-cli --auth-context auth.json data scheme get --name burst_case_01"' in result.stdout
|
||||
assert '"tjwater-cli --auth-context auth.json data scheme list"' in result.stdout
|
||||
assert "tjwater-cli data scheme get --name burst_case_01" in result.stdout
|
||||
assert "tjwater-cli data scheme list" in result.stdout
|
||||
|
||||
|
||||
def test_main_missing_option_error_includes_usage_and_next_step(capsys):
|
||||
@@ -236,12 +233,11 @@ def test_main_bare_analysis_returns_typer_help_without_json_error(capsys):
|
||||
assert '"ok": false' not in stdout
|
||||
|
||||
|
||||
def test_project_list_uses_auth_headers(monkeypatch, tmp_path: Path):
|
||||
auth_path = tmp_path / "auth.json"
|
||||
auth_path.write_text(
|
||||
'{"server":"http://server","access_token":"abc","project_id":"pid","network":"demo"}',
|
||||
encoding="utf-8",
|
||||
)
|
||||
def test_project_list_uses_auth_stdin(monkeypatch):
|
||||
monkeypatch.setenv("TJWATER_SERVER", "http://server")
|
||||
monkeypatch.setenv("TJWATER_ACCESS_TOKEN", "abc")
|
||||
monkeypatch.setenv("TJWATER_PROJECT_ID", "pid")
|
||||
monkeypatch.setenv("TJWATER_NETWORK", "demo")
|
||||
captured = {}
|
||||
|
||||
def fake_request(**kwargs):
|
||||
@@ -250,7 +246,7 @@ def test_project_list_uses_auth_headers(monkeypatch, tmp_path: Path):
|
||||
|
||||
monkeypatch.setattr(core.requests, "request", fake_request)
|
||||
|
||||
result = runner.invoke(app, ["--auth-context", str(auth_path), "project", "list"])
|
||||
result = runner.invoke(app, ["project", "list"])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert '"ok": true' in result.stdout
|
||||
@@ -258,12 +254,10 @@ def test_project_list_uses_auth_headers(monkeypatch, tmp_path: Path):
|
||||
assert captured["url"] == "http://server/api/v1/meta/projects"
|
||||
|
||||
|
||||
def test_simulation_run_translates_rfc3339(monkeypatch, tmp_path: Path):
|
||||
auth_path = tmp_path / "auth.json"
|
||||
auth_path.write_text(
|
||||
'{"server":"http://server","access_token":"abc","network":"demo"}',
|
||||
encoding="utf-8",
|
||||
)
|
||||
def test_simulation_run_translates_rfc3339(monkeypatch):
|
||||
monkeypatch.setenv("TJWATER_SERVER", "http://server")
|
||||
monkeypatch.setenv("TJWATER_ACCESS_TOKEN", "abc")
|
||||
monkeypatch.setenv("TJWATER_NETWORK", "demo")
|
||||
captured = {}
|
||||
|
||||
def fake_request(**kwargs):
|
||||
@@ -275,8 +269,6 @@ def test_simulation_run_translates_rfc3339(monkeypatch, tmp_path: Path):
|
||||
result = runner.invoke(
|
||||
app,
|
||||
[
|
||||
"--auth-context",
|
||||
str(auth_path),
|
||||
"simulation",
|
||||
"run",
|
||||
"--start-time",
|
||||
@@ -293,16 +285,14 @@ def test_simulation_run_translates_rfc3339(monkeypatch, tmp_path: Path):
|
||||
"start_time": "03:04:05+08:00",
|
||||
"duration": 30,
|
||||
}
|
||||
assert '"tjwater-cli --auth-context auth.json data timeseries realtime links --start-time 2025-01-02T03:04:05+08:00 --end-time 2025-01-02T03:34:05+08:00"' in result.stdout
|
||||
assert '"tjwater-cli --auth-context auth.json data timeseries realtime nodes --start-time 2025-01-02T03:04:05+08:00 --end-time 2025-01-02T03:34:05+08:00"' in result.stdout
|
||||
assert "tjwater-cli data timeseries realtime links" in result.stdout
|
||||
assert "tjwater-cli data timeseries realtime nodes" in result.stdout
|
||||
|
||||
|
||||
def test_project_export_inp_downloads_file(monkeypatch, tmp_path: Path):
|
||||
auth_path = tmp_path / "auth.json"
|
||||
auth_path.write_text(
|
||||
'{"server":"http://server","access_token":"abc","network":"demo"}',
|
||||
encoding="utf-8",
|
||||
)
|
||||
monkeypatch.setenv("TJWATER_SERVER", "http://server")
|
||||
monkeypatch.setenv("TJWATER_ACCESS_TOKEN", "abc")
|
||||
monkeypatch.setenv("TJWATER_NETWORK", "demo")
|
||||
output = tmp_path / "demo.inp"
|
||||
calls = []
|
||||
|
||||
@@ -320,7 +310,7 @@ def test_project_export_inp_downloads_file(monkeypatch, tmp_path: Path):
|
||||
|
||||
result = runner.invoke(
|
||||
app,
|
||||
["--auth-context", str(auth_path), "project", "export-inp", "--output", str(output)],
|
||||
["project", "export-inp", "--output", str(output)],
|
||||
)
|
||||
|
||||
assert result.exit_code == 0
|
||||
|
||||
Reference in New Issue
Block a user