Files
web/cms/cms_test.go
T
2026-09-10 18:00:25 -04:00

84 lines
2.6 KiB
Go

// SPDX-License-Identifier: MPL-2.0
package cms
import (
"encoding/json"
"strings"
"testing"
)
func TestAssociationsBoundsAndIdentity(t *testing.T) {
source := Reference{Kind: "project", ID: "project-one"}
valid := Associations{Terms: []string{"go"}, Links: []Reference{{Kind: "news", ID: "launch"}}}
if valid.Validate(source) != nil {
t.Fatal("valid association rejected")
}
for _, value := range []Associations{
{Terms: []string{"go", "go"}}, {Terms: []string{"../private"}},
{Links: []Reference{source}}, {Links: []Reference{{Kind: "news", ID: "launch"}, {Kind: "news", ID: "launch"}}},
{Links: []Reference{{Kind: "<script>", ID: "safe"}}},
{Terms: make([]string, MaxTerms+1)}, {Links: make([]Reference, MaxLinks+1)},
} {
if value.Validate(source) == nil {
t.Fatalf("accepted %#v", value)
}
}
}
func TestAssociationsAreOptional(t *testing.T) {
for _, source := range []Reference{{Kind: "news", ID: "article"}, {Kind: "project", ID: "project"}, {Kind: "policy", ID: "terms"}} {
for _, raw := range []string{`{}`, `{"terms":null,"links":null}`, `{"terms":[],"links":[]}`, `{"terms":["go"]}`, `{"links":[{"kind":"project","id":"other"}]}`} {
var value Associations
if err := json.Unmarshal([]byte(raw), &value); err != nil {
t.Fatal(err)
}
if err := value.Validate(source); err != nil {
t.Fatalf("optional associations rejected: %s: %v", raw, err)
}
}
}
}
func TestTaxonomyAndTermText(t *testing.T) {
tax := Taxonomy{ID: "categories", Slug: "categories", Name: "Categories", Revision: 1, Active: true}
if tax.Validate() != nil {
t.Fatal("valid taxonomy")
}
for _, name := range []string{"", " bad", "bad\nname", string([]byte{255}), strings.Repeat("a", 121)} {
v := tax
v.Name = name
if v.Validate() == nil {
t.Fatal("accepted invalid name")
}
}
term := Term{ID: "go", TaxonomyID: tax.ID, Slug: "go", Name: "Go", Revision: 1, Active: true}
if term.Validate() != nil {
t.Fatal("valid term")
}
term.Slug = "../go"
if term.Validate() == nil {
t.Fatal("unsafe slug")
}
}
func FuzzAssociations(f *testing.F) {
f.Add(`{"terms":["go"],"links":[{"kind":"news","id":"launch"}]}`)
f.Fuzz(func(t *testing.T, raw string) {
if len(raw) > 20000 {
return
}
var value Associations
if json.Unmarshal([]byte(raw), &value) != nil {
return
}
if value.Validate(Reference{Kind: "project", ID: "one"}) == nil {
b, e := json.Marshal(value)
if e != nil {
t.Fatal(e)
}
var again Associations
if json.Unmarshal(b, &again) != nil || again.Validate(Reference{Kind: "project", ID: "one"}) != nil {
t.Fatal("round trip")
}
}
})
}