|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | +"context" |
| 5 | +"encoding/json" |
| 6 | +"net/http" |
| 7 | +"testing" |
| 8 | + |
| 9 | +"github.com/google/go-github/v69/github" |
| 10 | +"github.com/migueleliasweb/go-github-mock/src/mock" |
| 11 | +"github.com/stretchr/testify/assert" |
| 12 | +"github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +funcTest_GetCodeScanningAlert(t*testing.T) { |
| 16 | +// Verify tool definition once |
| 17 | +mockClient:=github.NewClient(nil) |
| 18 | +tool,_:=getCodeScanningAlert(mockClient) |
| 19 | + |
| 20 | +assert.Equal(t,"get_code_scanning_alert",tool.Name) |
| 21 | +assert.NotEmpty(t,tool.Description) |
| 22 | +assert.Contains(t,tool.InputSchema.Properties,"owner") |
| 23 | +assert.Contains(t,tool.InputSchema.Properties,"repo") |
| 24 | +assert.Contains(t,tool.InputSchema.Properties,"alert_number") |
| 25 | +assert.ElementsMatch(t,tool.InputSchema.Required, []string{"owner","repo","alert_number"}) |
| 26 | + |
| 27 | +// Setup mock alert for success case |
| 28 | +mockAlert:=&github.Alert{ |
| 29 | +Number:github.Ptr(42), |
| 30 | +State:github.Ptr("open"), |
| 31 | +Rule:&github.Rule{ID:github.Ptr("test-rule"),Description:github.Ptr("Test Rule Description")}, |
| 32 | +HTMLURL:github.Ptr("https://github.com/owner/repo/security/code-scanning/42"), |
| 33 | +} |
| 34 | + |
| 35 | +tests:= []struct { |
| 36 | +namestring |
| 37 | +mockedClient*http.Client |
| 38 | +requestArgsmap[string]interface{} |
| 39 | +expectErrorbool |
| 40 | +expectedAlert*github.Alert |
| 41 | +expectedErrMsgstring |
| 42 | +}{ |
| 43 | +{ |
| 44 | +name:"successful alert fetch", |
| 45 | +mockedClient:mock.NewMockedHTTPClient( |
| 46 | +mock.WithRequestMatch( |
| 47 | +mock.GetReposCodeScanningAlertsByOwnerByRepoByAlertNumber, |
| 48 | +mockAlert, |
| 49 | +), |
| 50 | +), |
| 51 | +requestArgs:map[string]interface{}{ |
| 52 | +"owner":"owner", |
| 53 | +"repo":"repo", |
| 54 | +"alert_number":float64(42), |
| 55 | +}, |
| 56 | +expectError:false, |
| 57 | +expectedAlert:mockAlert, |
| 58 | +}, |
| 59 | +{ |
| 60 | +name:"alert fetch fails", |
| 61 | +mockedClient:mock.NewMockedHTTPClient( |
| 62 | +mock.WithRequestMatchHandler( |
| 63 | +mock.GetReposCodeScanningAlertsByOwnerByRepoByAlertNumber, |
| 64 | +http.HandlerFunc(func(w http.ResponseWriter,r*http.Request) { |
| 65 | +w.WriteHeader(http.StatusNotFound) |
| 66 | +_,_=w.Write([]byte(`{"message": "Not Found"}`)) |
| 67 | +}), |
| 68 | +), |
| 69 | +), |
| 70 | +requestArgs:map[string]interface{}{ |
| 71 | +"owner":"owner", |
| 72 | +"repo":"repo", |
| 73 | +"alert_number":float64(9999), |
| 74 | +}, |
| 75 | +expectError:true, |
| 76 | +expectedErrMsg:"failed to get alert", |
| 77 | +}, |
| 78 | +} |
| 79 | + |
| 80 | +for_,tc:=rangetests { |
| 81 | +t.Run(tc.name,func(t*testing.T) { |
| 82 | +// Setup client with mock |
| 83 | +client:=github.NewClient(tc.mockedClient) |
| 84 | +_,handler:=getCodeScanningAlert(client) |
| 85 | + |
| 86 | +// Create call request |
| 87 | +request:=createMCPRequest(tc.requestArgs) |
| 88 | + |
| 89 | +// Call handler |
| 90 | +result,err:=handler(context.Background(),request) |
| 91 | + |
| 92 | +// Verify results |
| 93 | +iftc.expectError { |
| 94 | +require.Error(t,err) |
| 95 | +assert.Contains(t,err.Error(),tc.expectedErrMsg) |
| 96 | +return |
| 97 | +} |
| 98 | + |
| 99 | +require.NoError(t,err) |
| 100 | + |
| 101 | +// Parse the result and get the text content if no error |
| 102 | +textContent:=getTextResult(t,result) |
| 103 | + |
| 104 | +// Unmarshal and verify the result |
| 105 | +varreturnedAlert github.Alert |
| 106 | +err=json.Unmarshal([]byte(textContent.Text),&returnedAlert) |
| 107 | +assert.NoError(t,err) |
| 108 | +assert.Equal(t,*tc.expectedAlert.Number,*returnedAlert.Number) |
| 109 | +assert.Equal(t,*tc.expectedAlert.State,*returnedAlert.State) |
| 110 | +assert.Equal(t,*tc.expectedAlert.Rule.ID,*returnedAlert.Rule.ID) |
| 111 | +assert.Equal(t,*tc.expectedAlert.HTMLURL,*returnedAlert.HTMLURL) |
| 112 | + |
| 113 | +}) |
| 114 | +} |
| 115 | +} |
| 116 | + |
| 117 | +funcTest_ListCodeScanningAlerts(t*testing.T) { |
| 118 | +// Verify tool definition once |
| 119 | +mockClient:=github.NewClient(nil) |
| 120 | +tool,_:=listCodeScanningAlerts(mockClient) |
| 121 | + |
| 122 | +assert.Equal(t,"list_code_scanning_alerts",tool.Name) |
| 123 | +assert.NotEmpty(t,tool.Description) |
| 124 | +assert.Contains(t,tool.InputSchema.Properties,"owner") |
| 125 | +assert.Contains(t,tool.InputSchema.Properties,"repo") |
| 126 | +assert.Contains(t,tool.InputSchema.Properties,"ref") |
| 127 | +assert.Contains(t,tool.InputSchema.Properties,"state") |
| 128 | +assert.Contains(t,tool.InputSchema.Properties,"severity") |
| 129 | +assert.ElementsMatch(t,tool.InputSchema.Required, []string{"owner","repo"}) |
| 130 | + |
| 131 | +// Setup mock alerts for success case |
| 132 | +mockAlerts:= []*github.Alert{ |
| 133 | +{ |
| 134 | +Number:github.Ptr(42), |
| 135 | +State:github.Ptr("open"), |
| 136 | +Rule:&github.Rule{ID:github.Ptr("test-rule-1"),Description:github.Ptr("Test Rule 1")}, |
| 137 | +HTMLURL:github.Ptr("https://github.com/owner/repo/security/code-scanning/42"), |
| 138 | +}, |
| 139 | +{ |
| 140 | +Number:github.Ptr(43), |
| 141 | +State:github.Ptr("fixed"), |
| 142 | +Rule:&github.Rule{ID:github.Ptr("test-rule-2"),Description:github.Ptr("Test Rule 2")}, |
| 143 | +HTMLURL:github.Ptr("https://github.com/owner/repo/security/code-scanning/43"), |
| 144 | +}, |
| 145 | +} |
| 146 | + |
| 147 | +tests:= []struct { |
| 148 | +namestring |
| 149 | +mockedClient*http.Client |
| 150 | +requestArgsmap[string]interface{} |
| 151 | +expectErrorbool |
| 152 | +expectedAlerts []*github.Alert |
| 153 | +expectedErrMsgstring |
| 154 | +}{ |
| 155 | +{ |
| 156 | +name:"successful alerts listing", |
| 157 | +mockedClient:mock.NewMockedHTTPClient( |
| 158 | +mock.WithRequestMatch( |
| 159 | +mock.GetReposCodeScanningAlertsByOwnerByRepo, |
| 160 | +mockAlerts, |
| 161 | +), |
| 162 | +), |
| 163 | +requestArgs:map[string]interface{}{ |
| 164 | +"owner":"owner", |
| 165 | +"repo":"repo", |
| 166 | +"ref":"main", |
| 167 | +"state":"open", |
| 168 | +"severity":"high", |
| 169 | +}, |
| 170 | +expectError:false, |
| 171 | +expectedAlerts:mockAlerts, |
| 172 | +}, |
| 173 | +{ |
| 174 | +name:"alerts listing fails", |
| 175 | +mockedClient:mock.NewMockedHTTPClient( |
| 176 | +mock.WithRequestMatchHandler( |
| 177 | +mock.GetReposCodeScanningAlertsByOwnerByRepo, |
| 178 | +http.HandlerFunc(func(w http.ResponseWriter,r*http.Request) { |
| 179 | +w.WriteHeader(http.StatusUnauthorized) |
| 180 | +_,_=w.Write([]byte(`{"message": "Unauthorized access"}`)) |
| 181 | +}), |
| 182 | +), |
| 183 | +), |
| 184 | +requestArgs:map[string]interface{}{ |
| 185 | +"owner":"owner", |
| 186 | +"repo":"repo", |
| 187 | +}, |
| 188 | +expectError:true, |
| 189 | +expectedErrMsg:"failed to list alerts", |
| 190 | +}, |
| 191 | +} |
| 192 | + |
| 193 | +for_,tc:=rangetests { |
| 194 | +t.Run(tc.name,func(t*testing.T) { |
| 195 | +// Setup client with mock |
| 196 | +client:=github.NewClient(tc.mockedClient) |
| 197 | +_,handler:=listCodeScanningAlerts(client) |
| 198 | + |
| 199 | +// Create call request |
| 200 | +request:=createMCPRequest(tc.requestArgs) |
| 201 | + |
| 202 | +// Call handler |
| 203 | +result,err:=handler(context.Background(),request) |
| 204 | + |
| 205 | +// Verify results |
| 206 | +iftc.expectError { |
| 207 | +require.Error(t,err) |
| 208 | +assert.Contains(t,err.Error(),tc.expectedErrMsg) |
| 209 | +return |
| 210 | +} |
| 211 | + |
| 212 | +require.NoError(t,err) |
| 213 | + |
| 214 | +// Parse the result and get the text content if no error |
| 215 | +textContent:=getTextResult(t,result) |
| 216 | + |
| 217 | +// Unmarshal and verify the result |
| 218 | +varreturnedAlerts []*github.Alert |
| 219 | +err=json.Unmarshal([]byte(textContent.Text),&returnedAlerts) |
| 220 | +assert.NoError(t,err) |
| 221 | +assert.Len(t,returnedAlerts,len(tc.expectedAlerts)) |
| 222 | +fori,alert:=rangereturnedAlerts { |
| 223 | +assert.Equal(t,*tc.expectedAlerts[i].Number,*alert.Number) |
| 224 | +assert.Equal(t,*tc.expectedAlerts[i].State,*alert.State) |
| 225 | +assert.Equal(t,*tc.expectedAlerts[i].Rule.ID,*alert.Rule.ID) |
| 226 | +assert.Equal(t,*tc.expectedAlerts[i].HTMLURL,*alert.HTMLURL) |
| 227 | +} |
| 228 | +}) |
| 229 | +} |
| 230 | +} |