69 lines
2.2 KiB
Go
69 lines
2.2 KiB
Go
package quizzes_test
|
|
|
|
import (
|
|
"slices"
|
|
"testing"
|
|
|
|
"git.golem15.com/golem15/sm-quizzes-plugin/models"
|
|
"git.golem15.com/golem15/summercms/modules/bouncer"
|
|
"git.golem15.com/golem15/summercms/modules/cabana"
|
|
"git.golem15.com/golem15/summercms/modules/pact"
|
|
)
|
|
|
|
func TestAdminCompiles(t *testing.T) {
|
|
app, plugins := activate(t, nil, nil)
|
|
routes, err := cabana.Activate(app, plugins)
|
|
if err != nil {
|
|
t.Fatalf("cabana.Activate: %v", err)
|
|
}
|
|
if routes == nil {
|
|
t.Fatal("no admin routes")
|
|
}
|
|
p := quizzesPlugin(t, plugins)
|
|
fsys := p.(pact.AdminAssets).AdminFS()
|
|
var ids []string
|
|
for _, ctl := range p.(pact.HasAdminControllers).AdminControllers() {
|
|
ids = append(ids, ctl.ID())
|
|
if _, err := cabana.CompileList(p.ID(), ctl, fsys); err != nil {
|
|
t.Errorf("%s list: %v", ctl.ID(), err)
|
|
}
|
|
_, formErr := cabana.CompileForm(p.ID(), ctl, fsys)
|
|
if ctl.ID() == "golem15.quizzes.attempts" {
|
|
if formErr == nil {
|
|
t.Error("attempts has a form; it must stay read-only")
|
|
}
|
|
} else if formErr != nil {
|
|
t.Errorf("%s form: %v", ctl.ID(), formErr)
|
|
}
|
|
}
|
|
slices.Sort(ids)
|
|
want := []string{"golem15.quizzes.answers", "golem15.quizzes.attempts", "golem15.quizzes.questions", "golem15.quizzes.quizzes"}
|
|
if !slices.Equal(ids, want) {
|
|
t.Errorf("controllers = %v, want %v", ids, want)
|
|
}
|
|
|
|
editor := &bouncer.Principal{ID: 1, Backend: true, PermissionGrants: map[string]bool{"golem15.quizzes.access_quizzes": true}}
|
|
for _, ctl := range p.(pact.HasAdminControllers).AdminControllers() {
|
|
perms := ctl.(pact.AdminPermissioned).RequiredPermissions()
|
|
allowed := cabana.Allows(editor, perms)
|
|
if wantAllowed := ctl.ID() != "golem15.quizzes.attempts"; allowed != wantAllowed {
|
|
t.Errorf("%s allowed for a quiz editor = %v, want %v", ctl.ID(), allowed, wantAllowed)
|
|
}
|
|
}
|
|
|
|
nav := p.(pact.HasNavigation).Navigation()
|
|
if len(nav) != 1 || nav[0].Label != "golem15.quizzes::lang.plugin.name" || len(nav[0].SideMenu) != 4 {
|
|
t.Errorf("navigation = %+v", nav)
|
|
}
|
|
}
|
|
|
|
func TestQuestionFilterScopes(t *testing.T) {
|
|
q := models.Question{}
|
|
if got := q.FilterOptions(models.ScopeByType); len(got) != 3 {
|
|
t.Errorf("type options = %v", got)
|
|
}
|
|
if got := q.FilterScope("unknown", nil, "x"); got != nil {
|
|
t.Errorf("FilterScope on a nil db = %v", got)
|
|
}
|
|
}
|