127 lines
4.2 KiB
Go
127 lines
4.2 KiB
Go
package api
|
|
|
|
import (
|
|
"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 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 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 err := app.Publish(reg); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
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)
|
|
}
|
|
})
|
|
}
|