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
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: <OID>` 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 <OID>`, 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`,
Expand Down
24 changes: 17 additions & 7 deletions limacharlie/commands/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand All @@ -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 <OID>'.",
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}'.")


# ---------------------------------------------------------------------------
Expand Down
13 changes: 2 additions & 11 deletions limacharlie/commands/org.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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}")

Expand Down
27 changes: 27 additions & 0 deletions limacharlie/sdk/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
46 changes: 45 additions & 1 deletion tests/unit/test_cli_auth_signup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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="-")
Expand All @@ -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")
Loading