|
| 1 | +package gogs |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "os" |
| 10 | + "reflect" |
| 11 | + "testing" |
| 12 | + |
| 13 | + client "github.com/gogits/go-gogs-client" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +// NOTES: |
| 18 | +// - Run "go test" to run tests |
| 19 | +// - Run "gocov test | gocov report" to report on test converage by file |
| 20 | +// - Run "gocov test | gocov annotate -" to report on all code and functions, those ,marked with "MISS" were never called |
| 21 | +// |
| 22 | +// or |
| 23 | +// |
| 24 | +// -- may be a good idea to change to output path to somewherelike /tmp |
| 25 | +// go test -coverprofile cover.out && go tool cover -html=cover.out -o cover.html |
| 26 | +// |
| 27 | + |
| 28 | +const ( |
| 29 | + path = "/webhooks" |
| 30 | +) |
| 31 | + |
| 32 | +var hook *Webhook |
| 33 | + |
| 34 | +func TestMain(m *testing.M) { |
| 35 | + |
| 36 | + // setup |
| 37 | + var err error |
| 38 | + hook, err = New(Options.Secret("sampleToken!")) |
| 39 | + if err != nil { |
| 40 | + log.Fatal(err) |
| 41 | + } |
| 42 | + os.Exit(m.Run()) |
| 43 | + |
| 44 | + //teardown |
| 45 | + |
| 46 | +} |
| 47 | + |
| 48 | +func newServer(handler http.HandlerFunc) *httptest.Server { |
| 49 | + mux := http.NewServeMux() |
| 50 | + mux.HandleFunc(path, handler) |
| 51 | + return httptest.NewServer(mux) |
| 52 | +} |
| 53 | + |
| 54 | +func TestBadRequests(t *testing.T) { |
| 55 | + assert := require.New(t) |
| 56 | + tests := []struct { |
| 57 | + name string |
| 58 | + event Event |
| 59 | + payload io.Reader |
| 60 | + headers http.Header |
| 61 | + }{ |
| 62 | + { |
| 63 | + name: "BadNoEventHeader", |
| 64 | + event: CreateEvent, |
| 65 | + payload: bytes.NewBuffer([]byte("{}")), |
| 66 | + headers: http.Header{}, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "UnsubscribedEvent", |
| 70 | + event: CreateEvent, |
| 71 | + payload: bytes.NewBuffer([]byte("{}")), |
| 72 | + headers: http.Header{ |
| 73 | + "X-Gogs-Event": []string{"noneexistant_event"}, |
| 74 | + }, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "BadBody", |
| 78 | + event: PushEvent, |
| 79 | + payload: bytes.NewBuffer([]byte("")), |
| 80 | + headers: http.Header{ |
| 81 | + "X-Gogs-Event": []string{"push"}, |
| 82 | + "X-Gogs-Signature": []string{"0dacdb7c00bc1cdc0c24038b7b244cf65146b33da862a769e2608a063529fffc"}, |
| 83 | + }, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "BadSignatureLength", |
| 87 | + event: PushEvent, |
| 88 | + payload: bytes.NewBuffer([]byte("{}")), |
| 89 | + headers: http.Header{ |
| 90 | + "X-Gogs-Event": []string{"push"}, |
| 91 | + "X-Gogs-Signature": []string{""}, |
| 92 | + }, |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "BadSignatureMatch", |
| 96 | + event: PushEvent, |
| 97 | + payload: bytes.NewBuffer([]byte("{}")), |
| 98 | + headers: http.Header{ |
| 99 | + "X-Github-Event": []string{"push"}, |
| 100 | + "X-Gogs-Signature": []string{"111"}, |
| 101 | + }, |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + for _, tt := range tests { |
| 106 | + tc := tt |
| 107 | + client := &http.Client{} |
| 108 | + t.Run(tt.name, func(t *testing.T) { |
| 109 | + t.Parallel() |
| 110 | + var parseError error |
| 111 | + server := newServer(func(w http.ResponseWriter, r *http.Request) { |
| 112 | + _, parseError = hook.Parse(r, tc.event) |
| 113 | + }) |
| 114 | + defer server.Close() |
| 115 | + req, err := http.NewRequest(http.MethodPost, server.URL+path, tc.payload) |
| 116 | + assert.NoError(err) |
| 117 | + req.Header = tc.headers |
| 118 | + req.Header.Set("Content-Type", "application/json") |
| 119 | + |
| 120 | + resp, err := client.Do(req) |
| 121 | + assert.NoError(err) |
| 122 | + assert.Equal(http.StatusOK, resp.StatusCode) |
| 123 | + assert.Error(parseError) |
| 124 | + }) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func TestWebhooks(t *testing.T) { |
| 129 | + assert := require.New(t) |
| 130 | + tests := []struct { |
| 131 | + name string |
| 132 | + event Event |
| 133 | + typ interface{} |
| 134 | + filename string |
| 135 | + headers http.Header |
| 136 | + }{ |
| 137 | + { |
| 138 | + name: "CreateEvent", |
| 139 | + event: CreateEvent, |
| 140 | + typ: client.CreatePayload{}, |
| 141 | + filename: "../testdata/gogs/create-event.json", |
| 142 | + headers: http.Header{ |
| 143 | + "X-Gogs-Delivery": []string{"f6266f16-1bf3-46a5-9ea4-602e06ead473"}, |
| 144 | + "X-Gogs-Event": []string{"create"}, |
| 145 | + "X-Gogs-Signature": []string{"d3c99a5711de365c4ff897be0692d9016de2cee6e3e1d6b3fb49154f9efb354d"}, |
| 146 | + }, |
| 147 | + }, |
| 148 | + { |
| 149 | + name: "DeleteEvent", |
| 150 | + event: DeleteEvent, |
| 151 | + typ: client.DeletePayload{}, |
| 152 | + filename: "../testdata/gogs/delete-event.json", |
| 153 | + headers: http.Header{ |
| 154 | + "X-Gogs-Delivery": []string{"f6266f16-1bf3-46a5-9ea4-602e06ead473"}, |
| 155 | + "X-Gogs-Event": []string{"delete"}, |
| 156 | + "X-Gogs-Signature": []string{"6208970efc3c5283df65f726d69ae8331491d7857f812dfaeaedf11a68d8da79"}, |
| 157 | + }, |
| 158 | + }, |
| 159 | + { |
| 160 | + name: "ForkEvent", |
| 161 | + event: ForkEvent, |
| 162 | + typ: client.ForkPayload{}, |
| 163 | + filename: "../testdata/gogs/fork-event.json", |
| 164 | + headers: http.Header{ |
| 165 | + "X-Gogs-Delivery": []string{"f6266f16-1bf3-46a5-9ea4-602e06ead473"}, |
| 166 | + "X-Gogs-Event": []string{"fork"}, |
| 167 | + "X-Gogs-Signature": []string{"bd5fda972bd27745d9e108c3d92a926a47baf8b715a422f73ff9acdfa6a86402"}, |
| 168 | + }, |
| 169 | + }, |
| 170 | + { |
| 171 | + name: "PushEvent", |
| 172 | + event: PushEvent, |
| 173 | + typ: client.PushPayload{}, |
| 174 | + filename: "../testdata/gogs/push-event.json", |
| 175 | + headers: http.Header{ |
| 176 | + "X-Gogs-Delivery": []string{"f6266f16-1bf3-46a5-9ea4-602e06ead473"}, |
| 177 | + "X-Gogs-Event": []string{"push"}, |
| 178 | + "X-Gogs-Signature": []string{"83d4163fb936904aeb9ffd6ce22cf86e4b36273b2e1c63a57f5a6ddc371ce3ba"}, |
| 179 | + }, |
| 180 | + }, |
| 181 | + { |
| 182 | + name: "IssuesEvent", |
| 183 | + event: IssuesEvent, |
| 184 | + typ: client.IssuesPayload{}, |
| 185 | + filename: "../testdata/gogs/issues-event.json", |
| 186 | + headers: http.Header{ |
| 187 | + "X-Gogs-Delivery": []string{"f6266f16-1bf3-46a5-9ea4-602e06ead473"}, |
| 188 | + "X-Gogs-Event": []string{"issues"}, |
| 189 | + "X-Gogs-Signature": []string{"bb61f632278cc601f37bc0a611e2a06bba3b53453f3e58092dc20ffa0cec6916"}, |
| 190 | + }, |
| 191 | + }, |
| 192 | + { |
| 193 | + name: "IssueCommentEvent", |
| 194 | + event: IssueCommentEvent, |
| 195 | + typ: client.IssueCommentPayload{}, |
| 196 | + filename: "../testdata/gogs/issue-comment-event.json", |
| 197 | + headers: http.Header{ |
| 198 | + "X-Gogs-Delivery": []string{"f6266f16-1bf3-46a5-9ea4-602e06ead473"}, |
| 199 | + "X-Gogs-Event": []string{"issue_comment"}, |
| 200 | + "X-Gogs-Signature": []string{"1d45142f03ed44a06d6630e57aa8c8c0ad7ed90a57cc0ee9fec8fb5434efd92e"}, |
| 201 | + }, |
| 202 | + }, |
| 203 | + { |
| 204 | + name: "PullRequestEvent", |
| 205 | + event: PullRequestEvent, |
| 206 | + typ: client.PullRequestPayload{}, |
| 207 | + filename: "../testdata/gogs/pull-request-event.json", |
| 208 | + headers: http.Header{ |
| 209 | + "X-Gogs-Delivery": []string{"f6266f16-1bf3-46a5-9ea4-602e06ead473"}, |
| 210 | + "X-Gogs-Event": []string{"pull_request"}, |
| 211 | + "X-Gogs-Signature": []string{"69105ac4cada9f0b099be34722b26d3854be20988c26bd9e135fec74a830d00a"}, |
| 212 | + }, |
| 213 | + }, |
| 214 | + { |
| 215 | + name: "ReleaseEvent", |
| 216 | + event: ReleaseEvent, |
| 217 | + typ: client.ReleasePayload{}, |
| 218 | + filename: "../testdata/gogs/release-event.json", |
| 219 | + headers: http.Header{ |
| 220 | + "X-Gogs-Delivery": []string{"f6266f16-1bf3-46a5-9ea4-602e06ead473"}, |
| 221 | + "X-Gogs-Event": []string{"release"}, |
| 222 | + "X-Gogs-Signature": []string{"93bfcfb879642dbc28136266b1f7d56d053fa08332aa1f4d884a66ced2b5f10a"}, |
| 223 | + }, |
| 224 | + }, |
| 225 | + } |
| 226 | + |
| 227 | + for _, tt := range tests { |
| 228 | + tc := tt |
| 229 | + client := &http.Client{} |
| 230 | + t.Run(tt.name, func(t *testing.T) { |
| 231 | + t.Parallel() |
| 232 | + payload, err := os.Open(tc.filename) |
| 233 | + assert.NoError(err) |
| 234 | + defer func() { |
| 235 | + _ = payload.Close() |
| 236 | + }() |
| 237 | + |
| 238 | + var parseError error |
| 239 | + var results interface{} |
| 240 | + server := newServer(func(w http.ResponseWriter, r *http.Request) { |
| 241 | + results, parseError = hook.Parse(r, tc.event) |
| 242 | + }) |
| 243 | + defer server.Close() |
| 244 | + req, err := http.NewRequest(http.MethodPost, server.URL+path, payload) |
| 245 | + assert.NoError(err) |
| 246 | + req.Header = tc.headers |
| 247 | + req.Header.Set("Content-Type", "application/json") |
| 248 | + |
| 249 | + resp, err := client.Do(req) |
| 250 | + assert.NoError(err) |
| 251 | + assert.Equal(http.StatusOK, resp.StatusCode) |
| 252 | + assert.NoError(parseError) |
| 253 | + assert.Equal(reflect.TypeOf(tc.typ), reflect.TypeOf(results)) |
| 254 | + }) |
| 255 | + } |
| 256 | +} |
0 commit comments