From fbc8bfe178b19b952c022c9d8640eb7deb614eda Mon Sep 17 00:00:00 2001 From: Fabio Bonelli Date: Wed, 25 Feb 2026 11:05:58 +0100 Subject: [PATCH] test: add /v1/validate tests Fix #41. --- main_test.go | 55 +++++++++++++++++++++++++++++++++ testdata/invalid.publiccode.yml | 51 ++++++++++++++++++++++++++++++ testdata/valid.publiccode.yml | 50 ++++++++++++++++++++++++++++++ 3 files changed, 156 insertions(+) create mode 100644 testdata/invalid.publiccode.yml create mode 100644 testdata/valid.publiccode.yml diff --git a/main_test.go b/main_test.go index 9212ede..4d049b0 100644 --- a/main_test.go +++ b/main_test.go @@ -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 @@ -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{ { diff --git a/testdata/invalid.publiccode.yml b/testdata/invalid.publiccode.yml new file mode 100644 index 0000000..63daa3c --- /dev/null +++ b/testdata/invalid.publiccode.yml @@ -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 diff --git a/testdata/valid.publiccode.yml b/testdata/valid.publiccode.yml new file mode 100644 index 0000000..712ebe3 --- /dev/null +++ b/testdata/valid.publiccode.yml @@ -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