Public Access
DEVX-111: feat: add check_api_identity_checks, setup_ssh_key, and api utils
Post-merge / detect-type (push) Successful in 9s
Post-merge / validate-commit-msg (push) Successful in 9s
Build Images / detect-type (push) Successful in 42s
Post-merge / vikunja (push) Successful in 17s
Post-merge / release (push) Successful in 52s
Post-merge / configure-repo (push) Successful in 23s
Post-merge / badges (push) Successful in 55s
Post-merge / sync-wiki (push) Successful in 58s
Post-merge / publish (push) Successful in 21s
Build Images / build-and-push (push) Successful in 3m15s
Build Images / cleanup (push) Successful in 3m38s
Post-merge / detect-type (push) Successful in 9s
Post-merge / validate-commit-msg (push) Successful in 9s
Build Images / detect-type (push) Successful in 42s
Post-merge / vikunja (push) Successful in 17s
Post-merge / release (push) Successful in 52s
Post-merge / configure-repo (push) Successful in 23s
Post-merge / badges (push) Successful in 55s
Post-merge / sync-wiki (push) Successful in 58s
Post-merge / publish (push) Successful in 21s
Build Images / build-and-push (push) Successful in 3m15s
Build Images / cleanup (push) Successful in 3m38s
This commit was merged in pull request #170.
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
"""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
|
||||
Reference in New Issue
Block a user