98 lines
3.3 KiB
Python
98 lines
3.3 KiB
Python
from pathlib import Path
|
|
from unittest.mock import MagicMock, patch
|
|
import pytest
|
|
|
|
from gitea.workspace import WorkspaceManager
|
|
|
|
|
|
@patch("gitea.workspace.subprocess.run")
|
|
@patch("gitea.workspace.GiteaClient")
|
|
def test_workspace_manager_configure_repo_user(
|
|
mock_client_class: MagicMock,
|
|
mock_run: MagicMock
|
|
) -> None:
|
|
mock_client = MagicMock()
|
|
mock_client_class.return_value = mock_client
|
|
mock_user = MagicMock()
|
|
mock_user.full_name = "Agent Tester"
|
|
mock_user.login = "agent-test"
|
|
mock_user.email = "agent-test@example.com"
|
|
mock_client.get_authenticated_user.return_value = mock_user
|
|
|
|
workspace = WorkspaceManager()
|
|
repo_path = Path("/tmp/mock-repo")
|
|
|
|
workspace._configure_repo_user(repo_path)
|
|
|
|
assert mock_run.call_count >= 3
|
|
calls = [c[0][0] for c in mock_run.call_args_list]
|
|
|
|
assert any("http.extraHeader" in call for call in calls)
|
|
assert any("user.name" in call for call in calls)
|
|
assert any("user.email" in call for call in calls)
|
|
|
|
|
|
@patch("gitea.workspace.subprocess.run")
|
|
@patch("gitea.workspace.GiteaClient")
|
|
def test_workspace_manager_clone_repo(
|
|
mock_client_class: MagicMock,
|
|
mock_run: MagicMock
|
|
) -> None:
|
|
mock_client = MagicMock()
|
|
mock_client_class.return_value = mock_client
|
|
mock_user = MagicMock()
|
|
mock_user.full_name = "Agent Tester"
|
|
mock_user.login = "agent-test"
|
|
mock_user.email = "agent-test@example.com"
|
|
mock_client.get_authenticated_user.return_value = mock_user
|
|
|
|
workspace = WorkspaceManager()
|
|
|
|
with patch.object(workspace, "_configure_repo_user") as mock_configure:
|
|
with patch.object(workspace, "get_repo_path") as mock_get_path:
|
|
mock_repo_path = MagicMock(spec=Path)
|
|
mock_repo_path.exists.return_value = False
|
|
mock_get_path.return_value = mock_repo_path
|
|
|
|
workspace.clone_repo("meeks/repo1")
|
|
|
|
mock_run.assert_called_once()
|
|
args = mock_run.call_args[0][0]
|
|
assert "clone" in args
|
|
assert any("http.extraHeader=Authorization: Basic" in arg for arg in args)
|
|
mock_configure.assert_called_once_with(mock_repo_path)
|
|
|
|
|
|
@patch("gitea.workspace.GiteaClient")
|
|
def test_workspace_manager_fails_if_no_authenticated_user(
|
|
mock_client_class: MagicMock
|
|
) -> None:
|
|
mock_client = MagicMock()
|
|
mock_client_class.return_value = mock_client
|
|
mock_client.get_authenticated_user.return_value = None
|
|
|
|
workspace = WorkspaceManager()
|
|
with pytest.raises(RuntimeError, match="No authenticated user found."):
|
|
workspace.clone_repo("meeks/repo1")
|
|
|
|
with pytest.raises(RuntimeError, match="No authenticated user found."):
|
|
workspace._configure_repo_user(Path("/tmp/mock-repo"))
|
|
|
|
|
|
@patch("gitea.workspace.GiteaClient")
|
|
def test_workspace_manager_fails_if_authenticated_user_has_no_login(
|
|
mock_client_class: MagicMock
|
|
) -> None:
|
|
mock_client = MagicMock()
|
|
mock_client_class.return_value = mock_client
|
|
mock_user = MagicMock()
|
|
mock_user.login = ""
|
|
mock_client.get_authenticated_user.return_value = mock_user
|
|
|
|
workspace = WorkspaceManager()
|
|
with pytest.raises(RuntimeError, match="No authenticated user found."):
|
|
workspace.clone_repo("meeks/repo1")
|
|
|
|
with pytest.raises(RuntimeError, match="No authenticated user found."):
|
|
workspace._configure_repo_user(Path("/tmp/mock-repo"))
|