mirror of
https://github.com/navidrome/navidrome.git
synced 2025-06-10 20:32:27 +03:00
Add test for ServeIndex
This commit is contained in:
parent
9b272c8021
commit
c3edc7f449
17
server/app/app_suite_test.go
Normal file
17
server/app/app_suite_test.go
Normal file
@ -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")
|
||||||
|
}
|
98
server/app/serve_index_test.go
Normal file
98
server/app/serve_index_test.go
Normal file
@ -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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user