106 lines
3.6 KiB
Go
106 lines
3.6 KiB
Go
package journal_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.golem15.com/golem15/sm-journal-plugin/controllers"
|
|
"git.golem15.com/golem15/sm-journal-plugin/models"
|
|
)
|
|
|
|
func TestPostsFormCompiles(t *testing.T) {
|
|
env := newPostsEnv(t)
|
|
token := env.admin(t, controllers.PermissionAccessPosts)
|
|
rec := env.expect(t, http.StatusOK, http.MethodGet, adminAPI(adminPostsPath+"/schema/form"), token, nil)
|
|
body := rec.Body.String()
|
|
if !strings.Contains(body, "mlmarkdown") {
|
|
t.Fatalf("form schema missing mlmarkdown: %s", body)
|
|
}
|
|
if !strings.Contains(body, "mltext") {
|
|
t.Fatalf("form schema missing mltext: %s", body)
|
|
}
|
|
if !strings.Contains(body, "mltextarea") {
|
|
t.Fatalf("form schema missing mltextarea: %s", body)
|
|
}
|
|
if !strings.Contains(body, `"preset":{"field":"title","type":"slug"}`) {
|
|
t.Fatalf("form schema missing slug preset: %s", body)
|
|
}
|
|
}
|
|
|
|
func TestPostsAdminCreateSmoke(t *testing.T) {
|
|
env := newPostsEnv(t)
|
|
privileged := env.admin(t,
|
|
controllers.PermissionAccessPosts,
|
|
controllers.PermissionAccessPublish,
|
|
controllers.PermissionAccessOtherPosts,
|
|
)
|
|
rec := env.expect(t, http.StatusCreated, http.MethodPost, adminAPI(adminPostsPath), privileged, map[string]any{
|
|
"title": map[string]any{"en": "Hello", "pl": "Witaj"},
|
|
"slug": map[string]any{"en": "hello-admin", "pl": "witaj-admin"},
|
|
"content": map[string]any{"en": "Hello **world**", "pl": "Witaj **świecie**"},
|
|
"excerpt": map[string]any{"en": "Excerpt", "pl": "Wyjątek"},
|
|
})
|
|
var created struct {
|
|
ID uint `json:"id"`
|
|
}
|
|
envData := adminDecode(t, rec)
|
|
if err := json.Unmarshal(envData.Data, &created); err != nil || created.ID == 0 {
|
|
t.Fatalf("create data %s: %v", rec.Body.String(), err)
|
|
}
|
|
var stored models.Post
|
|
if err := env.gdb.First(&stored, created.ID).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if stored.Title != "Hello" {
|
|
t.Fatalf("host title = %q, want Hello", stored.Title)
|
|
}
|
|
if stored.Slug != "hello-admin" {
|
|
t.Fatalf("host slug = %q, want hello-admin", stored.Slug)
|
|
}
|
|
if stored.ContentHTML == "" {
|
|
t.Fatal("content_html is empty")
|
|
}
|
|
if strings.Contains(strings.ToLower(stored.ContentHTML), "<script") {
|
|
t.Fatalf("content_html contains script: %s", stored.ContentHTML)
|
|
}
|
|
if !strings.Contains(stored.ContentHTML, "<strong>") && !strings.Contains(stored.ContentHTML, "<p>") {
|
|
t.Fatalf("content_html missing markup: %s", stored.ContentHTML)
|
|
}
|
|
|
|
var plCount int64
|
|
if err := env.gdb.Table("golem15_translate_attributes").
|
|
Where("model_id = ? AND model_type = ? AND locale = ?", fmt.Sprint(stored.ID), stored.MorphName(), "pl").
|
|
Count(&plCount).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if plCount == 0 {
|
|
t.Fatal("Polish translate attributes were not written")
|
|
}
|
|
|
|
t.Run("publish without access_publish", func(t *testing.T) {
|
|
limited := env.admin(t, controllers.PermissionAccessPosts)
|
|
denied := env.call(t, http.MethodPost, adminAPI(adminPostsPath), limited, map[string]any{
|
|
"title": map[string]any{"en": "Draft", "pl": "Szkic"},
|
|
"slug": map[string]any{"en": "draft-admin", "pl": "szkic-admin"},
|
|
"content": map[string]any{"en": "Draft body", "pl": "Treść szkicu"},
|
|
"published": true,
|
|
})
|
|
if denied.Code != http.StatusForbidden {
|
|
t.Fatalf("publish without access_publish = %d, want 403: %s", denied.Code, denied.Body.String())
|
|
}
|
|
if adminDecode(t, denied).Error.Code != "forbidden" {
|
|
t.Fatalf("error code = %s", denied.Body.String())
|
|
}
|
|
var n int64
|
|
if err := env.gdb.Model(&models.Post{}).Where("slug = ?", "draft-admin").Count(&n).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n != 0 {
|
|
t.Fatal("published=true persisted without access_publish")
|
|
}
|
|
})
|
|
}
|