Skip to content
Merged
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
40 changes: 39 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
Loading