package oltp
import (
"errors"
"path/filepath"
"sync"
"testing"
)
func siteAdminRig(t *testing.T) *DB {
t.Helper()
db, err := Open(filepath.Join(t.TempDir(), "o.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { db.Close() })
for _, a := range []struct {
name string
admin bool
}{{"root", true}, {"ann", true}, {"bo", false}} {
if err := db.AddAccount(a.name, "a-long-enough-password", a.admin); err != nil {
t.Fatal(err)
}
}
return db
}
func TestSetSiteAdminSetsClearsAndJournals(t *testing.T) {
db := siteAdminRig(t)
if err := db.SetSiteAdmin("bo", true, "root"); err != nil {
t.Fatalf("FAIL: set: %v", err)
}
if a, _ := db.AccountByName("bo"); !a.Admin {
t.Error("FAIL: the flag was not set")
}
if err := db.SetSiteAdmin("bo", false, "host:op"); err != nil {
t.Fatalf("FAIL: clear: %v", err)
}
if a, _ := db.AccountByName("bo"); a.Admin {
t.Error("FAIL: the flag was not cleared")
}
acts, err := db.AccountActs("bo", 10)
if err != nil {
t.Fatal(err)
}
if len(acts) != 2 {
t.Fatalf("FAIL: want two journal rows, got %+v", acts)
}
// Newest first: the clear, by the console; then the set, by root.
if acts[0].Op != "account.siteadmin.clear" || acts[0].Actor != "host:op" || acts[0].Owner != "bo" ||
acts[1].Op != "account.siteadmin.set" || acts[1].Actor != "root" {
t.Errorf("FAIL: journal = %+v", acts)
}
if !t.Failed() {
t.Log("ok")
}
}
func TestSetSiteAdminRefusals(t *testing.T) {
db := siteAdminRig(t)
if err := db.NewAgent("bo-bot", "bo", 5); err != nil {
t.Fatal(err)
}
if err := db.AddAccount("cy", "a-long-enough-password", false); err != nil {
t.Fatal(err)
}
if err := db.SetDisabled("cy", true); err != nil {
t.Fatal(err)
}
for _, c := range []struct{ name, who string }{
{"bo-bot", "an agent"}, {"cy", "a disabled account"}, {"nobody", "no account"},
} {
var ref Refusal
err := db.SetSiteAdmin(c.name, true, "root")
if err == nil || (c.who != "no account" && !errors.As(err, &ref)) {
t.Errorf("FAIL: %s: want a refusal, got %v", c.who, err)
}
if a, ok := db.AccountByName(c.name); ok && a.Admin {
t.Errorf("FAIL: %s was flagged", c.who)
}
}
if !t.Failed() {
t.Log("ok")
}
}
func TestSetSiteAdminKeepsTheLastAdmin(t *testing.T) {
db := siteAdminRig(t)
if err := db.SetSiteAdmin("ann", false, "root"); err != nil {
t.Fatalf("FAIL: demoting one of two: %v", err)
}
var ref Refusal
if err := db.SetSiteAdmin("root", false, "root"); !errors.As(err, &ref) {
t.Fatalf("FAIL: clearing the last admin: %v", err)
}
if a, _ := db.AccountByName("root"); !a.Admin {
t.Error("FAIL: the last admin lost the flag")
}
// A shut admin is not one that can act: the open one is the last.
if err := db.SetSiteAdmin("ann", true, "root"); err != nil {
t.Fatal(err)
}
if err := db.SetDisabled("ann", true); err != nil {
t.Fatal(err)
}
if err := db.SetSiteAdmin("root", false, "root"); !errors.As(err, &ref) {
t.Errorf("FAIL: a shut admin counted as a second: %v", err)
}
if !t.Failed() {
t.Log("ok")
}
}
func TestTwoAdminsDemotingEachOtherLeaveOne(t *testing.T) {
for i := 0; i < 20; i++ {
db := siteAdminRig(t)
var wg sync.WaitGroup
errs := make([]error, 2)
for j, who := range []string{"root", "ann"} {
wg.Add(1)
go func() {
defer wg.Done()
errs[j] = db.SetSiteAdmin(who, false, who)
}()
}
wg.Wait()
n := 0
for _, who := range []string{"root", "ann"} {
if a, _ := db.AccountByName(who); a.Admin {
n++
}
}
if n != 1 || (errs[0] == nil) == (errs[1] == nil) {
t.Fatalf("FAIL: round %d left %d admins, errs=%v", i, n, errs)
}
}
t.Log("ok")
}
func TestADemotedAdminsSessionLosesThePowerAtOnce(t *testing.T) {
db := siteAdminRig(t)
tok, _, ok := db.NewSession("ann", "a-long-enough-password")
if !ok {
t.Fatal("FAIL: no session")
}
if a, ok := db.SessionAccount(tok); !ok || !a.Admin {
t.Fatalf("FAIL: before: %+v %v", a, ok)
}
if err := db.SetSiteAdmin("ann", false, "root"); err != nil {
t.Fatal(err)
}
if a, ok := db.SessionAccount(tok); !ok || a.Admin {
t.Errorf("FAIL: the open session kept the flag: %+v %v", a, ok)
}
if !t.Failed() {
t.Log("ok")
}
}
func TestADemotedOperatorsInheritingAgentLosesTheFlagAtOnce(t *testing.T) {
db := siteAdminRig(t)
if err := db.NewAgent("ann-bot", "ann", 5); err != nil {
t.Fatal(err)
}
if err := db.SetSiteOpt("ann", "ann-bot"); err != nil {
t.Fatal(err)
}
if err := db.SetInherit("ann", "ann-bot", true); err != nil {
t.Fatal(err)
}
if !db.InheritsSiteAdmin("ann-bot") {
t.Fatal("FAIL: the agent does not inherit before the demotion")
}
if err := db.SetSiteAdmin("ann", false, "root"); err != nil {
t.Fatal(err)
}
if db.InheritsSiteAdmin("ann-bot") {
t.Error("FAIL: the agent kept the flag after its operator lost it")
}
if !t.Failed() {
t.Log("ok")
}
}
// A pending invitee cannot sign in, so the last person who can is the
// last site admin however many ghosts hold the flag.
func TestAnInvitedAdminWithNoPasswordIsNotASecondAdmin(t *testing.T) {
db := siteAdminRig(t)
if err := db.SetSiteAdmin("ann", false, "root"); err != nil {
t.Fatal(err)
}
if _, err := db.InviteAccount("ghost", "[email protected]", true); err != nil {
t.Fatal(err)
}
var ref Refusal
if err := db.SetSiteAdmin("root", false, "root"); !errors.As(err, &ref) {
t.Errorf("FAIL: root cleared itself with only an invitee left: %v", err)
}
if a, _ := db.AccountByName("root"); !a.Admin {
t.Error("FAIL: nobody who can sign in is a site admin now")
}
if !t.Failed() {
t.Log("ok")
}
}
// Every flag change goes through SetSiteAdmin: a re-invite that would
// change a pending account's flag is refused, and one that leaves it
// as it is still works.
func TestAReInviteCannotChangeTheFlag(t *testing.T) {
db := siteAdminRig(t)
if _, err := db.InviteAccount("ghost", "[email protected]", true); err != nil {
t.Fatal(err)
}
var ref Refusal
if _, err := db.InviteAccount("ghost", "[email protected]", false); !errors.As(err, &ref) {
t.Errorf("FAIL: a re-invite cleared the flag: %v", err)
}
if _, err := db.InviteAccount("pat", "[email protected]", false); err != nil {
t.Fatal(err)
}
if _, err := db.InviteAccount("pat", "[email protected]", true); !errors.As(err, &ref) {
t.Errorf("FAIL: a re-invite set the flag: %v", err)
}
if _, err := db.InviteAccount("ghost", "[email protected]", true); err != nil {
t.Errorf("FAIL: a re-send that keeps the flag: %v", err)
}
if a, _ := db.AccountByName("ghost"); !a.Admin || a.Email != "[email protected]" {
t.Errorf("FAIL: ghost = %+v", a)
}
if a, _ := db.AccountByName("pat"); a.Admin {
t.Error("FAIL: pat was flagged by a re-invite")
}
if err := db.SetSiteAdmin("pat", true, "root"); err != nil {
t.Errorf("FAIL: the guarded path on a pending account: %v", err)
}
if !t.Failed() {
t.Log("ok")
}
}
// Marking an account as a person's agent used to clear its flag with no
// record; now it is refused until the flag is cleared the guarded way.
func TestMarkingAnAdminAsAnAgentIsRefusedNotSilent(t *testing.T) {
db := siteAdminRig(t)
var ref Refusal
if err := db.SetOperator("ann", "bo"); !errors.As(err, &ref) {
t.Fatalf("FAIL: an admin was marked as an agent: %v", err)
}
if a, _ := db.AccountByName("ann"); a.Operator != "" || !a.Admin {
t.Errorf("FAIL: ann = %+v", a)
}
if err := db.SetSiteAdmin("ann", false, "root"); err != nil {
t.Fatal(err)
}
if err := db.SetOperator("ann", "bo"); err != nil {
t.Errorf("FAIL: after clearing the flag: %v", err)
}
if !t.Failed() {
t.Log("ok")
}
}
// RFC 0021: a site admin sets the flag on a forge agent as on any
// account; a forge agent is never the last site admin; a personal
// agent's power is inherited, never stored.
func TestTheFlagOnAForgeAgent(t *testing.T) {
db := siteAdminRig(t)
if err := db.NewForgeAgent("fbot", "root"); err != nil {
t.Fatal(err)
}
if err := db.SetSiteAdmin("fbot", true, "root"); err != nil {
t.Fatalf("FAIL: set on a forge agent: %v", err)
}
if a, _ := db.AccountByName("fbot"); !a.Admin {
t.Error("FAIL: the forge agent has no flag")
}
if err := db.SetSiteAdmin("ann", false, "root"); err != nil {
t.Fatal(err)
}
var ref Refusal
if err := db.SetSiteAdmin("root", false, "root"); !errors.As(err, &ref) {
t.Errorf("FAIL: a forge agent counted as the second site admin: %v", err)
}
if err := db.SetSiteAdmin("fbot", false, "root"); err != nil {
t.Errorf("FAIL: clear on a forge agent: %v", err)
}
acts, _ := db.AccountActs("fbot", 10)
if len(acts) != 2 || acts[0].Op != "account.siteadmin.clear" {
t.Errorf("FAIL: journal = %+v", acts)
}
if !t.Failed() {
t.Log("ok")
}
}
// A site admin clears a forge agent's flag and its site token loses the
// power on its next request: the flag is asked at each one.
func TestClearingAForgeAgentsFlagEndsItsSiteToken(t *testing.T) {
db := siteRig(t)
_, tok, err := db.MintToken("zeitgeist", TokenSpec{Name: "site", Scopes: []string{ScopeAPI, ScopeSite}})
if err != nil {
t.Fatal(err)
}
if !db.SiteToken(tok) {
t.Fatal("FAIL: the token has no site power before the change")
}
if err := db.SetSiteAdmin("zeitgeist", false, "root"); err != nil {
t.Fatalf("FAIL: clear: %v", err)
}
if db.SiteToken(tok) || db.TokenCaps(tok, fleetRepo).HasAny("s") {
t.Error("FAIL: the site token kept its power after the flag was cleared")
}
if err := db.SetSiteAdmin("zeitgeist", true, "root"); err != nil {
t.Fatalf("FAIL: set: %v", err)
}
if !db.SiteToken(tok) {
t.Error("FAIL: setting the flag did not restore the token's power")
}
if !t.Failed() {
t.Log("ok")
}
}