-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from dreadnode/feature/agent-link-improvements
feat: Agent link improvements
- Loading branch information
Showing
4 changed files
with
126 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
from pathlib import Path | ||
from unittest.mock import patch | ||
from uuid import UUID | ||
|
||
import pytest | ||
|
||
from dreadnode_cli.agent.cli import ensure_profile | ||
from dreadnode_cli.agent.config import AgentConfig | ||
from dreadnode_cli.config import ServerConfig, UserConfig | ||
|
||
|
||
def test_agent_config_read_not_initialized(tmp_path: Path) -> None: | ||
|
@@ -28,8 +31,8 @@ def test_agent_config_add_link() -> None: | |
assert config.links["test"].runs == [] | ||
assert config.links["test"].profile == "test" | ||
assert config.linked_profiles == ["test"] | ||
assert config.is_linked_to_profile("test") | ||
assert not config.is_linked_to_profile("other") | ||
assert config.has_link_to_profile("test") | ||
assert not config.has_link_to_profile("other") | ||
|
||
|
||
def test_agent_config_add_run() -> None: | ||
|
@@ -82,3 +85,49 @@ def test_agent_config_update_active() -> None: | |
config.links.clear() | ||
config._update_active() | ||
assert config.active is None | ||
|
||
|
||
def test_ensure_profile() -> None: | ||
agent_config = AgentConfig(project_name="test") | ||
user_config = UserConfig() | ||
|
||
# We don't have any profiles | ||
with pytest.raises(Exception, match="No server profile is set"): | ||
ensure_profile(agent_config, user_config=user_config) | ||
|
||
server_config = ServerConfig( | ||
url="http://test", | ||
email="[email protected]", | ||
username="test", | ||
api_key="test", | ||
access_token="test", | ||
refresh_token="test", | ||
) | ||
|
||
user_config.set_server_config(server_config, profile="main") | ||
user_config.set_server_config(server_config, profile="other") | ||
user_config.active = "main" | ||
|
||
# We have no links | ||
with pytest.raises(Exception, match="No agent is currently linked"): | ||
ensure_profile(agent_config, user_config=user_config) | ||
|
||
# We have a link, but none are available for the current profile | ||
agent_config.add_link("test-other", UUID("00000000-0000-0000-0000-000000000000"), "other") | ||
with pytest.raises(Exception, match="This agent is linked to the"): | ||
ensure_profile(agent_config, user_config=user_config) | ||
|
||
# We have another link, but the profiles don't match | ||
agent_config.add_link("test-main", UUID("00000000-0000-0000-0000-000000000000"), "main") | ||
agent_config.active = "test-other" | ||
with patch("rich.prompt.Prompt.ask", return_value="n"): | ||
with pytest.raises(Exception, match="Agent link does not match the current server profile"): | ||
ensure_profile(agent_config, user_config=user_config) | ||
|
||
# We should switch if the user agrees | ||
assert user_config.active == "main" | ||
with patch("rich.prompt.Prompt.ask", return_value="y"), patch("dreadnode_cli.config.UserConfig.write"), patch( | ||
"dreadnode_cli.config.UserConfig.read", return_value=user_config | ||
): | ||
ensure_profile(agent_config, user_config=user_config) | ||
assert user_config.active == "other" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters