Skip to content

Commit f7b7001

Browse files
SiddarthAAclaude
andcommitted
[failproofai-feat-failproofai-cloud-login] Keep the old prompts when stderr is redirected, and say when the plain flow is back
`fp login 2>file` on a terminal now offered the browser flow, with the link and the waiting going to that file: Enter started a login the person couldn't see or finish. Main asked for an email, so this broke the claim that only a terminal sees anything new. The plain menu now needs stderr to be a terminal too, which it is on Windows, the case it exists for. The test fixture stubs the new check for that reason: the condition it pins changed. `_browser_login_plain` also never said "back online" when the very next poll was the approval, as CodeRabbit pointed out, leaving "reconnecting" as its last word. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent cfa9e57 commit f7b7001

3 files changed

Lines changed: 52 additions & 2 deletions

File tree

‎fp-cloud-cli/fp_cli/commands/auth_cmds.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ def tick(_left: float, offline: bool) -> None:
194194

195195
outcome = auth.await_browser_approval(base, login, tick=tick, **options)
196196
if outcome == "approved":
197+
if was_offline:
198+
output.info("back online")
197199
return auth.claim_browser_login(base, login.poll_secret, **options)
198200
raise AuthError(
199201
f"{_LINK_ENDED.get(outcome, 'That link cannot be used any more')}.".capitalize(),
@@ -409,8 +411,9 @@ def login(
409411
output.auth_header()
410412
method = "email"
411413
creds = None
412-
# A terminal this can't draw the box on (Windows). Pipes, CI and `--json` skip this.
413-
if not state.json and not email and selectmod.stdin_is_tty():
414+
# A terminal this can't draw the box on (Windows). Pipes, CI and `--json` skip this, and so
415+
# does a redirected stderr: the link and the waiting would go to that file, out of sight.
416+
if not state.json and not email and selectmod.stdin_is_tty() and selectmod.stderr_is_tty():
414417
answer = ""
415418
while answer not in _SIGN_IN_WITH:
416419
answer = str(output.prompt("sign in with [browser/email]", default="browser")).strip().lower()

‎fp-cloud-cli/fp_cli/select.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ def stdin_is_tty() -> bool: # noqa: D401
2525
return False
2626

2727

28+
def stderr_is_tty() -> bool:
29+
"""True iff stderr is an interactive terminal, so a person can see what's written there."""
30+
try:
31+
return sys.stderr.isatty()
32+
except Exception:
33+
return False
34+
35+
2836
def choose_org(slugs: Sequence[str], default: Optional[str] = None) -> str:
2937
"""Render ``slugs`` and prompt until the user picks one (by slug or number).
3038

‎fp-cloud-cli/tests/test_browser_login.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ def interrupted(*_a, **_k):
426426
@pytest.fixture
427427
def plain_terminal(monkeypatch):
428428
monkeypatch.setattr("fp_cli.select.stdin_is_tty", lambda: True)
429+
monkeypatch.setattr("fp_cli.select.stderr_is_tty", lambda: True)
429430
monkeypatch.setattr("fp_cli.select.login_box_supported", lambda: False)
430431
# Never a real browser from a test run, wherever it runs.
431432
monkeypatch.setattr(auth, "open_url", lambda *_a, **_k: pytest.fail("opened a browser"))
@@ -565,6 +566,44 @@ def test_the_plain_flow_says_when_it_is_reconnecting_once(home, runner, plain_te
565566
assert "back online" in result.output
566567

567568

569+
@respx.mock
570+
def test_the_plain_flow_says_it_is_back_when_the_next_answer_is_the_approval(home, runner, plain_terminal, monkeypatch):
571+
_fast_links(monkeypatch)
572+
respx.post(f"{BASE}/api/auth/cli/start").mock(return_value=_start())
573+
respx.post(f"{BASE}/api/auth/cli/poll").mock(
574+
side_effect=[httpx.ConnectError("x"), httpx.Response(200, json={"status": "approved"})]
575+
)
576+
respx.post(f"{BASE}/api/auth/cli/claim").mock(return_value=httpx.Response(201, json=CLAIMED))
577+
respx.get(f"{BASE}/api/auth/session").mock(return_value=httpx.Response(200, json=SESSION))
578+
579+
result = runner.invoke(app, ["--base-url", BASE, "login"], input="browser\n")
580+
581+
assert result.exit_code == 0, result.output
582+
assert result.output.index("reconnecting") < result.output.index("back online")
583+
584+
585+
@respx.mock
586+
def test_a_redirected_stderr_keeps_the_old_prompts(home, runner, plain_terminal, monkeypatch):
587+
# The link and the waiting would go to that file, and the person would watch nothing.
588+
monkeypatch.setattr("fp_cli.select.stderr_is_tty", lambda: False)
589+
start = respx.post(f"{BASE}/api/auth/cli/start")
590+
respx.post(f"{BASE}/api/auth/otp/request").mock(return_value=httpx.Response(200, json={"ok": True}))
591+
respx.post(f"{BASE}/api/auth/otp/verify").mock(
592+
return_value=httpx.Response(
593+
200,
594+
json={"user": {"id": "u1", "email": "me@test"}, "expires_in_secs": 3600},
595+
headers={"set-cookie": "ae_session=tok-otp; Path=/"},
596+
)
597+
)
598+
respx.get(f"{BASE}/api/auth/session").mock(return_value=httpx.Response(200, json=SESSION))
599+
600+
result = runner.invoke(app, ["--base-url", BASE, "login"], input="me@test\n123456\n")
601+
602+
assert result.exit_code == 0, result.output
603+
assert not start.called
604+
assert config.load_config().session_token == "tok-otp"
605+
606+
568607
@respx.mock
569608
def test_choosing_email_never_touches_the_browser_routes(home, runner, plain_terminal):
570609
start = respx.post(f"{BASE}/api/auth/cli/start")

0 commit comments

Comments
 (0)