Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ func TestMain(m *testing.M) {
os.Exit(code)
}

func loadTestdata(t *testing.T, name string) string {
t.Helper()
b, err := os.ReadFile("testdata/" + name)
if err != nil {
t.Fatalf("cannot read testdata/%s: %v", name, err)
}
return string(b)
}

func runTestCases(t *testing.T, tests []TestCase) {
for _, test := range tests {
description := test.description
Expand Down Expand Up @@ -117,6 +126,52 @@ func TestApi(t *testing.T) {
runTestCases(t, tests)
}

func TestValidateEndpoint(t *testing.T) {
validYml := loadTestdata(t, "valid.publiccode.yml")
invalidYml := loadTestdata(t, "invalid.publiccode.yml")

tests := []TestCase{
{
description: "validate: empty body",
query: "QUERY /v1/validate",
body: "",
expectedCode: 400,
expectedBody: `{"title":"empty body","detail":"need a body to validate","status":400}`,
expectedContentType: "application/problem+json",
},
{
description: "validate: valid file",
query: "QUERY /v1/validate",
body: validYml,
expectedCode: 200,
expectedContentType: "application/json; charset=utf-8",
validateFunc: func(t *testing.T, response map[string]any) {
assert.Equal(t, true, response["valid"])
results, ok := response["results"].([]any)
assert.True(t, ok)
assert.Len(t, results, 0)
assert.NotNil(t, response["normalized"])
},
},
{
description: "validate: invalid file",
query: "QUERY /v1/validate",
body: invalidYml,
expectedCode: 200,
expectedContentType: "application/json; charset=utf-8",
validateFunc: func(t *testing.T, response map[string]any) {
assert.Equal(t, false, response["valid"])
results, ok := response["results"].([]any)
assert.True(t, ok)
assert.NotEmpty(t, results)
assert.Nil(t, response["normalized"])
},
},
}

runTestCases(t, tests)
}

func TestStatusEndpoints(t *testing.T) {
tests := []TestCase{
{
Expand Down
51 changes: 51 additions & 0 deletions testdata/invalid.publiccode.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
publiccodeYmlVersion: "0"

name: Medusa
url: "https://github.com/italia/developers.italia.it.git"

platforms:
- web

categories:
- cloud-management

developmentStatus: development

# INVALID: bad softwareType
softwareType: "XXX"

description:
en-GB:
localisedName: Medusa
shortDescription: >
A rather short description which
is probably useless
longDescription: >
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 158 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 316 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 474 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 632 characters.
features:
- Just one feature

legal:
license: AGPL-3.0-or-later

maintenance:
type: "community"

contacts:
- name: Francesco Rossi

localisation:
localisationReady: true
availableLanguages:
- en
50 changes: 50 additions & 0 deletions testdata/valid.publiccode.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
publiccodeYmlVersion: "0"

name: Medusa
url: "https://github.com/italia/developers.italia.it.git"

platforms:
- web

categories:
- cloud-management

developmentStatus: development

softwareType: "standalone/other"

description:
en-GB:
localisedName: Medusa
shortDescription: >
A rather short description which
is probably useless
longDescription: >
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 158 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 316 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 474 characters.
Very long description of this software, also split
on multiple rows. You should note what the software
is and why one should need it. This is 632 characters.
features:
- Just one feature

legal:
license: AGPL-3.0-or-later

maintenance:
type: "community"

contacts:
- name: Francesco Rossi

localisation:
localisationReady: true
availableLanguages:
- en