package main
import (
"path/filepath"
"strings"
"testing"
"craftdesign.group/quarry/internal/oltp"
)
func TestTheConsoleGivesAndTakesTheSiteAdminFlagAndJournalsIt(t *testing.T) {
dir := t.TempDir()
for _, who := range []string{"root", "jo"} {
args := []string{"add", "--data", dir, "--user", who, "--password", "a-long-enough-password"}
if who == "root" {
args = append(args, "--admin")
}
if err := accountCmd(args); err != nil {
t.Fatal(err)
}
}
if err := accountCmd([]string{"admin", "--data", dir, "--user", "jo"}); err != nil {
t.Fatalf("FAIL: give: %v", err)
}
if err := accountCmd([]string{"admin", "--data", dir, "--user", "jo", "--clear"}); err != nil {
t.Fatalf("FAIL: clear: %v", err)
}
if err := accountCmd([]string{"admin", "--data", dir, "--user", "root", "--clear"}); err == nil {
t.Error("FAIL: the console cleared the last site admin")
}
if err := accountCmd([]string{"admin", "--data", dir}); err == nil {
t.Error("FAIL: admin without --user answered")
}
db, err := oltp.Open(filepath.Join(dir, "oltp.db"))
if err != nil {
t.Fatal(err)
}
defer db.Close()
if a, _ := db.AccountByName("jo"); a.Admin {
t.Error("FAIL: jo kept the flag")
}
acts, err := db.AccountActs("jo", 10)
if err != nil {
t.Fatal(err)
}
if len(acts) != 2 || acts[0].Op != "account.siteadmin.clear" || acts[1].Op != "account.siteadmin.set" ||
!strings.HasPrefix(acts[0].Actor, "host:") || !strings.HasPrefix(acts[1].Actor, "host:") {
t.Errorf("FAIL: journal = %+v", acts)
}
if !t.Failed() {
t.Log("ok")
}
}
func TestAccountDisableAndRevokeTokens(t *testing.T) {
dir := t.TempDir()
if err := accountCmd([]string{"add", "--data", dir, "--user", "root", "--password", "a-long-enough-password", "--admin"}); err != nil {
t.Fatal(err)
}
db, err := oltp.Open(filepath.Join(dir, "oltp.db"))
if err != nil {
t.Fatal(err)
}
if err := db.NewAgent("idle-bot", "root", -1); err != nil {
t.Fatal(err)
}
val, _, err := db.MintToken("idle-bot", oltp.TokenSpec{Name: "t", Scopes: []string{oltp.ScopeAPI}})
if err != nil {
t.Fatal(err)
}
db.Close()
if err := accountCmd([]string{"disable", "--data", dir, "--user", "idle-bot", "--clear", "--revoke-tokens"}); err == nil {
t.Error("FAIL: --clear and --revoke-tokens together should fail")
}
if err := accountCmd([]string{"disable", "--data", dir, "--user", "idle-bot", "--revoke-tokens"}); err != nil {
t.Fatalf("FAIL: disable --revoke-tokens: %v", err)
}
db, err = oltp.Open(filepath.Join(dir, "oltp.db"))
if err != nil {
t.Fatal(err)
}
if a, ok := db.AccountByName("idle-bot"); !ok || a.Disabled == "" {
t.Fatalf("FAIL: idle-bot not disabled: %+v", a)
}
if _, ok := db.ResolveToken("idle-bot", val, oltp.ScopeAPI); ok {
t.Error("FAIL: token survived --revoke-tokens")
}
toks, err := db.Tokens("idle-bot")
if err != nil || len(toks) != 1 || toks[0].Revoked == "" {
t.Fatalf("FAIL: expected revoked token row, got %+v %v", toks, err)
}
db.Close()
if err := accountCmd([]string{"disable", "--data", dir, "--user", "idle-bot", "--clear"}); err != nil {
t.Fatalf("FAIL: disable --clear: %v", err)
}
db, err = oltp.Open(filepath.Join(dir, "oltp.db"))
if err != nil {
t.Fatal(err)
}
if a, ok := db.AccountByName("idle-bot"); !ok || a.Disabled != "" || a.Reopened == "" {
t.Fatalf("FAIL: idle-bot did not reopen cleanly: %+v", a)
}
// Human account: disable without --revoke-tokens ends sessions and keeps tokens;
// disable with --revoke-tokens revokes sync and login tokens atomically;
// --clear on a suspended agent is refused while its operator is disabled.
if err := db.AddAccount("alice", "alices-long-password", false); err != nil {
t.Fatal(err)
}
if err := db.NewAgent("alice-bot", "alice", -1); err != nil {
t.Fatal(err)
}
sess, _, ok := db.NewSession("alice", "alices-long-password")
if !ok {
t.Fatal("FAIL: alice session failed")
}
syncTok, _, err := db.MintToken("alice", oltp.TokenSpec{Name: "push", Scopes: []string{oltp.ScopeSync}})
if err != nil {
t.Fatal(err)
}
loginTok, _, err := db.MintToken("alice", oltp.TokenSpec{Name: "laptop", Scopes: []string{oltp.ScopeAPI, oltp.ScopeSync, oltp.ScopeLogin}})
if err != nil {
t.Fatal(err)
}
db.Close()
if err := accountCmd([]string{"disable", "--data", dir, "--user", "alice"}); err != nil {
t.Fatalf("FAIL: disable human without --revoke-tokens: %v", err)
}
// Suspended agent cannot be reopened via --clear while operator is disabled.
if err := accountCmd([]string{"disable", "--data", dir, "--user", "alice-bot", "--clear"}); err == nil {
t.Error("FAIL: --clear unsuspended alice-bot while alice was disabled")
}
db, err = oltp.Open(filepath.Join(dir, "oltp.db"))
if err != nil {
t.Fatal(err)
}
if _, ok := db.SessionAccount(sess); ok {
t.Error("FAIL: session survived disable")
}
db.Close()
if err := accountCmd([]string{"disable", "--data", dir, "--user", "alice", "--clear"}); err != nil {
t.Fatalf("FAIL: reopen alice: %v", err)
}
db, err = oltp.Open(filepath.Join(dir, "oltp.db"))
if err != nil {
t.Fatal(err)
}
if _, ok := db.ResolveToken("alice", syncTok, oltp.ScopeSync); !ok {
t.Error("FAIL: sync token did not survive disable without --revoke-tokens")
}
if _, ok := db.ResolveToken("alice", loginTok, oltp.ScopeAPI); !ok {
t.Error("FAIL: login token did not survive disable without --revoke-tokens")
}
db.Close()
if err := accountCmd([]string{"disable", "--data", dir, "--user", "alice", "--revoke-tokens"}); err != nil {
t.Fatalf("FAIL: disable human with --revoke-tokens: %v", err)
}
db, err = oltp.Open(filepath.Join(dir, "oltp.db"))
if err != nil {
t.Fatal(err)
}
defer db.Close()
toks, err = db.Tokens("alice")
if err != nil || len(toks) != 2 || toks[0].Revoked == "" || toks[1].Revoked == "" {
t.Fatalf("FAIL: expected both human tokens revoked, got %+v %v", toks, err)
}
t.Log("ok")
}