Files
DriverVault/API Server/internal/api/control_e2e_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

246 lines
8.3 KiB
Go

package api
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"drivervault/apiserver/internal/config"
"drivervault/apiserver/internal/ocpp"
"drivervault/apiserver/internal/pb"
"drivervault/apiserver/internal/plugins"
)
// This test exercises the two PocketBase-touching control paths end-to-end
// through the real Handler + middleware chain: step-up re-authentication for a
// destructive action (reset), and best-effort audit persistence to the
// control_audit collection. A stand-in PocketBase (fakePB) serves exactly the
// endpoints the flow hits; a simulated charge point connects to the live CSMS so
// the command has a real session to reach.
const (
e2eUserID = "u1"
e2eUserMail = "owner@test.local"
e2ePassword = "correct-horse-battery"
e2eSerial = "CP-TEST"
e2eToken = "charger-control-token-abc"
e2eBearer = "user-bearer-token"
)
// fakePB records audit creates and answers the identity, user-record,
// re-auth and service-auth calls the control flow makes.
type fakePB struct {
mu sync.Mutex
audits []map[string]any
userPluginSettings json.RawMessage
}
func (f *fakePB) handler(t *testing.T) http.Handler {
mux := http.NewServeMux()
// Service-account auth (pb.Client.Authenticate).
mux.HandleFunc("POST /api/collections/_superusers/auth-with-password", func(w http.ResponseWriter, r *http.Request) {
writeJSON(w, 200, map[string]any{"token": "svc-token"})
})
// Identify the bearer (withAuth → identify → AuthRefresh).
mux.HandleFunc("POST /api/collections/users/auth-refresh", func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") == "" {
writeJSON(w, 401, map[string]any{})
return
}
writeJSON(w, 200, map[string]any{"record": map[string]any{
"id": e2eUserID, "email": e2eUserMail, "name": "Owner", "role": "user", "organization": "",
}})
})
// User record (userPluginSettings + callerForUser both GET this).
mux.HandleFunc("GET /api/collections/users/records/"+e2eUserID, func(w http.ResponseWriter, r *http.Request) {
writeJSON(w, 200, map[string]any{
"id": e2eUserID, "email": e2eUserMail, "role": "user", "organization": "",
"pluginSettings": f.userPluginSettings,
})
})
// Step-up re-auth (reauthenticate → LoginWithPassword).
mux.HandleFunc("POST /api/collections/users/auth-with-password", func(w http.ResponseWriter, r *http.Request) {
var body struct{ Identity, Password string }
_ = json.NewDecoder(r.Body).Decode(&body)
if body.Identity == e2eUserMail && body.Password == e2ePassword {
writeJSON(w, 200, map[string]any{"token": "reauth-ok", "record": map[string]any{"id": e2eUserID}})
return
}
writeJSON(w, 400, map[string]any{"message": "Failed to authenticate."})
})
// Durable audit sink.
mux.HandleFunc("POST /api/collections/control_audit/records", func(w http.ResponseWriter, r *http.Request) {
var rec map[string]any
_ = json.NewDecoder(r.Body).Decode(&rec)
f.mu.Lock()
f.audits = append(f.audits, rec)
f.mu.Unlock()
writeJSON(w, 200, map[string]any{"id": "audit-rec"})
})
return mux
}
func (f *fakePB) auditsWith(action, result string) int {
f.mu.Lock()
defer f.mu.Unlock()
n := 0
for _, a := range f.audits {
if a["action"] == action && strings.Contains(toStr(a["result"]), result) {
n++
}
}
return n
}
func toStr(v any) string {
s, _ := v.(string)
return s
}
func TestControlStepUpAndAuditE2E(t *testing.T) {
tokenHashHex := hashToken(e2eToken)
fake := &fakePB{
userPluginSettings: json.RawMessage(`{"ankerSolix":{"enabled":true,"config":{"controlMode":"own"},` +
`"controlChargers":{"` + e2eSerial + `":{"tokenHash":"` + tokenHashHex + `","tokenHint":"` + tokenHint(e2eToken) + `"}}}}`),
}
pbSrv := httptest.NewServer(fake.handler(t))
defer pbSrv.Close()
// Enable the anker-solix plugin globally via a plugins.json.
dir := t.TempDir()
pluginsFile := filepath.Join(dir, "plugins.json")
if err := os.WriteFile(pluginsFile, []byte(`{"anker-solix":{"enabled":true}}`), 0o600); err != nil {
t.Fatal(err)
}
s := New(config.Config{
UsersCollection: "users",
PluginsFile: pluginsFile,
OCPPRequireTLS: false, // httptest is plaintext; TLS enforcement covered elsewhere
}, pb.New(pbSrv.URL, "admin@test.local", "pw"))
// The global layer normally lives in PocketBase; point it at the file above
// so this test does not have to stand up an app_settings collection too.
s.pluginStore = plugins.NewFileStore(pluginsFile)
s.plugins = plugins.NewManager(s.pluginStore)
if err := s.plugins.Load(context.Background()); err != nil {
t.Fatalf("load plugins: %v", err)
}
// Seed the control-token index (mirrors what generating a token does).
s.control.setUser(e2eUserID, map[string]ankerChargerBinding{
e2eSerial: {TokenHash: tokenHashHex, TokenHint: tokenHint(e2eToken)},
})
appSrv := httptest.NewServer(s.Handler())
defer appSrv.Close()
// Connect a simulated charger to the live CSMS (own mode).
stop := connectCharger(t, "ws"+strings.TrimPrefix(appSrv.URL, "http")+"/ocpp/"+e2eSerial, e2eSerial, e2eToken)
defer stop()
// Wait for the session to register.
if !waitFor(2*time.Second, func() bool { _, ok := s.ocpp.SessionFor(e2eSerial); return ok }) {
t.Fatal("charger session never registered on the CSMS")
}
// 1. Destructive reset without confirm → 400.
if code, _ := postControl(t, appSrv.URL, "reset", map[string]any{}); code != http.StatusBadRequest {
t.Errorf("reset without confirm: status %d, want 400", code)
}
// 2. Confirmed reset with the WRONG password → 401 (step-up fails).
if code, _ := postControl(t, appSrv.URL, "reset", map[string]any{"confirm": true, "password": "wrong"}); code != http.StatusUnauthorized {
t.Errorf("reset with wrong password: status %d, want 401", code)
}
// 3. Confirmed reset with the CORRECT password → 200 and the charger accepts.
code, body := postControl(t, appSrv.URL, "reset", map[string]any{"confirm": true, "password": e2ePassword})
if code != http.StatusOK {
t.Fatalf("reset with correct password: status %d body %s, want 200", code, body)
}
if got := body["status"]; got != "Accepted" {
t.Errorf("reset status = %v, want Accepted", got)
}
// The audit sink must have recorded the failed re-auth and the accepted reset
// (persisted asynchronously, so poll).
if !waitFor(2*time.Second, func() bool { return fake.auditsWith("reset", "reauth-failed") >= 1 }) {
t.Error("expected a control_audit record for the failed re-auth")
}
if !waitFor(2*time.Second, func() bool { return fake.auditsWith("reset", "Accepted") >= 1 }) {
t.Error("expected a control_audit record for the accepted reset")
}
}
// postControl issues an authenticated control command and returns the status and
// decoded JSON body.
func postControl(t *testing.T, base, action string, payload map[string]any) (int, map[string]any) {
t.Helper()
b, _ := json.Marshal(payload)
req, _ := http.NewRequest(http.MethodPost,
base+"/api/integrations/anker-solix/chargers/"+e2eSerial+"/"+action, bytes.NewReader(b))
req.Header.Set("Authorization", e2eBearer)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("%s: %v", action, err)
}
defer resp.Body.Close()
var out map[string]any
_ = json.NewDecoder(resp.Body).Decode(&out)
return resp.StatusCode, out
}
// connectCharger dials the CSMS as an OCPP charge point and answers every inbound
// control CALL with {status:"Accepted"}. Returns a stop function.
func connectCharger(t *testing.T, url, serial, token string) func() {
t.Helper()
h := http.Header{}
h.Set("Authorization", ocpp.BasicAuthHeader(serial, token))
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
conn, err := ocpp.Dial(ctx, url, []string{"ocpp1.6"}, h)
if err != nil {
t.Fatalf("charger dial: %v", err)
}
go func() {
for {
data, err := conn.ReadMessage()
if err != nil {
return
}
msg, err := ocpp.DecodeMessage(data)
if err != nil || msg.Type != ocpp.MessageTypeCall {
continue
}
out, _ := ocpp.EncodeCallResult(msg.ID, map[string]any{"status": "Accepted"})
_ = conn.WriteMessage(out)
}
}()
return func() { _ = conn.Close() }
}
func waitFor(d time.Duration, cond func() bool) bool {
deadline := time.Now().Add(d)
for time.Now().Before(deadline) {
if cond() {
return true
}
time.Sleep(10 * time.Millisecond)
}
return cond()
}