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
This commit is contained in:
@@ -3,11 +3,14 @@ package journal_test
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.golem15.com/golem15/sm-journal-plugin/controllers"
|
||||
"git.golem15.com/golem15/sm-journal-plugin/models"
|
||||
)
|
||||
|
||||
@@ -129,3 +132,145 @@ func TestJournalCategoryContentAPI(t *testing.T) {
|
||||
t.Fatalf("post listed-content missing from /posts: %s", rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
const adminCategoriesPath = "/golem15/journal/categories"
|
||||
|
||||
func TestCategoriesAdminContent(t *testing.T) {
|
||||
env := newPostsEnv(t)
|
||||
token := env.admin(t, controllers.PermissionAccessCategories)
|
||||
|
||||
schema := adminDecode(t, env.expect(t, http.StatusOK, http.MethodGet, adminAPI(adminCategoriesPath+"/schema/form"), token, nil))
|
||||
var form struct {
|
||||
Fields []struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Size string `json:"size"`
|
||||
} `json:"fields"`
|
||||
}
|
||||
if err := json.Unmarshal(schema.Data, &form); err != nil {
|
||||
t.Fatalf("form schema %s: %v", schema.Data, err)
|
||||
}
|
||||
contentAt := -1
|
||||
for i, f := range form.Fields {
|
||||
if f.Name != "content" {
|
||||
continue
|
||||
}
|
||||
contentAt = i
|
||||
if f.Type != "textarea" || f.Size != "huge" {
|
||||
t.Fatalf("content field = %+v, want textarea size huge", f)
|
||||
}
|
||||
}
|
||||
if contentAt < 1 || form.Fields[contentAt-1].Name != "description" {
|
||||
t.Fatalf("content field must follow description: %s", schema.Data)
|
||||
}
|
||||
|
||||
safe := "<h2>Acme</h2>\n<p>Pillar <a href=\"https://example.test/a\">copy</a></p>"
|
||||
name := map[string]any{"en": "Acme news", "pl": "Wiadomości Acme"}
|
||||
slug := map[string]any{"en": "acme-news", "pl": "wiadomosci-acme"}
|
||||
rec := env.expect(t, http.StatusCreated, http.MethodPost, adminAPI(adminCategoriesPath), token, map[string]any{
|
||||
"name": name,
|
||||
"slug": slug,
|
||||
"content": safe,
|
||||
})
|
||||
var created struct {
|
||||
ID uint `json:"id"`
|
||||
}
|
||||
if err := json.Unmarshal(adminDecode(t, rec).Data, &created); err != nil || created.ID == 0 {
|
||||
t.Fatalf("create data %s: %v", rec.Body.String(), err)
|
||||
}
|
||||
storedContent := func(t *testing.T) *string {
|
||||
t.Helper()
|
||||
var cat models.Category
|
||||
if err := env.gdb.First(&cat, created.ID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return cat.Content
|
||||
}
|
||||
if got := storedContent(t); got == nil || *got != safe {
|
||||
t.Fatalf("stored content = %#v, want %q", got, safe)
|
||||
}
|
||||
rows := categoryRowsBySlug(t, env, "/_journal/api/v1/categories?include_empty=1")
|
||||
if got, ok := rows["acme-news"]["content"].(string); !ok || got != safe {
|
||||
t.Fatalf("API content = %#v, want %q", rows["acme-news"]["content"], safe)
|
||||
}
|
||||
|
||||
assertContentRefused := func(t *testing.T, rec *httptest.ResponseRecorder) {
|
||||
t.Helper()
|
||||
if rec.Code != http.StatusUnprocessableEntity {
|
||||
t.Fatalf("status = %d, want 422: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var body struct {
|
||||
Error struct {
|
||||
Code string `json:"code"`
|
||||
Details map[string]any `json:"details"`
|
||||
} `json:"error"`
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
||||
t.Fatalf("422 body %s: %v", rec.Body.String(), err)
|
||||
}
|
||||
if body.Error.Code != "validation_failed" {
|
||||
t.Fatalf("error code = %q, want validation_failed", body.Error.Code)
|
||||
}
|
||||
if _, ok := body.Error.Details["content"]; !ok {
|
||||
t.Fatalf("details do not name content: %s", rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("unsafe create refused", func(t *testing.T) {
|
||||
rec := env.call(t, http.MethodPost, adminAPI(adminCategoriesPath), token, map[string]any{
|
||||
"name": map[string]any{"en": "Unsafe", "pl": "Niebezpieczna"},
|
||||
"slug": map[string]any{"en": "unsafe-acme", "pl": "niebezpieczna-acme"},
|
||||
"content": "<p onclick=\"steal()\">x</p>",
|
||||
})
|
||||
assertContentRefused(t, rec)
|
||||
var n int64
|
||||
if err := env.gdb.Model(&models.Category{}).Where("slug = ?", "unsafe-acme").Count(&n).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n != 0 {
|
||||
t.Fatalf("unsafe-acme rows = %d, want 0", n)
|
||||
}
|
||||
})
|
||||
|
||||
itemPath := adminAPI(adminCategoriesPath + "/" + strconv.FormatUint(uint64(created.ID), 10))
|
||||
|
||||
t.Run("unsafe update refused", func(t *testing.T) {
|
||||
rec := env.call(t, http.MethodPut, itemPath, token, map[string]any{
|
||||
"name": name,
|
||||
"slug": slug,
|
||||
"content": "<a href=\"javascript:alert(1)\">x</a>",
|
||||
})
|
||||
assertContentRefused(t, rec)
|
||||
if got := storedContent(t); got == nil || *got != safe {
|
||||
t.Fatalf("stored content after refused update = %#v, want %q", got, safe)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("blank update stores null", func(t *testing.T) {
|
||||
rec := env.call(t, http.MethodPut, itemPath, token, map[string]any{
|
||||
"name": name,
|
||||
"slug": slug,
|
||||
"content": " ",
|
||||
})
|
||||
if rec.Code < 200 || rec.Code > 299 {
|
||||
t.Fatalf("blank update = %d, want 2xx: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if got := storedContent(t); got != nil {
|
||||
t.Fatalf("stored content = %q, want NULL", *got)
|
||||
}
|
||||
rows := categoryRowsBySlug(t, env, "/_journal/api/v1/categories?include_empty=1")
|
||||
assertNullContent(t, rows, "acme-news")
|
||||
})
|
||||
|
||||
t.Run("posts-only token forbidden", func(t *testing.T) {
|
||||
postsOnly := env.admin(t, controllers.PermissionAccessPosts)
|
||||
rec := env.call(t, http.MethodPost, adminAPI(adminCategoriesPath), postsOnly, map[string]any{
|
||||
"name": map[string]any{"en": "Denied", "pl": "Odmowa"},
|
||||
"slug": map[string]any{"en": "denied-acme", "pl": "odmowa-acme"},
|
||||
"content": safe,
|
||||
})
|
||||
if rec.Code != http.StatusForbidden {
|
||||
t.Fatalf("posts-only create = %d, want 403: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user