feat: improve user feedback when user is not authenticated

This commit is contained in:
Lucas Gomide
2025-06-09 10:56:01 -03:00
parent 5b241f1459
commit 9de40c53f3
2 changed files with 82 additions and 31 deletions

View File

@@ -1,6 +1,7 @@
from rich.console import Console from rich.console import Console
from rich.table import Table from rich.table import Table
from requests import HTTPError
from crewai.cli.command import BaseCommand, PlusAPIMixin from crewai.cli.command import BaseCommand, PlusAPIMixin
from crewai.cli.config import Settings from crewai.cli.config import Settings
@@ -28,6 +29,12 @@ class OrganizationCommand(BaseCommand, PlusAPIMixin):
table.add_row(org["name"], org["uuid"]) table.add_row(org["name"], org["uuid"])
console.print(table) console.print(table)
except HTTPError as e:
if e.response.status_code == 401:
console.print("You are not logged in to any organization. Use 'crewai login' to login.", style="bold red")
return
console.print(f"Failed to retrieve organization list: {str(e)}", style="bold red")
raise SystemExit(1)
except Exception as e: except Exception as e:
console.print(f"Failed to retrieve organization list: {str(e)}", style="bold red") console.print(f"Failed to retrieve organization list: {str(e)}", style="bold red")
raise SystemExit(1) raise SystemExit(1)
@@ -49,6 +56,12 @@ class OrganizationCommand(BaseCommand, PlusAPIMixin):
settings.dump() settings.dump()
console.print(f"Successfully switched to {org['name']} ({org['uuid']})", style="bold green") console.print(f"Successfully switched to {org['name']} ({org['uuid']})", style="bold green")
except HTTPError as e:
if e.response.status_code == 401:
console.print("You are not logged in to any organization. Use 'crewai login' to login.", style="bold red")
return
console.print(f"Failed to retrieve organization list: {str(e)}", style="bold red")
raise SystemExit(1)
except Exception as e: except Exception as e:
console.print(f"Failed to switch organization: {str(e)}", style="bold red") console.print(f"Failed to switch organization: {str(e)}", style="bold red")
raise SystemExit(1) raise SystemExit(1)

View File

@@ -204,3 +204,41 @@ class TestOrganizationCommand(unittest.TestCase):
"You're not currently logged in to any organization.", "You're not currently logged in to any organization.",
style="yellow" style="yellow"
) )
@patch('crewai.cli.organization.main.console')
def test_list_organizations_unauthorized(self, mock_console):
mock_response = MagicMock()
mock_http_error = requests.exceptions.HTTPError(
"401 Client Error: Unauthorized",
response=MagicMock(status_code=401)
)
mock_response.raise_for_status.side_effect = mock_http_error
self.org_command.plus_api_client.get_organizations.return_value = mock_response
self.org_command.list()
self.org_command.plus_api_client.get_organizations.assert_called_once()
mock_console.print.assert_called_once_with(
"You are not logged in to any organization. Use 'crewai login' to login.",
style="bold red"
)
@patch('crewai.cli.organization.main.console')
def test_switch_organization_unauthorized(self, mock_console):
mock_response = MagicMock()
mock_http_error = requests.exceptions.HTTPError(
"401 Client Error: Unauthorized",
response=MagicMock(status_code=401)
)
mock_response.raise_for_status.side_effect = mock_http_error
self.org_command.plus_api_client.get_organizations.return_value = mock_response
self.org_command.switch("test-id")
self.org_command.plus_api_client.get_organizations.assert_called_once()
mock_console.print.assert_called_once_with(
"You are not logged in to any organization. Use 'crewai login' to login.",
style="bold red"
)