navidrome/plugins/host_artwork_test.go
Deluan Quintão f853ca604a
refactor(db): migrate all ids to a uniform canonical 128-bit base62 encoding (#5824)
* refactor(model): extract canonical 128-bit base62 id codec

* feat(model): generate random ids as canonical 128-bit base62 values

* feat(scanner): emit legacy PIDs in canonical base62 encoding

* feat(db): add id canonicalization transform for the uniform-ids migration

* feat(db): migrate all ids to canonical 128-bit base62 encoding

* fix(db): canonicalize ids in junction tables and JSON columns

* chore(jellyfin): update id-family notes for uniform canonical ids

* test(ids): harden codec input contract and migration edge coverage

* refactor(model): use log.Fatal for Encode128 contract guard per project convention

* fix(db): force full rescan after id migration for legacy PID configs

* test(db): guard id-column inventory against schema drift

* refactor(ids): compile-time Encode128 contract and unified column rewrite helper

* refactor(db): apply review feedback to id migration

Filter empty strings in collectColumn's SQL, reuse a prepared statement
for rewriteColumn updates, and clarify the legacy ID functions' comment
now that they emit the canonical encoding.

* feat(auth): split session and public-link JWT secrets, rotating sessions on id migration

* test(subsonic): initialize public token secret in helpers suite

The suite sets auth.TokenAuth directly instead of calling auth.Init, so the
new PublicTokenAuth was nil whenever Ginkgo's spec order ran a helpers spec
before any spec that calls auth.Init, panicking in publicurl.ImageURL.

* refactor(db): inline canonicalID into its only consumer, the uniform-ids migration

* refactor(model): rename Encode128/Decode128 to Encode/Decode

With every id now exactly 128 bits, the width suffix is redundant; the
package-qualified id.Encode/id.Decode carries the same information.

* test(db): make the id-columns guard classify JSON columns too

The guard only inspected columns named id/pid/*_id, so it could not see ids
embedded in JSON. Widen it to *_ids and to every JSON column, and drive the
"covered" set from a new embeddedIDColumns list instead of the inline calls
in the migration.

Every JSON column the schema has now carries a verdict. The four denormalized
caches -- media_file/album.participants, media_file/album.tags,
album.folder_ids and artist.similar_artists -- hold only artist, tag and
folder ids. Those all come from id.NewHash, whose 22-char base62 encoding of
a 128-bit MD5 is already in canonical range, so canonicalID is the identity
on them and the migration correctly leaves them alone. A new codec test pins
that invariant, since the exemptions depend on it.

Verified on a copy of a 727MB/96k-track production database: canonicalizing
those four columns changed zero rows, and artist, tag and folder ids were
themselves unchanged by the migration (only media_file ids moved, 95108 of
96666).
2026-08-02 12:58:53 -04:00

241 lines
6.7 KiB
Go

//go:build !windows
package plugins
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"net/http"
"os"
"path/filepath"
"strings"
"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/tests"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("ArtworkService", Ordered, func() {
var (
manager *Manager
tmpDir string
)
BeforeAll(func() {
var err error
tmpDir, err = os.MkdirTemp("", "artwork-test-*")
Expect(err).ToNot(HaveOccurred())
// Copy the test-artwork plugin
srcPath := filepath.Join(testdataDir, "test-artwork"+PackageExtension)
destPath := filepath.Join(tmpDir, "test-artwork"+PackageExtension)
data, err := os.ReadFile(srcPath)
Expect(err).ToNot(HaveOccurred())
err = os.WriteFile(destPath, data, 0600)
Expect(err).ToNot(HaveOccurred())
// Compute SHA256 for the plugin
hash := sha256.Sum256(data)
hashHex := hex.EncodeToString(hash[:])
// Setup config
DeferCleanup(configtest.SetupConfig())
conf.Server.Plugins.Enabled = true
conf.Server.Plugins.Folder = conf.NewDir(tmpDir)
conf.Server.Plugins.AutoReload = false
// Initialize auth (required for token generation)
ds := &tests.MockDataStore{MockedProperty: &tests.MockedPropertyRepo{}}
auth.Init(ds)
// Setup mock DataStore with pre-enabled plugin
mockPluginRepo := tests.CreateMockPluginRepo()
mockPluginRepo.Permitted = true
mockPluginRepo.SetData(model.Plugins{{
ID: "test-artwork",
Path: destPath,
SHA256: hashHex,
Enabled: true,
}})
dataStore := &tests.MockDataStore{
MockedProperty: &tests.MockedPropertyRepo{},
MockedPlugin: mockPluginRepo,
}
// 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())
DeferCleanup(func() {
_ = manager.Stop()
_ = os.RemoveAll(tmpDir)
})
})
Describe("Plugin Loading", func() {
It("should load plugin with artwork permission", func() {
manager.mu.RLock()
p, ok := manager.plugins["test-artwork"]
manager.mu.RUnlock()
Expect(ok).To(BeTrue())
Expect(p.manifest.Permissions).ToNot(BeNil())
Expect(p.manifest.Permissions.Artwork).ToNot(BeNil())
})
})
Describe("Artwork URL Generation", func() {
type testArtworkInput struct {
ArtworkType string `json:"artwork_type"`
ID string `json:"id"`
Size int32 `json:"size"`
}
type testArtworkOutput struct {
URL string `json:"url,omitempty"`
Error *string `json:"error,omitempty"`
}
callTestArtwork := func(ctx context.Context, artworkType, id string, size int32) (string, error) {
manager.mu.RLock()
p := manager.plugins["test-artwork"]
manager.mu.RUnlock()
instance, err := p.instance(ctx)
if err != nil {
return "", err
}
defer instance.Close(ctx)
input := testArtworkInput{
ArtworkType: artworkType,
ID: id,
Size: size,
}
inputBytes, _ := json.Marshal(input)
_, outputBytes, err := instance.Call("nd_test_artwork", inputBytes)
if err != nil {
return "", err
}
var output testArtworkOutput
if err := json.Unmarshal(outputBytes, &output); err != nil {
return "", err
}
if output.Error != nil {
return "", Errorf(*output.Error)
}
return output.URL, nil
}
It("should generate artist artwork URL", func() {
url, err := callTestArtwork(GinkgoT().Context(), "artist", "ar-123", 0)
Expect(err).ToNot(HaveOccurred())
Expect(url).To(ContainSubstring("/img/"))
Expect(url).ToNot(ContainSubstring("size="))
// Decode JWT and verify artwork ID
artID := decodeArtworkURL(url)
Expect(artID.Kind).To(Equal(model.KindArtistArtwork))
Expect(artID.ID).To(Equal("ar-123"))
})
It("should generate album artwork URL", func() {
url, err := callTestArtwork(GinkgoT().Context(), "album", "al-456", 0)
Expect(err).ToNot(HaveOccurred())
Expect(url).To(ContainSubstring("/img/"))
artID := decodeArtworkURL(url)
Expect(artID.Kind).To(Equal(model.KindAlbumArtwork))
Expect(artID.ID).To(Equal("al-456"))
})
It("should generate track artwork URL", func() {
url, err := callTestArtwork(GinkgoT().Context(), "track", "mf-789", 0)
Expect(err).ToNot(HaveOccurred())
Expect(url).To(ContainSubstring("/img/"))
artID := decodeArtworkURL(url)
Expect(artID.Kind).To(Equal(model.KindMediaFileArtwork))
Expect(artID.ID).To(Equal("mf-789"))
})
It("should generate playlist artwork URL", func() {
url, err := callTestArtwork(GinkgoT().Context(), "playlist", "pl-abc", 0)
Expect(err).ToNot(HaveOccurred())
Expect(url).To(ContainSubstring("/img/"))
artID := decodeArtworkURL(url)
Expect(artID.Kind).To(Equal(model.KindPlaylistArtwork))
Expect(artID.ID).To(Equal("pl-abc"))
})
It("should include size parameter when specified", func() {
url, err := callTestArtwork(GinkgoT().Context(), "album", "al-456", 300)
Expect(err).ToNot(HaveOccurred())
Expect(url).To(ContainSubstring("size=300"))
artID := decodeArtworkURL(url)
Expect(artID.Kind).To(Equal(model.KindAlbumArtwork))
Expect(artID.ID).To(Equal("al-456"))
})
It("should handle unknown artwork type", func() {
_, err := callTestArtwork(GinkgoT().Context(), "unknown", "id-123", 0)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("unknown artwork type"))
})
})
})
// Errorf creates an error from a format string (helper for tests)
func Errorf(format string, args ...any) error {
return &errorString{s: format}
}
type errorString struct {
s string
}
func (e *errorString) Error() string {
return e.s
}
// decodeArtworkURL extracts and decodes the JWT token from an artwork URL,
// returning the parsed ArtworkID. Panics on error (test helper).
func decodeArtworkURL(artworkURL string) model.ArtworkID {
// URL format: http://localhost/img/<token>?size=...
// Extract token from path after /img/
idx := strings.Index(artworkURL, "/img/")
Expect(idx).To(BeNumerically(">=", 0), "URL should contain /img/")
tokenPart := artworkURL[idx+5:] // skip "/img/"
// Remove query string if present
if qIdx := strings.Index(tokenPart, "?"); qIdx >= 0 {
tokenPart = tokenPart[:qIdx]
}
// Decode JWT token
token, err := auth.PublicTokenAuth.Decode(tokenPart)
Expect(err).ToNot(HaveOccurred(), "Failed to decode JWT token")
c := auth.ClaimsFromToken(token)
id := c.ID
Expect(id).ToNot(BeEmpty(), "Token should contain 'id' claim")
artID, err := model.ParseArtworkID(id)
Expect(err).ToNot(HaveOccurred(), "Failed to parse artwork ID from token")
return artID
}