navidrome/server/subsonic/media_retrieval_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

80 lines
1.9 KiB
Go
Raw Normal View History

package subsonic
2020-01-09 02:45:07 +01:00
2020-01-10 03:58:03 +01:00
import (
2020-11-17 19:30:37 +01:00
"bytes"
"context"
2020-01-10 03:58:03 +01:00
"errors"
"io"
2020-11-17 19:30:37 +01:00
"io/ioutil"
2020-01-10 03:58:03 +01:00
"net/http/httptest"
2020-01-24 01:44:08 +01:00
"github.com/navidrome/navidrome/model"
2020-11-13 20:57:49 +01:00
"github.com/navidrome/navidrome/tests"
2020-01-10 03:58:03 +01:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("MediaRetrievalController", func() {
var controller *MediaRetrievalController
var artwork *fakeArtwork
2020-01-10 03:58:03 +01:00
var w *httptest.ResponseRecorder
BeforeEach(func() {
artwork = &fakeArtwork{}
2020-11-13 20:57:49 +01:00
controller = NewMediaRetrievalController(artwork, &tests.MockDataStore{})
2020-01-10 03:58:03 +01:00
w = httptest.NewRecorder()
})
Describe("GetCoverArt", func() {
It("should return data for that id", func() {
artwork.data = "image data"
r := newGetRequest("id=34", "size=128")
2020-01-10 03:58:03 +01:00
_, err := controller.GetCoverArt(w, r)
Expect(err).To(BeNil())
Expect(artwork.recvId).To(Equal("34"))
Expect(artwork.recvSize).To(Equal(128))
Expect(w.Body.String()).To(Equal(artwork.data))
2020-01-10 03:58:03 +01:00
})
It("should fail if missing id parameter", func() {
r := newGetRequest()
2020-01-10 03:58:03 +01:00
_, err := controller.GetCoverArt(w, r)
2020-10-27 20:23:29 +01:00
Expect(err).To(MatchError("required 'id' parameter is missing"))
2020-01-10 03:58:03 +01:00
})
It("should fail when the file is not found", func() {
artwork.err = model.ErrNotFound
r := newGetRequest("id=34", "size=128")
2020-01-10 03:58:03 +01:00
_, err := controller.GetCoverArt(w, r)
Expect(err).To(MatchError("Artwork not found"))
2020-01-10 03:58:03 +01:00
})
It("should fail when there is an unknown error", func() {
artwork.err = errors.New("weird error")
r := newGetRequest("id=34", "size=128")
2020-01-10 03:58:03 +01:00
_, err := controller.GetCoverArt(w, r)
2020-10-27 20:23:29 +01:00
Expect(err).To(MatchError("weird error"))
2020-01-10 03:58:03 +01:00
})
})
})
type fakeArtwork struct {
data string
err error
recvId string
recvSize int
}
2020-11-17 19:30:37 +01:00
func (c *fakeArtwork) Get(ctx context.Context, id string, size int) (io.ReadCloser, error) {
if c.err != nil {
2020-11-17 19:30:37 +01:00
return nil, c.err
}
c.recvId = id
c.recvSize = size
2020-11-17 19:30:37 +01:00
return ioutil.NopCloser(bytes.NewReader([]byte(c.data))), nil
}