Add revision-aware CMS taxonomies and relationships
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
// 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 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")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user