Skip to content
quarry Artifact [9a701d6dfe]
Login

Artifact [9a701d6dfe]

Artifact 9a701d6dfe5e290f3f5270912c36e0c6358a72cc94f0ccfb86cea7cfa9ca5f31:


package web

import (
	"net/http/httptest"
	"strings"
	"testing"

	"craftdesign.group/quarry/internal/api"
	"craftdesign.group/quarry/internal/oltp"
	"craftdesign.group/quarry/internal/ops"
)

// inheritCaps is a caps plane in which some accounts hold no string
// here and reach the repository by inheritance (RFC 0026).
type inheritCaps struct {
	*memCaps
	inherits map[string]string
}

func (m inheritCaps) CapHolders(repo string) ([]CapHolder, error) {
	out, _ := m.memCaps.CapHolders(repo)
	for n, op := range m.inherits {
		out = append(out, CapHolder{Account: n, Inherits: op})
	}
	return out, nil
}

// peopleAgentsPage renders alpha's settings as viewer, who is a site
// admin only when site says so.
func peopleAgentsPage(t *testing.T, viewer string, site bool, stored map[string]string, inherits map[string]string) string {
	t.Helper()
	plane := inheritCaps{&memCaps{stored: stored}, inherits}
	r := ops.NewRegistry()
	if err := api.RegisterAll(r, api.Deps{Version: "v",
		Repos:  api.NewRepos(map[string]*api.Repo{"alpha": {Chat: "alpha", Index: landingIndex(t, false)}}),
		Roster: capsRoster{plane.memCaps}}); err != nil {
		t.Fatal(err)
	}
	roles := map[string]ops.Auth{"alpha": ops.Admin}
	if site {
		roles[""] = ops.Admin
	}
	srv := httptest.NewServer(HandlerWith(r, nil, nil, []Option{
		WithSessions(fakeSessions{user: viewer, roles: roles}),
		WithCaps(plane),
		WithAgents(func(n string) (string, string) {
			switch n {
			case "dan-bot", "dan-narrow":
				return "dan", ""
			case "repo-bot":
				return "repo:alpha", "dan"
			}
			return "", ""
		})}))
	t.Cleanup(srv.Close)
	return asDan(t, srv, "/r/alpha/settings")
}

// group is the markup of the roster list carrying data-group=name.
func group(body, name string) (string, bool) {
	open := `data-group="` + name + `"`
	i := strings.Index(body, open)
	if i < 0 {
		return "", false
	}
	rest := body[i:]
	if j := strings.Index(rest, "</ul>"); j >= 0 {
		rest = rest[:j]
	}
	return rest, true
}

func has(markup string, names ...string) bool {
	for _, n := range names {
		if !strings.Contains(markup, `href="/r/alpha/people/`+n+`"`) {
			return false
		}
	}
	return true
}

func TestPeopleAndAgentsAreTwoGroups(t *testing.T) {
	stored := map[string]string{"dan": "a", "eve": oltp.Write.Caps(), "dan-narrow": oltp.Write.Caps(), "repo-bot": oltp.Write.Caps()}
	body := peopleAgentsPage(t, "dan", false, stored, map[string]string{"dan-bot": "dan"})
	people, okP := group(body, "people")
	agents, okA := group(body, "agents")
	if !okP || !okA {
		t.Fatalf("FAIL: groups present: people %v agents %v", okP, okA)
	}
	if !has(people, "dan", "eve") || has(people, "dan-bot") || has(people, "dan-narrow") || has(people, "repo-bot") {
		t.Errorf("FAIL: people group holds the wrong members")
	}
	if !has(agents, "dan-bot", "dan-narrow", "repo-bot") || has(agents, "dan") || has(agents, "eve") {
		t.Errorf("FAIL: agents group holds the wrong members")
	}
	if strings.Count(agents, `data-hook="people-inherits"`) != 1 || !strings.Contains(agents, `data-inherits="dan"`) {
		t.Errorf("FAIL: only the inheriting agent carries data-inherits")
	}
	if strings.Contains(people, "people-inherits") {
		t.Errorf("FAIL: a person row carries the inherited note")
	}
	// An agent with its own string keeps its role control.
	narrow := between(agents, `href="/r/alpha/people/dan-narrow"`, "</li>")
	if !strings.Contains(narrow, `name="role"`) || strings.Contains(narrow, "people-inherits") {
		t.Errorf("FAIL: an agent with its own string lost its controls or reads as inherited")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestInheritedAgentChangeLinkIsForOperatorAndSiteAdmin(t *testing.T) {
	stored := map[string]string{"dan": "a", "eve": "a"}
	inh := map[string]string{"dan-bot": "dan"}
	if b := peopleAgentsPage(t, "dan", false, stored, inh); hookCount(b, "people-inherits-change") != 1 {
		t.Errorf("FAIL: the operator is not offered the change link")
	}
	if b := peopleAgentsPage(t, "eve", false, stored, inh); hookCount(b, "people-inherits-change") != 0 || hookCount(b, "people-inherits") != 1 {
		t.Errorf("FAIL: another repository admin sees the change link, or loses the note")
	}
	if b := peopleAgentsPage(t, "eve", true, stored, inh); hookCount(b, "people-inherits-change") != 1 {
		t.Errorf("FAIL: a site admin is not offered the link")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestNoAgentsNoAgentsGroup(t *testing.T) {
	body := peopleAgentsPage(t, "dan", false, map[string]string{"dan": "a", "eve": oltp.Write.Caps()}, nil)
	if _, ok := group(body, "agents"); ok {
		t.Errorf("FAIL: an agents group with no agents")
	}
	if _, ok := group(body, "people"); !ok {
		t.Errorf("FAIL: no people group")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestOnlyAgentsNoPeopleGroup(t *testing.T) {
	body := peopleAgentsPage(t, "dan", false, map[string]string{"repo-bot": oltp.Write.Caps()}, nil)
	if _, ok := group(body, "people"); ok {
		t.Errorf("FAIL: a people group with only agents")
	}
	if _, ok := group(body, "agents"); !ok {
		t.Errorf("FAIL: no agents group")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// TestSettingsMenuCountsPersons: the People entry in the settings menu
// counts persons, not the agents listed beside them.
func TestSettingsMenuCountsPersons(t *testing.T) {
	d := pageData{Roster: &rosterPanel{Rows: []rosterRow{
		{Name: "pat", Caps: "a"}, {Name: "robin", Caps: "v"},
		{Name: "glint", Agent: true, Inherits: "pat"}, {Name: "shop-release", Caps: "v", Agent: true},
	}}}
	found := false
	for _, s := range d.SetSections() {
		if s.ID == "people" {
			found = true
			if s.N != 2 {
				t.Errorf("FAIL: people count = %d, want 2 (agents are not persons)", s.N)
			}
		}
	}
	if !found {
		t.Errorf("FAIL: no people section in the settings menu")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}