163 lines
5.7 KiB
Go
163 lines
5.7 KiB
Go
package cabana_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.golem15.com/golem15/summercms/modules/bouncer"
|
|
"git.golem15.com/golem15/summercms/modules/cabana"
|
|
"git.golem15.com/golem15/summercms/modules/pact"
|
|
)
|
|
|
|
func TestMediaFinderFieldSchema(t *testing.T) {
|
|
t.Run("valid_defaults_and_options", func(t *testing.T) {
|
|
yaml := datepickerFields(` cover:
|
|
label: Cover
|
|
type: mediafinder
|
|
gallery:
|
|
label: Gallery
|
|
type: mediafinder
|
|
mode: file
|
|
multiple: true
|
|
folder: journal
|
|
`)
|
|
form, err := cabana.CompileForm("acme.conform", conformController{}, datepickerFS(yaml))
|
|
if err != nil {
|
|
t.Fatalf("CompileForm: %v", err)
|
|
}
|
|
raw, err := form.CanonicalJSON()
|
|
if err != nil {
|
|
t.Fatalf("CanonicalJSON: %v", err)
|
|
}
|
|
var doc struct {
|
|
Fields []map[string]any `json:"fields"`
|
|
}
|
|
if err := json.Unmarshal(raw, &doc); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
byName := map[string]map[string]any{}
|
|
for _, f := range doc.Fields {
|
|
byName[f["name"].(string)] = f
|
|
}
|
|
cover := byName["cover"]
|
|
if cover == nil || cover["type"] != "mediafinder" || cover["mode"] != "image" {
|
|
t.Fatalf("cover field = %v, want type=mediafinder mode=image", cover)
|
|
}
|
|
gallery := byName["gallery"]
|
|
if gallery == nil || gallery["type"] != "mediafinder" || gallery["mode"] != "file" || gallery["multiple"] != true || gallery["folder"] != "journal" {
|
|
t.Fatalf("gallery field = %v, want type=mediafinder mode=file multiple=true folder=journal", gallery)
|
|
}
|
|
})
|
|
|
|
t.Run("invalid_options_rejected", func(t *testing.T) {
|
|
cases := map[string]struct {
|
|
field string
|
|
want string
|
|
}{
|
|
"invalid mode": {" cover:\n type: mediafinder\n mode: video\n", "must be image or file on type: mediafinder"},
|
|
"traversal folder": {" cover:\n type: mediafinder\n folder: ../etc\n", "must be a single folder slug"},
|
|
"nested slash folder": {" cover:\n type: mediafinder\n folder: a/b\n", "must be a single folder slug"},
|
|
"multiple on text": {" title:\n type: text\n multiple: true\n", "multiple is only valid on type: mediafinder"},
|
|
"folder on text": {" title:\n type: text\n folder: mail\n", "folder is only valid on type: mediafinder"},
|
|
"non-bool multiple": {" cover:\n type: mediafinder\n multiple: yes-please\n", "multiple:"},
|
|
}
|
|
for name, tc := range cases {
|
|
t.Run(name, func(t *testing.T) {
|
|
err := activateFields(t, datepickerFields(tc.field))
|
|
if err == nil || !strings.Contains(err.Error(), tc.want) {
|
|
t.Fatalf("err = %v, want substring %q", err, tc.want)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestMediaMCPPluginSurfaceAndScopes(t *testing.T) {
|
|
plugin := cabana.MediaMCPPlugin("/admin")
|
|
if plugin.ID() != "summercms.media" {
|
|
t.Fatalf("ID = %q, want summercms.media", plugin.ID())
|
|
}
|
|
|
|
hasPerms, ok := plugin.(pact.HasPermissions)
|
|
if !ok {
|
|
t.Fatal("MediaMCPPlugin must implement pact.HasPermissions")
|
|
}
|
|
permCodes := map[string]bool{}
|
|
for _, p := range hasPerms.Permissions() {
|
|
permCodes[p.Code] = true
|
|
if len(p.Roles) > 0 {
|
|
t.Fatalf("media permission %q must not have default role grants: %v", p.Code, p.Roles)
|
|
}
|
|
}
|
|
if !permCodes[cabana.PermissionViewMedia] || !permCodes[cabana.PermissionManageMedia] {
|
|
t.Fatalf("missing media permissions: %v", permCodes)
|
|
}
|
|
|
|
hasScopes, ok := plugin.(pact.HasTokenScopes)
|
|
if !ok {
|
|
t.Fatal("MediaMCPPlugin must implement pact.HasTokenScopes")
|
|
}
|
|
scopes := map[string]pact.TokenScope{}
|
|
for _, s := range hasScopes.TokenScopes() {
|
|
scopes[s.Name] = s
|
|
}
|
|
readScope, ok := scopes["media:read"]
|
|
if !ok || readScope.Group != "backend::lang.api_tokens.group_media" {
|
|
t.Fatalf("media:read scope = %+v", readScope)
|
|
}
|
|
writeScope, ok := scopes["media"]
|
|
if !ok || writeScope.Group != "backend::lang.api_tokens.group_media" {
|
|
t.Fatalf("media scope = %+v", writeScope)
|
|
}
|
|
|
|
hasMCP, ok := plugin.(pact.HasMCP)
|
|
if !ok {
|
|
t.Fatal("MediaMCPPlugin must implement pact.HasMCP")
|
|
}
|
|
tools := hasMCP.MCP().Tools
|
|
if len(tools) != 7 {
|
|
t.Fatalf("MediaMCPPlugin tools = %d, want 7", len(tools))
|
|
}
|
|
wantTools := map[string]bool{
|
|
"prepare_upload": false,
|
|
"upload": false,
|
|
"list": false,
|
|
"get": false,
|
|
"update": false,
|
|
"delete": false,
|
|
"create_folder": false,
|
|
}
|
|
for _, tool := range tools {
|
|
if _, exists := wantTools[tool.Name]; !exists {
|
|
t.Errorf("unexpected MCP tool %q", tool.Name)
|
|
}
|
|
wantTools[tool.Name] = true
|
|
if tool.Guard != "backend" {
|
|
t.Errorf("tool %q Guard = %q, want backend", tool.Name, tool.Guard)
|
|
}
|
|
if tool.Name == "delete" && (tool.Destructive == nil || !*tool.Destructive) {
|
|
t.Errorf("delete tool must be marked Destructive=true")
|
|
}
|
|
}
|
|
for name, seen := range wantTools {
|
|
if !seen {
|
|
t.Errorf("missing expected MCP tool %q", name)
|
|
}
|
|
}
|
|
|
|
// Verify Allows helper respects media.view_media vs media.manage_media.
|
|
viewer := &bouncer.Principal{ID: 1, Backend: true, PermissionGrants: map[string]bool{cabana.PermissionViewMedia: true}}
|
|
manager := &bouncer.Principal{ID: 2, Backend: true, PermissionGrants: map[string]bool{cabana.PermissionManageMedia: true}}
|
|
none := &bouncer.Principal{ID: 3, Backend: true, PermissionGrants: map[string]bool{"acme.demo.access": true}}
|
|
if !cabana.Allows(viewer, []string{cabana.PermissionViewMedia}) || cabana.Allows(viewer, []string{cabana.PermissionManageMedia}) {
|
|
t.Fatal("viewer permission check mismatch")
|
|
}
|
|
if !cabana.Allows(manager, []string{cabana.PermissionManageMedia}) {
|
|
t.Fatal("manager permission check mismatch")
|
|
}
|
|
if cabana.Allows(none, []string{cabana.PermissionViewMedia}) || cabana.Allows(none, []string{cabana.PermissionManageMedia}) {
|
|
t.Fatal("none principal unexpectedly allowed media permissions")
|
|
}
|
|
}
|