Deleting your own user succeeded, and logged you out doing it: local_sessions.user_id is ON DELETE CASCADE, so the delete took the caller's own live session with it. Nothing refused this. The last-owner guard is the only thing in the path, and it passes cleanly as soon as a second owner exists -- which is exactly the state you are in just after creating one. The way back in was then whatever other account happened to exist, and -seed-admin could not help: it skipped whenever *any* local user was present, so the command documented as the way to create an administrator refused precisely when there was no usable one, because some other account still existed. It now asks whether the admin account itself is missing, which is what its own help text always claimed, and what makes it useful as recovery rather than only as first-run bootstrap. TestCanDeleteAnOwnerWhenAnotherRemains signed in as admin1 and deleted admin1, asserting 204 -- it encoded the lockout as intended behaviour. It now deletes the other owner, which is what it meant to cover, and a new test holds the refusal in place. runSeedAdmin takes a small interface so the bootstrap path is tested without a Postgres pool; it had no tests before. Signed-off-by: John Coffey <[email protected]>
97 lines
3.0 KiB
Go
97 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"io"
|
|
"log/slog"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/cairnobs/cairnobs/api/authz"
|
|
"github.com/cairnobs/cairnobs/api/localauth"
|
|
)
|
|
|
|
type fakeSeedStore struct {
|
|
usernames map[string]bool
|
|
created []createdUser
|
|
}
|
|
|
|
type createdUser struct {
|
|
username string
|
|
role authz.Role
|
|
}
|
|
|
|
func newFakeSeedStore(existing ...string) *fakeSeedStore {
|
|
f := &fakeSeedStore{usernames: map[string]bool{}}
|
|
for _, u := range existing {
|
|
f.usernames[u] = true
|
|
}
|
|
return f
|
|
}
|
|
|
|
func (f *fakeSeedStore) UsernameExists(_ context.Context, username string) (bool, error) {
|
|
return f.usernames[username], nil
|
|
}
|
|
|
|
func (f *fakeSeedStore) CreateUser(_ context.Context, username, _ string, role authz.Role) (*localauth.User, error) {
|
|
f.usernames[username] = true
|
|
f.created = append(f.created, createdUser{username: username, role: role})
|
|
return &localauth.User{ID: "id-" + username, Username: username, Role: role}, nil
|
|
}
|
|
|
|
func discardLogger() *slog.Logger {
|
|
return slog.New(slog.NewTextHandler(io.Discard, nil))
|
|
}
|
|
|
|
func TestSeedAdminCreatesTheAdminOnAFreshDeployment(t *testing.T) {
|
|
fs := newFakeSeedStore()
|
|
var out bytes.Buffer
|
|
|
|
if code := runSeedAdmin(context.Background(), discardLogger(), &out, fs); code != 0 {
|
|
t.Fatalf("runSeedAdmin: exit code = %d, want 0", code)
|
|
}
|
|
if len(fs.created) != 1 || fs.created[0].username != seedAdminUsername {
|
|
t.Fatalf("created = %+v, want one %q", fs.created, seedAdminUsername)
|
|
}
|
|
if fs.created[0].role != authz.RoleOwner {
|
|
t.Fatalf("created role = %q, want %q", fs.created[0].role, authz.RoleOwner)
|
|
}
|
|
if !strings.Contains(out.String(), "password:") {
|
|
t.Fatalf("output does not print the generated password: %q", out.String())
|
|
}
|
|
}
|
|
|
|
func TestSeedAdminSkipsWhenTheAdminAlreadyExists(t *testing.T) {
|
|
fs := newFakeSeedStore(seedAdminUsername)
|
|
var out bytes.Buffer
|
|
|
|
if code := runSeedAdmin(context.Background(), discardLogger(), &out, fs); code != 0 {
|
|
t.Fatalf("runSeedAdmin: exit code = %d, want 0", code)
|
|
}
|
|
if len(fs.created) != 0 {
|
|
t.Fatalf("created = %+v, want none -- a second admin must never be minted", fs.created)
|
|
}
|
|
if !strings.Contains(out.String(), "skipping") {
|
|
t.Fatalf("output does not say it skipped: %q", out.String())
|
|
}
|
|
}
|
|
|
|
// The recovery case this command exists for. It used to refuse here,
|
|
// because it asked whether the deployment had *any* local user rather
|
|
// than whether the admin account it creates was missing -- so an
|
|
// operator whose administrator account was gone, but whose deployment
|
|
// still held other accounts, was told "already provisioned" and left
|
|
// with no supported way back in.
|
|
func TestSeedAdminStillSeedsWhenOtherUsersExist(t *testing.T) {
|
|
fs := newFakeSeedStore("someone-else")
|
|
var out bytes.Buffer
|
|
|
|
if code := runSeedAdmin(context.Background(), discardLogger(), &out, fs); code != 0 {
|
|
t.Fatalf("runSeedAdmin: exit code = %d, want 0", code)
|
|
}
|
|
if len(fs.created) != 1 || fs.created[0].username != seedAdminUsername {
|
|
t.Fatalf("created = %+v, want one %q", fs.created, seedAdminUsername)
|
|
}
|
|
}
|