1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
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"])
|