"""Unit tests for devx.tools.setup_ssh_key.""" from __future__ import annotations import os from unittest.mock import MagicMock, patch from click.testing import CliRunner from devx.tools.setup_ssh_key import cli, setup_ssh_key class TestSetupSshKey: def test_success(self, tmp_path, monkeypatch) -> None: monkeypatch.setenv("HOME", str(tmp_path)) monkeypatch.setenv("SSH_PRIVATE_KEY", "-----BEGIN KEY-----\nfake\n-----END KEY-----") with ( patch("subprocess.run") as mock_run, patch("pathlib.Path.chmod"), ): agent_result = MagicMock() agent_result.returncode = 0 agent_result.stdout = "SSH_AUTH_SOCK=/tmp/agent.sock;\nSSH_AGENT_PID=12345;\n" agent_result.stderr = "" add_result = MagicMock() add_result.returncode = 0 add_result.stdout = "" add_result.stderr = "" mock_run.side_effect = [agent_result, add_result] assert setup_ssh_key() is True assert mock_run.call_count == 2 def test_missing_key(self, monkeypatch) -> None: monkeypatch.delenv("SSH_PRIVATE_KEY", raising=False) assert setup_ssh_key() is False def test_empty_key(self, monkeypatch) -> None: monkeypatch.setenv("SSH_PRIVATE_KEY", "") assert setup_ssh_key() is False def test_explicit_key_param(self, tmp_path, monkeypatch) -> None: monkeypatch.setenv("HOME", str(tmp_path)) monkeypatch.delenv("SSH_PRIVATE_KEY", raising=False) with ( patch("subprocess.run") as mock_run, patch("pathlib.Path.chmod"), ): agent_result = MagicMock() agent_result.returncode = 0 agent_result.stdout = "SSH_AUTH_SOCK=/tmp/agent.sock;\n" agent_result.stderr = "" add_result = MagicMock() add_result.returncode = 0 add_result.stdout = "" add_result.stderr = "" mock_run.side_effect = [agent_result, add_result] assert setup_ssh_key("-----BEGIN KEY-----\nfake\n-----END KEY-----") is True def test_ssh_agent_failure(self, tmp_path, monkeypatch) -> None: monkeypatch.setenv("HOME", str(tmp_path)) monkeypatch.setenv("SSH_PRIVATE_KEY", "fake-key") with ( patch("subprocess.run") as mock_run, patch("pathlib.Path.chmod"), ): agent_result = MagicMock() agent_result.returncode = 1 agent_result.stdout = "" agent_result.stderr = "ssh-agent failed" mock_run.return_value = agent_result assert setup_ssh_key() is False def test_key_file_written(self, tmp_path, monkeypatch) -> None: monkeypatch.setenv("HOME", str(tmp_path)) monkeypatch.setenv("SSH_PRIVATE_KEY", "my-secret-key") with ( patch("subprocess.run") as mock_run, patch("pathlib.Path.chmod") as mock_chmod, ): agent_result = MagicMock() agent_result.returncode = 0 agent_result.stdout = "SSH_AUTH_SOCK=/tmp/agent.sock;\n" agent_result.stderr = "" add_result = MagicMock() add_result.returncode = 0 add_result.stdout = "" add_result.stderr = "" mock_run.side_effect = [agent_result, add_result] setup_ssh_key() key_file = tmp_path / ".ssh" / "id_rsa" assert key_file.exists() assert "my-secret-key" in key_file.read_text() mock_chmod.assert_called_with(0o600) def test_env_vars_set_from_agent(self, tmp_path, monkeypatch) -> None: monkeypatch.setenv("HOME", str(tmp_path)) monkeypatch.setenv("SSH_PRIVATE_KEY", "fake-key") with ( patch("subprocess.run") as mock_run, patch("pathlib.Path.chmod"), ): agent_result = MagicMock() agent_result.returncode = 0 agent_result.stdout = "SSH_AUTH_SOCK=/tmp/agent.sock;\nSSH_AGENT_PID=999;\n" agent_result.stderr = "" add_result = MagicMock() add_result.returncode = 0 add_result.stdout = "" add_result.stderr = "" mock_run.side_effect = [agent_result, add_result] setup_ssh_key() assert os.environ.get("SSH_AUTH_SOCK") == "/tmp/agent.sock" assert os.environ.get("SSH_AGENT_PID") == "999" def test_agent_output_without_env_vars(self, tmp_path, monkeypatch) -> None: monkeypatch.setenv("HOME", str(tmp_path)) monkeypatch.setenv("SSH_PRIVATE_KEY", "fake-key") monkeypatch.delenv("SSH_AUTH_SOCK", raising=False) with ( patch("subprocess.run") as mock_run, patch("pathlib.Path.chmod"), ): agent_result = MagicMock() agent_result.returncode = 0 agent_result.stdout = "Agent started\nsome message without equals\n" agent_result.stderr = "" add_result = MagicMock() add_result.returncode = 0 add_result.stdout = "" add_result.stderr = "" mock_run.side_effect = [agent_result, add_result] assert setup_ssh_key() is True assert os.environ.get("SSH_AUTH_SOCK") is None class TestCli: def test_success(self, tmp_path, monkeypatch) -> None: monkeypatch.setenv("HOME", str(tmp_path)) monkeypatch.setenv("SSH_PRIVATE_KEY", "fake-key") runner = CliRunner() with patch("devx.tools.setup_ssh_key.setup_ssh_key") as mock_setup: mock_setup.return_value = True result = runner.invoke(cli, []) assert result.exit_code == 0 assert "successfully" in result.output def test_no_key(self, monkeypatch) -> None: monkeypatch.delenv("SSH_PRIVATE_KEY", raising=False) runner = CliRunner() with patch("devx.tools.setup_ssh_key.setup_ssh_key") as mock_setup: mock_setup.return_value = False result = runner.invoke(cli, []) assert result.exit_code == 1