diff options
Diffstat (limited to 'servers/gitlab_glab/tests/test_server.py')
| -rw-r--r-- | servers/gitlab_glab/tests/test_server.py | 221 |
1 files changed, 221 insertions, 0 deletions
diff --git a/servers/gitlab_glab/tests/test_server.py b/servers/gitlab_glab/tests/test_server.py new file mode 100644 index 0000000..1c27ea5 --- /dev/null +++ b/servers/gitlab_glab/tests/test_server.py @@ -0,0 +1,221 @@ +#!/usr/bin/env python3 +"""Tests for the GitLab CLI MCP server implementation.""" + +import json +from unittest.mock import MagicMock, patch + +from mcp_server_gitlab_glab.server import GitLabServer + + +class TestGitLabServer: + """Tests for the GitLabServer class.""" + + def test_init(self) -> None: + """Test initialization of GitLabServer.""" + server = GitLabServer() + assert hasattr(server, "auth_message") + assert "glab auth login" in server.auth_message + + @patch("subprocess.run") + def test_execute_glab_command_success(self, mock_run: MagicMock) -> None: + """Test successful execution of a glab command.""" + # Mock successful command execution + mock_process = MagicMock() + mock_process.returncode = 0 + mock_process.stdout = "command output" + mock_process.stderr = "" + mock_run.return_value = mock_process + + server = GitLabServer() + success, result = server.execute_glab_command(["--version"]) + + assert success is True + assert result == "command output" + mock_run.assert_called_once_with( + ["glab", "--version"], + capture_output=True, + text=True, + check=False, + ) + + @patch("subprocess.run") + def test_execute_glab_command_failure(self, mock_run: MagicMock) -> None: + """Test failed execution of a glab command.""" + # Mock failed command execution + mock_process = MagicMock() + mock_process.returncode = 1 + mock_process.stdout = "" + mock_process.stderr = "command failed" + mock_run.return_value = mock_process + + server = GitLabServer() + success, result = server.execute_glab_command(["--version"]) + + assert success is False + assert result == {"error": "command failed"} + mock_run.assert_called_once_with( + ["glab", "--version"], + capture_output=True, + text=True, + check=False, + ) + + @patch("subprocess.run") + def test_execute_glab_command_auth_error(self, mock_run: MagicMock) -> None: + """Test authentication error during glab command execution.""" + # Mock authentication error + mock_process = MagicMock() + mock_process.returncode = 1 + mock_process.stdout = "" + mock_process.stderr = "authentication required" + mock_run.return_value = mock_process + + server = GitLabServer() + success, result = server.execute_glab_command(["api", "/projects"]) + + assert success is False + assert "error" in result + assert "auth login" in result["error"] + mock_run.assert_called_once_with( + ["glab", "api", "/projects"], + capture_output=True, + text=True, + check=False, + ) + + @patch("subprocess.run") + def test_execute_glab_command_not_found(self, mock_run: MagicMock) -> None: + """Test glab command not found error.""" + # Mock FileNotFoundError + mock_run.side_effect = FileNotFoundError("No such file or directory: 'glab'") + + server = GitLabServer() + success, result = server.execute_glab_command(["--version"]) + + assert success is False + assert "error" in result + assert "glab command not found" in result["error"] + + @patch("subprocess.run") + def test_execute_glab_api_command_success(self, mock_run: MagicMock) -> None: + """Test successful execution of a glab api command with JSON response.""" + # Mock successful API command execution with JSON response + mock_process = MagicMock() + mock_process.returncode = 0 + mock_process.stdout = json.dumps([{"id": 1, "name": "test-project"}]) + mock_process.stderr = "" + mock_run.return_value = mock_process + + server = GitLabServer() + success, result = server.execute_glab_command(["api", "/projects"]) + + assert success is True + assert isinstance(result, list) + assert len(result) == 1 + assert result[0]["id"] == 1 + assert result[0]["name"] == "test-project" + mock_run.assert_called_once_with( + ["glab", "api", "/projects"], + capture_output=True, + text=True, + check=False, + ) + + @patch("subprocess.run") + def test_execute_glab_api_command_invalid_json(self, mock_run: MagicMock) -> None: + """Test glab api command with invalid JSON response.""" + # Mock API command execution with invalid JSON response + mock_process = MagicMock() + mock_process.returncode = 0 + mock_process.stdout = "invalid json" + mock_process.stderr = "" + mock_run.return_value = mock_process + + server = GitLabServer() + success, result = server.execute_glab_command(["api", "/projects"]) + + assert success is False + assert "error" in result + assert "Failed to parse JSON response" in result["error"] + + @patch.object(GitLabServer, "execute_glab_command") + def test_check_availability_success(self, mock_execute: MagicMock) -> None: + """Test successful check_availability.""" + # Mock successful command execution + mock_execute.return_value = (True, "glab version 1.0.0") + + server = GitLabServer() + result = server.check_availability() + + assert result["available"] is True + assert result["version"] == "glab version 1.0.0" + mock_execute.assert_called_once_with(["--version"]) + + @patch.object(GitLabServer, "execute_glab_command") + def test_check_availability_failure(self, mock_execute: MagicMock) -> None: + """Test failed check_availability.""" + # Mock failed command execution + mock_execute.return_value = (False, {"error": "glab command not found"}) + + server = GitLabServer() + result = server.check_availability() + + assert result["available"] is False + assert result["error"] == "glab command not found" + mock_execute.assert_called_once_with(["--version"]) + + @patch.object(GitLabServer, "execute_glab_command") + def test_find_project_success(self, mock_execute: MagicMock) -> None: + """Test successful find_project.""" + # Mock successful API response with a project + mock_execute.return_value = ( + True, + [ + { + "id": 1, + "name": "test-project", + "path_with_namespace": "group/test-project", + "web_url": "https://gitlab.com/group/test-project", + "description": "A test project", + } + ], + ) + + server = GitLabServer() + result = server.find_project("test-project") + + assert "id" in result + assert result["id"] == 1 + assert result["name"] == "test-project" + assert result["path_with_namespace"] == "group/test-project" + assert result["web_url"] == "https://gitlab.com/group/test-project" + assert result["description"] == "A test project" + mock_execute.assert_called_once_with(["api", "/projects?search=test-project"]) + + @patch.object(GitLabServer, "execute_glab_command") + def test_find_project_not_found(self, mock_execute: MagicMock) -> None: + """Test find_project with no results.""" + # Mock API response with no projects + mock_execute.return_value = (True, []) + + server = GitLabServer() + result = server.find_project("nonexistent-project") + + assert "error" in result + assert "not found" in result["error"] + mock_execute.assert_called_once_with( + ["api", "/projects?search=nonexistent-project"] + ) + + @patch.object(GitLabServer, "execute_glab_command") + def test_find_project_api_error(self, mock_execute: MagicMock) -> None: + """Test find_project with API error.""" + # Mock API error + mock_execute.return_value = (False, {"error": "API error"}) + + server = GitLabServer() + result = server.find_project("test-project") + + assert "error" in result + assert result["error"] == "API error" + mock_execute.assert_called_once_with(["api", "/projects?search=test-project"]) |
