Files
navidrome/plugins/host_users_test.go
Deluan Quintão c9385fbb6b test(plugins): build test plugins in Go instead of shelling out to make (#6060)
* test(plugins): build test plugins in Go instead of shelling out to make

The plugins suite built its .ndp test packages by running `make -C
plugins/testdata`, which needs make and zip on the PATH. That is the reason
the 26 WASM-dependent spec files are tagged //go:build !windows.

buildTestPlugins now does the same work in Go: the same mtime check make
performed, `GOOS=wasip1 GOARCH=wasm go build` per plugin, and archive/zip
for the package. TinyGo was already optional and unused in CI, so nothing is
lost there. The first plugin builds on its own so the shared wasip1 stdlib
and PDK objects land in the build cache before the rest fan out: on a cold
cache that is 2.2s against 3.4s for the sequential make and 7.3s for an
unrestrained fan-out.

Packaging moved into a writeNdp helper shared with createTestPackage, which
was already writing the same two-entry archive. Entries are written in a
fixed order, so the .ndp bytes are now reproducible; the loader hashes those
bytes, and `zip` also stored file mtimes, so the previous packages differed
on every rebuild.

The Makefile is unchanged and still works for building the plugins by hand.
Removing the !windows tags is a separate step, once CI is green here.

* test(plugins): run the WASM plugin specs on Windows

With the test plugins now built in Go, nothing in the suite needs a Unix
toolchain, so the //go:build !windows tags come off all 25 spec files. The
Windows CI job runs `go test ./...`, so it picks the suite up with no
workflow change.

plugins_suite_windows_test.go existed only to bootstrap the handful of specs
that compiled on Windows; plugins_suite_test.go now serves both.

* test(plugins): skip the planted-symlink spec where symlinks need privileges

os.Symlink needs an elevated token or Developer Mode on Windows, so the
unconditional Expect(...).To(Succeed()) would fail for contributors running
the suite on an ordinary Windows box. The elevated GitHub runner hides this.

The equivalent spec in sandbox_fs_internal_test.go already attempts the
symlink and skips on error; this does the same, keeping the pin live
everywhere it can run, including Windows CI.

* fix(ci): stop the Windows ndpgen test failing silently

The ndpgen suite builds its helper binary to %TEMP%\ndpgen-test, and Windows
will not exec a file without an executable extension, so the "supports
verbose mode" spec has been failing there. Nobody noticed because the
Test ndpgen step ran under pwsh, which carries on after a non-zero exit and
takes the step's status from the last command, so the job stayed green with
a FAIL line in its log.

Add the .exe suffix, and run the step under bash like the Linux job does, so
a failure in any of its three commands fails the job.
2026-08-31 21:42:10 -04:00

587 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(ctx).(*tests.MockedUserRepo)
// Add test users
_ = mockUserRepo.Put(&model.User{
ID: "user1",
UserName: "alice",
Name: "Alice Admin",
IsAdmin: true,
})
_ = mockUserRepo.Put(&model.User{
ID: "user2",
UserName: "bob",
Name: "Bob User",
IsAdmin: false,
})
_ = mockUserRepo.Put(&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(ctx).(*tests.MockedUserRepo)
// Add test users - alice is admin, bob and charlie are not
_ = mockUserRepo.Put(&model.User{
ID: "user1",
UserName: "alice",
Name: "Alice Admin",
IsAdmin: true,
})
_ = mockUserRepo.Put(&model.User{
ID: "user2",
UserName: "bob",
Name: "Bob User",
IsAdmin: false,
})
_ = mockUserRepo.Put(&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(mockUserRepo *tests.MockedUserRepo) {
_ = mockUserRepo.Put(&model.User{
ID: "user1",
UserName: "alice",
Name: "Alice Admin",
IsAdmin: true,
})
_ = mockUserRepo.Put(&model.User{
ID: "user2",
UserName: "bob",
Name: "Bob User",
IsAdmin: false,
})
_ = mockUserRepo.Put(&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(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
}