summaryrefslogtreecommitdiff
path: root/servers/gitlab_python/tests/test_server.py
diff options
context:
space:
mode:
Diffstat (limited to 'servers/gitlab_python/tests/test_server.py')
-rw-r--r--servers/gitlab_python/tests/test_server.py107
1 files changed, 89 insertions, 18 deletions
diff --git a/servers/gitlab_python/tests/test_server.py b/servers/gitlab_python/tests/test_server.py
index 99cb788..7c0e69f 100644
--- a/servers/gitlab_python/tests/test_server.py
+++ b/servers/gitlab_python/tests/test_server.py
@@ -111,8 +111,8 @@ def test_run_ci_pipeline_success_with_branch(mock_gitlab, mock_settings):
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
-@patch("subprocess.run")
-def test_run_ci_pipeline_success_current_branch(mock_subprocess, mock_gitlab, mock_settings):
+@patch("git.Repo")
+def test_run_ci_pipeline_success_current_branch(mock_repo, mock_gitlab, mock_settings):
server = GitLabPythonServer(working_directory="/tmp")
proj = MagicMock()
pipeline = MagicMock()
@@ -121,11 +121,10 @@ def test_run_ci_pipeline_success_current_branch(mock_subprocess, mock_gitlab, mo
proj.pipelines.create.return_value = pipeline
proj.default_branch = "main"
mock_gitlab.return_value.projects.get.return_value = proj
- # Simulate git branch detection
- mock_git = MagicMock()
- mock_git.returncode = 0
- mock_git.stdout = "feature-branch"
- mock_subprocess.return_value = mock_git
+ # Simulate git branch detection using GitPython
+ mock_branch = MagicMock()
+ mock_branch.name = "feature-branch"
+ mock_repo.return_value.active_branch = mock_branch
result = server.run_ci_pipeline(
project="project/path",
branch=None,
@@ -137,15 +136,12 @@ def test_run_ci_pipeline_success_current_branch(mock_subprocess, mock_gitlab, mo
assert result["pipeline_id"] == 456
assert result["pipeline_url"] == "https://gitlab.com/project/-/pipelines/456"
assert result["branch"] == "feature-branch"
- mock_subprocess.assert_called_once_with([
- "git", "branch", "--show-current"
- ], capture_output=True, text=True, check=False, cwd="/tmp")
proj.pipelines.create.assert_called_once()
@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
-@patch("subprocess.run")
-def test_run_ci_pipeline_fallback_to_default_branch(mock_subprocess, mock_gitlab, mock_settings):
+@patch("git.Repo")
+def test_run_ci_pipeline_fallback_to_default_branch(mock_repo, mock_gitlab, mock_settings):
server = GitLabPythonServer(working_directory="/tmp")
proj = MagicMock()
pipeline = MagicMock()
@@ -154,11 +150,8 @@ def test_run_ci_pipeline_fallback_to_default_branch(mock_subprocess, mock_gitlab
proj.pipelines.create.return_value = pipeline
proj.default_branch = "main"
mock_gitlab.return_value.projects.get.return_value = proj
- # Simulate git branch detection failure
- mock_git = MagicMock()
- mock_git.returncode = 1
- mock_git.stdout = ""
- mock_subprocess.return_value = mock_git
+ # Simulate git branch detection failure by raising an exception
+ mock_repo.return_value.active_branch = property(lambda self: (_ for _ in ()).throw(Exception("No branch")))
result = server.run_ci_pipeline(
project="project/path",
branch=None,
@@ -167,6 +160,8 @@ def test_run_ci_pipeline_fallback_to_default_branch(mock_subprocess, mock_gitlab
working_directory="/tmp"
)
assert result["success"] is True
+ assert result["pipeline_id"] == 789
+ assert result["pipeline_url"] == "https://gitlab.com/project/-/pipelines/789"
assert result["branch"] == "main"
proj.pipelines.create.assert_called_once()
@@ -206,4 +201,80 @@ def test_run_ci_pipeline_error_handling(mock_gitlab, mock_settings):
working_directory="/tmp"
)
assert "error" in result
- assert "Not found" in result["error"] \ No newline at end of file
+ assert "Not found" in result["error"]
+
+@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
+@patch("gitlab.Gitlab")
+def test_find_group(mock_gitlab, mock_settings):
+ server = GitLabPythonServer(working_directory="/tmp")
+ group1 = MagicMock()
+ group1.id = 1
+ group1.name = "Test Group"
+ group1.full_path = "test-group"
+ group1.web_url = "https://gitlab.com/groups/test-group"
+ group1.description = "A test group"
+ mock_gitlab.return_value.groups.list.return_value = [group1]
+ result = server.find_group("test-group")
+ assert isinstance(result, list)
+ assert result[0]["name"] == "Test Group"
+ assert result[0]["full_path"] == "test-group"
+
+@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
+@patch("gitlab.Gitlab")
+def test_search_epics_success(mock_gitlab, mock_settings):
+ server = GitLabPythonServer(working_directory="/tmp")
+ group = MagicMock()
+ epic = MagicMock()
+ epic.id = 1
+ epic.iid = 101
+ epic.title = "Epic Title"
+ epic.web_url = "https://gitlab.com/groups/test-group/-/epics/101"
+ epic.state = "opened"
+ epic.created_at = "2024-01-01T00:00:00Z"
+ epic.updated_at = "2024-01-02T00:00:00Z"
+ epic.author = {"id": 2, "name": "Author"}
+ epic.labels = ["label1"]
+ epic.description = "Epic description"
+ group.epics.list.return_value = [epic]
+ mock_gitlab.return_value.groups.get.return_value = group
+ result = server.search_epics("test-group", state="opened")
+ assert "epics" in result
+ assert result["epics"][0]["title"] == "Epic Title"
+ assert result["epics"][0]["state"] == "opened"
+ assert result["epics"][0]["author"]["name"] == "Author"
+
+@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
+@patch("gitlab.Gitlab")
+def test_search_epics_error(mock_gitlab, mock_settings):
+ server = GitLabPythonServer(working_directory="/tmp")
+ mock_gitlab.return_value.groups.get.side_effect = Exception("Group not found")
+ result = server.search_epics("bad-group")
+ assert "error" in result
+ assert "Group not found" in result["error"]
+
+@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
+@patch("gitlab.Gitlab")
+def test_create_epic_success(mock_gitlab, mock_settings):
+ server = GitLabPythonServer(working_directory="/tmp")
+ group = MagicMock()
+ epic = MagicMock()
+ epic.web_url = "https://gitlab.com/groups/test-group/-/epics/101"
+ group.epics.create.return_value = epic
+ mock_gitlab.return_value.groups.get.return_value = group
+ result = server.create_epic("test-group", "Epic Title", "Epic description", labels=["label1"])
+ assert "url" in result
+ assert result["url"] == "https://gitlab.com/groups/test-group/-/epics/101"
+ group.epics.create.assert_called_once()
+ args = group.epics.create.call_args[0][0]
+ assert args["title"] == "Epic Title"
+ assert args["description"] == "Epic description"
+ assert args["labels"] == ["label1"]
+
+@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
+@patch("gitlab.Gitlab")
+def test_create_epic_error(mock_gitlab, mock_settings):
+ server = GitLabPythonServer(working_directory="/tmp")
+ mock_gitlab.return_value.groups.get.side_effect = Exception("Group not found")
+ result = server.create_epic("bad-group", "Epic Title", "Epic description")
+ assert "error" in result
+ assert "Group not found" in result["error"] \ No newline at end of file