Files
sm-journal-plugin/models/fillable_test.go
Jakub Zych f22d08902e feat(admin): edit category content in the admin form behind the HTML gate
- add a content textarea (size huge) after description, with en and pl phrases
- add content to Category.Fillable
- export classes.CheckHTML and run it from FormBeforeCreate and FormBeforeUpdate:
  unsafe category content answers 422 on content, blank content is stored as NULL
2026-10-09 16:26:12 +02:00

112 lines
3.3 KiB
Go

package models_test
import (
"reflect"
"testing"
"git.golem15.com/golem15/sm-journal-plugin/models"
"git.golem15.com/golem15/summercms/modules/lagoon"
)
// TestFillable is JOURNAL-001/002 and T-15-04: Tag allow-list is name/slug/
// description only; Category excludes nest_*; extra JSON keys (including
// redactor_id and user_id on Post) are dropped by lagoon.Fill.
func TestFillable(t *testing.T) {
t.Run("tag_allow_list", func(t *testing.T) {
if got := (models.Tag{}).Fillable(); !reflect.DeepEqual(got, []string{"name", "slug", "description"}) {
t.Fatalf("Tag.Fillable = %#v", got)
}
tag := models.Tag{}
if err := lagoon.Fill(&tag, tag.Fillable(), map[string]any{
"name": "Notes",
"slug": "notes",
"description": "field notes",
"id": 99,
"nest_left": 1,
"created_at": "2020-01-01",
}, true); err != nil {
t.Fatal(err)
}
if tag.Name != "Notes" || tag.Slug != "notes" {
t.Fatalf("tag fill = %+v", tag)
}
if tag.ID != 0 {
t.Fatalf("id was mass-assigned: %d", tag.ID)
}
if tag.Description == nil || *tag.Description != "field notes" {
t.Fatalf("description = %#v", tag.Description)
}
})
t.Run("category_excludes_nest", func(t *testing.T) {
if got := (models.Category{}).Fillable(); !reflect.DeepEqual(got, []string{"name", "slug", "code", "description", "content", "parent_id"}) {
t.Fatalf("Category.Fillable = %#v", got)
}
left := 3
cat := models.Category{NestLeft: &left}
if err := lagoon.Fill(&cat, cat.Fillable(), map[string]any{
"name": "Essays",
"slug": "essays",
"code": "ess",
"content": "<p>Essay hub</p>",
"nest_left": 9,
"nest_right": 10,
"nest_depth": 2,
"id": 7,
}, true); err != nil {
t.Fatal(err)
}
if cat.Name != "Essays" || cat.Slug != "essays" {
t.Fatalf("category fill = %+v", cat)
}
if cat.Content == nil || *cat.Content != "<p>Essay hub</p>" {
t.Fatalf("category content fill = %#v", cat.Content)
}
if cat.ID != 0 {
t.Fatalf("id was mass-assigned: %d", cat.ID)
}
if cat.NestLeft == nil || *cat.NestLeft != 3 {
t.Fatalf("nest_left mutated: %#v", cat.NestLeft)
}
if cat.NestRight != nil || cat.NestDepth != nil {
t.Fatalf("nest_* assigned: right=%v depth=%v", cat.NestRight, cat.NestDepth)
}
})
t.Run("post_redactor_and_user_not_fillable", func(t *testing.T) {
post := models.Post{}
for _, name := range post.Fillable() {
if name == "redactor_id" || name == "user_id" || name == "content_html" {
t.Fatalf("Post.Fillable includes %q: %#v", name, post.Fillable())
}
}
rid := uint(99)
uid := uint(7)
post.RedactorID = &rid
post.UserID = &uid
if err := lagoon.Fill(&post, post.Fillable(), map[string]any{
"title": "Hello",
"slug": "hello",
"excerpt": "ex",
"content": "body",
"redactor_id": 1,
"user_id": 2,
"content_html": "<script>x</script>",
}, true); err != nil {
t.Fatal(err)
}
if post.Title != "Hello" || post.Slug != "hello" {
t.Fatalf("post fill = %+v", post)
}
if post.RedactorID == nil || *post.RedactorID != 99 {
t.Fatalf("redactor_id was filled: %#v", post.RedactorID)
}
if post.UserID == nil || *post.UserID != 7 {
t.Fatalf("user_id was filled: %#v", post.UserID)
}
if post.ContentHTML != "" {
t.Fatalf("content_html was filled: %q", post.ContentHTML)
}
})
}