Skip to content

Commit a91dc7f

Browse files
authored
test: add /v1/validate tests (#49)
Fix #41.
1 parent 3d2c4f3 commit a91dc7f

3 files changed

Lines changed: 156 additions & 0 deletions

File tree

main_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ func TestMain(m *testing.M) {
4646
os.Exit(code)
4747
}
4848

49+
func loadTestdata(t *testing.T, name string) string {
50+
t.Helper()
51+
b, err := os.ReadFile("testdata/" + name)
52+
if err != nil {
53+
t.Fatalf("cannot read testdata/%s: %v", name, err)
54+
}
55+
return string(b)
56+
}
57+
4958
func runTestCases(t *testing.T, tests []TestCase) {
5059
for _, test := range tests {
5160
description := test.description
@@ -117,6 +126,52 @@ func TestApi(t *testing.T) {
117126
runTestCases(t, tests)
118127
}
119128

129+
func TestValidateEndpoint(t *testing.T) {
130+
validYml := loadTestdata(t, "valid.publiccode.yml")
131+
invalidYml := loadTestdata(t, "invalid.publiccode.yml")
132+
133+
tests := []TestCase{
134+
{
135+
description: "validate: empty body",
136+
query: "QUERY /v1/validate",
137+
body: "",
138+
expectedCode: 400,
139+
expectedBody: `{"title":"empty body","detail":"need a body to validate","status":400}`,
140+
expectedContentType: "application/problem+json",
141+
},
142+
{
143+
description: "validate: valid file",
144+
query: "QUERY /v1/validate",
145+
body: validYml,
146+
expectedCode: 200,
147+
expectedContentType: "application/json; charset=utf-8",
148+
validateFunc: func(t *testing.T, response map[string]any) {
149+
assert.Equal(t, true, response["valid"])
150+
results, ok := response["results"].([]any)
151+
assert.True(t, ok)
152+
assert.Len(t, results, 0)
153+
assert.NotNil(t, response["normalized"])
154+
},
155+
},
156+
{
157+
description: "validate: invalid file",
158+
query: "QUERY /v1/validate",
159+
body: invalidYml,
160+
expectedCode: 200,
161+
expectedContentType: "application/json; charset=utf-8",
162+
validateFunc: func(t *testing.T, response map[string]any) {
163+
assert.Equal(t, false, response["valid"])
164+
results, ok := response["results"].([]any)
165+
assert.True(t, ok)
166+
assert.NotEmpty(t, results)
167+
assert.Nil(t, response["normalized"])
168+
},
169+
},
170+
}
171+
172+
runTestCases(t, tests)
173+
}
174+
120175
func TestStatusEndpoints(t *testing.T) {
121176
tests := []TestCase{
122177
{

testdata/invalid.publiccode.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
publiccodeYmlVersion: "0"
2+
3+
name: Medusa
4+
url: "https://github.com/italia/developers.italia.it.git"
5+
6+
platforms:
7+
- web
8+
9+
categories:
10+
- cloud-management
11+
12+
developmentStatus: development
13+
14+
# INVALID: bad softwareType
15+
softwareType: "XXX"
16+
17+
description:
18+
en-GB:
19+
localisedName: Medusa
20+
shortDescription: >
21+
A rather short description which
22+
is probably useless
23+
longDescription: >
24+
Very long description of this software, also split
25+
on multiple rows. You should note what the software
26+
is and why one should need it. This is 158 characters.
27+
Very long description of this software, also split
28+
on multiple rows. You should note what the software
29+
is and why one should need it. This is 316 characters.
30+
Very long description of this software, also split
31+
on multiple rows. You should note what the software
32+
is and why one should need it. This is 474 characters.
33+
Very long description of this software, also split
34+
on multiple rows. You should note what the software
35+
is and why one should need it. This is 632 characters.
36+
features:
37+
- Just one feature
38+
39+
legal:
40+
license: AGPL-3.0-or-later
41+
42+
maintenance:
43+
type: "community"
44+
45+
contacts:
46+
- name: Francesco Rossi
47+
48+
localisation:
49+
localisationReady: true
50+
availableLanguages:
51+
- en

testdata/valid.publiccode.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
publiccodeYmlVersion: "0"
2+
3+
name: Medusa
4+
url: "https://github.com/italia/developers.italia.it.git"
5+
6+
platforms:
7+
- web
8+
9+
categories:
10+
- cloud-management
11+
12+
developmentStatus: development
13+
14+
softwareType: "standalone/other"
15+
16+
description:
17+
en-GB:
18+
localisedName: Medusa
19+
shortDescription: >
20+
A rather short description which
21+
is probably useless
22+
longDescription: >
23+
Very long description of this software, also split
24+
on multiple rows. You should note what the software
25+
is and why one should need it. This is 158 characters.
26+
Very long description of this software, also split
27+
on multiple rows. You should note what the software
28+
is and why one should need it. This is 316 characters.
29+
Very long description of this software, also split
30+
on multiple rows. You should note what the software
31+
is and why one should need it. This is 474 characters.
32+
Very long description of this software, also split
33+
on multiple rows. You should note what the software
34+
is and why one should need it. This is 632 characters.
35+
features:
36+
- Just one feature
37+
38+
legal:
39+
license: AGPL-3.0-or-later
40+
41+
maintenance:
42+
type: "community"
43+
44+
contacts:
45+
- name: Francesco Rossi
46+
47+
localisation:
48+
localisationReady: true
49+
availableLanguages:
50+
- en

0 commit comments

Comments
 (0)