-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresults_test.go
433 lines (375 loc) · 10.5 KB
/
results_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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
Copyright 2019 Adevinta
*/
package api
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/goadesign/goa"
"github.com/goadesign/goa/goatest"
uuid "github.com/gofrs/uuid"
"github.com/adevinta/vulcan-results/app"
"github.com/adevinta/vulcan-results/app/test"
"github.com/adevinta/vulcan-results/storage"
)
var base64report = "{}"
var plainReport = `{ "vulnerabilities" : [ {} ] }`
var scanID = uuid.FromStringOrNil("e0c1ac1a-1036-4e0e-b5cc-d18ae6673eb0")
var checkID = uuid.FromStringOrNil("e0c1ac1a-1036-4e0e-b5cc-d18ae6673eb0")
var scanStartTime = time.Now()
var base64raw = "e30="
var testCasesReport = []struct {
name string
skip, skipAlways bool
payload *app.ReportPayload
stMock storage.Storage
psMock http.HandlerFunc
psURL string
f funcTestReport
}{
{
name: "positive case",
payload: &app.ReportPayload{
Report: &plainReport,
ScanID: &scanID,
CheckID: &checkID,
ScanStartTime: &scanStartTime,
},
stMock: storageMock{
//link: "https://s3-eu-west-1.amazonaws.com/vulcan-results/e0c1ac1a-1036-4e0e-b5cc-d18ae6673eb0/report.json",
link: "https://s3-eu-west-1.amazonaws.com/vulcan-core-reports-dev/dt=2017-09-01/scan=e0c1ac1a-1036-4e0e-b5cc-d18ae6673eb0/e0c1ac1a-1036-4e0e-b5cc-d18ae6673eb0.json",
err: nil,
},
psMock: ok,
f: test.ReportResultsCreated,
},
}
var testCasesRaw = []struct {
name string
checkID uuid.UUID
skip, skipAlways bool
payload *app.RawPayload
stMock storage.Storage
psMock http.HandlerFunc
psURL string
f funcTestRaw
}{
{
name: "positive case",
payload: &app.RawPayload{
Raw: &base64raw,
ScanID: &scanID,
CheckID: &checkID,
ScanStartTime: &scanStartTime,
},
stMock: storageMock{
link: "https://s3-eu-west-1.amazonaws.com/vulcan-results/e0c1ac1a-1036-4e0e-b5cc-d18ae6673eb0/raw.json",
err: nil,
},
psMock: ok,
f: test.RawResultsCreated,
},
}
type funcTestReport func(goatest.TInterface, context.Context, *goa.Service, app.ResultsController, *app.ReportPayload) http.ResponseWriter
type funcTestRaw func(goatest.TInterface, context.Context, *goa.Service, app.ResultsController, *app.RawPayload) http.ResponseWriter
type funcTestGetReport func(goatest.TInterface, context.Context, *goa.Service, app.ResultsController, string, string, string) http.ResponseWriter
type funcTestGetLog func(goatest.TInterface, context.Context, *goa.Service, app.ResultsController, string, string, string) http.ResponseWriter
type storageMock struct {
link string
report []byte
log []byte
err error
}
func (st storageMock) SaveLogs(checkID, scanID string, startedAt time.Time, raw []byte) (link string, err error) {
return st.link, st.err
}
func (st storageMock) SaveReports(checkID, scanID string, startedAt time.Time, result []byte, compress bool) (link string, err error) {
return st.link, st.err
}
func (st storageMock) GetReport(date, scanID, checkID string) ([]byte, error) {
return st.report, st.err
}
func (st storageMock) GetLog(date, scanID, checkID string) ([]byte, error) {
return st.log, st.err
}
func ok(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
dec := json.NewDecoder(r.Body)
var v map[string]interface{}
if err := dec.Decode(&v); err != nil {
http.Error(w, "error decoding payload", 500)
return
}
if len(v) != 1 {
http.Error(w, "more fields than one in the payload", 500)
return
}
raw, ok := v["raw"]
if !ok || raw != "https://s4-eu-west-1.amazonaws.com/vulcan-results/e0c1ac1a-1036-4e0e-b5cc-d18ae6673eb0/raw.json" {
http.Error(w, "incorrect payload content", 500)
return
}
}
func okIfStatus(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
dec := json.NewDecoder(r.Body)
var v map[string]interface{}
if err := dec.Decode(&v); err != nil {
http.Error(w, "error decoding payload", 500)
return
}
if len(v) != 1 {
http.Error(w, "more fields than one in the payload", 500)
return
}
status, ok := v["status"]
if !ok || status != "FAILED" {
http.Error(w, "incorrect payload content", 500)
return
}
}
func forbidden(w http.ResponseWriter, r *http.Request) {
http.Error(w, "", 403)
}
func TestReport(t *testing.T) {
// Test all the test cases defined in testCasesReport
for _, tc := range testCasesReport {
tc := tc
t.Run(tc.name, func(t *testing.T) {
if testing.Short() && tc.skip || tc.skipAlways {
t.SkipNow()
}
url := tc.psURL
if url == "" {
ts := httptest.NewServer(tc.psMock)
defer ts.Close()
url = ts.URL
}
service := goa.New("vulcan-results")
ctrl := NewResultsController(service, tc.stMock)
_ = tc.f(t, nil, service, ctrl, tc.payload)
})
}
}
func TestRaw(t *testing.T) {
// Test all the test cases defined in testCasesRaw
for _, tc := range testCasesRaw {
tc := tc
t.Run(tc.name, func(t *testing.T) {
if testing.Short() && tc.skip || tc.skipAlways {
t.SkipNow()
}
url := tc.psURL
if url == "" {
ts := httptest.NewServer(tc.psMock)
defer ts.Close()
url = ts.URL
}
service := goa.New("vulcan-results")
ctrl := NewResultsController(service, tc.stMock)
_ = tc.f(t, nil, service, ctrl, tc.payload)
})
}
}
var testCasesSaveResult = []struct {
name string
skip, skipAlways bool
checkID string
kind string
result string
stMock storage.Storage
psMock http.HandlerFunc
psURL string
nilErr bool
}{
{
name: "positive case",
checkID: "e0c1ac1a-1036-4e0e-b5cc-d18ae6673eb0",
kind: "raw",
result: "RAW_RESULT_CONTENT",
stMock: storageMock{
link: "https://s4-eu-west-1.amazonaws.com/vulcan-results/e0c1ac1a-1036-4e0e-b5cc-d18ae6673eb0/raw.json",
err: nil,
},
psMock: ok,
nilErr: true,
},
{
name: "s3 error",
checkID: "e0c1ac1a-1036-4e0e-b5cc-d18ae6673eb0",
kind: "raw",
result: "RAW_RESULT_CONTENT",
stMock: storageMock{
link: "",
err: errors.New("error storing in S3"),
},
psMock: okIfStatus,
nilErr: true,
},
{
name: "malformed persistence url",
checkID: "e0c1ac1a-1036-4e0e-b5cc-d18ae6673eb0",
kind: "raw",
result: "RAW_RESULT_CONTENT",
stMock: storageMock{
link: "https://s3-eu-west-1.amazonaws.com/vulcan-results/e0c1ac1a-1036-4e0e-b5cc-d18ae6673eb0/report.json",
err: nil,
},
psMock: ok,
psURL: "//fwefew//fewwf///.../",
nilErr: false,
},
{
name: "persistence service answers different from http.StatusOK",
checkID: "e0c1ac1a-1036-4e0e-b5cc-d18ae6673eb0",
kind: "raw",
result: "RAW_RESULT_CONTENT",
stMock: storageMock{
link: "https://s3-eu-west-1.amazonaws.com/vulcan-results/e0c1ac1a-1036-4e0e-b5cc-d18ae6673eb0/report.json",
err: nil,
},
psMock: forbidden,
nilErr: false,
},
}
func TestSaveResult(t *testing.T) {
// Test all the test cases defined in testCasesSaveResult
for _, tc := range testCasesSaveResult {
tc := tc
t.Run(tc.name, func(t *testing.T) {
if testing.Short() && tc.skip || tc.skipAlways {
t.SkipNow()
}
url := tc.psURL
if url == "" {
ts := httptest.NewServer(tc.psMock)
defer ts.Close()
url = ts.URL
}
service := goa.New("vulcan-results")
ctrl := NewResultsController(service, tc.stMock)
goaCtx := goa.NewContext(goa.WithAction(context.Background(), "ResultsTest"), nil, nil, nil)
ctx, _ := app.NewReportResultsContext(goaCtx, nil, service)
ctx.Payload = &app.ReportPayload{
CheckID: &checkID,
ScanID: &scanID,
ScanStartTime: &scanStartTime,
Report: &plainReport,
}
_, err := ctrl.saveReportToS3(ctx)
if (tc.nilErr && err != nil) || (!tc.nilErr && err == nil) {
//TODO: fix this test
//t.Errorf("(%v) nilErr expected: %v, got error: %v", tc.name, tc.nilErr, err)
}
})
}
}
var testCasesGetReport = []struct {
name string
date string
scan string
check string
stMock storage.Storage
psMock http.HandlerFunc
psURL string
f funcTestGetReport
}{
{
name: "Happy path OK",
date: "dt=2019-11-01",
scan: "scan=9126034c-7caf-4acd-93f3-bee1941aa140",
check: "e0c1ac1a-1036-4e0e-b5cc-d18ae6673eb0.json",
stMock: storageMock{
report: []byte("myReport"),
err: nil,
},
psMock: func(w http.ResponseWriter, r *http.Request) {},
f: test.GetReportResultsOK,
},
{
name: "Should return bad request",
date: "dt=2019-11-01",
scan: "scan=9126034c-7caf-4acd-93f3-bee1941aa140",
check: "e0c1ac1a-1036-4e0e-b5cc-d18ae6673eb0.json",
stMock: storageMock{
report: []byte("myReport"),
err: errors.New("Error"),
},
psMock: func(w http.ResponseWriter, r *http.Request) {},
f: test.GetReportResultsBadRequest,
},
}
func TestGetReport(t *testing.T) {
// Test all the test cases defined in testCasesGetReport
for _, tc := range testCasesGetReport {
tc := tc
t.Run(tc.name, func(t *testing.T) {
url := tc.psURL
if url == "" {
ts := httptest.NewServer(tc.psMock)
defer ts.Close()
url = ts.URL
}
service := goa.New("vulcan-results")
ctrl := NewResultsController(service, tc.stMock)
_ = tc.f(t, nil, service, ctrl, tc.date, tc.scan, tc.check)
})
}
}
var testCasesGetLog = []struct {
name string
date string
scan string
check string
stMock storage.Storage
psMock http.HandlerFunc
psURL string
f funcTestGetLog
}{
{
name: "Happy path OK",
date: "dt=2019-11-01",
scan: "scan=9126034c-7caf-4acd-93f3-bee1941aa140",
check: "e0c1ac1a-1036-4e0e-b5cc-d18ae6673eb0.json",
stMock: storageMock{
report: []byte("myLog"),
err: nil,
},
psMock: func(w http.ResponseWriter, r *http.Request) {},
f: test.GetLogResultsOK,
},
{
name: "Should return bad request",
date: "dt=2019-11-01",
scan: "scan=9126034c-7caf-4acd-93f3-bee1941aa140",
check: "e0c1ac1a-1036-4e0e-b5cc-d18ae6673eb0.json",
stMock: storageMock{
report: []byte("myLog"),
err: errors.New("Error"),
},
psMock: func(w http.ResponseWriter, r *http.Request) {},
f: test.GetLogResultsBadRequest,
},
}
func TestGetLog(t *testing.T) {
// Test all the test cases defined in testCasesGetLog
for _, tc := range testCasesGetLog {
tc := tc
t.Run(tc.name, func(t *testing.T) {
url := tc.psURL
if url == "" {
ts := httptest.NewServer(tc.psMock)
defer ts.Close()
url = ts.URL
}
service := goa.New("vulcan-results")
ctrl := NewResultsController(service, tc.stMock)
_ = tc.f(t, nil, service, ctrl, tc.date, tc.scan, tc.check)
})
}
}