package web
import (
"reflect"
"strings"
"testing"
"craftdesign.group/quarry/internal/oltp"
)
// The note is the order effective() builds access in, so its steps are
// asserted as that order, by their layer names rather than their words.
func TestPeopleSaysTheOrderAccessIsBuiltIn(t *testing.T) {
plane := &memCaps{stored: map[string]string{"dan": "a"}}
srv := peopleServer(t, plane)
body := asDan(t, srv, "/r/alpha/settings")
if !strings.Contains(body, `data-hook="access-order"`) {
t.Fatal("People has no access-order note")
}
at := -1
for _, layer := range []string{"nobody", "anonymous", "role", "category", "team", "site"} {
i := strings.Index(body, `data-layer="`+layer+`"`)
if i < 0 || i < at {
t.Fatalf("layer %s missing or out of order", layer)
}
at = i
}
if !strings.Contains(body, `data-hook="participation-is-anonymous"`) {
t.Error("Anyone signed in may also does not say it is the anonymous category")
}
if !strings.Contains(body, `data-hook="category-held-by"`) {
t.Error("Advanced does not name the roles that pull in a category")
}
}
// Which roles pull in a category is read from the presets: the reader
// category is not the Reader role's, and must not be said to be.
func TestCategoriesNameTheRolesThatPullThemIn(t *testing.T) {
var want []string
for _, p := range oltp.Presets {
if strings.ContainsRune(p.Caps, 'u') {
want = append(want, p.Name)
}
}
if len(want) == 0 {
t.Fatal("no stock role holds u; the test proves nothing")
}
panel := capsPanelFor(&memCaps{stored: map[string]string{}}, "alpha")
for _, row := range panel.Categories {
if row.Name != "reader" {
continue
}
if !reflect.DeepEqual(row.HeldBy, want) {
t.Fatalf("reader held by %v, want %v", row.HeldBy, want)
}
for _, n := range row.HeldBy {
if n == "Reader" {
t.Fatal("the Reader role is named as holding the reader category")
}
}
return
}
t.Fatal("no reader category row")
}
// siteCaps is a plane where one account is a site administrator by the
// account flag alone: no string of its own here, s in its effective set.
type siteCaps struct {
*memCaps
site string
}
func (s siteCaps) CapsOf(acct, repo string) CapSet {
if acct == s.site {
return oltp.CapSet("s")
}
return s.memCaps.CapsOf(acct, repo)
}
// A site administrator with no role here used to show a bare name with
// no chip and no controls, and nothing saying why.
func TestSiteAdminRowSaysWhyItHasNoRole(t *testing.T) {
plane := siteCaps{&memCaps{stored: map[string]string{"dan": "a", "root": ""}}, "root"}
p := rosterFor(plane, nil, "alpha")
var root, dan *rosterRow
for i := range p.Rows {
switch p.Rows[i].Name {
case "root":
root = &p.Rows[i]
case "dan":
dan = &p.Rows[i]
}
}
if root == nil || !root.SiteOnly {
t.Fatalf("site administrator row = %+v", root)
}
if dan == nil || dan.SiteOnly {
t.Fatalf("an admin by role is marked as site-only: %+v", dan)
}
}