Skip to content

Commit 8d7d233

Browse files
committed
Unit test the Subsonic endpoint for getting artwork
1 parent 6e9f3e7 commit 8d7d233

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package subsonic_test
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/ironsmile/euterpe/src/assert"
10+
"github.com/ironsmile/euterpe/src/config"
11+
"github.com/ironsmile/euterpe/src/library/libraryfakes"
12+
"github.com/ironsmile/euterpe/src/playlists/playlistsfakes"
13+
"github.com/ironsmile/euterpe/src/radio/radiofakes"
14+
"github.com/ironsmile/euterpe/src/webserver/subsonic"
15+
"github.com/ironsmile/euterpe/src/webserver/subsonic/subsonicfakes"
16+
)
17+
18+
// TestGetCoverArt checks that getting cover art for artists, albums and
19+
// tracks works and the correct artwork is recognized by the id string
20+
// format.
21+
func TestGetCoverArt(t *testing.T) {
22+
const (
23+
albumArtwork = `album artwork body`
24+
artistArtwork = `artist artwork body`
25+
)
26+
27+
albumArtFinder := &subsonicfakes.FakeCoverArtHandler{
28+
FindStub: func(w http.ResponseWriter, _ *http.Request, id int64) error {
29+
if id != 42 {
30+
w.WriteHeader(http.StatusNotFound)
31+
return nil
32+
}
33+
34+
fmt.Fprint(w, albumArtwork)
35+
return nil
36+
},
37+
}
38+
artistArtFinder := &subsonicfakes.FakeCoverArtHandler{
39+
FindStub: func(w http.ResponseWriter, _ *http.Request, id int64) error {
40+
if id != 42 {
41+
w.WriteHeader(http.StatusNotFound)
42+
return nil
43+
}
44+
45+
fmt.Fprint(w, artistArtwork)
46+
return nil
47+
},
48+
}
49+
50+
ssHandler := subsonic.NewHandler(
51+
subsonic.Prefix,
52+
&libraryfakes.FakeLibrary{},
53+
&libraryfakes.FakeBrowser{},
54+
&radiofakes.FakeStations{},
55+
&playlistsfakes.FakePlaylister{},
56+
config.Config{
57+
Authenticate: config.Auth{
58+
User: "test-user",
59+
},
60+
},
61+
albumArtFinder,
62+
artistArtFinder,
63+
)
64+
65+
req := httptest.NewRequest(http.MethodGet, "/rest/getCoverArt?id=al-42", nil)
66+
rec := httptest.NewRecorder()
67+
ssHandler.ServeHTTP(rec, req)
68+
assert.Equal(t, http.StatusOK, rec.Result().StatusCode, "HTTP status code")
69+
assert.Equal(t, albumArtwork, rec.Body.String(), "album response body")
70+
assert.Equal(t, 1, albumArtFinder.FindCallCount(), "wrong number of Find calls")
71+
_, findReq, findID := albumArtFinder.FindArgsForCall(albumArtFinder.FindCallCount() - 1)
72+
assert.Equal(t, 42, findID, "wrong album ID send to the art finder")
73+
assert.Equal(t, "", findReq.URL.Query().Get("size"),
74+
"size shouldn't have been set for art finder request",
75+
)
76+
77+
// Check for handling of clients which wrongly just send the album ID
78+
// without any prefix.
79+
req = httptest.NewRequest(http.MethodGet, "/rest/getCoverArt?id=42", nil)
80+
rec = httptest.NewRecorder()
81+
ssHandler.ServeHTTP(rec, req)
82+
assert.Equal(t, http.StatusOK, rec.Result().StatusCode, "HTTP status code")
83+
assert.Equal(t, albumArtwork, rec.Body.String(), "album response body")
84+
assert.Equal(t, 2, albumArtFinder.FindCallCount(), "wrong number of Find calls")
85+
86+
req = httptest.NewRequest(http.MethodGet, "/rest/getCoverArt?id=ar-42", nil)
87+
rec = httptest.NewRecorder()
88+
ssHandler.ServeHTTP(rec, req)
89+
assert.Equal(t, http.StatusOK, rec.Result().StatusCode, "HTTP status code")
90+
assert.Equal(t, artistArtwork, rec.Body.String(), "artist response body")
91+
assert.Equal(t, 1, artistArtFinder.FindCallCount(), "wrong number of Find calls")
92+
93+
// Check for handling of clients which wrongly just send the artist ID without any
94+
// prefix.
95+
artistArtURL := fmt.Sprintf("/rest/getCoverArt?id=%d", int64(1e9+42))
96+
req = httptest.NewRequest(http.MethodGet, artistArtURL, nil)
97+
rec = httptest.NewRecorder()
98+
ssHandler.ServeHTTP(rec, req)
99+
assert.Equal(t, http.StatusOK, rec.Result().StatusCode, "HTTP status code")
100+
assert.Equal(t, artistArtwork, rec.Body.String(), "artist response body")
101+
assert.Equal(t, 2, artistArtFinder.FindCallCount(), "wrong number of Find calls")
102+
103+
// Check the size argument handling.
104+
req = httptest.NewRequest(http.MethodGet, "/rest/getCoverArt?id=al-42&size=120", nil)
105+
rec = httptest.NewRecorder()
106+
ssHandler.ServeHTTP(rec, req)
107+
assert.Equal(t, http.StatusOK, rec.Result().StatusCode, "HTTP status code")
108+
assert.Equal(t, albumArtwork, rec.Body.String(), "album response body")
109+
assert.Equal(t, 3, albumArtFinder.FindCallCount(), "wrong number of Find calls")
110+
_, findReq, findID = albumArtFinder.FindArgsForCall(albumArtFinder.FindCallCount() - 1)
111+
assert.Equal(t, 42, findID, "wrong album ID send to the art finder")
112+
assert.Equal(t, "small", findReq.URL.Query().Get("size"),
113+
"wrong size requested from the art finder",
114+
)
115+
116+
notFoundTests := []struct {
117+
desc string
118+
url string
119+
}{
120+
{
121+
desc: "playlists have no artwork",
122+
url: "/rest/getCoverArt?id=pl-42",
123+
},
124+
{
125+
desc: "tracks have no artwork",
126+
url: fmt.Sprintf("/rest/getCoverArt?id=%d", int64(2e9+42)),
127+
},
128+
{
129+
desc: "malformed URLs have no artwork",
130+
url: "/rest/getCoverArt?id=baba",
131+
},
132+
{
133+
desc: "album with no artwork in the DB",
134+
url: "/rest/getCoverArt?id=al-12",
135+
},
136+
{
137+
desc: "artist with no artwork in the DB",
138+
url: "/rest/getCoverArt?id=ar-12",
139+
},
140+
{
141+
desc: "malformed artist URL",
142+
url: "/rest/getCoverArt?id=ar-baba",
143+
},
144+
{
145+
desc: "malformed album URL",
146+
url: "/rest/getCoverArt?id=al-baba",
147+
},
148+
}
149+
150+
for _, test := range notFoundTests {
151+
t.Run(test.desc, func(t *testing.T) {
152+
req = httptest.NewRequest(http.MethodGet, test.url, nil)
153+
rec = httptest.NewRecorder()
154+
ssHandler.ServeHTTP(rec, req)
155+
assert.Equal(t, http.StatusNotFound, rec.Result().StatusCode, "HTTP status code")
156+
})
157+
}
158+
}

0 commit comments

Comments
 (0)