test(cabana): unit tests for mail permissions, save validation, drift, reset, layout delete and test send
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
96
modules/cabana/mail_permissions_postgres_test.go
Normal file
96
modules/cabana/mail_permissions_postgres_test.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package cabana_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/postcard"
|
||||
)
|
||||
|
||||
func TestMailPermissions(t *testing.T) {
|
||||
env := newMailScreensEnv(t, nil)
|
||||
_, templatesTok := env.admin(t, "mail-perm-tmpl", false, `{"system.manage_mail_templates":1}`)
|
||||
_, layoutsTok := env.admin(t, "mail-perm-lay", false, `{"system.manage_mail_layouts":1}`)
|
||||
_, bothTok := env.admin(t, "mail-perm-both", false, `{"system.manage_mail_templates":1,"system.manage_mail_layouts":1}`)
|
||||
_, superTok := env.admin(t, "mail-perm-super", true, "")
|
||||
_, noneTok := env.admin(t, "mail-perm-none", false, `{"acme.demo.access":1}`)
|
||||
|
||||
tmplID := env.mailRowID(t, postcard.TemplatesTable, blogWelcome)
|
||||
layoutID := env.mailRowID(t, postcard.LayoutsTable, blogLayout)
|
||||
|
||||
type check struct {
|
||||
name string
|
||||
token string
|
||||
method string
|
||||
path string
|
||||
body map[string]any
|
||||
want int
|
||||
}
|
||||
checks := []check{
|
||||
{"templates list templates-only", templatesTok, http.MethodGet, mailTemplatesPath, nil, http.StatusOK},
|
||||
{"templates list layouts-only", layoutsTok, http.MethodGet, mailTemplatesPath, nil, http.StatusForbidden},
|
||||
{"templates list none", noneTok, http.MethodGet, mailTemplatesPath, nil, http.StatusForbidden},
|
||||
{"templates list super", superTok, http.MethodGet, mailTemplatesPath, nil, http.StatusOK},
|
||||
{"templates list both", bothTok, http.MethodGet, mailTemplatesPath, nil, http.StatusOK},
|
||||
{"layouts list layouts-only", layoutsTok, http.MethodGet, mailLayoutsPath, nil, http.StatusOK},
|
||||
{"layouts list templates-only", templatesTok, http.MethodGet, mailLayoutsPath, nil, http.StatusForbidden},
|
||||
{"partials list layouts-only", layoutsTok, http.MethodGet, mailPartialsPath, nil, http.StatusOK},
|
||||
{"partials list templates-only", templatesTok, http.MethodGet, mailPartialsPath, nil, http.StatusForbidden},
|
||||
{"branding get layouts-only", layoutsTok, http.MethodGet, "/settings/mail_brand", nil, http.StatusOK},
|
||||
{"branding get templates-only", templatesTok, http.MethodGet, "/settings/mail_brand", nil, http.StatusForbidden},
|
||||
{"images list both", bothTok, http.MethodGet, "/mail/images", nil, http.StatusOK},
|
||||
{"images list templates-only", templatesTok, http.MethodGet, "/mail/images", nil, http.StatusOK},
|
||||
{"images list none", noneTok, http.MethodGet, "/mail/images", nil, http.StatusForbidden},
|
||||
{"preview template templates-only", templatesTok, http.MethodPost, "/mail/preview", map[string]any{
|
||||
"mode": "template", "id": tmplID,
|
||||
"values": map[string]any{"subject": "s", "body": "b", "layout": layoutID},
|
||||
}, http.StatusOK},
|
||||
{"preview layout templates-only", templatesTok, http.MethodPost, "/mail/preview", map[string]any{
|
||||
"mode": "layout", "id": 0,
|
||||
"values": map[string]any{"content_html": "{{ .Content }}", "content_text": "{{ .Content }}"},
|
||||
}, http.StatusForbidden},
|
||||
{"preview layout layouts-only", layoutsTok, http.MethodPost, "/mail/preview", map[string]any{
|
||||
"mode": "layout", "id": 0,
|
||||
"values": map[string]any{"content_html": "{{ .Content }}", "content_text": "{{ .Content }}"},
|
||||
}, http.StatusOK},
|
||||
{"test send templates-only", templatesTok, http.MethodPost, "/mail/test-send", map[string]any{
|
||||
"mode": "template", "id": tmplID,
|
||||
"values": map[string]any{"subject": "s", "body": "b", "layout": layoutID},
|
||||
}, http.StatusOK},
|
||||
{"test send layouts-only template", layoutsTok, http.MethodPost, "/mail/test-send", map[string]any{
|
||||
"mode": "template", "id": tmplID,
|
||||
"values": map[string]any{"subject": "s", "body": "b", "layout": layoutID},
|
||||
}, http.StatusForbidden},
|
||||
}
|
||||
for _, tc := range checks {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
env.expect(t, tc.want, tc.method, tc.path, tc.token, tc.body)
|
||||
})
|
||||
}
|
||||
|
||||
rec := mailUpload(t, env.h, templatesTok, tinyPNG(), "x.png")
|
||||
if rec.Code != http.StatusForbidden {
|
||||
t.Fatalf("templates-only upload status=%d want 403", rec.Code)
|
||||
}
|
||||
rec = mailUpload(t, env.h, layoutsTok, tinyPNG(), "y.png")
|
||||
if rec.Code != http.StatusOK && rec.Code != http.StatusCreated {
|
||||
t.Fatalf("layouts-only upload status=%d want 200/201 body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
var granted int64
|
||||
if err := env.gdb.Raw(`SELECT COUNT(*) FROM backend_user_roles WHERE permissions LIKE '%system.manage_mail%' AND code NOT LIKE 'code-%'`).Scan(&granted).Error; err != nil || granted != 0 {
|
||||
t.Fatalf("roles granted a mail code: %d err=%v", granted, err)
|
||||
}
|
||||
_ = fmt.Sprintf("%d", tmplID)
|
||||
}
|
||||
|
||||
func tinyPNG() []byte {
|
||||
return []byte{
|
||||
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
|
||||
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0x15, 0xc4,
|
||||
0x89, 0x00, 0x00, 0x00, 0x0a, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0x00, 0x01, 0x00, 0x00,
|
||||
0x05, 0x00, 0x01, 0x0d, 0x0a, 0x2d, 0xb4, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae,
|
||||
0x42, 0x60, 0x82,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user