/
generic_test.go
175 lines (140 loc) · 4.87 KB
/
generic_test.go
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
package webhook
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
"github.com/Azure/brigade/pkg/brigade"
"github.com/Azure/brigade/pkg/storage"
"github.com/Azure/brigade/pkg/storage/mock"
restful "github.com/emicklei/go-restful"
)
func newTestGenericWebhookHandler(store storage.Store) *genericWebhook {
return &genericWebhook{store}
}
func newGenericProject() *brigade.Project {
return &brigade.Project{
ID: "brigade-1234",
Name: "dgkanatsios/o365-notify",
Repo: brigade.Repo{
Name: "github.com/dgkanatsios/o365-notify",
CloneURL: "https://github.com/dgkanatsios/o365-notify",
},
Secrets: map[string]string{
"mysecret": "value",
},
}
}
func TestGenericWebhook(t *testing.T) {
proj := newGenericProject()
store := newTestStore()
h := newTestGenericWebhookHandler(store)
gwData := &genericWebhookData{
Commit: "63c09efb6eb544f41a48901a6d0cc6ddfa4adb28",
}
if err := h.genericWebhookEvent(proj, []byte(exampleGenericWebhook), gwData); err != nil {
t.Errorf("failed generic webhook event: %s", err)
}
if payload := string(store.builds[0].Payload); payload != exampleGenericWebhook {
t.Errorf("unexpected payload: %s", payload)
}
if provider := string(store.builds[0].Provider); provider != "GenericWebhook" {
t.Errorf("unexpected provider: %s", provider)
}
if etype := string(store.builds[0].Type); etype != "webhook" {
t.Errorf("unexpected type: %s", etype)
}
}
func TestGenericWebhookHandlerBadBody(t *testing.T) {
store := newEmptyTestStore()
s := newTestGenericWebhookHandler(store)
ws := mockWebService(s.Handle)
container := restful.NewContainer()
container.Add(ws)
httpRequest := httptest.NewRequest("POST", "/webhook/fakeProject/fakeCode", bytes.NewBuffer(nil))
httpRequest.Header.Add("Content-Type", "application/json")
rw := httptest.NewRecorder()
container.ServeHTTP(rw, httpRequest)
if rw.Result().StatusCode != http.StatusBadRequest {
t.Errorf("expected error 400, got %d", rw.Result().StatusCode)
}
}
func TestGenericWebhookHandlerWrongProject(t *testing.T) {
store := newTestStoreWithFakeProject2()
s := newTestGenericWebhookHandler(store)
ws := mockWebService(s.Handle)
container := restful.NewContainer()
container.Add(ws)
httpRequest := httptest.NewRequest("POST", "/webhook/brigade-fakeProject/fakeCode", bytes.NewBuffer([]byte(exampleGenericWebhook)))
httpRequest.Header.Add("Content-Type", "application/json")
rw := httptest.NewRecorder()
container.ServeHTTP(rw, httpRequest)
if rw.Result().StatusCode != http.StatusBadRequest {
t.Errorf("expected error 400, got %d", rw.Result().StatusCode)
}
}
func TestGenericWebhookHandlerCorrectProjectEmptySecret(t *testing.T) {
store := newTestStoreWithFakeProject()
s := newTestGenericWebhookHandler(store)
ws := mockWebService(s.Handle)
container := restful.NewContainer()
container.Add(ws)
httpRequest := httptest.NewRequest("POST", "/webhook/brigade-fakeProject/fakeCode", bytes.NewBuffer([]byte(exampleGenericWebhook)))
httpRequest.Header.Add("Content-Type", "application/json")
rw := httptest.NewRecorder()
container.ServeHTTP(rw, httpRequest)
if rw.Result().StatusCode != http.StatusUnauthorized {
t.Errorf("expected error 401, got %d", rw.Result().StatusCode)
}
}
func TestGenericWebhookHandlerCorrectProjectCorrectSecret(t *testing.T) {
store := newTestStoreWithFakeProject()
store.ProjectList[0].GenericWebhookSecret = "fakeCode"
s := newTestGenericWebhookHandler(store)
ws := mockWebService(s.Handle)
container := restful.NewContainer()
container.Add(ws)
httpRequest := httptest.NewRequest("POST", "/webhook/brigade-fakeProject/fakeCode", bytes.NewBuffer([]byte(exampleGenericWebhook)))
httpRequest.Header.Add("Content-Type", "application/json")
rw := httptest.NewRecorder()
container.ServeHTTP(rw, httpRequest)
if rw.Result().StatusCode != http.StatusOK {
t.Errorf("expected error 200, got %d", rw.Result().StatusCode)
}
}
const exampleGenericWebhook = `
{
"ref": "refs/heads/changes",
"commit": "63c09efb6eb544f41a48901a6d0cc6ddfa4adb28"
}
`
func mockWebService(handler restful.RouteFunction) *restful.WebService {
ws := new(restful.WebService)
ws.Path("/webhook").
Consumes(restful.MIME_JSON).
Produces(restful.MIME_JSON, restful.MIME_XML, "plain/text", "text/javascript")
ws.Route(ws.POST("/{projectID}/{secret}").To(handler).
Param(ws.PathParameter("projectID", "identifier of the projectID").DataType("string")).
Param(ws.PathParameter("secret", "shared secret").DataType("string")).
Writes([]byte{}).
Returns(200, "OK", []byte{}).
Returns(404, "Not Found", nil))
return ws
}
func newEmptyTestStore() *mock.Store {
return &mock.Store{}
}
func newTestStoreWithFakeProject() *mock.Store {
return &mock.Store{
ProjectList: []*brigade.Project{{
ID: "brigade-fakeProject",
}},
}
}
func newTestStoreWithFakeProject2() *mock.Store {
return &mock.Store{
ProjectList: []*brigade.Project{{
ID: "brigade-fakeProject2",
}},
}
}