2021-06-13 18:46:36 +02:00
|
|
|
package server
|
2020-04-06 21:37:15 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http/httptest"
|
2021-03-12 17:06:51 +01:00
|
|
|
"os"
|
2020-04-06 21:37:15 +02:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2021-04-07 04:18:48 +02:00
|
|
|
"strings"
|
2020-04-06 21:37:15 +02:00
|
|
|
|
|
|
|
"github.com/navidrome/navidrome/conf"
|
2022-11-29 20:40:44 +01:00
|
|
|
"github.com/navidrome/navidrome/conf/configtest"
|
2020-04-08 17:00:30 +02:00
|
|
|
"github.com/navidrome/navidrome/consts"
|
2020-04-06 21:37:15 +02:00
|
|
|
"github.com/navidrome/navidrome/model"
|
2020-10-27 16:01:40 +01:00
|
|
|
"github.com/navidrome/navidrome/tests"
|
2022-07-26 22:47:16 +02:00
|
|
|
. "github.com/onsi/ginkgo/v2"
|
2020-04-06 21:37:15 +02:00
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
2020-10-01 15:56:09 +02:00
|
|
|
var _ = Describe("serveIndex", func() {
|
2020-04-06 21:37:15 +02:00
|
|
|
var ds model.DataStore
|
|
|
|
mockUser := &mockedUserRepo{}
|
2021-03-12 17:06:51 +01:00
|
|
|
fs := os.DirFS("tests/fixtures")
|
2020-04-06 21:37:15 +02:00
|
|
|
|
|
|
|
BeforeEach(func() {
|
2020-10-27 16:01:40 +01:00
|
|
|
ds = &tests.MockDataStore{MockedUser: mockUser}
|
2022-11-29 20:40:44 +01:00
|
|
|
DeferCleanup(configtest.SetupConfig())
|
2020-04-06 21:37:15 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
It("adds app_config to index.html", func() {
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2020-10-01 15:56:09 +02:00
|
|
|
serveIndex(ds, fs)(w, r)
|
2020-04-06 21:37:15 +02:00
|
|
|
|
|
|
|
Expect(w.Code).To(Equal(200))
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
Expect(config).To(BeAssignableToTypeOf(map[string]interface{}{}))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("sets firstTime = true when User table is empty", func() {
|
|
|
|
mockUser.empty = true
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2020-10-01 15:56:09 +02:00
|
|
|
serveIndex(ds, fs)(w, r)
|
2020-04-06 21:37:15 +02:00
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
Expect(config).To(HaveKeyWithValue("firstTime", true))
|
|
|
|
})
|
|
|
|
|
2021-09-27 17:52:23 +02:00
|
|
|
It("includes the VariousArtistsID", func() {
|
|
|
|
mockUser.empty = true
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
serveIndex(ds, fs)(w, r)
|
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
Expect(config).To(HaveKeyWithValue("variousArtistsId", consts.VariousArtistsID))
|
|
|
|
})
|
|
|
|
|
2020-04-06 21:37:15 +02:00
|
|
|
It("sets firstTime = false when User table is not empty", func() {
|
|
|
|
mockUser.empty = false
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2020-10-01 15:56:09 +02:00
|
|
|
serveIndex(ds, fs)(w, r)
|
2020-04-06 21:37:15 +02:00
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
Expect(config).To(HaveKeyWithValue("firstTime", false))
|
|
|
|
})
|
|
|
|
|
2020-04-07 22:58:55 +02:00
|
|
|
It("sets baseURL", func() {
|
2020-04-06 21:37:15 +02:00
|
|
|
conf.Server.BaseURL = "base_url_test"
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2020-10-01 15:56:09 +02:00
|
|
|
serveIndex(ds, fs)(w, r)
|
2020-04-06 21:37:15 +02:00
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
Expect(config).To(HaveKeyWithValue("baseURL", "base_url_test"))
|
|
|
|
})
|
2020-04-07 22:58:55 +02:00
|
|
|
|
2020-07-03 17:51:15 +02:00
|
|
|
It("sets the welcomeMessage", func() {
|
|
|
|
conf.Server.UIWelcomeMessage = "Hello"
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2020-10-01 15:56:09 +02:00
|
|
|
serveIndex(ds, fs)(w, r)
|
2020-07-03 17:51:15 +02:00
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
Expect(config).To(HaveKeyWithValue("welcomeMessage", "Hello"))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("sets the enableTranscodingConfig", func() {
|
|
|
|
conf.Server.EnableTranscodingConfig = true
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2020-10-01 15:56:09 +02:00
|
|
|
serveIndex(ds, fs)(w, r)
|
2020-07-03 17:51:15 +02:00
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
Expect(config).To(HaveKeyWithValue("enableTranscodingConfig", true))
|
|
|
|
})
|
|
|
|
|
2020-11-10 22:14:43 +01:00
|
|
|
It("sets the enableDownloads", func() {
|
|
|
|
conf.Server.EnableDownloads = true
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
serveIndex(ds, fs)(w, r)
|
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
Expect(config).To(HaveKeyWithValue("enableDownloads", true))
|
|
|
|
})
|
|
|
|
|
2021-03-29 02:35:49 +02:00
|
|
|
It("sets the enableLoved", func() {
|
|
|
|
conf.Server.EnableFavourites = true
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
serveIndex(ds, fs)(w, r)
|
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
Expect(config).To(HaveKeyWithValue("enableFavourites", true))
|
|
|
|
})
|
|
|
|
|
2021-04-07 22:02:52 +02:00
|
|
|
It("sets the enableStarRating", func() {
|
|
|
|
conf.Server.EnableStarRating = true
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
serveIndex(ds, fs)(w, r)
|
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
Expect(config).To(HaveKeyWithValue("enableStarRating", true))
|
|
|
|
})
|
|
|
|
|
2021-04-18 19:51:00 +02:00
|
|
|
It("sets the defaultTheme", func() {
|
|
|
|
conf.Server.DefaultTheme = "Light"
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
serveIndex(ds, fs)(w, r)
|
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
Expect(config).To(HaveKeyWithValue("defaultTheme", "Light"))
|
|
|
|
})
|
|
|
|
|
2022-09-28 01:16:10 +02:00
|
|
|
It("sets the defaultLanguage", func() {
|
2022-10-18 17:44:16 +02:00
|
|
|
conf.Server.DefaultLanguage = "pt"
|
2022-09-28 01:16:10 +02:00
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
serveIndex(ds, fs)(w, r)
|
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
Expect(config).To(HaveKeyWithValue("defaultLanguage", "pt"))
|
|
|
|
})
|
|
|
|
|
2022-11-11 22:31:28 +01:00
|
|
|
It("sets the defaultUIVolume", func() {
|
|
|
|
conf.Server.DefaultUIVolume = 45
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
serveIndex(ds, fs)(w, r)
|
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
Expect(config).To(HaveKeyWithValue("defaultUIVolume", float64(45)))
|
|
|
|
})
|
|
|
|
|
2021-06-28 23:10:58 +02:00
|
|
|
It("sets the enableCoverAnimation", func() {
|
|
|
|
conf.Server.EnableCoverAnimation = true
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
serveIndex(ds, fs)(w, r)
|
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
Expect(config).To(HaveKeyWithValue("enableCoverAnimation", true))
|
|
|
|
})
|
|
|
|
|
2020-07-03 18:53:35 +02:00
|
|
|
It("sets the gaTrackingId", func() {
|
|
|
|
conf.Server.GATrackingID = "UA-12345"
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2020-10-01 15:56:09 +02:00
|
|
|
serveIndex(ds, fs)(w, r)
|
2020-07-03 18:53:35 +02:00
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
Expect(config).To(HaveKeyWithValue("gaTrackingId", "UA-12345"))
|
|
|
|
})
|
|
|
|
|
2020-04-08 17:00:30 +02:00
|
|
|
It("sets the version", func() {
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2020-10-01 15:56:09 +02:00
|
|
|
serveIndex(ds, fs)(w, r)
|
2020-04-08 17:00:30 +02:00
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
2022-09-15 03:09:39 +02:00
|
|
|
Expect(config).To(HaveKeyWithValue("version", consts.Version))
|
2020-04-08 17:00:30 +02:00
|
|
|
})
|
2021-04-07 04:18:48 +02:00
|
|
|
|
|
|
|
It("sets the losslessFormats", func() {
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
serveIndex(ds, fs)(w, r)
|
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
expected := strings.ToUpper(strings.Join(consts.LosslessFormats, ","))
|
|
|
|
Expect(config).To(HaveKeyWithValue("losslessFormats", expected))
|
|
|
|
})
|
2021-05-02 23:10:56 +02:00
|
|
|
|
|
|
|
It("sets the enableUserEditing", func() {
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
serveIndex(ds, fs)(w, r)
|
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
Expect(config).To(HaveKeyWithValue("enableUserEditing", true))
|
|
|
|
})
|
2021-05-30 21:36:10 +02:00
|
|
|
|
|
|
|
It("sets the devEnableShare", func() {
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
serveIndex(ds, fs)(w, r)
|
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
Expect(config).To(HaveKeyWithValue("devEnableShare", false))
|
|
|
|
})
|
2021-06-11 03:04:26 +02:00
|
|
|
|
2021-09-11 19:11:15 +02:00
|
|
|
It("sets the devSidebarPlaylists", func() {
|
|
|
|
conf.Server.DevSidebarPlaylists = true
|
|
|
|
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
serveIndex(ds, fs)(w, r)
|
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
Expect(config).To(HaveKeyWithValue("devSidebarPlaylists", true))
|
|
|
|
})
|
|
|
|
|
2021-07-02 15:50:07 +02:00
|
|
|
It("sets the lastFMEnabled", func() {
|
2021-06-11 03:04:26 +02:00
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
serveIndex(ds, fs)(w, r)
|
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
2021-07-02 15:50:07 +02:00
|
|
|
Expect(config).To(HaveKeyWithValue("lastFMEnabled", true))
|
2021-06-11 03:04:26 +02:00
|
|
|
})
|
2021-06-21 23:09:34 +02:00
|
|
|
|
|
|
|
It("sets the lastFMApiKey", func() {
|
|
|
|
conf.Server.LastFM.ApiKey = "APIKEY-123"
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
serveIndex(ds, fs)(w, r)
|
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
Expect(config).To(HaveKeyWithValue("lastFMApiKey", "APIKEY-123"))
|
|
|
|
})
|
2021-10-30 18:17:42 +02:00
|
|
|
|
2021-09-26 21:32:40 +02:00
|
|
|
It("sets the devShowArtistPage", func() {
|
|
|
|
conf.Server.DevShowArtistPage = true
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
serveIndex(ds, fs)(w, r)
|
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
Expect(config).To(HaveKeyWithValue("devShowArtistPage", true))
|
|
|
|
})
|
|
|
|
|
2021-11-18 03:11:53 +01:00
|
|
|
It("sets the listenBrainzEnabled", func() {
|
|
|
|
conf.Server.ListenBrainz.Enabled = true
|
2021-10-30 18:17:42 +02:00
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
serveIndex(ds, fs)(w, r)
|
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
2021-11-18 03:11:53 +01:00
|
|
|
Expect(config).To(HaveKeyWithValue("listenBrainzEnabled", true))
|
2021-10-30 18:17:42 +02:00
|
|
|
})
|
2022-11-29 20:40:44 +01:00
|
|
|
|
|
|
|
Describe("loginBackgroundURL", func() {
|
|
|
|
Context("empty BaseURL", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
conf.Server.BaseURL = "/"
|
|
|
|
})
|
|
|
|
When("it is the default URL", func() {
|
|
|
|
It("points to the default URL", func() {
|
|
|
|
conf.Server.UILoginBackgroundURL = consts.DefaultUILoginBackgroundURL
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
serveIndex(ds, fs)(w, r)
|
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
Expect(config).To(HaveKeyWithValue("loginBackgroundURL", consts.DefaultUILoginBackgroundURL))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
When("it is the default offline URL", func() {
|
|
|
|
It("points to the offline URL", func() {
|
|
|
|
conf.Server.UILoginBackgroundURL = consts.DefaultUILoginBackgroundURLOffline
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
serveIndex(ds, fs)(w, r)
|
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
Expect(config).To(HaveKeyWithValue("loginBackgroundURL", consts.DefaultUILoginBackgroundURLOffline))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
When("it is a custom URL", func() {
|
|
|
|
It("points to the offline URL", func() {
|
|
|
|
conf.Server.UILoginBackgroundURL = "https://example.com/images/1.jpg"
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
serveIndex(ds, fs)(w, r)
|
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
Expect(config).To(HaveKeyWithValue("loginBackgroundURL", "https://example.com/images/1.jpg"))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
Context("with a BaseURL", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
conf.Server.BaseURL = "/music"
|
|
|
|
})
|
|
|
|
When("it is the default URL", func() {
|
|
|
|
It("points to the default URL with BaseURL prefix", func() {
|
|
|
|
conf.Server.UILoginBackgroundURL = consts.DefaultUILoginBackgroundURL
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
serveIndex(ds, fs)(w, r)
|
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
Expect(config).To(HaveKeyWithValue("loginBackgroundURL", "/music"+consts.DefaultUILoginBackgroundURL))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
When("it is the default offline URL", func() {
|
|
|
|
It("points to the offline URL", func() {
|
|
|
|
conf.Server.UILoginBackgroundURL = consts.DefaultUILoginBackgroundURLOffline
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
serveIndex(ds, fs)(w, r)
|
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
Expect(config).To(HaveKeyWithValue("loginBackgroundURL", consts.DefaultUILoginBackgroundURLOffline))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
When("it is a custom URL", func() {
|
|
|
|
It("points to the offline URL", func() {
|
|
|
|
conf.Server.UILoginBackgroundURL = "https://example.com/images/1.jpg"
|
|
|
|
r := httptest.NewRequest("GET", "/index.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
serveIndex(ds, fs)(w, r)
|
|
|
|
|
|
|
|
config := extractAppConfig(w.Body.String())
|
|
|
|
Expect(config).To(HaveKeyWithValue("loginBackgroundURL", "https://example.com/images/1.jpg"))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2020-04-06 21:37:15 +02:00
|
|
|
})
|
|
|
|
|
2022-11-29 20:40:44 +01:00
|
|
|
var appConfigRegex = regexp.MustCompile(`(?m)window.__APP_CONFIG__=(.*);</script>`)
|
2020-04-06 21:37:15 +02:00
|
|
|
|
|
|
|
func extractAppConfig(body string) map[string]interface{} {
|
|
|
|
config := make(map[string]interface{})
|
|
|
|
match := appConfigRegex.FindStringSubmatch(body)
|
|
|
|
if match == nil {
|
|
|
|
return config
|
|
|
|
}
|
2022-11-29 20:40:44 +01:00
|
|
|
str, err := strconv.Unquote(match[1])
|
2020-04-06 21:37:15 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("%s: %s", match[1], err))
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal([]byte(str), &config); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockedUserRepo struct {
|
|
|
|
model.UserRepository
|
|
|
|
empty bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *mockedUserRepo) CountAll(...model.QueryOptions) (int64, error) {
|
|
|
|
if u.empty {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
return 1, nil
|
|
|
|
}
|