-
Notifications
You must be signed in to change notification settings - Fork 6
/
events_test.go
260 lines (216 loc) · 7.83 KB
/
events_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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package devopstic
import (
"bytes"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/splisson/devopstic/entities"
"github.com/splisson/devopstic/handlers"
"github.com/splisson/devopstic/persistence"
"github.com/splisson/devopstic/representations"
"github.com/splisson/devopstic/services"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
"time"
)
var (
r *gin.Engine
token string
)
const (
PIPELINE_ID = "web"
ENVIRONMENT = "dev"
)
// Helper function to process a request and test its response
func testHTTPResponse(t *testing.T, r *gin.Engine, req *http.Request, f func(w *httptest.ResponseRecorder) bool) {
// Create a response recorder
w := httptest.NewRecorder()
// Create the service and process the above request.
r.ServeHTTP(w, req)
if !f(w) {
t.Fail()
}
}
func Login() (string, error) {
message := map[string]interface{}{
"username": "admin",
"password": "admin",
}
bytesRepresentation, _ := json.Marshal(message)
body := bytes.NewBuffer(bytesRepresentation)
req, _ := http.NewRequest("POST", "/tokens", body)
req.Header.Set("Content-Type", "application/json")
var result map[string]interface{}
// Create a response recorder
w := httptest.NewRecorder()
// Create the service and process the above request.
r.ServeHTTP(w, req)
err := json.NewDecoder(w.Body).Decode(&result)
if err != nil {
return "", err
}
token := fmt.Sprintf("%v", result["token"])
return token, nil
}
func authenticate(req *http.Request) {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
}
func TestMain(m *testing.M) {
db := persistence.NewPostgresqlConnectionLocalhost()
persistence.CreateTables(db)
commitStore := persistence.NewCommitStoreDB(db)
eventStore := persistence.NewEventStoreDB(db)
incidentStore := persistence.NewIncidentStoreDB(db)
deploymentStore := persistence.NewDeploymentStoreDB(db)
eventService := services.NewEventService(eventStore)
commitService := services.NewCommitService(commitStore, deploymentStore)
incidentService := services.NewIncidentService(incidentStore)
commitHandlers := handlers.NewCommitHandlers(commitService)
eventHandlers := handlers.NewEventHandlers(eventService, commitService, incidentService)
githubHandlers := handlers.NewGithubEventHandlers(eventService, commitService, incidentService)
r = BuildEngine(commitHandlers, eventHandlers, githubHandlers)
var err error
token, err = Login()
if err != nil {
panic(err)
}
m.Run()
}
func TestGetEvents(t *testing.T) {
t.Run("should get events in the response", func(t *testing.T) {
// Create a request to send to the above route
req, _ := http.NewRequest("GET", "/events", nil)
authenticate(req)
testHTTPResponse(t, r, req, func(w *httptest.ResponseRecorder) bool {
// Test that the http status code is 200
statusOK := w.Code == http.StatusOK
var pageOK bool
if statusOK {
var payload representations.CommitResults
err := json.NewDecoder(w.Body).Decode(&payload)
pageOK = err == nil && payload.Items != nil
}
return statusOK && pageOK
})
})
}
func TestPostEvents(t *testing.T) {
t.Run("Post event deploy without and with authorization", func(t *testing.T) {
//r := BuildEngine()
// Create a request to send to the above route
req, _ := http.NewRequest("POST", "/events", nil)
testHTTPResponse(t, r, req, func(w *httptest.ResponseRecorder) bool {
// Test that the http status code is 401 because of missing authentication
statusOK := w.Code == http.StatusUnauthorized
return statusOK
})
message := map[string]interface{}{
"type": entities.EVENT_COMMIT,
"status": entities.STATUS_SUCCESS,
"commit_id": uuid.New().String(),
"pipeline_id": PIPELINE_ID,
"environment": ENVIRONMENT,
"timestamp": time.Now().Unix(),
}
bytesRepresentation, _ := json.Marshal(message)
body := bytes.NewBuffer(bytesRepresentation)
req, _ = http.NewRequest("POST", "/events", body)
req.Header.Set("Content-Type", "application/json")
authenticate(req)
testHTTPResponse(t, r, req, func(w *httptest.ResponseRecorder) bool {
// Test that the http status code is 200
statusOK := w.Code == http.StatusOK
var pageOK bool
if statusOK {
var result representations.Commit
err := json.NewDecoder(w.Body).Decode(&result)
pageOK = err == nil && result.Id != ""
}
return statusOK && pageOK
})
})
t.Run("Post event committed and approve", func(t *testing.T) {
message := map[string]interface{}{
"type": entities.EVENT_COMMIT,
"status": entities.STATUS_SUCCESS,
"commit_id": uuid.New().String(),
"pipeline_id": PIPELINE_ID,
"environment": ENVIRONMENT,
"timestamp": time.Now().Add(-5 * time.Minute).Unix(), //.Format(time.RFC3339),
}
message["commit_id"] = uuid.New().String()
postEvent(t, "header", message)
message["type"] = "approve"
message["timestamp"] = time.Now().Unix() //.Format(time.RFC3339)
event, err := postEvent(t, "header", message)
assert.Nil(t, err, "no error")
assert.True(t, event.Type == entities.EVENT_APPROVE, "approve event")
})
t.Run("Post event incident status change", func(t *testing.T) {
message := map[string]interface{}{
"type": entities.EVENT_INCIDENT_STATUS_CHANGE,
"status": entities.STATUS_FAILURE,
"incident_id": uuid.New().String(),
"pipeline_id": PIPELINE_ID,
"environment": ENVIRONMENT,
"timestamp": time.Now().Add(-5 * time.Minute).Unix(), //.Format(time.RFC3339),
}
message["commit_id"] = uuid.New().String()
event, err := postEvent(t, "header", message)
assert.Nil(t, err, "no error")
assert.True(t, event.Type == entities.EVENT_INCIDENT_STATUS_CHANGE, "incident event")
message["type"] = entities.EVENT_INCIDENT_STATUS_CHANGE
message["status"] = entities.STATUS_SUCCESS
message["timestamp"] = time.Now().Unix() //.Format(time.RFC3339)
event, err = postEvent(t, "header", message)
assert.Nil(t, err, "no error")
assert.True(t, event.Type == entities.EVENT_INCIDENT_STATUS_CHANGE, "incident event")
})
t.Run("Post event deploy", func(t *testing.T) {
message := map[string]interface{}{
"type": entities.EVENT_COMMIT,
"status": entities.STATUS_SUCCESS,
"commit_id": uuid.New().String(),
"pipeline_id": PIPELINE_ID,
"environment": ENVIRONMENT,
"timestamp": time.Now().Add(-20 * time.Minute).Unix(), //.Format(time.RFC3339),
}
event, err := postEvent(t, "header", message)
message["timestamp"] = time.Now().Add(-10 * time.Minute).Unix()
message["type"] = entities.EVENT_DEPLOY
event, err = postEvent(t, "header", message)
assert.Nil(t, err, "no error")
assert.True(t, event.Type == entities.EVENT_DEPLOY, "deploy event")
message["timestamp"] = time.Now().Add(-2 * time.Minute).Unix() //.Format(time.RFC3339)
event, err = postEvent(t, "header", message)
assert.True(t, event.Type == entities.EVENT_DEPLOY, "deploy event")
message["timestamp"] = time.Now().Unix() //.Format(time.RFC3339)
message["status"] = entities.STATUS_SUCCESS
event, err = postEvent(t, "header", message)
assert.True(t, event.Type == entities.EVENT_DEPLOY, "deploy event")
})
}
func postEvent(t *testing.T, authMethod string, message map[string]interface{}) (representations.Event, error) {
bytesRepresentation, _ := json.Marshal(message)
body := bytes.NewBuffer(bytesRepresentation)
var req *http.Request
if authMethod == "webhook" {
req, _ = http.NewRequest("POST", fmt.Sprintf("/webhook/%s/events", token), body)
} else {
req, _ = http.NewRequest("POST", "/events", body)
authenticate(req)
}
req.Header.Set("Content-Type", "application/json")
var result representations.Event
var err error = nil
testHTTPResponse(t, r, req, func(w *httptest.ResponseRecorder) bool {
statusOK := w.Code == http.StatusOK
err = json.NewDecoder(w.Body).Decode(&result)
pageOK := err == nil && result.Id != ""
return statusOK && pageOK
})
return result, err
}