|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/github/github-mcp-server/internal/toolsnaps" |
| 10 | + "github.com/github/github-mcp-server/pkg/translations" |
| 11 | + "github.com/google/go-github/v72/github" |
| 12 | + "github.com/migueleliasweb/go-github-mock/src/mock" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +func Test_GetDependabotAlert(t *testing.T) { |
| 18 | + // Verify tool definition |
| 19 | + mockClient := github.NewClient(nil) |
| 20 | + tool, _ := GetDependabotAlert(stubGetClientFn(mockClient), translations.NullTranslationHelper) |
| 21 | + require.NoError(t, toolsnaps.Test(tool.Name, tool)) |
| 22 | + |
| 23 | + // Validate tool schema |
| 24 | + assert.Equal(t, "get_dependabot_alert", tool.Name) |
| 25 | + assert.NotEmpty(t, tool.Description) |
| 26 | + assert.Contains(t, tool.InputSchema.Properties, "owner") |
| 27 | + assert.Contains(t, tool.InputSchema.Properties, "repo") |
| 28 | + assert.Contains(t, tool.InputSchema.Properties, "alertNumber") |
| 29 | + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "alertNumber"}) |
| 30 | + |
| 31 | + // Setup mock alert for success case |
| 32 | + mockAlert := &github.DependabotAlert{ |
| 33 | + Number: github.Ptr(42), |
| 34 | + State: github.Ptr("open"), |
| 35 | + HTMLURL: github.Ptr("https://github.com/owner/repo/security/dependabot/42"), |
| 36 | + } |
| 37 | + |
| 38 | + tests := []struct { |
| 39 | + name string |
| 40 | + mockedClient *http.Client |
| 41 | + requestArgs map[string]interface{} |
| 42 | + expectError bool |
| 43 | + expectedAlert *github.DependabotAlert |
| 44 | + expectedErrMsg string |
| 45 | + }{ |
| 46 | + { |
| 47 | + name: "successful alert fetch", |
| 48 | + mockedClient: mock.NewMockedHTTPClient( |
| 49 | + mock.WithRequestMatch( |
| 50 | + mock.GetReposDependabotAlertsByOwnerByRepoByAlertNumber, |
| 51 | + mockAlert, |
| 52 | + ), |
| 53 | + ), |
| 54 | + requestArgs: map[string]interface{}{ |
| 55 | + "owner": "owner", |
| 56 | + "repo": "repo", |
| 57 | + "alertNumber": float64(42), |
| 58 | + }, |
| 59 | + expectError: false, |
| 60 | + expectedAlert: mockAlert, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "alert fetch fails", |
| 64 | + mockedClient: mock.NewMockedHTTPClient( |
| 65 | + mock.WithRequestMatchHandler( |
| 66 | + mock.GetReposDependabotAlertsByOwnerByRepoByAlertNumber, |
| 67 | + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 68 | + w.WriteHeader(http.StatusNotFound) |
| 69 | + _, _ = w.Write([]byte(`{"message": "Not Found"}`)) |
| 70 | + }), |
| 71 | + ), |
| 72 | + ), |
| 73 | + requestArgs: map[string]interface{}{ |
| 74 | + "owner": "owner", |
| 75 | + "repo": "repo", |
| 76 | + "alertNumber": float64(9999), |
| 77 | + }, |
| 78 | + expectError: true, |
| 79 | + expectedErrMsg: "failed to get alert", |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + for _, tc := range tests { |
| 84 | + t.Run(tc.name, func(t *testing.T) { |
| 85 | + // Setup client with mock |
| 86 | + client := github.NewClient(tc.mockedClient) |
| 87 | + _, handler := GetDependabotAlert(stubGetClientFn(client), translations.NullTranslationHelper) |
| 88 | + |
| 89 | + // Create call request |
| 90 | + request := createMCPRequest(tc.requestArgs) |
| 91 | + |
| 92 | + // Call handler |
| 93 | + result, err := handler(context.Background(), request) |
| 94 | + |
| 95 | + // Verify results |
| 96 | + if tc.expectError { |
| 97 | + require.NoError(t, err) |
| 98 | + require.True(t, result.IsError) |
| 99 | + errorContent := getErrorResult(t, result) |
| 100 | + assert.Contains(t, errorContent.Text, tc.expectedErrMsg) |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + require.NoError(t, err) |
| 105 | + require.False(t, result.IsError) |
| 106 | + |
| 107 | + // Parse the result and get the text content if no error |
| 108 | + textContent := getTextResult(t, result) |
| 109 | + |
| 110 | + // Unmarshal and verify the result |
| 111 | + var returnedAlert github.DependabotAlert |
| 112 | + err = json.Unmarshal([]byte(textContent.Text), &returnedAlert) |
| 113 | + assert.NoError(t, err) |
| 114 | + assert.Equal(t, *tc.expectedAlert.Number, *returnedAlert.Number) |
| 115 | + assert.Equal(t, *tc.expectedAlert.State, *returnedAlert.State) |
| 116 | + assert.Equal(t, *tc.expectedAlert.HTMLURL, *returnedAlert.HTMLURL) |
| 117 | + }) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func Test_ListDependabotAlerts(t *testing.T) { |
| 122 | + // Verify tool definition once |
| 123 | + mockClient := github.NewClient(nil) |
| 124 | + tool, _ := ListDependabotAlerts(stubGetClientFn(mockClient), translations.NullTranslationHelper) |
| 125 | + require.NoError(t, toolsnaps.Test(tool.Name, tool)) |
| 126 | + |
| 127 | + assert.Equal(t, "list_dependabot_alerts", tool.Name) |
| 128 | + assert.NotEmpty(t, tool.Description) |
| 129 | + assert.Contains(t, tool.InputSchema.Properties, "owner") |
| 130 | + assert.Contains(t, tool.InputSchema.Properties, "repo") |
| 131 | + assert.Contains(t, tool.InputSchema.Properties, "state") |
| 132 | + assert.Contains(t, tool.InputSchema.Properties, "severity") |
| 133 | + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo"}) |
| 134 | + |
| 135 | + // Setup mock alerts for success case |
| 136 | + criticalAlert := github.DependabotAlert{ |
| 137 | + Number: github.Ptr(1), |
| 138 | + HTMLURL: github.Ptr("https://github.com/owner/repo/security/dependabot/1"), |
| 139 | + State: github.Ptr("open"), |
| 140 | + SecurityAdvisory: &github.DependabotSecurityAdvisory{ |
| 141 | + Severity: github.Ptr("critical"), |
| 142 | + }, |
| 143 | + } |
| 144 | + highSeverityAlert := github.DependabotAlert{ |
| 145 | + Number: github.Ptr(2), |
| 146 | + HTMLURL: github.Ptr("https://github.com/owner/repo/security/dependabot/2"), |
| 147 | + State: github.Ptr("fixed"), |
| 148 | + SecurityAdvisory: &github.DependabotSecurityAdvisory{ |
| 149 | + Severity: github.Ptr("high"), |
| 150 | + }, |
| 151 | + } |
| 152 | + |
| 153 | + tests := []struct { |
| 154 | + name string |
| 155 | + mockedClient *http.Client |
| 156 | + requestArgs map[string]interface{} |
| 157 | + expectError bool |
| 158 | + expectedAlerts []*github.DependabotAlert |
| 159 | + expectedErrMsg string |
| 160 | + }{ |
| 161 | + { |
| 162 | + name: "successful open alerts listing", |
| 163 | + mockedClient: mock.NewMockedHTTPClient( |
| 164 | + mock.WithRequestMatchHandler( |
| 165 | + mock.GetReposDependabotAlertsByOwnerByRepo, |
| 166 | + expectQueryParams(t, map[string]string{ |
| 167 | + "state": "open", |
| 168 | + }).andThen( |
| 169 | + mockResponse(t, http.StatusOK, []*github.DependabotAlert{&criticalAlert}), |
| 170 | + ), |
| 171 | + ), |
| 172 | + ), |
| 173 | + requestArgs: map[string]interface{}{ |
| 174 | + "owner": "owner", |
| 175 | + "repo": "repo", |
| 176 | + "state": "open", |
| 177 | + }, |
| 178 | + expectError: false, |
| 179 | + expectedAlerts: []*github.DependabotAlert{&criticalAlert}, |
| 180 | + }, |
| 181 | + { |
| 182 | + name: "successful severity filtered listing", |
| 183 | + mockedClient: mock.NewMockedHTTPClient( |
| 184 | + mock.WithRequestMatchHandler( |
| 185 | + mock.GetReposDependabotAlertsByOwnerByRepo, |
| 186 | + expectQueryParams(t, map[string]string{ |
| 187 | + "severity": "high", |
| 188 | + }).andThen( |
| 189 | + mockResponse(t, http.StatusOK, []*github.DependabotAlert{&highSeverityAlert}), |
| 190 | + ), |
| 191 | + ), |
| 192 | + ), |
| 193 | + requestArgs: map[string]interface{}{ |
| 194 | + "owner": "owner", |
| 195 | + "repo": "repo", |
| 196 | + "severity": "high", |
| 197 | + }, |
| 198 | + expectError: false, |
| 199 | + expectedAlerts: []*github.DependabotAlert{&highSeverityAlert}, |
| 200 | + }, |
| 201 | + { |
| 202 | + name: "successful all alerts listing", |
| 203 | + mockedClient: mock.NewMockedHTTPClient( |
| 204 | + mock.WithRequestMatchHandler( |
| 205 | + mock.GetReposDependabotAlertsByOwnerByRepo, |
| 206 | + expectQueryParams(t, map[string]string{}).andThen( |
| 207 | + mockResponse(t, http.StatusOK, []*github.DependabotAlert{&criticalAlert, &highSeverityAlert}), |
| 208 | + ), |
| 209 | + ), |
| 210 | + ), |
| 211 | + requestArgs: map[string]interface{}{ |
| 212 | + "owner": "owner", |
| 213 | + "repo": "repo", |
| 214 | + }, |
| 215 | + expectError: false, |
| 216 | + expectedAlerts: []*github.DependabotAlert{&criticalAlert, &highSeverityAlert}, |
| 217 | + }, |
| 218 | + { |
| 219 | + name: "alerts listing fails", |
| 220 | + mockedClient: mock.NewMockedHTTPClient( |
| 221 | + mock.WithRequestMatchHandler( |
| 222 | + mock.GetReposDependabotAlertsByOwnerByRepo, |
| 223 | + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 224 | + w.WriteHeader(http.StatusUnauthorized) |
| 225 | + _, _ = w.Write([]byte(`{"message": "Unauthorized access"}`)) |
| 226 | + }), |
| 227 | + ), |
| 228 | + ), |
| 229 | + requestArgs: map[string]interface{}{ |
| 230 | + "owner": "owner", |
| 231 | + "repo": "repo", |
| 232 | + }, |
| 233 | + expectError: true, |
| 234 | + expectedErrMsg: "failed to list alerts", |
| 235 | + }, |
| 236 | + } |
| 237 | + |
| 238 | + for _, tc := range tests { |
| 239 | + t.Run(tc.name, func(t *testing.T) { |
| 240 | + client := github.NewClient(tc.mockedClient) |
| 241 | + _, handler := ListDependabotAlerts(stubGetClientFn(client), translations.NullTranslationHelper) |
| 242 | + |
| 243 | + request := createMCPRequest(tc.requestArgs) |
| 244 | + |
| 245 | + result, err := handler(context.Background(), request) |
| 246 | + |
| 247 | + if tc.expectError { |
| 248 | + require.NoError(t, err) |
| 249 | + require.True(t, result.IsError) |
| 250 | + errorContent := getErrorResult(t, result) |
| 251 | + assert.Contains(t, errorContent.Text, tc.expectedErrMsg) |
| 252 | + return |
| 253 | + } |
| 254 | + |
| 255 | + require.NoError(t, err) |
| 256 | + require.False(t, result.IsError) |
| 257 | + |
| 258 | + textContent := getTextResult(t, result) |
| 259 | + |
| 260 | + // Unmarshal and verify the result |
| 261 | + var returnedAlerts []*github.DependabotAlert |
| 262 | + err = json.Unmarshal([]byte(textContent.Text), &returnedAlerts) |
| 263 | + assert.NoError(t, err) |
| 264 | + assert.Len(t, returnedAlerts, len(tc.expectedAlerts)) |
| 265 | + for i, alert := range returnedAlerts { |
| 266 | + assert.Equal(t, *tc.expectedAlerts[i].Number, *alert.Number) |
| 267 | + assert.Equal(t, *tc.expectedAlerts[i].HTMLURL, *alert.HTMLURL) |
| 268 | + assert.Equal(t, *tc.expectedAlerts[i].State, *alert.State) |
| 269 | + if tc.expectedAlerts[i].SecurityAdvisory != nil && tc.expectedAlerts[i].SecurityAdvisory.Severity != nil && |
| 270 | + alert.SecurityAdvisory != nil && alert.SecurityAdvisory.Severity != nil { |
| 271 | + assert.Equal(t, *tc.expectedAlerts[i].SecurityAdvisory.Severity, *alert.SecurityAdvisory.Severity) |
| 272 | + } |
| 273 | + } |
| 274 | + }) |
| 275 | + } |
| 276 | +} |
0 commit comments