forked from github/github-mcp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_scanning_test.go
More file actions
316 lines (281 loc) · 9.67 KB
/
code_scanning_test.go
File metadata and controls
316 lines (281 loc) · 9.67 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package github
import (
"context"
"encoding/json"
"net/http"
"testing"
"github.com/github/github-mcp-server/internal/toolsnaps"
"github.com/github/github-mcp-server/pkg/translations"
"github.com/google/go-github/v74/github"
"github.com/migueleliasweb/go-github-mock/src/mock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_GetCodeScanningAlert(t *testing.T) {
// Verify tool definition once
mockClient := github.NewClient(nil)
tool, _ := GetCodeScanningAlert(stubGetClientFn(mockClient), translations.NullTranslationHelper)
require.NoError(t, toolsnaps.Test(tool.Name, tool))
assert.Equal(t, "get_code_scanning_alert", tool.Name)
assert.NotEmpty(t, tool.Description)
assert.Contains(t, tool.InputSchema.Properties, "owner")
assert.Contains(t, tool.InputSchema.Properties, "repo")
assert.Contains(t, tool.InputSchema.Properties, "alertNumber")
assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "alertNumber"})
// Setup mock alert for success case
mockAlert := &github.Alert{
Number: github.Ptr(42),
State: github.Ptr("open"),
Rule: &github.Rule{ID: github.Ptr("test-rule"), Description: github.Ptr("Test Rule Description")},
HTMLURL: github.Ptr("https://github.com/owner/repo/security/code-scanning/42"),
}
tests := []struct {
name string
mockedClient *http.Client
requestArgs map[string]interface{}
expectError bool
expectedAlert *github.Alert
expectedErrMsg string
}{
{
name: "successful alert fetch",
mockedClient: mock.NewMockedHTTPClient(
mock.WithRequestMatch(
mock.GetReposCodeScanningAlertsByOwnerByRepoByAlertNumber,
mockAlert,
),
),
requestArgs: map[string]interface{}{
"owner": "owner",
"repo": "repo",
"alertNumber": float64(42),
},
expectError: false,
expectedAlert: mockAlert,
},
{
name: "alert fetch fails",
mockedClient: mock.NewMockedHTTPClient(
mock.WithRequestMatchHandler(
mock.GetReposCodeScanningAlertsByOwnerByRepoByAlertNumber,
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte(`{"message": "Not Found"}`))
}),
),
),
requestArgs: map[string]interface{}{
"owner": "owner",
"repo": "repo",
"alertNumber": float64(9999),
},
expectError: true,
expectedErrMsg: "failed to get alert",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Setup client with mock
client := github.NewClient(tc.mockedClient)
_, handler := GetCodeScanningAlert(stubGetClientFn(client), translations.NullTranslationHelper)
// Create call request
request := createMCPRequest(tc.requestArgs)
// Call handler
result, err := handler(context.Background(), request)
// Verify results
if tc.expectError {
require.NoError(t, err)
require.True(t, result.IsError)
errorContent := getErrorResult(t, result)
assert.Contains(t, errorContent.Text, tc.expectedErrMsg)
return
}
require.NoError(t, err)
require.False(t, result.IsError)
// Parse the result and get the text content if no error
textContent := getTextResult(t, result)
// Unmarshal and verify the result
var returnedAlert github.Alert
err = json.Unmarshal([]byte(textContent.Text), &returnedAlert)
assert.NoError(t, err)
assert.Equal(t, *tc.expectedAlert.Number, *returnedAlert.Number)
assert.Equal(t, *tc.expectedAlert.State, *returnedAlert.State)
assert.Equal(t, *tc.expectedAlert.Rule.ID, *returnedAlert.Rule.ID)
assert.Equal(t, *tc.expectedAlert.HTMLURL, *returnedAlert.HTMLURL)
})
}
}
func Test_ListCodeScanningAlerts(t *testing.T) {
// Verify tool definition once
mockClient := github.NewClient(nil)
tool, _ := ListCodeScanningAlerts(stubGetClientFn(mockClient), translations.NullTranslationHelper)
require.NoError(t, toolsnaps.Test(tool.Name, tool))
assert.Equal(t, "list_code_scanning_alerts", tool.Name)
assert.NotEmpty(t, tool.Description)
assert.Contains(t, tool.InputSchema.Properties, "owner")
assert.Contains(t, tool.InputSchema.Properties, "repo")
assert.Contains(t, tool.InputSchema.Properties, "ref")
assert.Contains(t, tool.InputSchema.Properties, "state")
assert.Contains(t, tool.InputSchema.Properties, "severity")
assert.Contains(t, tool.InputSchema.Properties, "tool_name")
assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo"})
// Setup mock alerts for success case
mockAlerts := []*github.Alert{
{
Number: github.Ptr(42),
State: github.Ptr("open"),
Rule: &github.Rule{ID: github.Ptr("test-rule-1"), Description: github.Ptr("Test Rule 1")},
HTMLURL: github.Ptr("https://github.com/owner/repo/security/code-scanning/42"),
},
{
Number: github.Ptr(43),
State: github.Ptr("fixed"),
Rule: &github.Rule{ID: github.Ptr("test-rule-2"), Description: github.Ptr("Test Rule 2")},
HTMLURL: github.Ptr("https://github.com/owner/repo/security/code-scanning/43"),
},
}
tests := []struct {
name string
mockedClient *http.Client
requestArgs map[string]interface{}
expectError bool
expectedAlerts []*github.Alert
expectedErrMsg string
}{
{
name: "successful alerts listing",
mockedClient: mock.NewMockedHTTPClient(
mock.WithRequestMatchHandler(
mock.GetReposCodeScanningAlertsByOwnerByRepo,
expectQueryParams(t, map[string]string{
"ref": "main",
"state": "open",
"severity": "high",
"tool_name": "codeql",
}).andThen(
mockResponse(t, http.StatusOK, mockAlerts),
),
),
),
requestArgs: map[string]interface{}{
"owner": "owner",
"repo": "repo",
"ref": "main",
"state": "open",
"severity": "high",
"tool_name": "codeql",
},
expectError: false,
expectedAlerts: mockAlerts,
},
{
name: "alerts listing fails",
mockedClient: mock.NewMockedHTTPClient(
mock.WithRequestMatchHandler(
mock.GetReposCodeScanningAlertsByOwnerByRepo,
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte(`{"message": "Unauthorized access"}`))
}),
),
),
requestArgs: map[string]interface{}{
"owner": "owner",
"repo": "repo",
},
expectError: true,
expectedErrMsg: "failed to list alerts",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Setup client with mock
client := github.NewClient(tc.mockedClient)
_, handler := ListCodeScanningAlerts(stubGetClientFn(client), translations.NullTranslationHelper)
// Create call request
request := createMCPRequest(tc.requestArgs)
// Call handler
result, err := handler(context.Background(), request)
// Verify results
if tc.expectError {
require.NoError(t, err)
require.True(t, result.IsError)
errorContent := getErrorResult(t, result)
assert.Contains(t, errorContent.Text, tc.expectedErrMsg)
return
}
require.NoError(t, err)
require.False(t, result.IsError)
// Parse the result and get the text content if no error
textContent := getTextResult(t, result)
// Unmarshal and verify the result
var returnedAlerts []*github.Alert
err = json.Unmarshal([]byte(textContent.Text), &returnedAlerts)
assert.NoError(t, err)
assert.Len(t, returnedAlerts, len(tc.expectedAlerts))
for i, alert := range returnedAlerts {
assert.Equal(t, *tc.expectedAlerts[i].Number, *alert.Number)
assert.Equal(t, *tc.expectedAlerts[i].State, *alert.State)
assert.Equal(t, *tc.expectedAlerts[i].Rule.ID, *alert.Rule.ID)
assert.Equal(t, *tc.expectedAlerts[i].HTMLURL, *alert.HTMLURL)
}
})
}
}
func Test_ManageCodeScanningAlerts(t *testing.T) {
// Verify tool definition
mockClient := github.NewClient(nil)
tool, _ := ManageCodeScanningAlerts(stubGetClientFn(mockClient), translations.NullTranslationHelper)
assert.Equal(t, "manage_code_scanning_alerts", tool.Name)
assert.NotEmpty(t, tool.Description)
assert.Contains(t, tool.InputSchema.Properties, "operation")
assert.Contains(t, tool.InputSchema.Required, "operation")
t.Run("list operation", func(t *testing.T) {
// Setup mock alerts for success case
mockAlerts := []*github.Alert{
{
Number: github.Ptr(1),
Rule: &github.Rule{
ID: github.Ptr("rule1"),
Severity: github.Ptr("error"),
Description: github.Ptr("Test rule"),
},
State: github.Ptr("open"),
},
}
mockedHTTPClient := mock.NewMockedHTTPClient(
mock.WithRequestMatch(mock.GetReposCodeScanningAlertsByOwnerByRepo, mockAlerts),
)
client := github.NewClient(mockedHTTPClient)
_, handler := ManageCodeScanningAlerts(stubGetClientFn(client), translations.NullTranslationHelper)
request := createMCPRequest(map[string]interface{}{
"operation": "list",
"owner": "testowner",
"repo": "testrepo",
})
result, err := handler(context.Background(), request)
require.NoError(t, err)
assert.False(t, result.IsError)
var alerts []*github.Alert
textContent := getTextResult(t, result)
err = json.Unmarshal([]byte(textContent.Text), &alerts)
require.NoError(t, err)
assert.Len(t, alerts, 1)
assert.Equal(t, 1, *alerts[0].Number)
})
t.Run("unsupported operation", func(t *testing.T) {
mockedHTTPClient := mock.NewMockedHTTPClient()
client := github.NewClient(mockedHTTPClient)
_, handler := ManageCodeScanningAlerts(stubGetClientFn(client), translations.NullTranslationHelper)
request := createMCPRequest(map[string]interface{}{
"operation": "delete",
"owner": "testowner",
"repo": "testrepo",
})
result, err := handler(context.Background(), request)
require.NoError(t, err)
assert.True(t, result.IsError)
textContent := getTextResult(t, result)
assert.Contains(t, textContent.Text, "unsupported operation: delete")
})
}