Files
summercms/modules/cabana/example_test.go
Jakub Zych becb31fbef docs(cabana,compass,conga): compile README Go examples via src= (quick-261118-qsd)
- cabana: the hand-written controller fence points at Post, PostsController
  and BlogPlugin declarations; TestDocsDeclarations now calls each of them
- compass: mailSettings and ExampleOpen; the YAML block matches the
  directory ExampleOpen writes
- conga: job, dispatch and progress fences reuse the Jobs page targets; new
  reindex and worker regions run in TestDocsDispatch (Docker), and the
  schedule fence is FeedPlugin.Schedule, checked by TestDocsSchedule
2026-11-18 19:44:21 +01:00

138 lines
4.9 KiB
Go

package cabana_test
import (
"fmt"
"io/fs"
"os"
"testing"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/bouncer"
"git.golem15.com/golem15/summercms/modules/cabana"
"git.golem15.com/golem15/summercms/modules/compass"
"git.golem15.com/golem15/summercms/modules/party"
)
func ExampleCompileList() {
// The plugin embeds its controllers/ and models/ trees; the example reads
// the same files from testdata.
fsys := os.DirFS("testdata/docs")
list, err := cabana.CompileList("acme.blog", PostsController{}, fsys)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(list.Title, list.RecordsPerPage, list.PerPageOptions, list.ToolbarButtons)
for _, c := range list.Columns {
fmt.Printf("column %s type=%q searchable=%v sortable=%v\n", c.Key, c.Type, c.Searchable, c.Sortable)
}
for _, f := range list.Filters {
fmt.Printf("filter %s type=%s column=%s\n", f.Name, f.Type, f.Column)
}
// Output:
// acme.blog::lang.posts.title 20 [20 50 100] [create delete]
// column title type="" searchable=true sortable=true
// column published type="switch" searchable=false sortable=true
// column published_at type="datetime" searchable=false sortable=true
// filter published type=switch column=published
// filter published_at type=daterange column=published_at
}
func ExampleCompileForm() {
fsys := os.DirFS("testdata/docs")
form, err := cabana.CompileForm("acme.blog", PostsController{}, fsys)
if err != nil {
fmt.Println(err)
return
}
for _, f := range form.Fields {
fmt.Printf("%s %s span=%q tab=%q required=%v options=%d\n", f.Name, f.Type, f.Span, f.Tab, f.Required, len(f.Options))
}
// Output:
// title text span="left" tab="" required=true options=0
// slug text span="right" tab="" required=false options=0
// status dropdown span="left" tab="" required=false options=2
// published switch span="right" tab="" required=false options=0
// content textarea span="" tab="acme.blog::lang.posts.tab_content" required=false options=0
}
func ExampleActivate() {
cfg, err := compass.Open(compass.Options{Dir: "config", Env: "development", Environ: []string{}})
if err != nil {
fmt.Println(err)
return
}
// Set through SUMMER_ADMIN__JWT__SECRET in a real deployment.
_ = cfg.Set("admin.jwt.secret", "test-only-secret-with-at-least-32-bytes")
_ = cfg.Set("backend.uri", "/admin")
routes, err := cabana.Activate(backpack.New(cfg), []party.Plugin{&BlogPlugin{}})
if err != nil {
fmt.Println(err)
return
}
fmt.Println(routes.Prefix)
editor := &bouncer.Principal{ID: 7, Backend: true, PermissionGrants: map[string]bool{"acme.blog.*": true}}
fmt.Println(cabana.Allows(editor, PostsController{}.RequiredPermissions()))
fmt.Println(cabana.Allows(editor, []string{"acme.shop.access_orders"}))
// Output:
// /admin
// true
// false
}
// TestDocsDeclarations checks the plugin declarations the Backend pages and
// the README show.
func TestDocsDeclarations(t *testing.T) {
p := &BlogPlugin{}
c := PostsController{}
if c.ID() != "acme.blog.posts" || c.ModelName() != "Post" || c.ConfigDir() != "controllers/posts" {
t.Fatalf("controller = %q %q %q", c.ID(), c.ModelName(), c.ConfigDir())
}
if _, ok := c.NewRecord().(*Post); !ok || (Post{}).TableName() != "acme_blog_posts" {
t.Fatalf("NewRecord() = %T, table %q", c.NewRecord(), Post{}.TableName())
}
if ctrls := p.AdminControllers(); len(ctrls) != 1 || ctrls[0].ID() != c.ID() {
t.Fatalf("AdminControllers() = %+v", ctrls)
}
for _, name := range []string{"controllers/posts/config_list.yaml", "controllers/posts/config_form.yaml", "models/post/columns.yaml", "models/post/fields.yaml"} {
if _, err := fs.Stat(p.AdminFS(), name); err != nil {
t.Fatalf("AdminFS(): %v", err)
}
}
if n := len(p.Permissions()); n != 2 {
t.Fatalf("Permissions() = %d entries", n)
}
if nav := p.Navigation(); len(nav) != 1 || nav[0].Controller != "acme.blog.posts" {
t.Fatalf("Navigation() = %+v", nav)
}
settings := p.Settings()
if len(settings) != 1 {
t.Fatalf("Settings() = %+v", settings)
}
if _, ok := settings[0].NewModel().(*BlogSettings); !ok {
t.Fatal("settings model is not *BlogSettings")
}
if rule := (BlogSettings{}).Rules()["posts_per_page"]; rule == "" {
t.Fatal("BlogSettings has no posts_per_page rule")
}
}
// TestDocsSettingsLink activates the plugin with a settings entry that links
// to its posts controller, as the Settings page shows.
func TestDocsSettingsLink(t *testing.T) {
cfg, err := compass.Open(compass.Options{Dir: "config", Env: "development", Environ: []string{}})
if err != nil {
t.Fatal(err)
}
_ = cfg.Set("admin.jwt.secret", "test-only-secret-with-at-least-32-bytes")
if _, err := cabana.Activate(backpack.New(cfg), []party.Plugin{&LinkedBlogPlugin{}}); err != nil {
t.Fatal(err)
}
settings := (&LinkedBlogPlugin{}).Settings()
if len(settings) != 2 || settings[1].Controller != "acme.blog.posts" || settings[1].Model != "" {
t.Fatalf("Settings() = %+v", settings)
}
}