-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_test.go
374 lines (326 loc) · 11 KB
/
main_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
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"os"
"sort"
"strconv"
"strings"
"testing"
"github.com/italia/publiccode-validator/utils"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
var app App
type Msg utils.Message
func (msg Msg) Len() int { return len(msg.ValidationError) }
func (msg Msg) Swap(i, j int) {
msg.ValidationError[i], msg.ValidationError[j] = msg.ValidationError[j], msg.ValidationError[i]
}
func (msg Msg) Less(i, j int) bool { return msg.ValidationError[i].Key < msg.ValidationError[j].Key }
type Es []utils.ErrorInvalidValue
func (a Es) Len() int { return len(a) }
func (a Es) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a Es) Less(i, j int) bool { return a[i].Key < a[j].Key }
func TestMain(m *testing.M) {
app = App{}
app.initializeRouters()
code := m.Run()
os.Exit(code)
}
func TestGitHub(t *testing.T) {
url, _ := url.Parse("https://github.com/italia/publiccode-validator")
assert.Equal(t, utils.GetRawURL(url), "https://raw.githubusercontent.com/italia/publiccode-validator/master/")
url, _ = url.Parse("https://github.com/italia/publiccode-validator.git")
assert.Equal(t, utils.GetRawURL(url), "https://raw.githubusercontent.com/italia/publiccode-validator/master/")
}
func TestConversion(t *testing.T) {
fileYML, err := os.Open("tests/valid.minimal.yml") // For read access.
fileJSON, err := os.Open("tests/valid.minimal.json") // For read access.
if err != nil {
log.Fatal(err)
}
yml, err := ioutil.ReadAll(fileYML)
json, err := ioutil.ReadAll(fileJSON)
assert.Equal(t, string(utils.Yaml2json(yml)), strings.TrimSpace(string(json)))
}
func TestValidationZeroPayload(t *testing.T) {
req, _ := http.NewRequest("POST", "/pc/validate?disableNetwork=true", nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
if body := response.Body.String(); body != "" {
t.Errorf("Expected an message. Got %s", body)
}
}
func TestValidationErrWithNetwork(t *testing.T) {
log.SetLevel(log.DebugLevel)
var errs []utils.ErrorInvalidValue //[]map[string]interface{}
var errOut []utils.ErrorInvalidValue //[]map[string]interface{}
fileYML, err := os.Open("tests/invalid.yml")
if err != nil {
log.Fatal(err)
}
out, err := ioutil.ReadFile("tests/out_invalid.json")
if err != nil {
log.Fatal(err)
}
req, _ := http.NewRequest("POST", "/pc/validate?disableNetwork=false", fileYML)
response := executeRequest(req)
err = json.Unmarshal(out, &errOut)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(response.Body.Bytes(), &errs)
if err != nil {
log.Fatal(err)
}
sort.Sort(Es(errs))
sort.Sort(Es(errOut))
log.Println(errs)
assert.Equal(t, errs, errOut)
}
func TestValidationWithNoNetwork(t *testing.T) {
checks := []bool{true, false}
for _, check := range checks {
fileYML, err := os.Open("tests/valid.minimal.yml")
if err != nil {
log.Fatal(err)
}
out, err := ioutil.ReadFile("tests/out_valid.minimal.yml")
if err != nil {
log.Fatal(err)
}
req, _ := http.NewRequest("POST", "/pc/validate?disableNetwork="+strconv.FormatBool(check), fileYML)
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
assert.Equal(t, string(out), response.Body.String())
}
}
func TestValidationWithNetwork(t *testing.T) {
fileYML, err := os.Open("tests/valid.minimal.yml")
if err != nil {
log.Fatal(err)
}
out, err := ioutil.ReadFile("tests/out_valid.minimal.yml")
if err != nil {
log.Fatal(err)
}
req, _ := http.NewRequest("POST", "/pc/validate", fileYML)
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
assert.Equal(t, string(out), response.Body.String())
}
func TestInvalidRemoteURL(t *testing.T) {
log.SetLevel(log.InfoLevel)
//url invalid
urlString := "https://raw.githubusercontent.com/italia/404-publiccode-validator/master/tests/invalid.yml"
req, _ := http.NewRequest("POST", "/pc/validateURL?url="+urlString, nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusBadRequest, response.Code)
//url not present
urlString = ""
req, _ = http.NewRequest("POST", "/pc/validateURL?url="+urlString, nil)
response = executeRequest(req)
checkResponseCode(t, http.StatusNotFound, response.Code)
// invalid
out, err := ioutil.ReadFile("tests/out_invalid_network.json")
if err != nil {
log.Fatal(err)
}
urlString = "https://raw.githubusercontent.com/italia/publiccode-validator/master/tests/invalid.yml"
req, _ = http.NewRequest("POST", "/pc/validateURL?url="+urlString, nil)
response = executeRequest(req)
checkResponseCode(t, http.StatusUnprocessableEntity, response.Code)
var resMessage utils.Message
json.Unmarshal(response.Body.Bytes(), &resMessage)
var outMessage utils.Message
json.Unmarshal(out, &outMessage)
resOut := resMessage.ValidationError
localOut := outMessage.ValidationError
sort.Slice(resOut, func(i, j int) bool {
return resOut[i].Key < resOut[j].Key
})
sort.Slice(localOut, func(i, j int) bool {
return localOut[i].Key < localOut[j].Key
})
assert.Equal(t, localOut, resOut)
}
func TestValidationRemoteURL(t *testing.T) {
log.SetLevel(log.DebugLevel)
out, err := ioutil.ReadFile("tests/out_valid.minimal.yml")
if err != nil {
log.Fatal(err)
}
urlString := "https://raw.githubusercontent.com/italia/publiccode-validator/master/tests/valid.minimal.yml"
req, _ := http.NewRequest("POST", "/pc/validateURL?url="+urlString, nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
assert.Equal(t, string(out), response.Body.String())
}
// API v1
func TestEmptyPayloadv1(t *testing.T) {
log.SetLevel(log.DebugLevel)
out, err := ioutil.ReadFile("tests/out_empty_payload.yml")
if err != nil {
log.Fatal(err)
}
req, _ := http.NewRequest("POST", "/api/v1/validate", nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusBadRequest, response.Code)
assert.Equal(t, string(out), response.Body.String())
out, err = ioutil.ReadFile("tests/out_empty_payload.json")
if err != nil {
log.Fatal(err)
}
req, _ = http.NewRequest("POST", "/api/v1/validate", nil)
req.Header.Set("Accept", "application/json")
response = executeRequest(req)
checkResponseCode(t, http.StatusBadRequest, response.Code)
assert.Equal(t, string(out), response.Body.String())
}
func TestEmptyURLv1(t *testing.T) {
log.SetLevel(log.DebugLevel)
out, err := ioutil.ReadFile("tests/out_empty_url.yml")
if err != nil {
log.Fatal(err)
}
req, _ := http.NewRequest("POST", "/api/v1/validateURL?url=", nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusNotFound, response.Code)
assert.Equal(t, string(out), response.Body.String())
out, err = ioutil.ReadFile("tests/out_empty_url.json")
if err != nil {
log.Fatal(err)
}
req, _ = http.NewRequest("POST", "/api/v1/validateURL?url=", nil)
req.Header.Set("Accept", "application/json")
response = executeRequest(req)
checkResponseCode(t, http.StatusNotFound, response.Code)
assert.Equal(t, string(out), response.Body.String())
}
func TestValidationErrWithNetworkv1(t *testing.T) {
log.SetLevel(log.InfoLevel)
var errs utils.Message
var errOut utils.Message
fileYML, err := os.Open("tests/invalid.yml")
if err != nil {
log.Fatal(err)
}
out, err := ioutil.ReadFile("tests/out_invalid_v1.json")
if err != nil {
log.Fatal(err)
}
req, _ := http.NewRequest("POST", "/api/v1/validate?disableNetwork=false", fileYML)
req.Header.Set("Accept", "application/json")
response := executeRequest(req)
err = json.Unmarshal(out, &errOut)
if err != nil {
log.Fatal(err)
}
log.Info(response.Body.String())
err = json.Unmarshal(response.Body.Bytes(), &errs)
if err != nil {
log.Fatal(err)
}
sort.Sort(Msg(errs))
sort.Sort(Msg(errOut))
log.Println(errs)
assert.Equal(t, errs, errOut)
}
func TestValidationWithNoNetworkv1(t *testing.T) {
checks := []bool{true, false}
for _, check := range checks {
fileYML, err := os.Open("tests/valid.minimal.yml")
if err != nil {
log.Fatal(err)
}
out, err := ioutil.ReadFile("tests/out_valid.minimal.yml")
if err != nil {
log.Fatal(err)
}
req, _ := http.NewRequest("POST", "/api/v1/validate?disableNetwork="+strconv.FormatBool(check), fileYML)
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
assert.Equal(t, string(out), response.Body.String())
}
}
func TestValidationWithNetworkv1(t *testing.T) {
fileYML, err := os.Open("tests/valid.minimal.yml")
if err != nil {
log.Fatal(err)
}
out, err := ioutil.ReadFile("tests/out_valid.minimal.yml")
if err != nil {
log.Fatal(err)
}
req, _ := http.NewRequest("POST", "/api/v1/validate", fileYML)
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
assert.Equal(t, string(out), response.Body.String())
}
func TestInvalidRemoteURLv1(t *testing.T) {
log.SetLevel(log.InfoLevel)
//url invalid
urlString := "https://raw.githubusercontent.com/italia/404-publiccode-validator/master/tests/invalid.yml"
req, _ := http.NewRequest("POST", "/api/v1/validateURL?url="+urlString, nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusBadRequest, response.Code)
//url not present
urlString = ""
req, _ = http.NewRequest("POST", "/api/v1/validateURL?url="+urlString, nil)
response = executeRequest(req)
checkResponseCode(t, http.StatusNotFound, response.Code)
// invalid
out, err := ioutil.ReadFile("tests/out_invalid_network_v1.json")
if err != nil {
log.Fatal(err)
}
urlString = "https://raw.githubusercontent.com/italia/publiccode-validator/master/tests/invalid.yml"
req, _ = http.NewRequest("POST", "/api/v1/validateURL?url="+urlString, nil)
req.Header.Set("Accept", "application/json")
response = executeRequest(req)
checkResponseCode(t, http.StatusUnprocessableEntity, response.Code)
var resMessage utils.Message
err = json.Unmarshal(response.Body.Bytes(), &resMessage)
if err != nil {
log.Fatal(err)
}
var outMessage utils.Message
json.Unmarshal(out, &outMessage)
log.Info(response.Body.String())
resOut := resMessage.ValidationError
localOut := outMessage.ValidationError
sort.Slice(resOut, func(i, j int) bool {
return resOut[i].Key < resOut[j].Key
})
sort.Slice(localOut, func(i, j int) bool {
return localOut[i].Key < localOut[j].Key
})
assert.Equal(t, localOut, resOut)
}
func TestValidationRemoteURLv1(t *testing.T) {
log.SetLevel(log.DebugLevel)
out, err := ioutil.ReadFile("tests/out_valid.minimal.yml")
if err != nil {
log.Fatal(err)
}
urlString := "https://raw.githubusercontent.com/italia/publiccode-validator/master/tests/valid.minimal.yml"
req, _ := http.NewRequest("POST", "/api/v1/validateURL?url="+urlString, nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
assert.Equal(t, string(out), response.Body.String())
}
// Utility functions to make mock request and check response
func executeRequest(req *http.Request) *httptest.ResponseRecorder {
rr := httptest.NewRecorder()
app.Router.ServeHTTP(rr, req)
return rr
}
func checkResponseCode(t *testing.T, expected, actual int) {
if expected != actual {
t.Errorf("Expected response code %d. Got %d\n", expected, actual)
}
}