package oltp
import "testing"
// RFC 0026 step 6: a site admin's personal site agent is a site admin
// only with the opt-in, and only while its owner is one.
func TestSiteAdminByInheritanceIsOptInAndLive(t *testing.T) {
db := agentRig(t)
if err := db.NewAgent("root-bot", "root", -1); err != nil {
t.Fatal(err)
}
if db.InheritsSiteAdmin("root-bot") || db.EffectiveCaps("root-bot", fleetRepo).HasAny("s") {
t.Errorf("FAIL: a site admin's agent is a site admin without the opt-in")
}
if err := db.SetSiteOpt("root", "root-bot"); err != nil {
t.Fatal(err)
}
if !db.InheritsSiteAdmin("root-bot") || !db.EffectiveCaps("root-bot", fleetRepo).HasAny("s") {
t.Errorf("FAIL: with the opt-in the agent is not a site admin")
}
if db.EffectiveCaps("root-bot", "priv").HasAny("s") {
t.Errorf("FAIL: the inherited flag shows on a repository, not only at forge scope")
}
if _, err := db.w.Exec(`UPDATE account SET admin = 0 WHERE name = 'root'`); err != nil {
t.Fatal(err)
}
if db.InheritsSiteAdmin("root-bot") || db.EffectiveCaps("root-bot", fleetRepo).HasAny("s") {
t.Errorf("FAIL: the agent stayed a site admin after its owner stopped being one")
}
t.Log("ok: opt-in, live")
}
func TestOnlyASiteAdminsSiteWideAgentMayOptIn(t *testing.T) {
db := agentRig(t)
if err := db.SetSiteOpt("dmestas", "dmestas-coder"); err == nil {
t.Errorf("FAIL: a person who is no site admin opted their agent in")
}
if err := db.Grant("root", "pub", Write); err != nil {
t.Fatal(err)
}
if err := db.NewScopedAgent("root-pub", "root", "repo:pub", -1); err != nil {
t.Fatal(err)
}
if err := db.SetSiteOpt("root", "root-pub"); err == nil {
t.Errorf("FAIL: a repository-scoped agent was opted in to the site-admin flag")
}
t.Log("ok: who may opt in")
}