summaryrefslogtreecommitdiff
path: root/servers/gitlab_python/tests/test_server.py
blob: 99cb788b83a2c72b306d178cd78d75149e0da51f (plain)
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
import pytest
from mcp_server_gitlab_python.server import create_server, GitLabPythonServer
from unittest.mock import MagicMock, patch

def test_create_server():
    server = create_server()
    assert server is not None 

def make_mock_diff(old_path, new_path, diff):
    m = MagicMock()
    m.old_path = old_path
    m.new_path = new_path
    m.diff = diff
    return m

@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_get_mr_diff_small_diff(mock_gitlab, mock_settings):
    server = GitLabPythonServer(working_directory="/tmp")
    proj = MagicMock()
    mr = MagicMock()
    mock_gitlab.return_value.projects.get.return_value = proj
    proj.mergerequests.get.return_value = mr
    mr.diffs.list.return_value = [
        make_mock_diff("file1.txt", "file1.txt", "diff content 1"),
        make_mock_diff("file2.py", "file2.py", "diff content 2"),
    ]
    result = server.get_mr_diff("project/path", 1, max_size_kb=100)
    assert "diff" in result
    assert "file1.txt" in result["diff"]
    assert result["temp_file_path"] is None
    assert result["size_kb"] < 100

@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
@patch("tempfile.NamedTemporaryFile")
def test_get_mr_diff_large_diff_temp_file(mock_tempfile, mock_gitlab, mock_settings):
    server = GitLabPythonServer(working_directory="/tmp")
    proj = MagicMock()
    mr = MagicMock()
    mock_gitlab.return_value.projects.get.return_value = proj
    proj.mergerequests.get.return_value = mr
    # Create a large diff
    large_diff = "x" * (101 * 1024)
    mock_diff = make_mock_diff("bigfile.txt", "bigfile.txt", large_diff)
    mr.diffs.list.return_value = [mock_diff]
    mock_file = MagicMock()
    mock_file.name = "/tmp/mr_diff_12345.diff"
    mock_tempfile.return_value.__enter__.return_value = mock_file
    result = server.get_mr_diff("project/path", 1, max_size_kb=100)
    assert result["diff_too_large"] is True
    assert result["temp_file_path"] == "/tmp/mr_diff_12345.diff"
    assert result["size_kb"] > 100
    assert "message" in result
    mock_file.write.assert_called_once()

@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_get_mr_diff_filter_extensions(mock_gitlab, mock_settings):
    server = GitLabPythonServer(working_directory="/tmp")
    proj = MagicMock()
    mr = MagicMock()
    mock_gitlab.return_value.projects.get.return_value = proj
    proj.mergerequests.get.return_value = mr
    # One file should be filtered out
    mr.diffs.list.return_value = [
        make_mock_diff("file.lock", "file.lock", "should be filtered"),
        make_mock_diff("file.txt", "file.txt", "should be present"),
    ]
    result = server.get_mr_diff("project/path", 1, max_size_kb=100, filter_extensions=[".lock"])
    assert "file.lock" not in result["diff"]
    assert "file.txt" in result["diff"]
    assert "should be present" in result["diff"]
    assert "should be filtered" not in result["diff"]

@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_get_mr_diff_error_handling(mock_gitlab, mock_settings):
    server = GitLabPythonServer(working_directory="/tmp")
    mock_gitlab.return_value.projects.get.side_effect = Exception("Not found")
    result = server.get_mr_diff("project/path", 1)
    assert "error" in result
    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_run_ci_pipeline_success_with_branch(mock_gitlab, mock_settings):
    server = GitLabPythonServer(working_directory="/tmp")
    proj = MagicMock()
    pipeline = MagicMock()
    pipeline.id = 123
    pipeline.web_url = "https://gitlab.com/project/-/pipelines/123"
    proj.pipelines.create.return_value = pipeline
    mock_gitlab.return_value.projects.get.return_value = proj
    result = server.run_ci_pipeline(
        project="project/path",
        branch="main",
        variables={"FOO": "bar"},
        web_mode=False,
        working_directory="/tmp"
    )
    assert result["success"] is True
    assert result["pipeline_id"] == 123
    assert result["pipeline_url"] == "https://gitlab.com/project/-/pipelines/123"
    assert result["branch"] == "main"
    assert result["web_mode"] is False
    proj.pipelines.create.assert_called_once()
    args = proj.pipelines.create.call_args[0][0]
    assert args["ref"] == "main"
    assert {"key": "FOO", "value": "bar"} in args["variables"]

@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):
    server = GitLabPythonServer(working_directory="/tmp")
    proj = MagicMock()
    pipeline = MagicMock()
    pipeline.id = 456
    pipeline.web_url = "https://gitlab.com/project/-/pipelines/456"
    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
    result = server.run_ci_pipeline(
        project="project/path",
        branch=None,
        variables=None,
        web_mode=False,
        working_directory="/tmp"
    )
    assert result["success"] is True
    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):
    server = GitLabPythonServer(working_directory="/tmp")
    proj = MagicMock()
    pipeline = MagicMock()
    pipeline.id = 789
    pipeline.web_url = "https://gitlab.com/project/-/pipelines/789"
    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
    result = server.run_ci_pipeline(
        project="project/path",
        branch=None,
        variables=None,
        web_mode=False,
        working_directory="/tmp"
    )
    assert result["success"] is True
    assert result["branch"] == "main"
    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")
def test_run_ci_pipeline_web_mode(mock_gitlab, mock_settings):
    server = GitLabPythonServer(working_directory="/tmp")
    proj = MagicMock()
    pipeline = MagicMock()
    pipeline.id = 321
    pipeline.web_url = "https://gitlab.com/project/-/pipelines/321"
    proj.pipelines.create.return_value = pipeline
    mock_gitlab.return_value.projects.get.return_value = proj
    result = server.run_ci_pipeline(
        project="project/path",
        branch="main",
        variables={"FOO": "bar"},
        web_mode=True,
        working_directory="/tmp"
    )
    assert result["success"] is True
    assert result["web_mode"] is True
    args = proj.pipelines.create.call_args[0][0]
    # Should include CI_PIPELINE_SOURCE=web
    assert {"key": "CI_PIPELINE_SOURCE", "value": "web"} in args["variables"]

@patch("mcp_server_gitlab_python.server.get_gitlab_settings", return_value=("https://gitlab.com", "dummy-token"))
@patch("gitlab.Gitlab")
def test_run_ci_pipeline_error_handling(mock_gitlab, mock_settings):
    server = GitLabPythonServer(working_directory="/tmp")
    mock_gitlab.return_value.projects.get.side_effect = Exception("Not found")
    result = server.run_ci_pipeline(
        project="project/path",
        branch="main",
        variables=None,
        web_mode=False,
        working_directory="/tmp"
    )
    assert "error" in result
    assert "Not found" in result["error"]