Files
DriverVault/API Server/internal/api/settings_test.go
T
tajniak81andClaude Opus 5 215c027ada Panel: give the API Server a name, and a tab to set it in
The Web App can be pointed at more than one DriverVault, but a server it
adds is only ever identified by the URL that was typed into the connect
dialog. Nothing on the other end says what it is called, so the switcher
has no name to show that the operator did not invent locally.

So the server now carries one. SERVER_NAME joins the config, defaulting to
"DriverVault API Server" so /api/health always has something a client can
display rather than an empty string every caller has to special-case.

GET/PUT /api/admin/server-config follow the pb-config and webapp-config
shape exactly: superadmin only, applied at runtime and then persisted to
.env, with the same "applied but could not be saved" warning when the write
fails. There is no /test sibling, because a name is a label and not an
address - there is nothing to probe. The length cap counts runes rather
than bytes, so a 64-character Polish or Danish name is not cut off at the
halfway mark.

/api/health reports it, unauthenticated, which is the point of the whole
change: a client adding this server by URL can label it from the probe it
already makes, instead of needing a second and authenticated call before it
can draw the entry.

In the panel it is a new API Server tab, first in the superadmin group
since it is this server itself, ahead of the PocketBase and Web App tabs
that describe what it talks to. Strings in all three languages, and the
route table in the README and the API reference tab both grow the two new
endpoints.

Known gap, deliberately not closed here: the compose files do not pass
SERVER_NAME, so under Docker a rename from the panel writes the container's
.env and no volume keeps it - it reverts to the default the next time the
container is recreated. Wiring it as ${SERVER_NAME:-} would make the host
.env authoritative, at the cost of the other trap the previous commit
documented, where the environment silently overrides the panel on every
restart. That is a call about the deployment, not about this endpoint.

Verified by new tests over the handler: the rename applies at runtime,
lands in .env, reaches /api/health, is rejected without touching .env when
blank or over-long, and accepts a name of exactly the limit in multi-byte
runes. go build, go vet and go test ./... pass. Drove the built panel in a
browser against a stub backend - the tab renders, loads the current name,
saves, and reads correctly in Polish - and ran the rebuilt api-server.exe
and webapp.exe end to end, confirming the embedded bundle really contains
the new tab and that SERVER_NAME reaches /api/health through both the
server itself and the Web App's proxy.

Not verified: no Docker build, so the images still serve the old panel
until they are rebuilt and pushed.

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

115 lines
3.6 KiB
Go

package api
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"drivervault/apiserver/internal/config"
"drivervault/apiserver/internal/pb"
)
// The server name is applied at runtime and persisted to .env, and /api/health
// reports it without a restart — the three things the panel's API Server tab
// relies on.
func TestServerConfigRenameAppliesAndPersists(t *testing.T) {
t.Chdir(t.TempDir()) // UpdateEnvFile writes ./.env
s := New(config.Config{ServerName: config.DefaultServerName}, pb.New("", "", ""))
rec := httptest.NewRecorder()
s.handleUpdateServerConfig(rec, httptest.NewRequest(http.MethodPut, "/api/admin/server-config",
strings.NewReader(`{"name":" Home Garage "}`)))
if rec.Code != http.StatusOK {
t.Fatalf("rename: got %d, want 200: %s", rec.Code, rec.Body)
}
var out struct {
Config serverConfigView `json:"config"`
Warning string `json:"warning"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil {
t.Fatalf("decode: %v", err)
}
if out.Warning != "" {
t.Fatalf("unexpected warning: %s", out.Warning)
}
if out.Config.Name != "Home Garage" {
t.Errorf("name = %q, want %q (surrounding space trimmed)", out.Config.Name, "Home Garage")
}
if got := s.serverName(); got != "Home Garage" {
t.Errorf("runtime name = %q, want %q", got, "Home Garage")
}
env, err := os.ReadFile(config.EnvFile)
if err != nil {
t.Fatalf("read .env: %v", err)
}
if !strings.Contains(string(env), "SERVER_NAME=Home Garage") {
t.Errorf(".env missing the rename, got:\n%s", env)
}
// The name reaches clients through the public liveness probe.
rec = httptest.NewRecorder()
s.handleHealth(rec, httptest.NewRequest(http.MethodGet, "/api/health", nil))
var health struct {
Name string `json:"name"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &health); err != nil {
t.Fatalf("decode health: %v", err)
}
if health.Name != "Home Garage" {
t.Errorf("health name = %q, want %q", health.Name, "Home Garage")
}
}
func TestServerConfigRejectsUnusableNames(t *testing.T) {
t.Chdir(t.TempDir())
cases := map[string]string{
"empty": `{"name":""}`,
"blank": `{"name":" "}`,
"too long": `{"name":"` + strings.Repeat("x", maxServerNameLen+1) + `"}`,
"bad json": `{`,
"no name": `{}`,
}
for name, body := range cases {
t.Run(name, func(t *testing.T) {
s := New(config.Config{ServerName: config.DefaultServerName}, pb.New("", "", ""))
rec := httptest.NewRecorder()
s.handleUpdateServerConfig(rec, httptest.NewRequest(http.MethodPut, "/api/admin/server-config",
strings.NewReader(body)))
if rec.Code != http.StatusBadRequest {
t.Fatalf("got %d, want 400: %s", rec.Code, rec.Body)
}
if got := s.serverName(); got != config.DefaultServerName {
t.Errorf("name changed to %q on a rejected request", got)
}
if _, err := os.Stat(config.EnvFile); err == nil {
t.Error(".env written for a rejected request")
}
})
}
}
// A name of exactly the limit is accepted — the bound is inclusive, and it is
// counted in runes, so a 64-character non-Latin name fits.
func TestServerConfigCountsRunesNotBytes(t *testing.T) {
t.Chdir(t.TempDir())
s := New(config.Config{ServerName: config.DefaultServerName}, pb.New("", "", ""))
name := strings.Repeat("ł", maxServerNameLen)
rec := httptest.NewRecorder()
s.handleUpdateServerConfig(rec, httptest.NewRequest(http.MethodPut, "/api/admin/server-config",
strings.NewReader(`{"name":"`+name+`"}`)))
if rec.Code != http.StatusOK {
t.Fatalf("got %d, want 200: %s", rec.Code, rec.Body)
}
if got := s.serverName(); got != name {
t.Errorf("name = %q, want the %d-rune name", got, maxServerNameLen)
}
}