navidrome/plugins/host_users_test.go
Deluan Quintão b293b96256
refactor(persistence): stateless repositories with per-call context (#6149)
* refactor(persistence): adopt generic deluan/rest repository API

Pin deluan/rest to the refactor branch. REST-facing repository methods
take a context and return typed values. Drop DataStore.Resource and
ResourceRepository; the native API names typed repositories directly
through a per-request adapter that later commits remove.

* refactor(persistence): base repository helpers take a context

* refactor(persistence): LibraryRepository takes a context per call

* refactor(persistence): PropertyRepository takes a context per call

* refactor(persistence): UserPropsRepository takes a context per call

* refactor(persistence): TranscodingRepository takes a context per call

* refactor(persistence): ShareRepository takes a context per call

* refactor(persistence): PlayerRepository takes a context per call

* refactor(persistence): RadioRepository takes a context per call

* refactor(persistence): PlayQueueRepository takes a context per call

* refactor(persistence): Tag and Genre repositories take a context per call

* refactor(persistence): PluginRepository takes a context per call

* refactor(persistence): Scrobble repositories take a context per call

* refactor(persistence): FolderRepository takes a context per call

* refactor(persistence): Artwork repositories take a context per call

* refactor(persistence): UserRepository takes a context per call

* refactor(persistence): ArtistRepository takes a context per call

ReadAll no longer rewrites the shared sort mappings for the role filter;
it works on a per-call copy.

* test(persistence): assert artist role sort sanitization in ReadAll

* refactor(persistence): AlbumRepository takes a context per call

* test(persistence): pass the test context to album repository helpers

* refactor(persistence): MediaFileRepository takes a context per call

* refactor(persistence): Playlist repositories take a context per call

* refactor(persistence): build all repositories once per store

* refactor(core): REST repository wrappers are built once

* refactor(persistence): repositories are stateless

Remove the context field from the base repository and the per-request
REST adapter. Enable the containedctx linter so no repository can hold a
request context again.

* chore(lint): skip containedctx in test files

* refactor: share simplifications from the stateless repositories sweep

Add deleteOwnedAll on sqlRepository and use it in player/share Delete
to remove the duplicated bulk-delete loop; have Share.Repository()
return model.ShareRepository so subsonic sharing.go drops its repeated
type assertions.

* chore(core): assert REST wrappers implement Persistable

* chore: reformat imports

* perf(persistence): build repositories on first use

Each transaction store used to construct all 21 repositories up front,
paying for filter and sort mapping setup the block never touched. Fields
are now sync.OnceValue thunks, so a store only builds what it uses.

* fix(persistence): clean plugin references per deleted user

A bulk user delete that fails on a later id had already removed the
earlier rows but skipped their plugin cleanup. Cleanup now runs right
after each successful delete.

* fix(core): unload disabled plugins even when a user delete fails

A bulk delete can fail on a later id after earlier users were removed
and their plugins auto-disabled. The wrapper returned before unloading,
leaving those plugins running until the next successful delete or a
restart.

* chore(deps): pin deluan/rest to v1.0.1

Replaces the pseudo-version of the refactor branch with the tagged
release. REST error messages now name the bare type (Artist, not
model.Artist).

* test: use the spec context instead of context.Background()

Replace the context.Background()/context.TODO() calls this branch added
to tests with the spec's ctx, GinkgoT().Context(), or t/b.Context(), so
repository calls are bound to the running spec's lifetime.

* test: declare the spec context once per Describe

Set ctx from GinkgoT().Context() first in each top-level BeforeEach and reuse it, building user contexts on top of it instead of repeating inline calls.
2026-09-25 18:06:10 -04:00

586 lines
16 KiB
Go

package plugins
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"net/http"
"os"
"path/filepath"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/conf/configtest"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/plugins/host"
"github.com/navidrome/navidrome/tests"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("UsersService", Ordered, func() {
var (
ctx context.Context
ds model.DataStore
service host.UsersService
)
BeforeEach(func() {
ctx = GinkgoT().Context()
ds = &tests.MockDataStore{}
})
Describe("GetUsers", func() {
var mockUserRepo *tests.MockedUserRepo
BeforeEach(func() {
mockUserRepo = ds.User().(*tests.MockedUserRepo)
// Add test users
_ = mockUserRepo.Put(ctx, &model.User{
ID: "user1",
UserName: "alice",
Name: "Alice Admin",
IsAdmin: true,
})
_ = mockUserRepo.Put(ctx, &model.User{
ID: "user2",
UserName: "bob",
Name: "Bob User",
IsAdmin: false,
})
_ = mockUserRepo.Put(ctx, &model.User{
ID: "user3",
UserName: "charlie",
Name: "Charlie User",
IsAdmin: false,
})
})
Context("with allUsers=true", func() {
BeforeEach(func() {
service = newUsersService(ds, nil, true)
})
It("should return all users", func() {
users, err := service.GetUsers(ctx)
Expect(err).ToNot(HaveOccurred())
Expect(users).To(HaveLen(3))
// Verify that the correct fields are returned
userNames := make([]string, len(users))
for i, u := range users {
userNames[i] = u.UserName
}
Expect(userNames).To(ContainElements("alice", "bob", "charlie"))
})
It("should return correct user properties", func() {
users, err := service.GetUsers(ctx)
Expect(err).ToNot(HaveOccurred())
// Find alice
var alice *host.User
for i := range users {
if users[i].UserName == "alice" {
alice = &users[i]
break
}
}
Expect(alice).ToNot(BeNil())
Expect(alice.UserName).To(Equal("alice"))
Expect(alice.Name).To(Equal("Alice Admin"))
Expect(alice.IsAdmin).To(BeTrue())
})
})
Context("with specific allowed users", func() {
BeforeEach(func() {
// Only allow access to user1 and user3
service = newUsersService(ds, []string{"user1", "user3"}, false)
})
It("should return only allowed users", func() {
users, err := service.GetUsers(ctx)
Expect(err).ToNot(HaveOccurred())
Expect(users).To(HaveLen(2))
userNames := make([]string, len(users))
for i, u := range users {
userNames[i] = u.UserName
}
Expect(userNames).To(ContainElements("alice", "charlie"))
Expect(userNames).ToNot(ContainElement("bob"))
})
})
Context("with empty allowed users and allUsers=false", func() {
BeforeEach(func() {
service = newUsersService(ds, []string{}, false)
})
It("should return no users", func() {
users, err := service.GetUsers(ctx)
Expect(err).ToNot(HaveOccurred())
Expect(users).To(BeEmpty())
})
})
Context("when datastore returns error", func() {
BeforeEach(func() {
mockUserRepo.Error = model.ErrNotFound
service = newUsersService(ds, nil, true)
})
It("should propagate the error", func() {
_, err := service.GetUsers(ctx)
Expect(err).To(HaveOccurred())
})
})
})
Describe("GetAdmins", func() {
var mockUserRepo *tests.MockedUserRepo
BeforeEach(func() {
mockUserRepo = ds.User().(*tests.MockedUserRepo)
// Add test users - alice is admin, bob and charlie are not
_ = mockUserRepo.Put(ctx, &model.User{
ID: "user1",
UserName: "alice",
Name: "Alice Admin",
IsAdmin: true,
})
_ = mockUserRepo.Put(ctx, &model.User{
ID: "user2",
UserName: "bob",
Name: "Bob User",
IsAdmin: false,
})
_ = mockUserRepo.Put(ctx, &model.User{
ID: "user3",
UserName: "charlie",
Name: "Charlie User",
IsAdmin: false,
})
})
Context("with allUsers=true", func() {
BeforeEach(func() {
service = newUsersService(ds, nil, true)
})
It("should return only admin users", func() {
admins, err := service.GetAdmins(ctx)
Expect(err).ToNot(HaveOccurred())
Expect(admins).To(HaveLen(1))
Expect(admins[0].UserName).To(Equal("alice"))
Expect(admins[0].IsAdmin).To(BeTrue())
})
})
Context("with specific allowed users including admin", func() {
BeforeEach(func() {
// Allow access to user1 (admin) and user2 (non-admin)
service = newUsersService(ds, []string{"user1", "user2"}, false)
})
It("should return only admin users from allowed list", func() {
admins, err := service.GetAdmins(ctx)
Expect(err).ToNot(HaveOccurred())
Expect(admins).To(HaveLen(1))
Expect(admins[0].UserName).To(Equal("alice"))
})
})
Context("with specific allowed users excluding admin", func() {
BeforeEach(func() {
// Only allow access to non-admin users
service = newUsersService(ds, []string{"user2", "user3"}, false)
})
It("should return empty when no admins in allowed list", func() {
admins, err := service.GetAdmins(ctx)
Expect(err).ToNot(HaveOccurred())
Expect(admins).To(BeEmpty())
})
})
Context("when datastore returns error", func() {
BeforeEach(func() {
mockUserRepo.Error = model.ErrNotFound
service = newUsersService(ds, nil, true)
})
It("should propagate the error", func() {
_, err := service.GetAdmins(ctx)
Expect(err).To(HaveOccurred())
})
})
})
})
var _ = Describe("UsersService Integration", Ordered, func() {
var manager *Manager
BeforeAll(func() {
var cleanup func()
manager, cleanup = setupUsersIntegrationManager(true, "")
DeferCleanup(cleanup)
})
Describe("Plugin Loading", func() {
It("should load plugin with users permission", func() {
manager.mu.RLock()
p, ok := manager.plugins["test-users"]
manager.mu.RUnlock()
Expect(ok).To(BeTrue())
Expect(p.manifest.Permissions).ToNot(BeNil())
Expect(p.manifest.Permissions.Users).ToNot(BeNil())
})
})
Describe("Users Operations via Plugin", func() {
It("should get all users when allUsers is true", func() {
output, err := callTestUsersPlugin(GinkgoT().Context(), manager, testUsersInput{Operation: "get_users"})
Expect(err).ToNot(HaveOccurred())
Expect(output.Users).To(HaveLen(3))
// Verify user names
userNames := make([]string, len(output.Users))
for i, u := range output.Users {
userNames[i] = u.UserName
}
Expect(userNames).To(ContainElements("alice", "bob", "charlie"))
})
It("should return correct user properties", func() {
output, err := callTestUsersPlugin(GinkgoT().Context(), manager, testUsersInput{Operation: "get_users"})
Expect(err).ToNot(HaveOccurred())
// Find alice
var alice *testUser
for i := range output.Users {
if output.Users[i].UserName == "alice" {
alice = &output.Users[i]
break
}
}
Expect(alice).ToNot(BeNil())
Expect(alice.UserName).To(Equal("alice"))
Expect(alice.Name).To(Equal("Alice Admin"))
Expect(alice.IsAdmin).To(BeTrue())
})
It("should return non-admin user correctly", func() {
output, err := callTestUsersPlugin(GinkgoT().Context(), manager, testUsersInput{Operation: "get_users"})
Expect(err).ToNot(HaveOccurred())
// Find bob
var bob *testUser
for i := range output.Users {
if output.Users[i].UserName == "bob" {
bob = &output.Users[i]
break
}
}
Expect(bob).ToNot(BeNil())
Expect(bob.UserName).To(Equal("bob"))
Expect(bob.Name).To(Equal("Bob User"))
Expect(bob.IsAdmin).To(BeFalse())
})
})
Describe("GetAdmins Operations via Plugin", func() {
It("should get only admin users when allUsers is true", func() {
output, err := callTestUsersPlugin(GinkgoT().Context(), manager, testUsersInput{Operation: "get_admins"})
Expect(err).ToNot(HaveOccurred())
Expect(output.Users).To(HaveLen(1))
Expect(output.Users[0].UserName).To(Equal("alice"))
Expect(output.Users[0].IsAdmin).To(BeTrue())
})
})
})
var _ = Describe("UsersService Integration with Specific Users", Ordered, func() {
var manager *Manager
BeforeAll(func() {
var cleanup func()
manager, cleanup = setupUsersIntegrationManager(false, `["user1", "user3"]`)
DeferCleanup(cleanup)
})
Describe("Users Operations with Specific Allowed Users", func() {
It("should only return allowed users", func() {
output, err := callTestUsersPlugin(GinkgoT().Context(), manager, testUsersInput{Operation: "get_users"})
Expect(err).ToNot(HaveOccurred())
Expect(output.Users).To(HaveLen(2))
// Verify only alice and charlie are returned, not bob
userNames := make([]string, len(output.Users))
for i, u := range output.Users {
userNames[i] = u.UserName
}
Expect(userNames).To(ContainElements("alice", "charlie"))
Expect(userNames).ToNot(ContainElement("bob"))
})
It("should only return admin users from allowed list via GetAdmins", func() {
output, err := callTestUsersPlugin(GinkgoT().Context(), manager, testUsersInput{Operation: "get_admins"})
Expect(err).ToNot(HaveOccurred())
// Only alice (user1) is admin, charlie (user3) is not
Expect(output.Users).To(HaveLen(1))
Expect(output.Users[0].UserName).To(Equal("alice"))
Expect(output.Users[0].IsAdmin).To(BeTrue())
})
})
})
var _ = Describe("UsersService Integration GetAdmins with No Admins", Ordered, func() {
var manager *Manager
BeforeAll(func() {
var cleanup func()
// Only allow user2 (bob) and user3 (charlie), both non-admins
manager, cleanup = setupUsersIntegrationManager(false, `["user2", "user3"]`)
DeferCleanup(cleanup)
})
Describe("GetAdmins with no admin users in allowed list", func() {
It("should return empty when no admins in allowed list", func() {
output, err := callTestUsersPlugin(GinkgoT().Context(), manager, testUsersInput{Operation: "get_admins"})
Expect(err).ToNot(HaveOccurred())
Expect(output.Users).To(BeEmpty())
})
})
})
var _ = Describe("UsersService Enable Gate", Ordered, func() {
var manager *Manager
BeforeAll(func() {
var cleanup func()
// Start with disabled plugin, no users configured
manager, cleanup = setupUsersIntegrationManagerWithEnabled(false, false, "")
DeferCleanup(cleanup)
})
Describe("Enable Gate Behavior", func() {
It("should block enabling when no users configured and allUsers is false", func() {
ctx := GinkgoT().Context()
err := manager.EnablePlugin(ctx, "test-users")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("users permission requires configuration"))
})
It("should allow enabling when allUsers is true", func() {
ctx := GinkgoT().Context()
// Update the plugin to have allUsers=true
err := manager.UpdatePluginUsers(ctx, "test-users", "", true)
Expect(err).ToNot(HaveOccurred())
// Now enabling should succeed
err = manager.EnablePlugin(ctx, "test-users")
Expect(err).ToNot(HaveOccurred())
// Verify plugin is loaded
manager.mu.RLock()
_, ok := manager.plugins["test-users"]
manager.mu.RUnlock()
Expect(ok).To(BeTrue())
})
It("should allow enabling when specific users are configured", func() {
ctx := GinkgoT().Context()
// First disable the plugin
err := manager.DisablePlugin(ctx, "test-users")
Expect(err).ToNot(HaveOccurred())
// Update to have specific users (and allUsers=false)
err = manager.UpdatePluginUsers(ctx, "test-users", `["user1"]`, false)
Expect(err).ToNot(HaveOccurred())
// Now enabling should succeed
err = manager.EnablePlugin(ctx, "test-users")
Expect(err).ToNot(HaveOccurred())
// Verify plugin is loaded
manager.mu.RLock()
_, ok := manager.plugins["test-users"]
manager.mu.RUnlock()
Expect(ok).To(BeTrue())
})
})
})
// testUsersSetup contains common setup data for users integration tests
type testUsersSetup struct {
tmpDir string
destPath string
hashHex string
}
// setupTestUsersPlugin creates a temporary directory with the test-users plugin and returns setup info
func setupTestUsersPlugin() (*testUsersSetup, error) {
tmpDir, err := os.MkdirTemp("", "users-integration-test-*")
if err != nil {
return nil, err
}
// Copy the test-users plugin
srcPath := filepath.Join(testdataDir, "test-users"+PackageExtension)
destPath := filepath.Join(tmpDir, "test-users"+PackageExtension)
data, err := os.ReadFile(srcPath)
if err != nil {
_ = os.RemoveAll(tmpDir)
return nil, err
}
if err := os.WriteFile(destPath, data, 0600); err != nil {
_ = os.RemoveAll(tmpDir)
return nil, err
}
// Compute SHA256 for the plugin
hash := sha256.Sum256(data)
hashHex := hex.EncodeToString(hash[:])
return &testUsersSetup{
tmpDir: tmpDir,
destPath: destPath,
hashHex: hashHex,
}, nil
}
// createTestUsers creates standard test users in the mock repo
func createTestUsers(ctx context.Context, mockUserRepo *tests.MockedUserRepo) {
_ = mockUserRepo.Put(ctx, &model.User{
ID: "user1",
UserName: "alice",
Name: "Alice Admin",
IsAdmin: true,
})
_ = mockUserRepo.Put(ctx, &model.User{
ID: "user2",
UserName: "bob",
Name: "Bob User",
IsAdmin: false,
})
_ = mockUserRepo.Put(ctx, &model.User{
ID: "user3",
UserName: "charlie",
Name: "Charlie User",
IsAdmin: false,
})
}
// setupTestUsersConfig sets up common plugin configuration
func setupTestUsersConfig(tmpDir string) {
conf.Server.Plugins.Enabled = true
conf.Server.Plugins.Folder = conf.NewDir(tmpDir)
conf.Server.Plugins.AutoReload = false
}
// testUsersInput represents input for test-users plugin calls
type testUsersInput struct {
Operation string `json:"operation"`
}
// testUser represents a user returned from test-users plugin
type testUser struct {
UserName string `json:"userName"`
Name string `json:"name"`
IsAdmin bool `json:"isAdmin"`
}
// testUsersOutput represents output from test-users plugin
type testUsersOutput struct {
Users []testUser `json:"users,omitempty"`
Error *string `json:"error,omitempty"`
}
// callTestUsersPlugin calls the test-users plugin with given input
func callTestUsersPlugin(ctx context.Context, manager *Manager, input testUsersInput) (*testUsersOutput, error) {
manager.mu.RLock()
p := manager.plugins["test-users"]
manager.mu.RUnlock()
instance, err := p.instance(ctx)
if err != nil {
return nil, err
}
defer instance.Close(ctx)
inputBytes, _ := json.Marshal(input)
_, outputBytes, err := instance.Call("nd_test_users", inputBytes)
if err != nil {
return nil, err
}
var output testUsersOutput
if err := json.Unmarshal(outputBytes, &output); err != nil {
return nil, err
}
if output.Error != nil {
return nil, errors.New(*output.Error)
}
return &output, nil
}
// setupUsersIntegrationManager creates a Manager for users integration tests with the given plugin settings.
// The plugin is enabled by default.
func setupUsersIntegrationManager(allUsers bool, allowedUsers string) (*Manager, func()) {
return setupUsersIntegrationManagerWithEnabled(true, allUsers, allowedUsers)
}
// setupUsersIntegrationManagerWithEnabled creates a Manager for users integration tests with full control over plugin state
func setupUsersIntegrationManagerWithEnabled(enabled, allUsers bool, allowedUsers string) (*Manager, func()) {
setup, err := setupTestUsersPlugin()
Expect(err).ToNot(HaveOccurred())
// Setup config
cleanupConfig := configtest.SetupConfig()
setupTestUsersConfig(setup.tmpDir)
// Setup mock DataStore with plugin and users
mockPluginRepo := tests.CreateMockPluginRepo()
mockPluginRepo.Permitted = true
mockPluginRepo.SetData(model.Plugins{{
ID: "test-users",
Path: setup.destPath,
SHA256: setup.hashHex,
Enabled: enabled,
AllUsers: allUsers,
Users: allowedUsers,
}})
mockUserRepo := tests.CreateMockUserRepo()
createTestUsers(GinkgoT().Context(), mockUserRepo)
dataStore := &tests.MockDataStore{
MockedPlugin: mockPluginRepo,
MockedUser: mockUserRepo,
}
// Create and start manager
manager := &Manager{
plugins: make(map[string]*plugin),
ds: dataStore,
subsonicRouter: http.NotFoundHandler(),
}
err = manager.Start(GinkgoT().Context())
Expect(err).ToNot(HaveOccurred())
cleanup := func() {
_ = manager.Stop()
_ = os.RemoveAll(setup.tmpDir)
cleanupConfig()
}
return manager, cleanup
}