Handling request validation/authentication

This commit is contained in:
Deluan
2016-02-24 18:06:49 -05:00
parent 9a55fa1c64
commit 975327a6cb
14 changed files with 220 additions and 14 deletions
+2 -1
View File
@@ -12,6 +12,7 @@ import (
. "github.com/smartystreets/goconvey/convey"
"encoding/xml"
"fmt"
"github.com/deluan/gosonic/tests"
)
func init() {
@@ -22,7 +23,7 @@ func init() {
// TestGet is a sample to run an endpoint test
func TestGetLicense(t *testing.T) {
r, _ := http.NewRequest("GET", "/rest/getLicense.view", nil)
r, _ := http.NewRequest("GET", test.AddParams("/rest/getLicense.view"), nil)
w := httptest.NewRecorder()
beego.BeeApp.Handlers.ServeHTTP(w, r)
+3 -3
View File
@@ -12,6 +12,7 @@ import (
. "github.com/smartystreets/goconvey/convey"
"fmt"
"github.com/deluan/gosonic/controllers/responses"
"github.com/deluan/gosonic/tests"
)
func init() {
@@ -20,13 +21,12 @@ func init() {
beego.TestBeegoInit(appPath)
}
// TestGet is a sample to run an endpoint test
func TestPing(t *testing.T) {
r, _ := http.NewRequest("GET", "/rest/ping.view", nil)
r, _ := http.NewRequest("GET", test.AddParams("/rest/ping.view"), nil)
w := httptest.NewRecorder()
beego.BeeApp.Handlers.ServeHTTP(w, r)
beego.Trace("testing", "TestPing", fmt.Sprintf("Code[%d]\n%s", w.Code, w.Body.String()))
beego.Trace("testing", "TestPing", fmt.Sprintf("\nUrl: %s\n\nCode[%d]\n%s", r.URL, w.Code, w.Body.String()))
Convey("Subject: Ping Endpoint\n", t, func() {
Convey("Status code should be 200", func() {
+67
View File
@@ -0,0 +1,67 @@
package test
import (
"net/http"
"net/http/httptest"
"testing"
"runtime"
"encoding/xml"
"path/filepath"
_ "github.com/deluan/gosonic/routers"
"github.com/astaxie/beego"
. "github.com/smartystreets/goconvey/convey"
"fmt"
"github.com/deluan/gosonic/controllers/responses"
)
func init() {
_, file, _, _ := runtime.Caller(1)
appPath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, "../.." + string(filepath.Separator))))
beego.TestBeegoInit(appPath)
}
func TestCheckParams(t *testing.T) {
r, _ := http.NewRequest("GET", "/rest/ping.view", nil)
w := httptest.NewRecorder()
beego.BeeApp.Handlers.ServeHTTP(w, r)
beego.Trace("testing", "TestCheckParams", fmt.Sprintf("\nUrl: %s\n\nCode[%d]\n%s", r.URL, w.Code, w.Body.String()))
Convey("Subject: Validation\n", t, func() {
Convey("Status code should be 200", func() {
So(w.Code, ShouldEqual, 200)
})
Convey("The errorCode should be 10", func() {
So(w.Body.String(), ShouldContainSubstring, `error code="10" message=`)
})
Convey("The status should be 'fail'", func() {
v := responses.Subsonic{}
xml.Unmarshal(w.Body.Bytes(), &v)
So(v.Status, ShouldEqual, "fail")
})
})
}
func TestAuthentication(t *testing.T) {
r, _ := http.NewRequest("GET", "/rest/ping.view?u=INVALID&p=INVALID&c=test&v=1.0.0", nil)
w := httptest.NewRecorder()
beego.BeeApp.Handlers.ServeHTTP(w, r)
beego.Trace("testing", "TestCheckParams", fmt.Sprintf("\nUrl: %s\n\nCode[%d]\n%s", r.URL, w.Code, w.Body.String()))
Convey("Subject: Validation\n", t, func() {
Convey("Status code should be 200", func() {
So(w.Code, ShouldEqual, 200)
})
Convey("The errorCode should be 10", func() {
So(w.Body.String(), ShouldContainSubstring, `error code="40" message=`)
})
Convey("The status should be 'fail'", func() {
v := responses.Subsonic{}
xml.Unmarshal(w.Body.Bytes(), &v)
So(v.Status, ShouldEqual, "fail")
})
})
}
+16
View File
@@ -0,0 +1,16 @@
package test
import "fmt"
const (
testUser = "deluan"
testPassword = "wordpass"
testClient = "test"
testVersion = "1.0.0"
)
func AddParams(url string) string {
return fmt.Sprintf("%s?u=%s&p=%s&c=%s&v=%s", url, testUser, testPassword, testClient, testVersion)
}