🎧 Your Personal Streaming Service
Go airsonicmadsonicmedia-servermusicmusic-servernavidromeopensubsonicraspberry-piself-hostedstreamerstreaming-apistreaming-audiosubsonicsubsonic-server
Add test for ServeIndex
Deluanc3edc7f4491c
diff --git a/server/app/app_suite_test.go b/server/app/app_suite_test.gonew file mode 100644index 000000000..d602d449c--- /dev/null+++ b/server/app/app_suite_test.go@@ -0,0 +1,17 @@+package app++import (+ "testing"++ "github.com/deluan/navidrome/log"+ "github.com/deluan/navidrome/tests"+ . "github.com/onsi/ginkgo"+ . "github.com/onsi/gomega"+)++func TestApp(t *testing.T) {+ tests.Init(t, false)+ log.SetLevel(log.LevelCritical)+ RegisterFailHandler(Fail)+ RunSpecs(t, "RESTful API Suite")+}diff --git a/server/app/serve_index_test.go b/server/app/serve_index_test.gonew file mode 100644index 000000000..9eaa58010--- /dev/null+++ b/server/app/serve_index_test.go@@ -0,0 +1,98 @@+package app++import (+ "encoding/json"+ "fmt"+ "net/http/httptest"+ "regexp"+ "strconv"++ "github.com/deluan/navidrome/conf"+ "github.com/deluan/navidrome/model"+ "github.com/deluan/navidrome/persistence"+ . "github.com/onsi/ginkgo"+ . "github.com/onsi/gomega"+)++var _ = Describe("ServeIndex", func() {+ var ds model.DataStore+ mockUser := &mockedUserRepo{}++ BeforeEach(func() {+ ds = &persistence.MockDataStore{MockedUser: mockUser}+ })++ It("adds app_config to index.html", func() {+ r := httptest.NewRequest("GET", "/index.html", nil)+ w := httptest.NewRecorder()++ ServeIndex(ds)(w, r)++ 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()++ ServeIndex(ds)(w, r)++ config := extractAppConfig(w.Body.String())+ Expect(config).To(HaveKeyWithValue("firstTime", true))+ })++ It("sets firstTime = false when User table is not empty", func() {+ mockUser.empty = false+ r := httptest.NewRequest("GET", "/index.html", nil)+ w := httptest.NewRecorder()++ ServeIndex(ds)(w, r)++ config := extractAppConfig(w.Body.String())+ Expect(config).To(HaveKeyWithValue("firstTime", false))+ })++ It("sets baseURL ", func() {+ conf.Server.BaseURL = "base_url_test"+ r := httptest.NewRequest("GET", "/index.html", nil)+ w := httptest.NewRecorder()++ ServeIndex(ds)(w, r)++ config := extractAppConfig(w.Body.String())+ Expect(config).To(HaveKeyWithValue("baseURL", "base_url_test"))+ })+})++var appConfigRegex = regexp.MustCompile(`(?m)window.__APP_CONFIG__="([^"]*)`)++func extractAppConfig(body string) map[string]interface{} {+ config := make(map[string]interface{})+ match := appConfigRegex.FindStringSubmatch(body)+ if match == nil {+ return config+ }+ str, err := strconv.Unquote("\"" + match[1] + "\"")+ 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+}