- Public and write groups under /_journal/api/v1 with journal-public-api and journal-api buckets (Max 120)
- Index omits drafts and future published_at; per_page default 9 max 30
- Optional backend JWT on GET; missing Bearer on writes returns PHP {error:Authentication required}
Co-authored-by: Cursor <cursoragent@cursor.com>
72 lines
1.8 KiB
Go
72 lines
1.8 KiB
Go
package journal_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.golem15.com/golem15/sm-journal-plugin/models"
|
|
)
|
|
|
|
func TestJournalPublicList(t *testing.T) {
|
|
env := newPostsEnv(t)
|
|
now := time.Now().UTC()
|
|
past := now.Add(-time.Hour)
|
|
published := models.Post{
|
|
Title: "Published",
|
|
Slug: "published-row",
|
|
Content: "Hello published",
|
|
Published: true,
|
|
PublishedAt: &past,
|
|
}
|
|
if err := env.gdb.Create(&published).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
draft := models.Post{
|
|
Title: "Draft",
|
|
Slug: "draft-row",
|
|
Content: "Hello draft",
|
|
Published: false,
|
|
}
|
|
if err := env.gdb.Create(&draft).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
rec := env.call(t, http.MethodGet, "/_journal/api/v1/posts", "", nil)
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("GET /_journal/api/v1/posts = %d, want 200: %s", rec.Code, rec.Body.String())
|
|
}
|
|
var body struct {
|
|
Data []struct {
|
|
Slug string `json:"slug"`
|
|
Title string `json:"title"`
|
|
Published bool `json:"published"`
|
|
} `json:"data"`
|
|
Meta struct {
|
|
CurrentPage int `json:"current_page"`
|
|
LastPage int `json:"last_page"`
|
|
PerPage int `json:"per_page"`
|
|
Total int64 `json:"total"`
|
|
} `json:"meta"`
|
|
}
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
|
t.Fatalf("list body: %s err=%v", rec.Body.String(), err)
|
|
}
|
|
if body.Meta.PerPage != 9 {
|
|
t.Fatalf("meta.per_page = %d, want 9", body.Meta.PerPage)
|
|
}
|
|
if body.Meta.Total != 1 {
|
|
t.Fatalf("meta.total = %d, want 1 (draft omitted); body=%s", body.Meta.Total, rec.Body.String())
|
|
}
|
|
if len(body.Data) != 1 {
|
|
t.Fatalf("data len = %d, want 1: %s", len(body.Data), rec.Body.String())
|
|
}
|
|
if body.Data[0].Slug != "published-row" {
|
|
t.Fatalf("listed slug = %q, want published-row", body.Data[0].Slug)
|
|
}
|
|
if !body.Data[0].Published {
|
|
t.Fatal("listed row is not published")
|
|
}
|
|
}
|