2026-04-27 19:36:57 -04:00
|
|
|
package public
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
2026-11-12 16:53:15 -05:00
|
|
|
"io"
|
2026-04-27 19:36:57 -04:00
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
2026-11-12 16:53:15 -05:00
|
|
|
"strings"
|
2026-04-27 19:36:57 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/go-chi/jwtauth/v5"
|
|
|
|
|
"github.com/navidrome/navidrome/core/auth"
|
|
|
|
|
"github.com/navidrome/navidrome/core/stream"
|
|
|
|
|
"github.com/navidrome/navidrome/model"
|
|
|
|
|
"github.com/navidrome/navidrome/tests"
|
|
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type mockStreamer struct {
|
2026-11-12 16:53:15 -05:00
|
|
|
req stream.Request
|
|
|
|
|
called bool
|
|
|
|
|
newStream func(*model.MediaFile) *stream.Stream
|
2026-04-27 19:36:57 -04:00
|
|
|
}
|
|
|
|
|
|
2026-11-12 16:53:15 -05:00
|
|
|
func (m *mockStreamer) NewStream(_ context.Context, mf *model.MediaFile, r stream.Request) (*stream.Stream, error) {
|
2026-04-27 19:36:57 -04:00
|
|
|
m.called = true
|
|
|
|
|
m.req = r
|
2026-11-12 16:53:15 -05:00
|
|
|
if m.newStream != nil {
|
|
|
|
|
return m.newStream(mf), nil
|
|
|
|
|
}
|
2026-04-27 19:36:57 -04:00
|
|
|
return nil, errors.New("mock: not implemented")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var _ = Describe("decodeStreamInfo", func() {
|
|
|
|
|
BeforeEach(func() {
|
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
|
|
|
auth.PublicTokenAuth = jwtauth.New("HS256", []byte("test-secret"), nil)
|
2026-04-27 19:36:57 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("decodes a valid token with all fields", func() {
|
|
|
|
|
claims := auth.Claims{ID: "mf-123", Format: "mp3", BitRate: 192, ShareID: "share123"}
|
|
|
|
|
token, _ := auth.CreateExpiringPublicToken(time.Now().Add(time.Hour), claims)
|
|
|
|
|
info, err := decodeStreamInfo(token)
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
Expect(info.id).To(Equal("mf-123"))
|
|
|
|
|
Expect(info.format).To(Equal("mp3"))
|
|
|
|
|
Expect(info.bitrate).To(Equal(192))
|
|
|
|
|
Expect(info.shareID).To(Equal("share123"))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("rejects an expired token", func() {
|
|
|
|
|
claims := auth.Claims{ID: "mf-123", ShareID: "share123"}
|
|
|
|
|
token, _ := auth.CreateExpiringPublicToken(time.Now().Add(-time.Hour), claims)
|
|
|
|
|
_, err := decodeStreamInfo(token)
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("accepts a token without exp (non-expiring share)", func() {
|
|
|
|
|
claims := auth.Claims{ID: "mf-123", ShareID: "share123"}
|
|
|
|
|
token, _ := auth.CreatePublicToken(claims)
|
|
|
|
|
info, err := decodeStreamInfo(token)
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
Expect(info.id).To(Equal("mf-123"))
|
|
|
|
|
Expect(info.shareID).To(Equal("share123"))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("rejects a token without an id claim", func() {
|
|
|
|
|
claims := auth.Claims{ShareID: "share123"}
|
|
|
|
|
token, _ := auth.CreatePublicToken(claims)
|
|
|
|
|
_, err := decodeStreamInfo(token)
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("rejects an invalid token string", func() {
|
|
|
|
|
_, err := decodeStreamInfo("not-a-valid-token")
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
|
})
|
|
|
|
|
|
2026-07-13 09:04:24 -04:00
|
|
|
It("rejects a token without a shareID claim", func() {
|
2026-04-27 19:36:57 -04:00
|
|
|
claims := auth.Claims{ID: "mf-123", Format: "opus"}
|
|
|
|
|
token, _ := auth.CreatePublicToken(claims)
|
2026-07-13 09:04:24 -04:00
|
|
|
_, err := decodeStreamInfo(token)
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
2026-04-27 19:36:57 -04:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
var _ = Describe("encodeMediafileShare", func() {
|
|
|
|
|
BeforeEach(func() {
|
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
|
|
|
auth.PublicTokenAuth = jwtauth.New("HS256", []byte("test-secret"), nil)
|
2026-04-27 19:36:57 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("includes the share ID in the token", func() {
|
2026-05-19 18:02:29 -03:00
|
|
|
exp := new(time.Now().Add(time.Hour))
|
2026-04-27 19:36:57 -04:00
|
|
|
s := model.Share{ID: "shareABC", Format: "mp3", MaxBitRate: 320, ExpiresAt: exp}
|
|
|
|
|
token := encodeMediafileShare(s, "mf-999")
|
|
|
|
|
info, err := decodeStreamInfo(token)
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
Expect(info.shareID).To(Equal("shareABC"))
|
|
|
|
|
Expect(info.id).To(Equal("mf-999"))
|
|
|
|
|
Expect(info.format).To(Equal("mp3"))
|
|
|
|
|
Expect(info.bitrate).To(Equal(320))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("creates a non-expiring token when share has no expiry", func() {
|
|
|
|
|
s := model.Share{ID: "shareXYZ", ExpiresAt: nil}
|
|
|
|
|
token := encodeMediafileShare(s, "mf-111")
|
|
|
|
|
info, err := decodeStreamInfo(token)
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
Expect(info.shareID).To(Equal("shareXYZ"))
|
|
|
|
|
Expect(info.id).To(Equal("mf-111"))
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
var _ = Describe("handleStream", func() {
|
2026-09-25 18:06:10 -04:00
|
|
|
var ctx context.Context
|
2026-04-27 19:36:57 -04:00
|
|
|
var ds *tests.MockDataStore
|
|
|
|
|
var shareRepo *tests.MockShareRepo
|
|
|
|
|
var streamer *mockStreamer
|
|
|
|
|
var pub *Router
|
|
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
2026-09-25 18:06:10 -04:00
|
|
|
ctx = GinkgoT().Context()
|
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
|
|
|
auth.PublicTokenAuth = jwtauth.New("HS256", []byte("test-secret"), nil)
|
fix(share): reuse cached transcodes for share streams and zip downloads (#6262)
* fix(share): reuse cached transcodes when streaming from share links
Public share streams built the stream request with only the share's format
and bit rate, leaving sample rate, bit depth and channels at zero. Regular
playback resolves those through the transcode decider (e.g. 48000 Hz for
Opus), and they are part of the transcoding cache key, so a track already
transcoded during normal playback was transcoded again into a separate,
identical cache entry when played through a share link.
The public router now resolves share stream requests with the same
TranscodeDecider.ResolveRequest used by the Subsonic stream endpoint, so
both paths produce the same request and share cache entries.
Fixes #6261
* fix(archiver): reuse cached transcodes when zipping downloads
Zip downloads (album, artist, playlist and share) built the stream request
with only the format and bit rate, leaving sample rate, bit depth and
channels at zero. Those are part of the transcoding cache key, so a track
already transcoded for playback was transcoded again into a separate cache
entry when downloaded in a zip, and vice versa.
The archiver now resolves each request with TranscodeDecider.ResolveRequest,
the same as single-song downloads and streams. This also applies the
decider's defaults, so a zip requested without a bit rate uses the target
format's default bit rate instead of leaving it to ffmpeg.
* fix(archiver): name zip entries after the resolved transcoding format
The transcode decider can pick a different format than the one requested
(for example a player's forced transcoding, or a fallback to the default
downsampling format when the requested one can't be produced). Zip entry
names and the playlist M3U were still built from the requested format, so
an entry could end in .mp3 or .flac while holding Opus data.
Each track's request is now resolved before its entry name is built, and
the name uses the resolved format.
2026-10-03 08:58:54 -07:00
|
|
|
ds = &tests.MockDataStore{MockedTranscoding: &tests.MockTranscodingRepo{}}
|
2026-04-27 19:36:57 -04:00
|
|
|
shareRepo = &tests.MockShareRepo{}
|
|
|
|
|
ds.MockedShare = shareRepo
|
|
|
|
|
streamer = &mockStreamer{}
|
fix(share): reuse cached transcodes for share streams and zip downloads (#6262)
* fix(share): reuse cached transcodes when streaming from share links
Public share streams built the stream request with only the share's format
and bit rate, leaving sample rate, bit depth and channels at zero. Regular
playback resolves those through the transcode decider (e.g. 48000 Hz for
Opus), and they are part of the transcoding cache key, so a track already
transcoded during normal playback was transcoded again into a separate,
identical cache entry when played through a share link.
The public router now resolves share stream requests with the same
TranscodeDecider.ResolveRequest used by the Subsonic stream endpoint, so
both paths produce the same request and share cache entries.
Fixes #6261
* fix(archiver): reuse cached transcodes when zipping downloads
Zip downloads (album, artist, playlist and share) built the stream request
with only the format and bit rate, leaving sample rate, bit depth and
channels at zero. Those are part of the transcoding cache key, so a track
already transcoded for playback was transcoded again into a separate cache
entry when downloaded in a zip, and vice versa.
The archiver now resolves each request with TranscodeDecider.ResolveRequest,
the same as single-song downloads and streams. This also applies the
decider's defaults, so a zip requested without a bit rate uses the target
format's default bit rate instead of leaving it to ffmpeg.
* fix(archiver): name zip entries after the resolved transcoding format
The transcode decider can pick a different format than the one requested
(for example a player's forced transcoding, or a fallback to the default
downsampling format when the requested one can't be produced). Zip entry
names and the playlist M3U were still built from the requested format, so
an entry could end in .mp3 or .flac while holding Opus data.
Each track's request is now resolved before its entry name is built, and
the name uses the resolved format.
2026-10-03 08:58:54 -07:00
|
|
|
pub = &Router{ds: ds, streamer: streamer, decider: stream.NewTranscodeDecider(ds, tests.NewMockFFmpeg(""))}
|
2026-04-27 19:36:57 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
makeRequest := func(token string) *httptest.ResponseRecorder {
|
2026-11-17 20:58:17 -05:00
|
|
|
r := httptest.NewRequest(http.MethodGet, "/public/s/token?%3Aid="+token, nil)
|
2026-04-27 19:36:57 -04:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
pub.handleStream(w, r)
|
|
|
|
|
return w
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-23 18:53:51 -04:00
|
|
|
shareOwnedBy := func(owner model.User, mf model.MediaFile) {
|
2026-04-27 19:36:57 -04:00
|
|
|
shareRepo.ID = "share123"
|
2026-07-13 09:04:24 -04:00
|
|
|
shareRepo.Entity = &model.Share{ID: "share123", UserID: owner.ID, Tracks: model.MediaFiles{mf}}
|
2026-06-23 18:53:51 -04:00
|
|
|
userRepo := tests.CreateMockUserRepo()
|
2026-09-25 18:06:10 -04:00
|
|
|
Expect(userRepo.Put(ctx, &owner)).To(Succeed())
|
2026-06-23 18:53:51 -04:00
|
|
|
ds.MockedUser = userRepo
|
2026-04-27 19:36:57 -04:00
|
|
|
mfRepo := tests.CreateMockMediaFileRepo()
|
2026-06-23 18:53:51 -04:00
|
|
|
mfRepo.SetData(model.MediaFiles{mf})
|
2026-04-27 19:36:57 -04:00
|
|
|
ds.MockedMediaFile = mfRepo
|
2026-06-23 18:53:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It("passes all validation and reaches the streamer for a valid token", func() {
|
|
|
|
|
shareOwnedBy(
|
|
|
|
|
model.User{ID: "owner1", UserName: "owner1", IsAdmin: true},
|
|
|
|
|
model.MediaFile{ID: "mf-123", Title: "Test Song"},
|
|
|
|
|
)
|
2026-04-27 19:36:57 -04:00
|
|
|
|
|
|
|
|
claims := auth.Claims{ID: "mf-123", Format: "mp3", BitRate: 192, ShareID: "share123"}
|
|
|
|
|
token, _ := auth.CreateExpiringPublicToken(time.Now().Add(time.Hour), claims)
|
|
|
|
|
makeRequest(token)
|
|
|
|
|
|
|
|
|
|
Expect(streamer.called).To(BeTrue())
|
fix(share): reuse cached transcodes for share streams and zip downloads (#6262)
* fix(share): reuse cached transcodes when streaming from share links
Public share streams built the stream request with only the share's format
and bit rate, leaving sample rate, bit depth and channels at zero. Regular
playback resolves those through the transcode decider (e.g. 48000 Hz for
Opus), and they are part of the transcoding cache key, so a track already
transcoded during normal playback was transcoded again into a separate,
identical cache entry when played through a share link.
The public router now resolves share stream requests with the same
TranscodeDecider.ResolveRequest used by the Subsonic stream endpoint, so
both paths produce the same request and share cache entries.
Fixes #6261
* fix(archiver): reuse cached transcodes when zipping downloads
Zip downloads (album, artist, playlist and share) built the stream request
with only the format and bit rate, leaving sample rate, bit depth and
channels at zero. Those are part of the transcoding cache key, so a track
already transcoded for playback was transcoded again into a separate cache
entry when downloaded in a zip, and vice versa.
The archiver now resolves each request with TranscodeDecider.ResolveRequest,
the same as single-song downloads and streams. This also applies the
decider's defaults, so a zip requested without a bit rate uses the target
format's default bit rate instead of leaving it to ffmpeg.
* fix(archiver): name zip entries after the resolved transcoding format
The transcode decider can pick a different format than the one requested
(for example a player's forced transcoding, or a fallback to the default
downsampling format when the requested one can't be produced). Zip entry
names and the playlist M3U were still built from the requested format, so
an entry could end in .mp3 or .flac while holding Opus data.
Each track's request is now resolved before its entry name is built, and
the name uses the resolved format.
2026-10-03 08:58:54 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("resolves the full stream request like the Subsonic endpoint, so transcodes share the cache", func() {
|
|
|
|
|
mf := model.MediaFile{ID: "mf-123", Suffix: "flac", BitRate: 1500, SampleRate: 44100, BitDepth: new(24), Channels: 2}
|
|
|
|
|
shareOwnedBy(model.User{ID: "owner1", UserName: "owner1", IsAdmin: true}, mf)
|
|
|
|
|
|
|
|
|
|
claims := auth.Claims{ID: "mf-123", Format: "opus", BitRate: 128, ShareID: "share123"}
|
|
|
|
|
token, _ := auth.CreateExpiringPublicToken(time.Now().Add(time.Hour), claims)
|
|
|
|
|
makeRequest(token)
|
|
|
|
|
|
|
|
|
|
Expect(streamer.req).To(Equal(stream.Request{Format: "opus", BitRate: 128, SampleRate: 48000, Channels: 2}))
|
2026-04-27 19:36:57 -04:00
|
|
|
})
|
|
|
|
|
|
2026-06-23 18:53:51 -04:00
|
|
|
It("returns 404 when the track is outside the share owner's libraries", func() {
|
|
|
|
|
shareOwnedBy(
|
|
|
|
|
model.User{ID: "owner1", UserName: "owner1", Libraries: model.Libraries{{ID: 1}}},
|
|
|
|
|
model.MediaFile{ID: "mf-restricted", Title: "Other Lib Track", LibraryID: 2},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
claims := auth.Claims{ID: "mf-restricted", ShareID: "share123"}
|
|
|
|
|
token, _ := auth.CreateExpiringPublicToken(time.Now().Add(time.Hour), claims)
|
|
|
|
|
w := makeRequest(token)
|
|
|
|
|
|
|
|
|
|
Expect(w.Code).To(Equal(http.StatusNotFound))
|
|
|
|
|
Expect(streamer.called).To(BeFalse())
|
|
|
|
|
})
|
|
|
|
|
|
2026-07-13 09:04:24 -04:00
|
|
|
It("returns 404 when the track is not a member of the share", func() {
|
|
|
|
|
owner := model.User{ID: "owner1", UserName: "owner1", IsAdmin: true}
|
|
|
|
|
userRepo := tests.CreateMockUserRepo()
|
2026-09-25 18:06:10 -04:00
|
|
|
Expect(userRepo.Put(ctx, &owner)).To(Succeed())
|
2026-07-13 09:04:24 -04:00
|
|
|
ds.MockedUser = userRepo
|
|
|
|
|
mfRepo := tests.CreateMockMediaFileRepo()
|
|
|
|
|
mfRepo.SetData(model.MediaFiles{{ID: "mf-shared"}, {ID: "mf-other"}})
|
|
|
|
|
ds.MockedMediaFile = mfRepo
|
|
|
|
|
shareRepo.ID = "share123"
|
|
|
|
|
shareRepo.Entity = &model.Share{ID: "share123", UserID: owner.ID, Tracks: model.MediaFiles{{ID: "mf-shared"}}}
|
|
|
|
|
|
|
|
|
|
claims := auth.Claims{ID: "mf-other", ShareID: "share123"}
|
|
|
|
|
token, _ := auth.CreateExpiringPublicToken(time.Now().Add(time.Hour), claims)
|
|
|
|
|
w := makeRequest(token)
|
|
|
|
|
|
|
|
|
|
Expect(w.Code).To(Equal(http.StatusNotFound))
|
|
|
|
|
Expect(streamer.called).To(BeFalse())
|
|
|
|
|
})
|
|
|
|
|
|
2026-06-23 18:53:51 -04:00
|
|
|
It("streams a track inside the share owner's libraries", func() {
|
|
|
|
|
shareOwnedBy(
|
|
|
|
|
model.User{ID: "owner1", UserName: "owner1", Libraries: model.Libraries{{ID: 1}}},
|
|
|
|
|
model.MediaFile{ID: "mf-ok", Title: "OK", LibraryID: 1},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
claims := auth.Claims{ID: "mf-ok", Format: "mp3", ShareID: "share123"}
|
|
|
|
|
token, _ := auth.CreateExpiringPublicToken(time.Now().Add(time.Hour), claims)
|
|
|
|
|
makeRequest(token)
|
|
|
|
|
|
|
|
|
|
Expect(streamer.called).To(BeTrue())
|
|
|
|
|
})
|
|
|
|
|
|
2026-11-12 16:53:15 -05:00
|
|
|
It("returns 200 on HEAD for a transcode that is not cached yet", func() {
|
|
|
|
|
shareOwnedBy(
|
|
|
|
|
model.User{ID: "owner1", UserName: "owner1", IsAdmin: true},
|
|
|
|
|
model.MediaFile{ID: "mf-123", Title: "Test Song"},
|
|
|
|
|
)
|
|
|
|
|
streamer.newStream = func(mf *model.MediaFile) *stream.Stream {
|
|
|
|
|
return stream.NewStream(mf, "mp3", 192, io.NopCloser(strings.NewReader("transcoded audio")))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
claims := auth.Claims{ID: "mf-123", Format: "mp3", BitRate: 192, ShareID: "share123"}
|
|
|
|
|
token, _ := auth.CreateExpiringPublicToken(time.Now().Add(time.Hour), claims)
|
|
|
|
|
r := httptest.NewRequest(http.MethodHead, "/public/s/token?%3Aid="+token, nil)
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
pub.handleStream(w, r)
|
|
|
|
|
|
|
|
|
|
Expect(w.Code).To(Equal(http.StatusOK))
|
|
|
|
|
})
|
|
|
|
|
|
2026-04-27 19:36:57 -04:00
|
|
|
It("returns 400 for an expired token", func() {
|
|
|
|
|
claims := auth.Claims{ID: "mf-123", ShareID: "share123"}
|
|
|
|
|
token, _ := auth.CreateExpiringPublicToken(time.Now().Add(-time.Hour), claims)
|
|
|
|
|
w := makeRequest(token)
|
|
|
|
|
Expect(w.Code).To(Equal(http.StatusBadRequest))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("returns 404 when share has been deleted", func() {
|
|
|
|
|
shareRepo.ID = "other-share"
|
|
|
|
|
claims := auth.Claims{ID: "mf-123", ShareID: "deleted-share"}
|
|
|
|
|
token, _ := auth.CreateExpiringPublicToken(time.Now().Add(time.Hour), claims)
|
|
|
|
|
w := makeRequest(token)
|
|
|
|
|
Expect(w.Code).To(Equal(http.StatusNotFound))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("returns 410 when share has been set to expired", func() {
|
|
|
|
|
shareRepo.ID = "share123"
|
2026-05-19 18:02:29 -03:00
|
|
|
shareRepo.Entity = &model.Share{ID: "share123", ExpiresAt: new(time.Now().Add(-time.Hour))}
|
2026-04-27 19:36:57 -04:00
|
|
|
|
|
|
|
|
claims := auth.Claims{ID: "mf-123", ShareID: "share123"}
|
|
|
|
|
token, _ := auth.CreatePublicToken(claims)
|
|
|
|
|
w := makeRequest(token)
|
|
|
|
|
Expect(w.Code).To(Equal(http.StatusGone))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("returns 500 when share lookup fails", func() {
|
|
|
|
|
shareRepo.Error = errors.New("db error")
|
|
|
|
|
claims := auth.Claims{ID: "mf-123", ShareID: "share123"}
|
|
|
|
|
token, _ := auth.CreateExpiringPublicToken(time.Now().Add(time.Hour), claims)
|
|
|
|
|
w := makeRequest(token)
|
|
|
|
|
Expect(w.Code).To(Equal(http.StatusInternalServerError))
|
|
|
|
|
})
|
|
|
|
|
|
2026-07-13 09:04:24 -04:00
|
|
|
It("returns 400 for tokens without a shareID", func() {
|
2026-04-27 19:36:57 -04:00
|
|
|
claims := auth.Claims{ID: "mf-123"}
|
|
|
|
|
token, _ := auth.CreatePublicToken(claims)
|
|
|
|
|
w := makeRequest(token)
|
2026-07-13 09:04:24 -04:00
|
|
|
Expect(w.Code).To(Equal(http.StatusBadRequest))
|
|
|
|
|
Expect(streamer.called).To(BeFalse())
|
2026-04-27 19:36:57 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("returns 400 for an invalid token", func() {
|
|
|
|
|
w := makeRequest("not-a-valid-token")
|
|
|
|
|
Expect(w.Code).To(Equal(http.StatusBadRequest))
|
|
|
|
|
})
|
|
|
|
|
})
|