forked from bubbuild/bub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_builtin_cli.py
More file actions
71 lines (52 loc) · 2.49 KB
/
test_builtin_cli.py
File metadata and controls
71 lines (52 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from __future__ import annotations
from pathlib import Path
from typer.testing import CliRunner
import bub.builtin.cli as cli
from bub.framework import BubFramework
def _create_app() -> object:
framework = BubFramework()
framework.load_hooks()
return framework.create_cli_app()
def test_login_openai_runs_oauth_flow_and_prints_usage_hint(
tmp_path: Path,
monkeypatch,
) -> None:
captured: dict[str, object] = {}
def fake_login_openai_codex_oauth(**kwargs: object) -> cli.OpenAICodexOAuthTokens:
captured.update(kwargs)
prompt_for_redirect = kwargs["prompt_for_redirect"]
assert callable(prompt_for_redirect)
callback = prompt_for_redirect("https://auth.openai.com/authorize")
assert callback == "http://localhost:1455/auth/callback?code=test"
return cli.OpenAICodexOAuthTokens(
access_token="access", # noqa: S106
refresh_token="refresh", # noqa: S106
expires_at=123,
account_id="acct_123",
)
monkeypatch.setattr(cli, "login_openai_codex_oauth", fake_login_openai_codex_oauth)
monkeypatch.setattr(cli.typer, "prompt", lambda message: "http://localhost:1455/auth/callback?code=test")
result = CliRunner().invoke(
_create_app(),
["login", "openai", "--manual", "--no-browser", "--codex-home", str(tmp_path)],
)
assert result.exit_code == 0
assert captured["codex_home"] == tmp_path
assert captured["open_browser"] is False
assert captured["redirect_uri"] == cli.DEFAULT_CODEX_REDIRECT_URI
assert captured["timeout_seconds"] == 300.0
assert "login: ok" in result.stdout
assert "account_id: acct_123" in result.stdout
assert f"auth_file: {tmp_path / 'auth.json'}" in result.stdout
assert "BUB_MODEL=openai:gpt-5-codex" in result.stdout
def test_login_openai_surfaces_oauth_errors(monkeypatch) -> None:
def fake_login_openai_codex_oauth(**kwargs: object) -> cli.OpenAICodexOAuthTokens:
raise cli.CodexOAuthLoginError("bad redirect")
monkeypatch.setattr(cli, "login_openai_codex_oauth", fake_login_openai_codex_oauth)
result = CliRunner().invoke(_create_app(), ["login", "openai", "--manual"])
assert result.exit_code == 1
assert "Codex login failed: bad redirect" in result.stderr
def test_login_rejects_unsupported_provider() -> None:
result = CliRunner().invoke(_create_app(), ["login", "anthropic"])
assert result.exit_code == 1
assert "Unsupported auth provider: anthropic" in result.stderr