Restore MediaRetrievalController tests

This commit is contained in:
Deluan
2020-01-09 21:58:03 -05:00
committed by Deluan Quintão
parent 0c3edc0279
commit b1e58352e9
5 changed files with 88 additions and 141 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ import (
) )
func TestSubsonicApi(t *testing.T) { func TestSubsonicApi(t *testing.T) {
log.SetLevel(log.LevelError) log.SetLevel(log.LevelCritical)
RegisterFailHandler(Fail) RegisterFailHandler(Fail)
RunSpecs(t, "Subsonic API Suite") RunSpecs(t, "Subsonic API Suite")
} }
+75 -73
View File
@@ -1,74 +1,76 @@
package api_test package api
// import (
//import ( "errors"
// "fmt" "io"
// "net/http" "net/http/httptest"
// "net/http/httptest"
// "testing" "github.com/cloudsonic/sonic-server/domain"
// . "github.com/onsi/ginkgo"
// "github.com/astaxie/beego" . "github.com/onsi/gomega"
// "github.com/cloudsonic/sonic-server/api/responses" )
// "github.com/cloudsonic/sonic-server/domain"
// "github.com/cloudsonic/sonic-server/persistence" type fakeCover struct {
// . "github.com/cloudsonic/sonic-server/tests" data string
// "github.com/cloudsonic/sonic-server/utils" err error
// . "github.com/smartystreets/goconvey/convey" recvId string
//) recvSize int
// }
//func getCoverArt(params ...string) (*http.Request, *httptest.ResponseRecorder) {
// url := AddParams("/rest/getCoverArt.view", params...) func (c *fakeCover) Get(id string, size int, out io.Writer) error {
// r, _ := http.NewRequest("GET", url, nil) if c.err != nil {
// w := httptest.NewRecorder() return c.err
// beego.BeeApp.Handlers.ServeHTTP(w, r) }
// log.Debug(r, "testing TestGetCoverArt", fmt.Sprintf("\nUrl: %s\nStatus Code: [%d]\n%#v", r.URL, w.Code, w.HeaderMap)) c.recvId = id
// return r, w c.recvSize = size
//} out.Write([]byte(c.data))
// return nil
//func TestGetCoverArt(t *testing.T) { }
// Init(t, false)
// var _ = Describe("MediaRetrievalController", func() {
// mockMediaFileRepo := persistence.CreateMockMediaFileRepo() var controller *MediaRetrievalController
// utils.DefineSingleton(new(domain.MediaFileRepository), func() domain.MediaFileRepository { var cover *fakeCover
// return mockMediaFileRepo var w *httptest.ResponseRecorder
// })
// BeforeEach(func() {
// Convey("Subject: GetCoverArt Endpoint", t, func() { cover = &fakeCover{}
// Convey("Should fail if missing Id parameter", func() { controller = NewMediaRetrievalController(cover)
// _, w := getCoverArt() w = httptest.NewRecorder()
// })
// So(w.Body, ShouldReceiveError, responses.ErrorMissingParameter)
// }) Describe("GetCoverArt", func() {
// Convey("When id is found", func() { It("should return data for that id", func() {
// mockMediaFileRepo.SetData(`[{"Id":"2","HasCoverArt":true,"Path":"tests/fixtures/01 Invisible (RED) Edit Version.mp3"}]`, 1) cover.data = "image data"
// _, w := getCoverArt("id=2") r := newTestRequest("id=34", "size=128")
// _, err := controller.GetCoverArt(w, r)
// So(w.Body.Bytes(), ShouldMatchMD5, "e859a71cd1b1aaeb1ad437d85b306668")
// So(w.Header().Get("Content-Type"), ShouldEqual, "image/jpeg") Expect(err).To(BeNil())
// }) Expect(cover.recvId).To(Equal("34"))
// Convey("When id is found but file is unavailable", func() { Expect(cover.recvSize).To(Equal(128))
// mockMediaFileRepo.SetData(`[{"Id":"2","HasCoverArt":true,"Path":"tests/fixtures/NOT_FOUND.mp3"}]`, 1) Expect(w.Body.String()).To(Equal(cover.data))
// _, w := getCoverArt("id=2") })
//
// So(w.Body, ShouldReceiveError, responses.ErrorDataNotFound) It("should fail if missing id parameter", func() {
// }) r := newTestRequest()
// Convey("When the engine reports an error", func() { _, err := controller.GetCoverArt(w, r)
// mockMediaFileRepo.SetData(`[{"Id":"2","HasCoverArt":true,"Path":"tests/fixtures/NOT_FOUND.mp3"}]`, 1)
// mockMediaFileRepo.SetError(true) Expect(err).To(MatchError("id parameter required"))
// _, w := getCoverArt("id=2") })
//
// So(w.Body, ShouldReceiveError, responses.ErrorGeneric) It("should fail when the file is not found", func() {
// }) cover.err = domain.ErrNotFound
// Convey("When specifying a size", func() { r := newTestRequest("id=34", "size=128")
// mockMediaFileRepo.SetData(`[{"Id":"2","HasCoverArt":true,"Path":"tests/fixtures/01 Invisible (RED) Edit Version.mp3"}]`, 1) _, err := controller.GetCoverArt(w, r)
// _, w := getCoverArt("id=2", "size=100")
// Expect(err).To(MatchError("Cover not found"))
// So(w.Body.Bytes(), ShouldMatchMD5, "04378f523ca3e8ead33bf7140d39799e") })
// So(w.Header().Get("Content-Type"), ShouldEqual, "image/jpeg")
// }) It("should fail when there is an unknown error", func() {
// Reset(func() { cover.err = errors.New("weird error")
// mockMediaFileRepo.SetData("[]", 0) r := newTestRequest("id=34", "size=128")
// mockMediaFileRepo.SetError(false) _, err := controller.GetCoverArt(w, r)
// })
// }) Expect(err).To(MatchError("Internal Error"))
//} })
})
})
+12 -19
View File
@@ -1,9 +1,9 @@
package api package api
import ( import (
"fmt"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"strings"
"github.com/cloudsonic/sonic-server/conf" "github.com/cloudsonic/sonic-server/conf"
"github.com/cloudsonic/sonic-server/log" "github.com/cloudsonic/sonic-server/log"
@@ -11,8 +11,8 @@ import (
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
) )
func newRequest(queryParams string) *http.Request { func newTestRequest(queryParams ...string) *http.Request {
r := httptest.NewRequest("get", "/ping?"+queryParams, nil) r := httptest.NewRequest("get", "/ping?"+strings.Join(queryParams, "&"), nil)
ctx := r.Context() ctx := r.Context()
return r.WithContext(log.NewContext(ctx)) return r.WithContext(log.NewContext(ctx))
} }
@@ -28,7 +28,7 @@ var _ = Describe("Middlewares", func() {
Describe("CheckParams", func() { Describe("CheckParams", func() {
It("passes when all required params are available", func() { It("passes when all required params are available", func() {
r := newRequest("u=user&v=1.15&c=test") r := newTestRequest("u=user", "v=1.15", "c=test")
cp := checkRequiredParameters(next) cp := checkRequiredParameters(next)
cp.ServeHTTP(w, r) cp.ServeHTTP(w, r)
@@ -39,7 +39,7 @@ var _ = Describe("Middlewares", func() {
}) })
It("fails when user is missing", func() { It("fails when user is missing", func() {
r := newRequest("v=1.15&c=test") r := newTestRequest("v=1.15", "c=test")
cp := checkRequiredParameters(next) cp := checkRequiredParameters(next)
cp.ServeHTTP(w, r) cp.ServeHTTP(w, r)
@@ -48,7 +48,7 @@ var _ = Describe("Middlewares", func() {
}) })
It("fails when version is missing", func() { It("fails when version is missing", func() {
r := newRequest("u=user&c=test") r := newTestRequest("u=user", "c=test")
cp := checkRequiredParameters(next) cp := checkRequiredParameters(next)
cp.ServeHTTP(w, r) cp.ServeHTTP(w, r)
@@ -57,7 +57,7 @@ var _ = Describe("Middlewares", func() {
}) })
It("fails when client is missing", func() { It("fails when client is missing", func() {
r := newRequest("u=user&v=1.15") r := newTestRequest("u=user", "v=1.15")
cp := checkRequiredParameters(next) cp := checkRequiredParameters(next)
cp.ServeHTTP(w, r) cp.ServeHTTP(w, r)
@@ -75,7 +75,7 @@ var _ = Describe("Middlewares", func() {
Context("Plaintext password", func() { Context("Plaintext password", func() {
It("authenticates with plaintext password ", func() { It("authenticates with plaintext password ", func() {
r := newRequest("u=admin&p=wordpass") r := newTestRequest("u=admin", "p=wordpass")
cp := authenticate(next) cp := authenticate(next)
cp.ServeHTTP(w, r) cp.ServeHTTP(w, r)
@@ -83,7 +83,7 @@ var _ = Describe("Middlewares", func() {
}) })
It("fails authentication with wrong password", func() { It("fails authentication with wrong password", func() {
r := newRequest("u=admin&p=INVALID") r := newTestRequest("u=admin", "p=INVALID")
cp := authenticate(next) cp := authenticate(next)
cp.ServeHTTP(w, r) cp.ServeHTTP(w, r)
@@ -94,7 +94,7 @@ var _ = Describe("Middlewares", func() {
Context("Encoded password", func() { Context("Encoded password", func() {
It("authenticates with simple encoded password ", func() { It("authenticates with simple encoded password ", func() {
r := newRequest("u=admin&p=enc:776f726470617373") r := newTestRequest("u=admin", "p=enc:776f726470617373")
cp := authenticate(next) cp := authenticate(next)
cp.ServeHTTP(w, r) cp.ServeHTTP(w, r)
@@ -104,11 +104,7 @@ var _ = Describe("Middlewares", func() {
Context("Token based authentication", func() { Context("Token based authentication", func() {
It("authenticates with token based authentication", func() { It("authenticates with token based authentication", func() {
token := "23b342970e25c7928831c3317edd0b67" r := newTestRequest("u=admin", "t=23b342970e25c7928831c3317edd0b67", "s=retnlmjetrymazgkt")
salt := "retnlmjetrymazgkt"
query := fmt.Sprintf("u=admin&t=%s&s=%s", token, salt)
r := newRequest(query)
cp := authenticate(next) cp := authenticate(next)
cp.ServeHTTP(w, r) cp.ServeHTTP(w, r)
@@ -116,10 +112,7 @@ var _ = Describe("Middlewares", func() {
}) })
It("fails if salt is missing", func() { It("fails if salt is missing", func() {
token := "23b342970e25c7928831c3317edd0b67" r := newTestRequest("u=admin", "t=23b342970e25c7928831c3317edd0b67")
query := fmt.Sprintf("u=admin&t=%s", token)
r := newRequest(query)
cp := authenticate(next) cp := authenticate(next)
cp.ServeHTTP(w, r) cp.ServeHTTP(w, r)
-48
View File
@@ -1,48 +0,0 @@
package api_test
//
//import (
// "encoding/json"
// "testing"
//
// "github.com/cloudsonic/sonic-server/api/responses"
// . "github.com/cloudsonic/sonic-server/tests"
// . "github.com/smartystreets/goconvey/convey"
//)
//
//func TestPing(t *testing.T) {
// Init(t, false)
//
// _, w := Get(AddParams("/rest/ping.view"), "TestPing")
//
// Convey("Subject: Ping Endpoint", t, func() {
// Convey("Status code should be 200", func() {
// So(w.Code, ShouldEqual, 200)
// })
// Convey("The result should not be empty", func() {
// So(w.Body.Len(), ShouldBeGreaterThan, 0)
// })
// Convey("The result should be a valid ping response", func() {
// v := responses.JsonWrapper{}
// err := json.Unmarshal(w.Body.Bytes(), &v)
// So(err, ShouldBeNil)
// So(v.Subsonic.Status, ShouldEqual, "ok")
// So(v.Subsonic.Version, ShouldEqual, "1.8.0")
// })
//
// })
//}
//func TestGetLicense(t *testing.T) {
// Init(t, false)
//
// _, w := Get(AddParams("/rest/getLicense.view"), "TestGetLicense")
//
// Convey("Subject: GetLicense Endpoint", t, func() {
// Convey("Status code should be 200", func() {
// So(w.Code, ShouldEqual, 200)
// })
// Convey("The license should always be valid", func() {
// So(UnindentJSON(w.Body.Bytes()), ShouldContainSubstring, `"license":{"valid":true}`)
// })
//
// })
//}