From c8487225b7ac353a3e9427d4eb1946db4ab8731f Mon Sep 17 00:00:00 2001 From: Maxime Lamothe-Brassard Date: Sun, 1 Nov 2026 13:27:55 -0800 Subject: [PATCH] fix(auth): signup reads the new org's OID from data.oid and selects it auth signup read resp["oid"], but POST /orgs/new nests the OID under "data", so signup created the org without saving it as the default or printing "Organization created". Share org create's parsing as oid_from_create_response(), and fail with exit code 6 and next steps when the response has no OID. The signup test mocked the wrong response shape. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 13 +++++++++ limacharlie/commands/auth.py | 24 +++++++++++----- limacharlie/commands/org.py | 13 ++------- limacharlie/sdk/organization.py | 27 ++++++++++++++++++ tests/unit/test_cli_auth_signup.py | 46 +++++++++++++++++++++++++++++- 5 files changed, 104 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3bd1430..0c13f562 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,19 @@ ## Unreleased +### Auth — `auth signup` selects the organization it creates + +- `auth signup` read the new organization's ID from the wrong place in the API + response, so it created the organization but never printed + `Organization created: ` and never made it the default. It now reads + `data.oid`, as `org create` does, saves it as the default organization and + prints it. +- When the response carries no ID, `auth signup` now exits with code 6 and says + to run `limacharlie org list` and `limacharlie auth use-org `, instead + of exiting 0 without a word. The sign-in is kept. +- New SDK helper `limacharlie.sdk.organization.oid_from_create_response()` + reads the OID from an `Organization.create_org()` response. + ### Hive — page-by-page listing in the CLI, and errors that say what to do - `hive list`, `dr list` and every hive shortcut `list` command (`lookup list`, diff --git a/limacharlie/commands/auth.py b/limacharlie/commands/auth.py index 939f6702..faeb8509 100644 --- a/limacharlie/commands/auth.py +++ b/limacharlie/commands/auth.py @@ -17,7 +17,7 @@ is_ephemeral, ) from ..client import Client -from ..sdk.organization import Organization +from ..sdk.organization import Organization, oid_from_create_response from ..discovery import register_explain from ._output_helpers import command_output as _output @@ -645,7 +645,7 @@ def signup(ctx: click.Context, provider: str, no_browser: bool, org_name: str | try: resp = Organization.create_org(client, name=org_name, location="auto") - chosen_oid = resp.get("oid") + chosen_oid = oid_from_create_response(resp) except Exception as e: click.echo(f"Error creating organization: {e}", err=True) click.echo( @@ -656,11 +656,21 @@ def signup(ctx: click.Context, provider: str, no_browser: bool, org_name: str | ctx.exit(5) return - if chosen_oid: - write_credentials(env_name, oid=chosen_oid, api_key=None) - if not ctx.obj.quiet: - click.echo(f"Organization created: {chosen_oid}") - click.echo(f"\nAll set! Credentials saved for environment '{env_name}'.") + if not chosen_oid: + click.echo( + "Error: the organization was requested, but the API response did " + "not include its ID.\n" + "Your OAuth credentials have been saved. Run 'limacharlie org list' " + "to find it, then 'limacharlie auth use-org '.", + err=True, + ) + ctx.exit(6) + return + + write_credentials(env_name, oid=chosen_oid, api_key=None) + if not ctx.obj.quiet: + click.echo(f"Organization created: {chosen_oid}") + click.echo(f"\nAll set! Credentials saved for environment '{env_name}'.") # --------------------------------------------------------------------------- diff --git a/limacharlie/commands/org.py b/limacharlie/commands/org.py index c5f202d8..a567dc39 100644 --- a/limacharlie/commands/org.py +++ b/limacharlie/commands/org.py @@ -13,7 +13,7 @@ from ..cli import pass_context from ..client import Client from ..config import load_config, save_config -from ..sdk.organization import Organization +from ..sdk.organization import Organization, oid_from_create_response from ..output import format_output, detect_output_format from ..discovery import register_explain @@ -298,16 +298,7 @@ def create(ctx: click.Context, name: str, location: str, template: str | None, u _output(ctx, data) # Show the web app URL for the new org on success. - oid = None - inner = data.get("data") if isinstance(data, dict) else None - if isinstance(inner, dict): - oid = inner.get("oid") - elif isinstance(inner, str): - try: - import json - oid = json.loads(inner).get("oid") - except (ValueError, AttributeError): - pass + oid = oid_from_create_response(data) if oid and not ctx.obj.quiet: click.echo(f"\nOrganization URL: https://app.limacharlie.io/orgs/{oid}") diff --git a/limacharlie/sdk/organization.py b/limacharlie/sdk/organization.py index 73a0266d..9b206370 100644 --- a/limacharlie/sdk/organization.py +++ b/limacharlie/sdk/organization.py @@ -1134,3 +1134,30 @@ def get_jobs(self, start_time: int | None = None, end_time: int | None = None, l return [] jobs = self._client.unwrap(raw_jobs) return [job for job_id, job in jobs.items()] + + +def oid_from_create_response(resp: Any) -> str | None: + """Return the new organization's OID from a :meth:`Organization.create_org` response. + + The API nests it under ``data`` (``{"data": {"oid": ...}}``). ``data`` is + also accepted as a JSON-encoded string, as ``org create`` always did. + + Args: + resp: The value returned by :meth:`Organization.create_org`. + + Returns: + str | None: The OID, or None when the response does not carry one. + """ + if not isinstance(resp, dict): + return None + inner = resp.get("data") + if isinstance(inner, str): + try: + inner = json.loads(inner) + except ValueError: + return None + if isinstance(inner, dict): + oid = inner.get("oid") + if isinstance(oid, str) and oid: + return oid + return None diff --git a/tests/unit/test_cli_auth_signup.py b/tests/unit/test_cli_auth_signup.py index f5b9ad16..f6198bcf 100644 --- a/tests/unit/test_cli_auth_signup.py +++ b/tests/unit/test_cli_auth_signup.py @@ -53,7 +53,8 @@ def test_signup_creates_org_without_account_creation_call(tmp_config_file): "limacharlie.commands.auth.Client.refresh_jwt", ) as refresh_jwt, mock.patch( "limacharlie.commands.auth.Organization.create_org", - return_value={"oid": "new-oid"}, + # The real POST /orgs/new response nests the OID under "data". + return_value={"data": {"oid": "new-oid"}}, ) as create_org, mock.patch( "requests.post", side_effect=AssertionError("unexpected HTTP POST"), ), mock.patch( @@ -64,6 +65,7 @@ def test_signup_creates_org_without_account_creation_call(tmp_config_file): ]) assert result.exit_code == 0, result.output + assert "Organization created: new-oid" in result.output assert "Step 1/2" in result.output assert "Step 2/2" in result.output refresh_jwt.assert_called_once_with(oid_override="-") @@ -74,3 +76,45 @@ def test_signup_creates_org_without_account_creation_call(tmp_config_file): assert config["oid"] == "new-oid" assert config["oauth"]["id_token"] == "id-token" assert "email" not in config["oauth"] + + +def _run_signup(create_org_response): + with mock.patch( + "limacharlie.oauth_firebase_simple.SimpleFirebaseAuth.start_auth_flow", + return_value=_oauth_tokens(), + ), mock.patch( + "limacharlie.commands.auth.Client.refresh_jwt", + ), mock.patch( + "limacharlie.commands.auth.Organization.create_org", + return_value=create_org_response, + ): + return CliRunner().invoke(cli, [ + "auth", "signup", "--no-browser", "--org-name", "My Org", + ]) + + +def test_signup_reads_oid_from_json_string_data(tmp_config_file): + result = _run_signup({"data": '{"oid": "str-oid"}'}) + + assert result.exit_code == 0, result.output + assert "Organization created: str-oid" in result.output + assert load_config()["oid"] == "str-oid" + + +@pytest.mark.parametrize("response", [ + {"oid": "top-level-oid"}, + {"data": {}}, + {"data": "not json"}, + {}, +]) +def test_signup_without_oid_in_response_fails_with_guidance(tmp_config_file, response): + result = _run_signup(response) + + assert result.exit_code == 6, result.output + assert "Organization created" not in result.output + assert "limacharlie org list" in result.output + assert "auth use-org" in result.output + config = load_config() + # The sign-in is kept so the person can select the org themselves. + assert config["oauth"]["id_token"] == "id-token" + assert not config.get("oid")