Files
Agent-System-Backend/internal/app/local_account_test.go
T

99 lines
3.0 KiB
Go

package app
import (
"errors"
"path/filepath"
"testing"
"agent-system-backend/internal/store"
)
func TestLocalOwnerAccountCreationAndReissuePreserveIdentity(t *testing.T) {
dir := t.TempDir()
seed, err := store.OpenSQLite(filepath.Join(dir, "agent-system.db"))
if err != nil {
t.Fatal(err)
}
if err := seed.EnsureSystemIdentity("test-local-owner", "sqlite"); err != nil {
t.Fatal(err)
}
bootstrap, err := seed.BootstrapFirstAdmin("root", "root", "test")
if err != nil || bootstrap == "" {
t.Fatalf("bootstrap: %v", err)
}
seed.Close()
first, _, err := CreateUserAccount("", dir, "alice", "", "", false, true, false)
if err != nil || first == "" {
t.Fatalf("local pending create: %v", err)
}
st, err := store.OpenExistingSQLite(filepath.Join(dir, "agent-system.db"))
if err != nil {
t.Fatal(err)
}
account, err := st.GetAccountByOwner("alice")
if err != nil {
t.Fatal(err)
}
st.Close()
second, owner, err := InitializeAccount("", dir, account.ID, account.StateVersion, account.InitializationGeneration)
if err != nil || owner != "alice" || second == first {
t.Fatalf("owner reissue: owner=%s err=%v", owner, err)
}
st, err = store.OpenExistingSQLite(filepath.Join(dir, "agent-system.db"))
if err != nil {
t.Fatal(err)
}
defer st.Close()
current, err := st.GetAccount(account.ID)
if err != nil || current.Owner != account.Owner || current.ID != account.ID || current.InitializationGeneration != account.InitializationGeneration+1 {
t.Fatalf("reissue changed account identity: %+v %v", current, err)
}
if _, _, err := st.RedeemLoginKey(first); err == nil {
t.Fatal("old activation grant survived reissue")
}
if _, _, err := st.RedeemLoginKey(second); err != nil {
t.Fatalf("new activation grant failed: %v", err)
}
}
func TestAPIKeyCLIRequiresExistingInitializedAccount(t *testing.T) {
dir := t.TempDir()
st, err := store.OpenSQLite(filepath.Join(dir, "agent-system.db"))
if err != nil {
t.Fatal(err)
}
if err := st.EnsureSystemIdentity("test-api-key-admission", "sqlite"); err != nil {
t.Fatal(err)
}
if _, err := CreateAPIKey("", dir, "alice", "integration"); !errors.Is(err, store.ErrNotFound) {
t.Fatalf("missing account was implicitly created: %v", err)
}
grant, err := st.BootstrapFirstAdmin("alice", "alice", "test")
if err != nil {
t.Fatal(err)
}
st.Close()
if _, err := CreateAPIKey("", dir, "alice", "integration"); !errors.Is(err, store.ErrAccountForbidden) {
t.Fatalf("pending account received a full API key: %v", err)
}
st, err = store.OpenExistingSQLite(filepath.Join(dir, "agent-system.db"))
if err != nil {
t.Fatal(err)
}
defer st.Close()
token, _, err := st.RedeemLoginKey(grant)
if err != nil {
t.Fatal(err)
}
hash, err := store.HashPassword("trusted-owner-password")
if err != nil {
t.Fatal(err)
}
if _, err := st.CompleteSetPasswordChecked(token, hash); err != nil {
t.Fatal(err)
}
if key, err := CreateAPIKey("", dir, "alice", "integration"); err != nil || key == "" {
t.Fatalf("initialized account could not receive its API key: %v", err)
}
}