diff --git a/main_test.go b/main_test.go index 8c339a5..5c83385 100644 --- a/main_test.go +++ b/main_test.go @@ -176,7 +176,14 @@ func assertNormalizedDocument(t *testing.T, response map[string]any) { parser, err := publiccodeParser.NewParser(publiccodeParser.ParserConfig{DisableExternalChecks: true}) require.NoError(t, err) parsed, err := parser.ParseStream(strings.NewReader(normalized)) - require.NoError(t, err, "normalized document must remain valid") + if err != nil { + var results publiccodeParser.ValidationResults + require.ErrorAs(t, err, &results) + for _, result := range results { + var validationError publiccodeParser.ValidationError + assert.False(t, errors.As(result, &validationError), "normalized document must remain valid: %v", result) + } + } document, ok := parsed.(publiccodeParser.PublicCodeV0) require.True(t, ok) @@ -192,10 +199,25 @@ func assertNormalizedDocument(t *testing.T, response map[string]any) { assert.Equal(t, []string{"Just one feature"}, *description.Features) } +func assertWarningOnlyResponse(t *testing.T, response map[string]any) { + t.Helper() + + assert.Equal(t, true, response["valid"]) + results, ok := response["results"].([]any) + require.True(t, ok) + require.Len(t, results, 1) + result, ok := results[0].(map[string]any) + require.True(t, ok) + assert.Equal(t, "warning", result["type"]) + assert.Equal(t, "publiccodeYmlVersion", result["key"]) + assertNormalizedDocument(t, response) +} + func TestValidateEndpoint(t *testing.T) { validYml := loadTestdata(t, "valid.publiccode.yml") invalidYml := loadTestdata(t, "invalid.publiccode.yml") malformedYml := loadTestdata(t, "malformed.publiccode.yml") + oldVersionYml := strings.Replace(validYml, `publiccodeYmlVersion: "0"`, `publiccodeYmlVersion: "0.2"`, 1) externalChecksYml := strings.Replace( validYml, "https://github.com/example/project.git", @@ -236,6 +258,14 @@ func TestValidateEndpoint(t *testing.T) { assertNormalizedDocument(t, response) }, }, + { + description: "validate with POST: file with only warnings", + query: "POST /v1/validate", + body: oldVersionYml, + expectedCode: 200, + expectedContentType: "application/json; charset=utf-8", + validateFunc: assertWarningOnlyResponse, + }, { description: "validate with POST: invalid file", query: "POST /v1/validate", @@ -294,6 +324,14 @@ func TestValidateEndpoint(t *testing.T) { assertNormalizedDocument(t, response) }, }, + { + description: "validate: file with only warnings", + query: "QUERY /v1/validate", + body: oldVersionYml, + expectedCode: 200, + expectedContentType: "application/json; charset=utf-8", + validateFunc: assertWarningOnlyResponse, + }, { description: "validate: external checks disabled", query: "QUERY /v1/validate",