Files
Agent-System-Backend/internal/store/plugin_account_write_test.go
T
weichen adcd8fefd2 Fence U3 account writes and stabilize directory cursors
Why: Account disable could be followed by late Backend business writes, API key creation could implicitly create users, account bootstrap locks could invert, and directory cursors were process-local or leaked out-of-scope changes.

Evidence: Local go test ./... and isolated PostgreSQL package suites passed on the staged candidate; exact committed-head SELF replay and stage 6 human review remain pending.

Impact: Adds final account transactions and external-effect checks across HTTP, Host, Bus and run manager; tightens API key issuance and persists versioned cursor signing. Delegated RDM rename still needs the separately proposed real-operator contract, so A1 and stage 6 are not complete.

Refs: Documents U3 r1, approved A1-A4 amendment fb3a821, pending delegated-projection amendment 5783940, Dev feature/iam-u3-account-directory-recovery.
2026-10-14 07:33:54 +00:00

103 lines
3.3 KiB
Go

package store
import (
"errors"
"path/filepath"
"testing"
"time"
core "agent-system-core"
)
func TestPluginAccountCheckedWritesStopAfterDisable(t *testing.T) {
st, err := OpenSQLite(filepath.Join(t.TempDir(), "plugins.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { st.Close() })
for _, a := range []struct {
owner string
admin bool
}{{"admin", true}, {"alice", false}} {
if err := st.CreateAccount(a.owner, "", "hash", a.admin); err != nil {
t.Fatal(err)
}
}
manifest := core.PluginManifest{ID: "p", Name: "P", Version: "1.0.0"}
if err := st.UpsertPluginAccountChecked("alice", "p", "/tmp/p", manifest, false); err != nil {
t.Fatal(err)
}
if err := st.SetPluginSchemaVersionAccountChecked("alice", "p", 2, false); err != nil {
t.Fatal(err)
}
account, err := st.GetAccountByOwner("alice")
if err != nil {
t.Fatal(err)
}
if _, err := st.SetAccountEnabled("admin", account.ID, account.StateVersion, false); err != nil {
t.Fatal(err)
}
manifest.Version = "2.0.0"
if err := st.UpsertPluginAccountChecked("alice", "p", "/tmp/p2", manifest, false); !errors.Is(err, ErrAccountForbidden) {
t.Fatalf("upsert after disable: %v", err)
}
if err := st.SetPluginSchemaVersionAccountChecked("alice", "p", 3, false); !errors.Is(err, ErrAccountForbidden) {
t.Fatalf("schema update after disable: %v", err)
}
plugins, err := st.ListPlugins()
if err != nil || len(plugins) != 1 || plugins[0].Version != "1.0.0" {
t.Fatalf("plugin row changed: %+v, %v", plugins, err)
}
if version, err := st.PluginSchemaVersion("p"); err != nil || version != 2 {
t.Fatalf("schema version changed: %d, %v", version, err)
}
}
func TestPGPluginAccountCheckedWriteSerializesWithDisable(t *testing.T) {
st := openTestStore(t)
if st.Dialect() != "postgres" {
t.Skip("real PostgreSQL required")
}
other := reopenAccountModelStore(t, st)
if err := st.CreateAccount("admin", "", "hash", true); err != nil {
t.Fatal(err)
}
if err := st.CreateAccount("alice", "", "hash", false); err != nil {
t.Fatal(err)
}
account, err := st.GetAccountByOwner("alice")
if err != nil {
t.Fatal(err)
}
release := holdPGAccountWrite(t, st, "plugins", "INSERT", "plugin_upsert")
writeDone := make(chan error, 1)
go func() {
writeDone <- st.UpsertPluginAccountChecked("alice", "p", "/tmp/p", core.PluginManifest{ID: "p", Name: "P", Version: "1"}, false)
}()
waitForPGAccountWrite(t, st, "plugin_upsert")
disableDone := make(chan error, 1)
go func() {
_, err := other.SetAccountEnabled("admin", account.ID, account.StateVersion, false)
disableDone <- err
}()
select {
case err := <-disableDone:
t.Fatalf("disable committed ahead of paused plugin upsert: %v", err)
case <-time.After(100 * time.Millisecond):
}
release()
if err := awaitPGAccountResult(t, writeDone); err != nil {
t.Fatal(err)
}
if err := awaitPGAccountResult(t, disableDone); err != nil {
t.Fatal(err)
}
if err := st.UpsertPluginAccountChecked("alice", "late", "/tmp/late", core.PluginManifest{ID: "late", Name: "Late", Version: "1"}, false); !errors.Is(err, ErrAccountForbidden) {
t.Fatalf("late plugin write passed after disable: %v", err)
}
plugins, err := st.ListPlugins()
if err != nil || len(plugins) != 1 || plugins[0].ID != "p" {
t.Fatalf("plugin writes crossed disable boundary: %+v, %v", plugins, err)
}
}