feat(admin): category content on the Blog Editor and editor image uploads
- category content is the blogeditor type (uploadFolder categories): it is stripped and reported like post content; the 422 CheckHTML gate is retired and blank content still becomes NULL - blogeditor and mlblogeditor Upload: magic-byte check for JPEG, PNG, GIF and WebP matching the extension, safe lowercase names, storage through classes.StoreMedia in the field's uploadFolder - TestBlogEditorUploadSmoke, TestCategoriesAdminContent asserts the strip, README describes the field types, admin saves and editor uploads
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"reflect"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -156,8 +157,8 @@ func TestCategoriesAdminContent(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
contentAt = i
|
||||
if f.Type != "textarea" || f.Size != "huge" {
|
||||
t.Fatalf("content field = %+v, want textarea size huge", f)
|
||||
if f.Type != "blogeditor" || f.Size != "huge" {
|
||||
t.Fatalf("content field = %+v, want blogeditor size huge", f)
|
||||
}
|
||||
}
|
||||
if contentAt < 1 || form.Fields[contentAt-1].Name != "description" {
|
||||
@@ -172,6 +173,9 @@ func TestCategoriesAdminContent(t *testing.T) {
|
||||
"slug": slug,
|
||||
"content": safe,
|
||||
})
|
||||
if notices := adminNotices(t, rec); notices != nil {
|
||||
t.Fatalf("safe create carries meta.notices: %s", rec.Body.String())
|
||||
}
|
||||
var created struct {
|
||||
ID uint `json:"id"`
|
||||
}
|
||||
@@ -194,55 +198,47 @@ func TestCategoriesAdminContent(t *testing.T) {
|
||||
t.Fatalf("API content = %#v, want %q", rows["acme-news"]["content"], safe)
|
||||
}
|
||||
|
||||
assertContentRefused := func(t *testing.T, rec *httptest.ResponseRecorder) {
|
||||
// assertContentStripped checks a save the blogeditor Decode stripped
|
||||
// (D-10 amended, D-19): the status, and one meta.notices entry for
|
||||
// content whose message names what was removed.
|
||||
assertContentStripped := func(t *testing.T, rec *httptest.ResponseRecorder, status int, removed string) {
|
||||
t.Helper()
|
||||
if rec.Code != http.StatusUnprocessableEntity {
|
||||
t.Fatalf("status = %d, want 422: %s", rec.Code, rec.Body.String())
|
||||
if rec.Code != status {
|
||||
t.Fatalf("status = %d, want %d: %s", rec.Code, status, 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())
|
||||
notices := adminNotices(t, rec)
|
||||
if len(notices) != 1 || notices[0].Field != "content" || !strings.Contains(notices[0].Message, removed) {
|
||||
t.Fatalf("meta.notices = %#v, want one content notice naming %s", notices, removed)
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("unsafe create refused", func(t *testing.T) {
|
||||
t.Run("unsafe create stripped", 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)
|
||||
assertContentStripped(t, rec, http.StatusCreated, "onclick")
|
||||
var cat models.Category
|
||||
if err := env.gdb.Where("slug = ?", "unsafe-acme").Take(&cat).Error; err != nil {
|
||||
t.Fatalf("unsafe-acme row: %v", err)
|
||||
}
|
||||
if n != 0 {
|
||||
t.Fatalf("unsafe-acme rows = %d, want 0", n)
|
||||
if cat.Content == nil || *cat.Content != "<p>x</p>" {
|
||||
t.Fatalf("stored content = %#v, want <p>x</p>", cat.Content)
|
||||
}
|
||||
})
|
||||
|
||||
itemPath := adminAPI(adminCategoriesPath + "/" + strconv.FormatUint(uint64(created.ID), 10))
|
||||
|
||||
t.Run("unsafe update refused", func(t *testing.T) {
|
||||
t.Run("unsafe update stripped", 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)
|
||||
assertContentStripped(t, rec, http.StatusOK, "removed: a")
|
||||
if got := storedContent(t); got == nil || *got != "x" {
|
||||
t.Fatalf("stored content after stripped update = %#v, want x", got)
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user