feat(journal): add category show/update API routes and MCP tools
This commit is contained in:
@@ -1,62 +1,126 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sort"
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.golem15.com/golem15/sm-journal-plugin/classes"
|
||||
"git.golem15.com/golem15/sm-journal-plugin/models"
|
||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||
"git.golem15.com/golem15/summercms/modules/bouncer"
|
||||
)
|
||||
|
||||
func sortedCategoryKeys(row map[string]any) []string {
|
||||
keys := make([]string, 0, len(row))
|
||||
for k := range row {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
return keys
|
||||
func TestUpdateCategoryAuthAndPermissions(t *testing.T) {
|
||||
app := backpack.New(nil)
|
||||
handler := UpdateCategory(app)
|
||||
|
||||
t.Run("unauthenticated returns 401", func(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodPut, "/_journal/api/v1/categories/1", strings.NewReader(`{"name":"News"}`))
|
||||
req.SetPathValue("id", "1")
|
||||
rec := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("status = %d, want 401; body = %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("dispatched backend principal without access_categories returns 403", func(t *testing.T) {
|
||||
pr := &bouncer.Principal{
|
||||
ID: 7,
|
||||
Backend: true,
|
||||
PermissionGrants: map[string]bool{permAccessPosts: true},
|
||||
}
|
||||
req := httptest.NewRequest(http.MethodPut, "/_journal/api/v1/categories/1", strings.NewReader(`{"name":"News"}`))
|
||||
req.SetPathValue("id", "1")
|
||||
req = req.WithContext(bouncer.WithDispatch(req.Context(), &bouncer.Dispatch{
|
||||
Principal: pr,
|
||||
Guards: []string{"backend"},
|
||||
}))
|
||||
rec := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusForbidden {
|
||||
t.Fatalf("status = %d, want 403; body = %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestSerializeCategoryNodeContent(t *testing.T) {
|
||||
html := "<h2>Acme</h2>\n<p>Pillar copy</p>\n"
|
||||
var parentID uint = 1
|
||||
root := models.Category{ID: 1, Name: "News", Slug: "news", Content: &html}
|
||||
child := models.Category{ID: 2, Name: "Quiet", Slug: "quiet", ParentID: &parentID}
|
||||
byParent := map[uint][]models.Category{1: {child}}
|
||||
counts := map[uint]int64{1: 2}
|
||||
|
||||
row := serializeCategoryNode(&root, byParent, counts, true, true)
|
||||
if row == nil {
|
||||
t.Fatal("root row is nil")
|
||||
func TestApplyCategoryUpdateSanitizationAndValidation(t *testing.T) {
|
||||
app := backpack.New(nil)
|
||||
reg := classes.NewEmbedRegistry()
|
||||
if err := reg.Register(classes.EmbedProvider{
|
||||
Key: "viz",
|
||||
Name: "Chart",
|
||||
IDPattern: `^[a-z0-9-]+$`,
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, ok := row["content"].(string); !ok || got != html {
|
||||
t.Fatalf("root content = %#v, want %q", row["content"], html)
|
||||
}
|
||||
wantRoot := []string{"children", "content", "content_h1_to_h2", "description", "id", "name", "nest_depth", "parent_id", "post_count", "slug"}
|
||||
if got := sortedCategoryKeys(row); !reflect.DeepEqual(got, wantRoot) {
|
||||
t.Fatalf("root keys = %v, want %v", got, wantRoot)
|
||||
}
|
||||
kids, ok := row["children"].([]map[string]any)
|
||||
if !ok || len(kids) != 1 {
|
||||
t.Fatalf("children = %#v, want one row", row["children"])
|
||||
}
|
||||
v, present := kids[0]["content"]
|
||||
if !present || v != nil {
|
||||
t.Fatalf("child content = %#v (present %v), want nil", v, present)
|
||||
}
|
||||
wantChild := []string{"content", "content_h1_to_h2", "description", "id", "name", "nest_depth", "parent_id", "post_count", "slug"}
|
||||
if got := sortedCategoryKeys(kids[0]); !reflect.DeepEqual(got, wantChild) {
|
||||
t.Fatalf("child keys = %v, want %v", got, wantChild)
|
||||
if err := app.Publish(reg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
listed := serializeCategoryNode(&root, byParent, counts, false, true)
|
||||
if listed == nil {
|
||||
t.Fatal("root omitted without include_empty")
|
||||
}
|
||||
if _, has := listed["children"]; has {
|
||||
t.Fatalf("empty child listed without include_empty: %#v", listed["children"])
|
||||
}
|
||||
if got, ok := listed["content"].(string); !ok || got != html {
|
||||
t.Fatalf("root content without include_empty = %#v, want %q", listed["content"], html)
|
||||
}
|
||||
t.Run("sanitizes HTML content with app EmbedRegistry and strips XSS", func(t *testing.T) {
|
||||
cat := &models.Category{ID: 4, Name: "Old", Slug: "old"}
|
||||
before := snapshotCategory(cat)
|
||||
verrs := applyCategoryUpdate(nil, app, cat, map[string]any{
|
||||
"name": "New Name",
|
||||
"description": " Short summary ",
|
||||
"content": `<h2>Intro</h2><script>alert(1)</script><figure data-embed="viz" data-id="chart-1"><img src="/media/abc/c.webp" alt="Chart"></figure>`,
|
||||
})
|
||||
if verrs != nil {
|
||||
t.Fatalf("unexpected validation errors: %v", verrs)
|
||||
}
|
||||
if cat.Name != "New Name" {
|
||||
t.Fatalf("Name = %q, want New Name", cat.Name)
|
||||
}
|
||||
if cat.Description == nil || *cat.Description != "Short summary" {
|
||||
t.Fatalf("Description = %v, want Short summary", cat.Description)
|
||||
}
|
||||
if cat.Content == nil {
|
||||
t.Fatal("Content is nil")
|
||||
}
|
||||
if strings.Contains(*cat.Content, "<script") {
|
||||
t.Fatalf("XSS survived sanitization: %q", *cat.Content)
|
||||
}
|
||||
if !strings.Contains(*cat.Content, `data-embed="viz"`) || !strings.Contains(*cat.Content, `data-id="chart-1"`) {
|
||||
t.Fatalf("registered custom embed lost: %q", *cat.Content)
|
||||
}
|
||||
|
||||
ev := categorySavedEvent(context.Background(), before, cat)
|
||||
if ev.Model != cat.MorphName() || ev.ID != "4" || ev.Created {
|
||||
t.Fatalf("unexpected event header: %+v", ev)
|
||||
}
|
||||
if len(ev.Changes) != 3 {
|
||||
t.Fatalf("expected 3 changed attributes (name, description, content), got %+v", ev.Changes)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("blank or null content and description clear fields to nil", func(t *testing.T) {
|
||||
desc := "Old desc"
|
||||
body := "<p>Old body</p>"
|
||||
cat := &models.Category{ID: 4, Name: "News", Slug: "news", Description: &desc, Content: &body}
|
||||
verrs := applyCategoryUpdate(nil, app, cat, map[string]any{
|
||||
"description": " ",
|
||||
"content": " \n\t ",
|
||||
})
|
||||
if verrs != nil {
|
||||
t.Fatalf("unexpected validation errors: %v", verrs)
|
||||
}
|
||||
if cat.Description != nil || cat.Content != nil {
|
||||
t.Fatalf("expected nil description & content, got desc=%v content=%v", cat.Description, cat.Content)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("rejects empty name and invalid slug format", func(t *testing.T) {
|
||||
cat := &models.Category{ID: 4, Name: "News", Slug: "news"}
|
||||
verrs := applyCategoryUpdate(nil, app, cat, map[string]any{
|
||||
"name": " ",
|
||||
"slug": "bad slug!",
|
||||
})
|
||||
if verrs == nil || len(verrs["name"]) == 0 || len(verrs["slug"]) == 0 {
|
||||
t.Fatalf("expected name and slug validation errors, got %v", verrs)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user