harness-drone/server/user_test.go
2015-04-30 22:48:15 -07:00

88 lines
2.2 KiB
Go

package server
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"testing"
"github.com/drone/drone/common"
"github.com/drone/drone/datastore/mock"
"github.com/drone/drone/server/recorder"
. "github.com/franela/goblin"
"github.com/gin-gonic/gin"
)
func TestUser(t *testing.T) {
store := new(mocks.Datastore)
g := Goblin(t)
g.Describe("User", func() {
g.It("should get", func() {
rw := recorder.New()
ctx := &gin.Context{Engine: gin.Default(), Writer: rw}
user := &common.User{Login: "octocat"}
ctx.Set("user", user)
GetUserCurr(ctx)
out := &common.User{}
json.NewDecoder(rw.Body).Decode(out)
g.Assert(rw.Code).Equal(200)
g.Assert(out).Equal(user)
})
g.It("should put", func() {
var buf bytes.Buffer
in := &common.User{Email: "octocat@github.com"}
json.NewEncoder(&buf).Encode(in)
rw := recorder.New()
ctx := &gin.Context{Engine: gin.Default(), Writer: rw}
ctx.Request = &http.Request{Body: ioutil.NopCloser(&buf)}
ctx.Request.Header = http.Header{}
ctx.Request.Header.Set("Content-Type", "application/json")
user := &common.User{Login: "octocat"}
ctx.Set("user", user)
ctx.Set("datastore", store)
store.On("SetUser", user).Return(nil).Once()
PutUserCurr(ctx)
out := &common.User{}
json.NewDecoder(rw.Body).Decode(out)
g.Assert(rw.Code).Equal(200)
g.Assert(out.Login).Equal(user.Login)
g.Assert(out.Email).Equal(in.Email)
g.Assert(out.Gravatar).Equal("7194e8d48fa1d2b689f99443b767316c")
})
g.It("should put, error", func() {
var buf bytes.Buffer
in := &common.User{Email: "octocat@github.com"}
json.NewEncoder(&buf).Encode(in)
rw := recorder.New()
ctx := &gin.Context{Engine: gin.Default(), Writer: rw}
ctx.Request = &http.Request{Body: ioutil.NopCloser(&buf)}
ctx.Request.Header = http.Header{}
ctx.Request.Header.Set("Content-Type", "application/json")
user := &common.User{Login: "octocat"}
ctx.Set("user", user)
ctx.Set("datastore", store)
store.On("SetUser", user).Return(errors.New("error")).Once()
PutUserCurr(ctx)
out := &common.User{}
json.NewDecoder(rw.Body).Decode(out)
g.Assert(rw.Code).Equal(400)
})
})
}