Skip to content

Commit 10cbf7f

Browse files
committed
fix: Fix get_active_profile to always return a profile
#13 Branch: Profiles-13 Signed-off-by: Gabe Goodhart <ghart@us.ibm.com>
1 parent 7a688e5 commit 10cbf7f

5 files changed

Lines changed: 33 additions & 60 deletions

File tree

cforge/commands/settings/whoami.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ def whoami() -> None:
1818
Displays where the authentication token is coming from (if any) and
1919
information about the active profile if one is set.
2020
"""
21+
2122
console = get_console()
2223
settings = get_settings()
2324
env_token = settings.mcpgateway_bearer_token
2425
stored_token = load_token()
2526
active_profile = get_active_profile()
2627

27-
# Display active profile information if available
28+
# Display active profile information
2829
console.print("[bold cyan]Active Profile:[/bold cyan]")
2930
console.print(f" [cyan]Name:[/cyan] {active_profile.name}")
3031
console.print(f" [cyan]ID:[/cyan] {active_profile.id}")

cforge/profile_utils.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,24 +210,26 @@ def get_profile(profile_id: str) -> Optional[AuthProfile]:
210210
return store.profiles.get(profile_id)
211211

212212

213-
def get_active_profile() -> Optional[AuthProfile]:
213+
def get_active_profile() -> AuthProfile:
214214
"""Get the currently active profile, including the virtual default profile.
215215
216216
Returns:
217-
AuthProfile if an active profile is set, or the virtual default profile
218-
if no Desktop default exists
217+
AuthProfile if an active profile is set, or default (virtual or desktop)
219218
"""
220219
if (store := load_profile_store()) and store.active_profile_id:
221-
return store.profiles.get(store.active_profile_id)
220+
profile = store.profiles.get(store.active_profile_id)
221+
if not profile:
222+
raise ValueError("BAD STATE: Profile store active profile id not found in profiles")
223+
return profile
222224

223225
# Check if Desktop app has created a default profile
224226
expected_default_url = get_default_api_url()
225227

226228
if store:
227229
for profile in store.profiles.values():
228230
if profile.api_url == expected_default_url and profile.metadata and profile.metadata.is_internal:
229-
# Desktop default exists, return None (no active profile)
230-
return None
231+
# If Desktop default exists, use that
232+
return profile
231233

232234
# Return virtual default profile if no Desktop default exists
233235
return AuthProfile(

tests/commands/settings/test_profiles.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ def test_profiles_create_success(self, mock_console, mock_settings) -> None:
552552
"created_at": datetime.now(),
553553
}
554554

555-
profiles_create()
555+
profiles_create(None)
556556

557557
# Verify success message
558558
print_calls = [str(call) for call in mock_console.print.call_args_list]
@@ -578,7 +578,7 @@ def test_profiles_create_and_enable(self, mock_console, mock_settings) -> None:
578578
"created_at": datetime.now(),
579579
}
580580

581-
profiles_create()
581+
profiles_create(None)
582582

583583
# Verify success and enable messages
584584
print_calls = [str(call) for call in mock_console.print.call_args_list]
@@ -591,7 +591,7 @@ def test_profiles_create_error(self, mock_console, mock_settings) -> None:
591591
with patch("cforge.commands.settings.profiles.get_console", return_value=mock_console):
592592
with patch("cforge.commands.settings.profiles.prompt_for_schema", side_effect=Exception("Test error")):
593593
with pytest.raises(typer.Exit) as exc_info:
594-
profiles_create()
594+
profiles_create(None)
595595

596596
assert exc_info.value.exit_code == 1
597597
assert any("Error creating profile" in str(call) for call in mock_console.print.call_args_list)
@@ -612,7 +612,7 @@ def test_profiles_create_enable_fails(self, mock_console, mock_settings) -> None
612612
"created_at": datetime.now(),
613613
}
614614

615-
profiles_create()
615+
profiles_create(None)
616616

617617
# Verify failure message
618618
print_calls = [str(call) for call in mock_console.print.call_args_list]
@@ -650,7 +650,7 @@ def test_profiles_create_with_existing_store(self, mock_console, mock_settings)
650650
"created_at": datetime.now(),
651651
}
652652

653-
profiles_create()
653+
profiles_create(None)
654654

655655
# Verify both profiles exist in the store
656656
updated_store = load_profile_store()

tests/commands/settings/test_whoami.py

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,11 @@ def test_whoami_with_env_token(self, mock_settings, mock_console) -> None:
2525
with patch("cforge.commands.settings.whoami.get_console", return_value=mock_console):
2626
with patch("cforge.commands.settings.whoami.get_settings", return_value=mock_settings):
2727
with patch("cforge.commands.settings.whoami.load_token", return_value=None):
28-
with patch("cforge.commands.settings.whoami.get_active_profile", return_value=None):
29-
whoami()
30-
31-
# Verify console output
32-
assert mock_console.print.call_count == 2
33-
34-
# Check first call - authentication status
35-
first_call = mock_console.print.call_args_list[0][0][0]
36-
assert "Authenticated via MCPGATEWAY_BEARER_TOKEN" in first_call
37-
assert "[green]" in first_call
28+
whoami()
3829

39-
# Check second call - token preview
40-
second_call = mock_console.print.call_args_list[1][0][0]
41-
assert "Token:" in second_call
42-
assert "env_token_" in second_call
43-
assert "..." in second_call
30+
call_messages = [call[0][0] for call in mock_console.print.call_args_list if call and call[0]]
31+
assert any("Authenticated via MCPGATEWAY_BEARER_TOKEN" in call for call in call_messages)
32+
assert any("env_token_" in call for call in call_messages)
4433

4534
def test_whoami_with_stored_token(self, mock_settings, mock_console) -> None:
4635
"""Test whoami when authenticated via stored token file."""
@@ -50,22 +39,11 @@ def test_whoami_with_stored_token(self, mock_settings, mock_console) -> None:
5039
with patch("cforge.commands.settings.whoami.get_console", return_value=mock_console):
5140
with patch("cforge.commands.settings.whoami.get_settings", return_value=mock_settings):
5241
with patch("cforge.commands.settings.whoami.load_token", return_value=stored_token):
53-
with patch("cforge.commands.settings.whoami.get_active_profile", return_value=None):
54-
whoami()
55-
56-
# Verify console output
57-
assert mock_console.print.call_count == 2
58-
59-
# Check first call - authentication status with file path
60-
first_call = mock_console.print.call_args_list[0][0][0]
61-
assert "Authenticated via stored token" in first_call
62-
assert "[green]" in first_call
42+
whoami()
6343

64-
# Check second call - token preview
65-
second_call = mock_console.print.call_args_list[1][0][0]
66-
assert "Token:" in second_call
67-
assert "stored_tok" in second_call
68-
assert "..." in second_call
44+
call_messages = [call[0][0] for call in mock_console.print.call_args_list if call and call[0]]
45+
assert any("Authenticated via stored token" in call for call in call_messages)
46+
assert any("stored_tok" in call for call in call_messages)
6947

7048
def test_whoami_not_authenticated(self, mock_settings, mock_console) -> None:
7149
"""Test whoami when not authenticated."""
@@ -75,17 +53,11 @@ def test_whoami_not_authenticated(self, mock_settings, mock_console) -> None:
7553
with patch("cforge.commands.settings.whoami.get_console", return_value=mock_console):
7654
with patch("cforge.commands.settings.whoami.get_settings", return_value=mock_settings):
7755
with patch("cforge.commands.settings.whoami.load_token", return_value=None):
78-
with patch("cforge.commands.settings.whoami.get_active_profile", return_value=None):
79-
whoami()
80-
81-
# Verify console output
82-
mock_console.print.assert_called_once()
56+
whoami()
8357

84-
# Check output message
85-
call_args = mock_console.print.call_args[0][0]
86-
assert "Not authenticated" in call_args
87-
assert "cforge login" in call_args
88-
assert "[yellow]" in call_args
58+
call_messages = [call[0][0] for call in mock_console.print.call_args_list if call and call[0]]
59+
assert any("Not authenticated" in call for call in call_messages)
60+
assert any("cforge login" in call for call in call_messages)
8961

9062
def test_whoami_env_token_takes_precedence(self, mock_settings, mock_console) -> None:
9163
"""Test that env token takes precedence over stored token."""
@@ -96,13 +68,11 @@ def test_whoami_env_token_takes_precedence(self, mock_settings, mock_console) ->
9668
with patch("cforge.commands.settings.whoami.get_console", return_value=mock_console):
9769
with patch("cforge.commands.settings.whoami.get_settings", return_value=mock_settings):
9870
with patch("cforge.commands.settings.whoami.load_token", return_value=stored_token):
99-
with patch("cforge.commands.settings.whoami.get_active_profile", return_value=None):
100-
whoami()
71+
whoami()
10172

102-
# Should show env token, not stored token
103-
first_call = mock_console.print.call_args_list[0][0][0]
104-
assert "MCPGATEWAY_BEARER_TOKEN" in first_call
105-
assert "stored token" not in first_call
73+
call_messages = [call[0][0] for call in mock_console.print.call_args_list if call and call[0]]
74+
assert any("MCPGATEWAY_BEARER_TOKEN" in call for call in call_messages)
75+
assert not any("stored token" in call for call in call_messages)
10676

10777

10878
class TestWhoamiWithProfiles:

tests/test_profile_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ def test_get_all_profiles_without_desktop_default(self, mock_settings) -> None:
699699
assert any(p.id == DEFAULT_PROFILE_ID for p in profiles)
700700

701701
def test_get_active_profile_with_desktop_default_inactive(self, mock_settings) -> None:
702-
"""Test that None is returned when Desktop default exists but is not active."""
702+
"""Test that the desktop default is returned when Desktop default exists but is not active."""
703703
# Create a Desktop-created default profile that is NOT active
704704
desktop_default = AuthProfile(
705705
id="random-desktop-id",
@@ -724,7 +724,7 @@ def test_get_active_profile_with_desktop_default_inactive(self, mock_settings) -
724724
result = get_active_profile()
725725

726726
# Should return None because Desktop default exists (even if not active)
727-
assert result is None
727+
assert result == desktop_default
728728

729729
def test_get_active_profile_without_desktop_default(self, mock_settings) -> None:
730730
"""Test that virtual default is returned when no Desktop default exists."""

0 commit comments

Comments
 (0)