Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion haystack_enterprise_sdk/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,12 @@ def _open_login_url(url: str) -> None:


def _show_device_code(user_code: str, verification_uri: str, verification_uri_complete: Optional[str]) -> None:
typer.echo(f"On any device, open:\n\n {verification_uri_complete or verification_uri}\n")
url = verification_uri_complete or verification_uri
typer.echo(f"Opening your browser to log in. If it doesn't open, on any device visit:\n\n {url}\n")
typer.echo(f"and confirm the code {user_code}. Waiting for approval...")
# Over SSH the only "browser" is a console one (lynx, w3m) that would take over the terminal.
if not os.environ.get("SSH_CONNECTION"):
webbrowser.open(url)


def _choose(label: str, options: List[str], default: Optional[str] = None) -> str:
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
_browser_login,
_configure_cli_logging,
_maybe_print_update_hint,
_show_device_code,
_ui_url_for,
cli_app,
run_packaged,
Expand Down Expand Up @@ -545,6 +546,19 @@ def test_browser_login_times_out(self) -> None:
with patch("haystack_enterprise_sdk.cli.webbrowser.open"), pytest.raises(typer.Exit):
_browser_login("https://cloud.deepset.ai", timeout=0.2)

@pytest.mark.parametrize("ssh, opened", [(False, True), (True, False)])
def test_show_device_code_opens_browser_unless_over_ssh(
self, ssh: bool, opened: bool, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.delenv("SSH_CONNECTION", raising=False)
if ssh:
monkeypatch.setenv("SSH_CONNECTION", "10.0.0.1 22 10.0.0.2 22")
with patch("haystack_enterprise_sdk.cli.webbrowser.open") as open_mock:
_show_device_code("123", "https://id.example/device", "https://id.example/device?code=123")
assert open_mock.called is opened
if opened:
open_mock.assert_called_once_with("https://id.example/device?code=123")

@pytest.mark.parametrize(
"api_url, ui_url",
[
Expand Down
Loading