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

View file

@ -9,7 +9,7 @@ import (
)
func TestSubsonicApi(t *testing.T) {
log.SetLevel(log.LevelError)
log.SetLevel(log.LevelCritical)
RegisterFailHandler(Fail)
RunSpecs(t, "Subsonic API Suite")
}

View file

@ -1,74 +1,76 @@
package api_test
package api
//
//import (
// "fmt"
// "net/http"
// "net/http/httptest"
// "testing"
//
// "github.com/astaxie/beego"
// "github.com/cloudsonic/sonic-server/api/responses"
// "github.com/cloudsonic/sonic-server/domain"
// "github.com/cloudsonic/sonic-server/persistence"
// . "github.com/cloudsonic/sonic-server/tests"
// "github.com/cloudsonic/sonic-server/utils"
// . "github.com/smartystreets/goconvey/convey"
//)
//
//func getCoverArt(params ...string) (*http.Request, *httptest.ResponseRecorder) {
// url := AddParams("/rest/getCoverArt.view", params...)
// r, _ := http.NewRequest("GET", url, nil)
// w := httptest.NewRecorder()
// 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))
// return r, w
//}
//
//func TestGetCoverArt(t *testing.T) {
// Init(t, false)
//
// mockMediaFileRepo := persistence.CreateMockMediaFileRepo()
// utils.DefineSingleton(new(domain.MediaFileRepository), func() domain.MediaFileRepository {
// return mockMediaFileRepo
// })
//
// Convey("Subject: GetCoverArt Endpoint", t, func() {
// Convey("Should fail if missing Id parameter", func() {
// _, w := getCoverArt()
//
// So(w.Body, ShouldReceiveError, responses.ErrorMissingParameter)
// })
// Convey("When id is found", func() {
// mockMediaFileRepo.SetData(`[{"Id":"2","HasCoverArt":true,"Path":"tests/fixtures/01 Invisible (RED) Edit Version.mp3"}]`, 1)
// _, w := getCoverArt("id=2")
//
// So(w.Body.Bytes(), ShouldMatchMD5, "e859a71cd1b1aaeb1ad437d85b306668")
// So(w.Header().Get("Content-Type"), ShouldEqual, "image/jpeg")
// })
// Convey("When id is found but file is unavailable", func() {
// mockMediaFileRepo.SetData(`[{"Id":"2","HasCoverArt":true,"Path":"tests/fixtures/NOT_FOUND.mp3"}]`, 1)
// _, w := getCoverArt("id=2")
//
// So(w.Body, ShouldReceiveError, responses.ErrorDataNotFound)
// })
// Convey("When the engine reports an error", func() {
// mockMediaFileRepo.SetData(`[{"Id":"2","HasCoverArt":true,"Path":"tests/fixtures/NOT_FOUND.mp3"}]`, 1)
// mockMediaFileRepo.SetError(true)
// _, w := getCoverArt("id=2")
//
// So(w.Body, ShouldReceiveError, responses.ErrorGeneric)
// })
// Convey("When specifying a size", func() {
// mockMediaFileRepo.SetData(`[{"Id":"2","HasCoverArt":true,"Path":"tests/fixtures/01 Invisible (RED) Edit Version.mp3"}]`, 1)
// _, w := getCoverArt("id=2", "size=100")
//
// So(w.Body.Bytes(), ShouldMatchMD5, "04378f523ca3e8ead33bf7140d39799e")
// So(w.Header().Get("Content-Type"), ShouldEqual, "image/jpeg")
// })
// Reset(func() {
// mockMediaFileRepo.SetData("[]", 0)
// mockMediaFileRepo.SetError(false)
// })
// })
//}
import (
"errors"
"io"
"net/http/httptest"
"github.com/cloudsonic/sonic-server/domain"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type fakeCover struct {
data string
err error
recvId string
recvSize int
}
func (c *fakeCover) Get(id string, size int, out io.Writer) error {
if c.err != nil {
return c.err
}
c.recvId = id
c.recvSize = size
out.Write([]byte(c.data))
return nil
}
var _ = Describe("MediaRetrievalController", func() {
var controller *MediaRetrievalController
var cover *fakeCover
var w *httptest.ResponseRecorder
BeforeEach(func() {
cover = &fakeCover{}
controller = NewMediaRetrievalController(cover)
w = httptest.NewRecorder()
})
Describe("GetCoverArt", func() {
It("should return data for that id", func() {
cover.data = "image data"
r := newTestRequest("id=34", "size=128")
_, err := controller.GetCoverArt(w, r)
Expect(err).To(BeNil())
Expect(cover.recvId).To(Equal("34"))
Expect(cover.recvSize).To(Equal(128))
Expect(w.Body.String()).To(Equal(cover.data))
})
It("should fail if missing id parameter", func() {
r := newTestRequest()
_, err := controller.GetCoverArt(w, r)
Expect(err).To(MatchError("id parameter required"))
})
It("should fail when the file is not found", func() {
cover.err = domain.ErrNotFound
r := newTestRequest("id=34", "size=128")
_, err := controller.GetCoverArt(w, r)
Expect(err).To(MatchError("Cover not found"))
})
It("should fail when there is an unknown error", func() {
cover.err = errors.New("weird error")
r := newTestRequest("id=34", "size=128")
_, err := controller.GetCoverArt(w, r)
Expect(err).To(MatchError("Internal Error"))
})
})
})

View file

@ -1,9 +1,9 @@
package api
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"github.com/cloudsonic/sonic-server/conf"
"github.com/cloudsonic/sonic-server/log"
@ -11,8 +11,8 @@ import (
. "github.com/onsi/gomega"
)
func newRequest(queryParams string) *http.Request {
r := httptest.NewRequest("get", "/ping?"+queryParams, nil)
func newTestRequest(queryParams ...string) *http.Request {
r := httptest.NewRequest("get", "/ping?"+strings.Join(queryParams, "&"), nil)
ctx := r.Context()
return r.WithContext(log.NewContext(ctx))
}
@ -28,7 +28,7 @@ var _ = Describe("Middlewares", func() {
Describe("CheckParams", 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.ServeHTTP(w, r)
@ -39,7 +39,7 @@ var _ = Describe("Middlewares", 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.ServeHTTP(w, r)
@ -48,7 +48,7 @@ var _ = Describe("Middlewares", func() {
})
It("fails when version is missing", func() {
r := newRequest("u=user&c=test")
r := newTestRequest("u=user", "c=test")
cp := checkRequiredParameters(next)
cp.ServeHTTP(w, r)
@ -57,7 +57,7 @@ var _ = Describe("Middlewares", 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.ServeHTTP(w, r)
@ -75,7 +75,7 @@ var _ = Describe("Middlewares", func() {
Context("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.ServeHTTP(w, r)
@ -83,7 +83,7 @@ var _ = Describe("Middlewares", func() {
})
It("fails authentication with wrong password", func() {
r := newRequest("u=admin&p=INVALID")
r := newTestRequest("u=admin", "p=INVALID")
cp := authenticate(next)
cp.ServeHTTP(w, r)
@ -94,7 +94,7 @@ var _ = Describe("Middlewares", func() {
Context("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.ServeHTTP(w, r)
@ -104,11 +104,7 @@ var _ = Describe("Middlewares", func() {
Context("Token based authentication", func() {
It("authenticates with token based authentication", func() {
token := "23b342970e25c7928831c3317edd0b67"
salt := "retnlmjetrymazgkt"
query := fmt.Sprintf("u=admin&t=%s&s=%s", token, salt)
r := newRequest(query)
r := newTestRequest("u=admin", "t=23b342970e25c7928831c3317edd0b67", "s=retnlmjetrymazgkt")
cp := authenticate(next)
cp.ServeHTTP(w, r)
@ -116,10 +112,7 @@ var _ = Describe("Middlewares", func() {
})
It("fails if salt is missing", func() {
token := "23b342970e25c7928831c3317edd0b67"
query := fmt.Sprintf("u=admin&t=%s", token)
r := newRequest(query)
r := newTestRequest("u=admin", "t=23b342970e25c7928831c3317edd0b67")
cp := authenticate(next)
cp.ServeHTTP(w, r)

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}`)
// })
//
// })
//}