From 7ba2b0e0e6def5ac2451a2ba0729d3903e935e58 Mon Sep 17 00:00:00 2001 From: Deluan Date: Fri, 13 Nov 2026 11:54:25 -0500 Subject: [PATCH] fix: adapt to copy-on-write config --- core/apiauth/apiauth_suite_test.go | 3 +++ core/apiauth/db_test.go | 6 +++--- core/apiauth/service.go | 2 +- core/apiauth/service_test.go | 8 +++----- core/auth/first_admin_test.go | 7 ++++--- persistence/user_repository_test.go | 6 ++---- server/apiv1/auth_handlers_test.go | 4 +--- server/apiv1/gate.go | 4 ++-- server/apiv1/gate_test.go | 4 +--- server/apiv1/server_handlers_test.go | 2 -- server/middlewares_test.go | 2 +- 11 files changed, 21 insertions(+), 27 deletions(-) diff --git a/core/apiauth/apiauth_suite_test.go b/core/apiauth/apiauth_suite_test.go index 5d6143b0e..eaeb11f05 100644 --- a/core/apiauth/apiauth_suite_test.go +++ b/core/apiauth/apiauth_suite_test.go @@ -3,6 +3,7 @@ package apiauth import ( "testing" + "github.com/navidrome/navidrome/conf/configtest" "github.com/navidrome/navidrome/log" "github.com/navidrome/navidrome/tests" . "github.com/onsi/ginkgo/v2" @@ -15,3 +16,5 @@ func TestAPIAuth(t *testing.T) { RegisterFailHandler(Fail) RunSpecs(t, "API Auth Suite") } + +var _ = configtest.RestoreAfterEachSpec() diff --git a/core/apiauth/db_test.go b/core/apiauth/db_test.go index 5f54e6007..0911676d7 100644 --- a/core/apiauth/db_test.go +++ b/core/apiauth/db_test.go @@ -5,7 +5,6 @@ import ( "path/filepath" "github.com/navidrome/navidrome/conf" - "github.com/navidrome/navidrome/conf/configtest" "github.com/navidrome/navidrome/db" "github.com/navidrome/navidrome/model" "github.com/navidrome/navidrome/model/id" @@ -18,8 +17,9 @@ var realDS model.DataStore // One database for the whole suite: db.Db() is a process-wide singleton. var _ = BeforeSuite(func() { - DeferCleanup(configtest.SetupConfig()) - conf.Server.DbPath = filepath.Join(GinkgoT().TempDir(), "apiauth.db") + "?_journal_mode=WAL&_foreign_keys=on&_busy_timeout=5000" + conf.Update(func(c *conf.Options) { + c.DbPath = filepath.Join(GinkgoT().TempDir(), "apiauth.db") + "?_journal_mode=WAL&_foreign_keys=on&_busy_timeout=5000" + }) DeferCleanup(db.Init(GinkgoT().Context())) realDS = persistence.New(db.Db()) }) diff --git a/core/apiauth/service.go b/core/apiauth/service.go index 3ec70476f..1b4a871e6 100644 --- a/core/apiauth/service.go +++ b/core/apiauth/service.go @@ -60,7 +60,7 @@ func New(ds model.DataStore) *Service { } func PasswordChangeable(u model.User) bool { - return u.IsAdmin || conf.Server.EnableUserEditing + return u.IsAdmin || conf.Server().EnableUserEditing } func (s *Service) Login(ctx context.Context, username, password string, meta ClientMeta, scopes []string) (*Issued, error) { diff --git a/core/apiauth/service_test.go b/core/apiauth/service_test.go index 6482122c7..82e6c87bd 100644 --- a/core/apiauth/service_test.go +++ b/core/apiauth/service_test.go @@ -7,7 +7,6 @@ import ( "time" "github.com/navidrome/navidrome/conf" - "github.com/navidrome/navidrome/conf/configtest" "github.com/navidrome/navidrome/core/auth" "github.com/navidrome/navidrome/model" "github.com/navidrome/navidrome/model/request" @@ -24,7 +23,6 @@ var _ = Describe("Service", func() { BeforeEach(func() { ctx = GinkgoT().Context() - DeferCleanup(configtest.SetupConfig()) now = time.Now().UTC().Truncate(time.Second) svc = New(realDS) svc.SetClock(func() time.Time { return now }) @@ -331,7 +329,7 @@ var _ = Describe("Service", func() { }) It("is forbidden for non-admins when user editing is off", func() { - conf.Server.EnableUserEditing = false + conf.Update(func(c *conf.Options) { c.EnableUserEditing = false }) u := createUser(ctx, "pw", false) _, p := login(ctx, svc, u, "pw", nil) err := svc.ChangePassword(request.WithUser(ctx, p.User), p, "pw", "pw2", true) @@ -391,10 +389,10 @@ var _ = Describe("Service", func() { Describe("PasswordChangeable", func() { It("follows EnableUserEditing for non-admins only", func() { - conf.Server.EnableUserEditing = false + conf.Update(func(c *conf.Options) { c.EnableUserEditing = false }) Expect(PasswordChangeable(model.User{IsAdmin: true})).To(BeTrue()) Expect(PasswordChangeable(model.User{})).To(BeFalse()) - conf.Server.EnableUserEditing = true + conf.Update(func(c *conf.Options) { c.EnableUserEditing = true }) Expect(PasswordChangeable(model.User{})).To(BeTrue()) }) }) diff --git a/core/auth/first_admin_test.go b/core/auth/first_admin_test.go index 0cbbcf575..4fda0b900 100644 --- a/core/auth/first_admin_test.go +++ b/core/auth/first_admin_test.go @@ -8,7 +8,6 @@ import ( "time" "github.com/navidrome/navidrome/conf" - "github.com/navidrome/navidrome/conf/configtest" "github.com/navidrome/navidrome/core/auth" "github.com/navidrome/navidrome/db" "github.com/navidrome/navidrome/model" @@ -22,8 +21,10 @@ var _ = Describe("CreateFirstAdmin", Ordered, func() { var ds model.DataStore BeforeAll(func() { - DeferCleanup(configtest.SetupConfig()) - conf.Server.DbPath = filepath.Join(GinkgoT().TempDir(), "first-admin.db") + "?_journal_mode=WAL&_foreign_keys=on&_busy_timeout=5000" + DeferCleanup(conf.SnapshotConfig()) + conf.Update(func(c *conf.Options) { + c.DbPath = filepath.Join(GinkgoT().TempDir(), "first-admin.db") + "?_journal_mode=WAL&_foreign_keys=on&_busy_timeout=5000" + }) DeferCleanup(db.Init(GinkgoT().Context())) ds = persistence.New(db.Db()) }) diff --git a/persistence/user_repository_test.go b/persistence/user_repository_test.go index 156d43253..ebe8a2a96 100644 --- a/persistence/user_repository_test.go +++ b/persistence/user_repository_test.go @@ -11,7 +11,6 @@ import ( "github.com/Masterminds/squirrel" "github.com/deluan/rest" "github.com/navidrome/navidrome/conf" - "github.com/navidrome/navidrome/conf/configtest" "github.com/navidrome/navidrome/consts" "github.com/navidrome/navidrome/log" "github.com/navidrome/navidrome/model" @@ -125,9 +124,8 @@ var _ = Describe("UserRepository", func() { Describe("initPasswordEncryptionKey", func() { It("never logs the encryption key checksum, but still logs its property id", func() { - DeferCleanup(configtest.SetupConfig()) - conf.Server.PasswordEncryptionKey = "a-new-password-encryption-key" - keySum := fmt.Sprintf("%x", sha256.Sum256(keyTo32Bytes(conf.Server.PasswordEncryptionKey))) + conf.Update(func(c *conf.Options) { c.PasswordEncryptionKey = "a-new-password-encryption-key" }) + keySum := fmt.Sprintf("%x", sha256.Sum256(keyTo32Bytes(conf.Server().PasswordEncryptionKey))) previousKey := encKey DeferCleanup(func() { encKey = previousKey }) tx, err := GetDBXBuilder().Begin() diff --git a/server/apiv1/auth_handlers_test.go b/server/apiv1/auth_handlers_test.go index 48c2748ad..1edce1bd6 100644 --- a/server/apiv1/auth_handlers_test.go +++ b/server/apiv1/auth_handlers_test.go @@ -9,7 +9,6 @@ import ( "sync" "github.com/navidrome/navidrome/conf" - "github.com/navidrome/navidrome/conf/configtest" "github.com/navidrome/navidrome/core/auth" "github.com/navidrome/navidrome/model" . "github.com/onsi/ginkgo/v2" @@ -22,8 +21,7 @@ var _ = Describe("auth endpoints", func() { BeforeEach(func() { ctx = GinkgoT().Context() - DeferCleanup(configtest.SetupConfig()) - conf.Server.AuthRequestLimit = 0 + conf.Update(func(c *conf.Options) { c.AuthRequestLimit = 0 }) resetDB() api = testClient{ctx: ctx, router: New(realDS)} }) diff --git a/server/apiv1/gate.go b/server/apiv1/gate.go index 9d6c20716..f9af390e4 100644 --- a/server/apiv1/gate.go +++ b/server/apiv1/gate.go @@ -81,8 +81,8 @@ func newGate(doc *openapi3.T, mux chi.Routes, auth authenticator, rules gateRule if err := rules.check(ids); err != nil { return nil, err } - if conf.Server.AuthRequestLimit > 0 { - g.limiter = server.ClientIPRateLimiter(conf.Server.AuthRequestLimit, conf.Server.AuthWindowLength, + if cfg := conf.Server(); cfg.AuthRequestLimit > 0 { + g.limiter = server.ClientIPRateLimiter(cfg.AuthRequestLimit, cfg.AuthWindowLength, // Counts are per node, so X-RateLimit-Remaining would mislead clients of a scaled-out server. httprate.WithResponseHeaders(httprate.ResponseHeaders{RetryAfter: "Retry-After"}), httprate.WithLimitHandler(func(w http.ResponseWriter, r *http.Request) { diff --git a/server/apiv1/gate_test.go b/server/apiv1/gate_test.go index f748957fc..ba2a61b9e 100644 --- a/server/apiv1/gate_test.go +++ b/server/apiv1/gate_test.go @@ -12,7 +12,6 @@ import ( "github.com/getkin/kin-openapi/openapi3" "github.com/go-chi/chi/v5" "github.com/navidrome/navidrome/conf" - "github.com/navidrome/navidrome/conf/configtest" "github.com/navidrome/navidrome/core/apiauth" "github.com/navidrome/navidrome/log" "github.com/navidrome/navidrome/model" @@ -321,8 +320,7 @@ var _ = Describe("spec gate", func() { }) It("rate-limits the listed operations with a 429 problem", func() { - DeferCleanup(configtest.SetupConfig()) - conf.Server.AuthRequestLimit = 1 + conf.Update(func(c *conf.Options) { c.AuthRequestLimit = 1 }) var err error mux, err = build(gateSpec) Expect(err).ToNot(HaveOccurred()) diff --git a/server/apiv1/server_handlers_test.go b/server/apiv1/server_handlers_test.go index 59d0dcb3d..6b10ce52e 100644 --- a/server/apiv1/server_handlers_test.go +++ b/server/apiv1/server_handlers_test.go @@ -8,7 +8,6 @@ import ( "net/http/httptest" "github.com/navidrome/navidrome/api" - "github.com/navidrome/navidrome/conf/configtest" "github.com/navidrome/navidrome/consts" "github.com/navidrome/navidrome/model" "github.com/navidrome/navidrome/tests" @@ -68,7 +67,6 @@ var _ = Describe("GET /capabilities", func() { BeforeEach(func() { ctx = GinkgoT().Context() - DeferCleanup(configtest.SetupConfig()) resetDB() api = testClient{ctx: ctx, router: New(realDS)} }) diff --git a/server/middlewares_test.go b/server/middlewares_test.go index f20600ad6..d707b20dd 100644 --- a/server/middlewares_test.go +++ b/server/middlewares_test.go @@ -495,7 +495,7 @@ var _ = Describe("middlewares", func() { var addr, ip string BeforeEach(func() { ctx = GinkgoT().Context() - conf.Server.ExtAuth.TrustedSources = "10.0.0.0/8" + conf.Update(func(c *conf.Options) { c.ExtAuth.TrustedSources = "10.0.0.0/8" }) }) call := func(h http.Handler, peer, xff string) { r := httptest.NewRequestWithContext(ctx, "POST", "/auth/login", nil)