Files
DriverVault/API Server/internal/plugins/store_pb_test.go
T
tajniak81andClaude Opus 5 9bd5c523c4 Plugins: the global layer moves into the database, beside the other two
The integration cascade stored its top layer differently from the two below
it: org (L2) and user (L3) plugin config lived in PocketBase, in a
pluginSettings field, while the global (L1) layer sat in a plugins.json
next to the binary. That split was accretion rather than design - the file
was the whole store in the v1 MVP, and the per-tenant layers were later
built on PocketBase and layered on top of it instead of replacing it.

It also cost something real. plugins.json was a second state store with
different durability from pb_data: its own volume, its own ownership, its
own backup. Losing pb_data is unmissable; losing api_data was silent, which
is how "every plugin comes back disabled after a redeploy" happened.

L1 now lives in the app_settings collection - one record keyed "global",
holding its settings in a pluginSettings field, the same mechanism and the
same field name the layers below use. The documents still differ in shape,
because only L1 carries enable state and the registration of external
plugins, but the storage is no longer a special case.

The Manager grows a Store seam (PocketBase in production, file for the
import, memory for tests) and, more importantly, a loaded gate. Settings in
a database mean the store can be unreachable at boot - a cold stack, or a
service account still to be set from the panel. That must not read as "no
plugins configured", or the first save would write emptiness over real
settings. So until a read succeeds the Manager stays unloaded, every
mutation is refused, /api/admin/plugins* answers 503, and a background
retry backs off to two minutes. The same gate covers a document that will
not parse: it is never replaced by one built from an empty map, which is a
stronger guarantee than the .corrupt backup it replaces.

Writing to a store also revealed a hole in the previous fix. Classifying a
save failure as errPersist was left to each Store, and a store that
returned a plain error would fall through to the "saved, but the plugin
failed to start" branch and be reported as a 200 - the same silent-success
bug through a different door. The Manager now classifies, whatever the
Store returns; a test pins it.

Upgrades are automatic: on the first boot that finds no settings in the
database, an existing plugins.json is imported and renamed to
plugins.json.migrated. The import is refused if the store is merely
unreachable, or if the file does not parse, so a stale or broken file can
never overwrite live settings. /data is still needed - the panel rewrites
.env there when it retargets PocketBase - but plugin settings no longer
depend on it.

21 tests in internal/plugins cover both stores, including the production
path against a fake PocketBase: create-then-update of the singleton,
round-trip across a restart, an outage that leaves settings intact, a
missing collection reading as not-ready rather than empty, and the import
running exactly once. go build, go vet and go test ./... pass. Schema
changes are mirrored into scripts/setup-pocketbase.mjs as that file
requires. Not verified: no Docker CLI here, so no image was built and the
bootstrap of app_settings against a real PocketBase is untested outside the
fake.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-21 16:52:47 +02:00

237 lines
7.2 KiB
Go

package plugins
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"drivervault/apiserver/internal/pb"
)
// fakePB is the slice of PocketBase the settings store touches: superuser auth,
// and list/create/update on one collection. It holds a single record so the
// singleton behaviour can be asserted directly.
type fakePB struct {
mu sync.Mutex
recordID string // "" while no record exists
settings json.RawMessage // the stored pluginSettings value
listErr bool // make list calls fail (database unreachable)
creates int
updates int
missingCol bool // answer list with a 404, as if bootstrap had not run
}
func (f *fakePB) server(t *testing.T) *httptest.Server {
t.Helper()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
f.mu.Lock()
defer f.mu.Unlock()
switch {
case strings.HasSuffix(r.URL.Path, "/auth-with-password"):
writeTestJSON(w, http.StatusOK, map[string]any{"token": "test-token"})
case r.Method == http.MethodGet && r.URL.Path == "/api/collections/app_settings/records":
if f.listErr {
http.Error(w, "connection refused", http.StatusBadGateway)
return
}
if f.missingCol {
http.Error(w, `{"message":"Missing collection context."}`, http.StatusNotFound)
return
}
items := []any{}
if f.recordID != "" {
items = append(items, map[string]any{
"id": f.recordID,
"key": globalSettingsKey,
"pluginSettings": f.settings,
})
}
writeTestJSON(w, http.StatusOK, map[string]any{
"page": 1, "perPage": 1, "totalItems": len(items), "totalPages": 1,
"items": items,
})
case r.Method == http.MethodPost && r.URL.Path == "/api/collections/app_settings/records":
var body struct {
Key string `json:"key"`
PluginSettings json.RawMessage `json:"pluginSettings"`
}
_ = json.NewDecoder(r.Body).Decode(&body)
if body.Key != globalSettingsKey {
t.Errorf("create used key %q, want %q", body.Key, globalSettingsKey)
}
f.creates++
f.recordID = "rec1"
f.settings = body.PluginSettings
writeTestJSON(w, http.StatusOK, map[string]any{"id": f.recordID})
case r.Method == http.MethodPatch &&
strings.HasPrefix(r.URL.Path, "/api/collections/app_settings/records/"):
id := strings.TrimPrefix(r.URL.Path, "/api/collections/app_settings/records/")
if id != f.recordID {
http.Error(w, `{"message":"not found"}`, http.StatusNotFound)
return
}
var body struct {
PluginSettings json.RawMessage `json:"pluginSettings"`
}
_ = json.NewDecoder(r.Body).Decode(&body)
f.updates++
f.settings = body.PluginSettings
writeTestJSON(w, http.StatusOK, map[string]any{"id": id})
default:
t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path)
http.Error(w, "unexpected", http.StatusInternalServerError)
}
}))
t.Cleanup(srv.Close)
return srv
}
func writeTestJSON(w http.ResponseWriter, status int, v any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(v)
}
func newPBStore(t *testing.T, f *fakePB) Store {
t.Helper()
srv := f.server(t)
return NewPocketBaseStore(pb.New(srv.URL, "admin@test.local", "pw"), "app_settings")
}
// The first save creates the singleton; later saves update it in place.
func TestPBStoreCreatesThenUpdatesOneRecord(t *testing.T) {
f := &fakePB{}
m := loadedManager(t, newPBStore(t, f))
ctx := context.Background()
if _, err := m.Upsert(ctx, "test-a", true, map[string]string{"user": "me"}); err != nil {
t.Fatalf("first save: %v", err)
}
if _, err := m.Upsert(ctx, "test-b", true, nil); err != nil {
t.Fatalf("second save: %v", err)
}
f.mu.Lock()
creates, updates := f.creates, f.updates
f.mu.Unlock()
if creates != 1 {
t.Fatalf("expected exactly one record to be created, got %d", creates)
}
if updates != 1 {
t.Fatalf("expected the second save to update, got %d updates", updates)
}
}
// Settings written through PocketBase must come back on the next boot.
func TestPBStoreRoundTrip(t *testing.T) {
f := &fakePB{}
store := newPBStore(t, f)
m := loadedManager(t, store)
if _, err := m.Upsert(context.Background(), "test-a", true, map[string]string{"user": "me"}); err != nil {
t.Fatal(err)
}
// A fresh Manager on a fresh store, as after a restart.
m2 := loadedManager(t, newPBStore(t, f))
v, ok := m2.Get("test-a")
if !ok || !v.Enabled || v.Config["user"] != "me" {
t.Fatalf("settings did not survive: ok=%v enabled=%v config=%v", ok, v.Enabled, v.Config)
}
}
// A fresh install (no record yet) loads cleanly as "nothing configured".
func TestPBStoreEmptyIsNotAnError(t *testing.T) {
m := loadedManager(t, newPBStore(t, &fakePB{}))
for _, v := range m.List() {
if v.Enabled {
t.Fatalf("%s should not be enabled on a fresh install", v.Name)
}
}
}
// An unreachable database must not read as "no plugins configured", and must
// not let anything be written over settings that were never read.
func TestPBStoreUnreachableStaysUnloaded(t *testing.T) {
f := &fakePB{recordID: "rec1", settings: json.RawMessage(`{"test-a":{"enabled":true}}`), listErr: true}
store := newPBStore(t, f)
m := NewManager(store)
err := m.Load(context.Background())
if !IsNotReady(err) {
t.Fatalf("expected not-ready, got %v", err)
}
if m.Ready() {
t.Fatal("manager must not be ready")
}
if _, err := m.Upsert(context.Background(), "test-a", false, nil); !IsNotReady(err) {
t.Fatalf("Upsert should refuse, got %v", err)
}
// The database comes back; the real settings load and are intact.
f.mu.Lock()
f.listErr = false
f.mu.Unlock()
if err := m.Load(context.Background()); err != nil {
t.Fatalf("reload after recovery: %v", err)
}
if v, _ := m.Get("test-a"); !v.Enabled {
t.Fatal("settings lost across the outage")
}
}
// A missing collection (bootstrap has not run) is "not ready", never "empty" —
// otherwise the first save would write a fresh document over nothing.
func TestPBStoreMissingCollectionIsNotReady(t *testing.T) {
m := NewManager(newPBStore(t, &fakePB{missingCol: true}))
if err := m.Load(context.Background()); !IsNotReady(err) {
t.Fatalf("expected not-ready for a missing collection, got %v", err)
}
if m.Ready() {
t.Fatal("manager must not be ready without its collection")
}
}
// The legacy file is imported into PocketBase exactly once.
func TestPBStoreLegacyImport(t *testing.T) {
f := &fakePB{}
store := newPBStore(t, f)
path := t.TempDir() + "/plugins.json"
if err := writeTestFile(path, `{"test-a":{"enabled":true,"config":{"user":"me"}}}`); err != nil {
t.Fatal(err)
}
migrated, err := MigrateLegacyFile(context.Background(), store, path)
if err != nil || !migrated {
t.Fatalf("import: migrated=%v err=%v", migrated, err)
}
m := loadedManager(t, store)
if v, _ := m.Get("test-a"); !v.Enabled || v.Config["user"] != "me" {
t.Fatalf("imported settings wrong: enabled=%v config=%v", v.Enabled, v.Config)
}
// Second boot: the store now has a document, so nothing is re-imported.
migrated, err = MigrateLegacyFile(context.Background(), store, path)
if err != nil || migrated {
t.Fatalf("second import: migrated=%v err=%v", migrated, err)
}
f.mu.Lock()
creates := f.creates
f.mu.Unlock()
if creates != 1 {
t.Fatalf("expected one create, got %d", creates)
}
}