sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/githubeventserver/githubeventserver_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package githubeventserver 18 19 import ( 20 "net/http" 21 "net/http/httptest" 22 "reflect" 23 "strings" 24 "testing" 25 26 "github.com/sirupsen/logrus" 27 28 "sigs.k8s.io/prow/pkg/plugins" 29 ) 30 31 func TestServeHTTPErrors(t *testing.T) { 32 pa := &plugins.ConfigAgent{} 33 pa.Set(&plugins.Configuration{}) 34 35 getSecret := func() []byte { 36 var repoLevelSecret = ` 37 '*': 38 - value: abc 39 created_at: 2019-10-02T15:00:00Z 40 - value: key2 41 created_at: 2020-10-02T15:00:00Z 42 foo/bar: 43 - value: 123abc 44 created_at: 2019-10-02T15:00:00Z 45 - value: key6 46 created_at: 2020-10-02T15:00:00Z 47 ` 48 return []byte(repoLevelSecret) 49 } 50 51 serveMuxHandler := &serveMuxHandler{ 52 hmacTokenGenerator: getSecret, 53 log: logrus.NewEntry(logrus.New()), 54 metrics: NewMetrics(), 55 } 56 57 // This is the SHA1 signature for payload "{}" and signature "abc" 58 // echo -n '{}' | openssl dgst -sha1 -hmac abc 59 const hmac string = "sha1=db5c76f4264d0ad96cf21baec394964b4b8ce580" 60 const body string = "{}" 61 var testcases = []struct { 62 name string 63 64 Method string 65 Header map[string]string 66 Body string 67 Code int 68 }{ 69 { 70 name: "Delete", 71 72 Method: http.MethodDelete, 73 Header: map[string]string{ 74 "X-GitHub-Event": "ping", 75 "X-GitHub-Delivery": "I am unique", 76 "X-Hub-Signature": hmac, 77 "content-type": "application/json", 78 }, 79 Body: body, 80 Code: http.StatusMethodNotAllowed, 81 }, 82 { 83 name: "No event", 84 85 Method: http.MethodPost, 86 Header: map[string]string{ 87 "X-GitHub-Delivery": "I am unique", 88 "X-Hub-Signature": hmac, 89 "content-type": "application/json", 90 }, 91 Body: body, 92 Code: http.StatusBadRequest, 93 }, 94 { 95 name: "No content type", 96 97 Method: http.MethodPost, 98 Header: map[string]string{ 99 "X-GitHub-Event": "ping", 100 "X-GitHub-Delivery": "I am unique", 101 "X-Hub-Signature": hmac, 102 }, 103 Body: body, 104 Code: http.StatusBadRequest, 105 }, 106 { 107 name: "No event guid", 108 109 Method: http.MethodPost, 110 Header: map[string]string{ 111 "X-GitHub-Event": "ping", 112 "X-Hub-Signature": hmac, 113 "content-type": "application/json", 114 }, 115 Body: body, 116 Code: http.StatusBadRequest, 117 }, 118 { 119 name: "No signature", 120 121 Method: http.MethodPost, 122 Header: map[string]string{ 123 "X-GitHub-Event": "ping", 124 "X-GitHub-Delivery": "I am unique", 125 "content-type": "application/json", 126 }, 127 Body: body, 128 Code: http.StatusForbidden, 129 }, 130 { 131 name: "Bad signature", 132 133 Method: http.MethodPost, 134 Header: map[string]string{ 135 "X-GitHub-Event": "ping", 136 "X-GitHub-Delivery": "I am unique", 137 "X-Hub-Signature": "this doesn't work", 138 "content-type": "application/json", 139 }, 140 Body: body, 141 Code: http.StatusForbidden, 142 }, 143 { 144 name: "Good", 145 146 Method: http.MethodPost, 147 Header: map[string]string{ 148 "X-GitHub-Event": "ping", 149 "X-GitHub-Delivery": "I am unique", 150 "X-Hub-Signature": hmac, 151 "content-type": "application/json", 152 }, 153 Body: body, 154 Code: http.StatusOK, 155 }, 156 { 157 name: "Good, again", 158 159 Method: http.MethodGet, 160 Header: map[string]string{ 161 "content-type": "application/json", 162 }, 163 Body: body, 164 Code: http.StatusMethodNotAllowed, 165 }, 166 } 167 168 for _, tc := range testcases { 169 t.Logf("Running scenario %q", tc.name) 170 171 w := httptest.NewRecorder() 172 r, err := http.NewRequest(tc.Method, "", strings.NewReader(tc.Body)) 173 if err != nil { 174 t.Fatal(err) 175 } 176 for k, v := range tc.Header { 177 r.Header.Set(k, v) 178 } 179 serveMuxHandler.ServeHTTP(w, r) 180 if w.Code != tc.Code { 181 t.Errorf("For test case: %+v\nExpected code %v, got code %v", tc, tc.Code, w.Code) 182 } 183 } 184 } 185 186 func TestGetExternalPluginsForEvent(t *testing.T) { 187 testCases := []struct { 188 name string 189 190 eventType string 191 org string 192 repo string 193 plugins map[string][]plugins.ExternalPlugin 194 195 expected []plugins.ExternalPlugin 196 }{ 197 { 198 name: "we have variety", 199 200 eventType: "issue_comment", 201 org: "kubernetes", 202 repo: "test-infra", 203 plugins: map[string][]plugins.ExternalPlugin{ 204 "kubernetes/test-infra": { 205 { 206 Name: "sandwich", 207 Events: []string{"pull_request"}, 208 }, 209 { 210 Name: "coffee", 211 Events: []string{"pull_request"}, 212 }, 213 { 214 Name: "beer", 215 Events: []string{"issue_comment", "issues"}, 216 }, 217 }, 218 "kubernetes/kubernetes": { 219 { 220 Name: "gumbo", 221 Events: []string{"issue_comment"}, 222 }, 223 }, 224 "kubernetes": { 225 { 226 Name: "chicken", 227 Events: []string{"push"}, 228 }, 229 { 230 Name: "water", 231 Events: []string{"pull_request"}, 232 }, 233 { 234 Name: "chocolate", 235 Events: []string{"pull_request", "issue_comment", "issues"}, 236 }, 237 }, 238 }, 239 240 expected: []plugins.ExternalPlugin{ 241 { 242 Name: "chocolate", 243 Events: []string{"pull_request", "issue_comment", "issues"}, 244 }, 245 { 246 Name: "beer", 247 Events: []string{"issue_comment", "issues"}, 248 }, 249 }, 250 }, 251 } 252 253 for _, tc := range testCases { 254 t.Run(tc.name, func(t *testing.T) { 255 s := &serveMuxHandler{externalPlugins: tc.plugins} 256 gotPlugins := s.getExternalPluginsForEvent(tc.org, tc.repo, tc.eventType) 257 258 if !reflect.DeepEqual(tc.expected, gotPlugins) { 259 t.Errorf("expected plugin: %+v, got: %+v", tc.expected, gotPlugins) 260 } 261 262 }) 263 } 264 }