Skip to content
quarry Artifact [5d3afb4096]
Login

Artifact [5d3afb4096]

Artifact 5d3afb4096f2f62197ceea77b7a46b6a28b1286a9435888184affb584f2cce8e:


package web

import (
	"net/url"
	"regexp"
	"strings"
	"testing"

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

func TestPeopleOffersTheSiteAdminFlagOnPersonsAndForgeAgentsNotPersonalAgents(t *testing.T) {
	srv, accts, _ := betaForge(t)
	_ = accts.AddAccount("bo", "longenough1", false)
	_ = accts.AddAccount("cy", "longenough1", true)
	accts.accts["bot"] = &api.Account{Name: "bot", Operator: "bo"}
	accts.accts["fbot"] = &api.Account{Name: "fbot", Operator: "forge", Answerable: "root"}
	accts.accts["off"] = &api.Account{Name: "off", Disabled: "2026-09-03T10:00:00Z"}
	page := body(as(t, srv, "root", "GET", "/fleet/people", nil))
	rows := strings.Split(page, `data-hook="person-row"`)
	forms := map[string]int{}
	for _, r := range rows[1:] {
		who := r[strings.Index(r, `class="pp-name">`)+len(`class="pp-name">`):]
		who = who[:strings.Index(who, "<")]
		forms[who] = strings.Count(rowUntilNext(r), `data-hook="person-siteadmin"`)
	}
	for who, want := range map[string]int{"bo": 1, "cy": 1, "root": 1, "bot": 0, "fbot": 1, "off": 0} {
		if forms[who] != want {
			t.Errorf("FAIL: %s has %d site-admin controls, want %d", who, forms[who], want)
		}
	}
	if !strings.Contains(page, `action="/fleet/people/siteadmin"`) || !strings.Contains(page, `data-confirm="bo becomes a site admin`) {
		t.Error("FAIL: the control has no confirmation sentence")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// One confirmation dialog serves every button on the page, so its id is
// on the page once; a second would make the script fill the wrong one.
func TestPeoplePageIDsAreUnique(t *testing.T) {
	srv, accts, _ := betaForge(t)
	_ = accts.AddAccount("bo", "longenough1", false)
	_ = accts.AddAccount("cy", "longenough1", true)
	page := body(as(t, srv, "root", "GET", "/fleet/people", nil))
	seen := map[string]int{}
	for _, m := range regexp.MustCompile(`\sid="([^"]+)"`).FindAllStringSubmatch(page, -1) {
		seen[m[1]]++
	}
	if len(seen) == 0 || seen["confirm"] != 1 {
		t.Errorf("FAIL: the confirmation dialog appears %d times", seen["confirm"])
	}
	for id, n := range seen {
		if n > 1 {
			t.Errorf("FAIL: id %q appears %d times on the People page", id, n)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func rowUntilNext(r string) string {
	if i := strings.Index(r, `data-hook="person-row"`); i >= 0 {
		return r[:i]
	}
	return r
}

func TestSiteAdminPostChangesTheFlagAndOnlyForASiteAdmin(t *testing.T) {
	srv, accts, _ := betaForge(t)
	_ = accts.AddAccount("bo", "longenough1", false)
	accts.accts["bot"] = &api.Account{Name: "bot", Operator: "bo"}

	if res := as(t, srv, "bo", "POST", "/fleet/people/siteadmin", url.Values{"name": {"bo"}, "on": {"true"}}); res.StatusCode == 303 && accts.accts["bo"].Admin {
		t.Error("FAIL: a non-admin made themself a site admin")
	}
	body(as(t, srv, "root", "POST", "/fleet/people/siteadmin", url.Values{"name": {"bo"}, "on": {"true"}}))
	if !accts.accts["bo"].Admin {
		t.Error("FAIL: the post did not give the flag")
	}
	page := body(as(t, srv, "root", "GET", "/fleet/people", nil))
	if !strings.Contains(page, "Remove site admin") {
		t.Error("FAIL: no way back after giving the flag")
	}
	body(as(t, srv, "root", "POST", "/fleet/people/siteadmin", url.Values{"name": {"bo"}, "on": {"false"}}))
	if accts.accts["bo"].Admin {
		t.Error("FAIL: the post did not clear the flag")
	}
	body(as(t, srv, "root", "POST", "/fleet/people/siteadmin", url.Values{"name": {"bot"}, "on": {"true"}}))
	if accts.accts["bot"].Admin {
		t.Error("FAIL: an agent was flagged")
	}
	if res := as(t, srv, "root", "POST", "/fleet/people/siteadmin", url.Values{"name": {"bo"}}); res.StatusCode == 303 || accts.accts["bo"].Admin {
		t.Errorf("FAIL: a post without its on field answered %d", res.StatusCode)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}