more mock examples

This commit is contained in:
Brad Rydzewski 2015-04-30 22:48:15 -07:00
parent 9c882c1a9e
commit 51e995e0a1
3 changed files with 89 additions and 1 deletions

View file

@ -37,7 +37,7 @@ func TestBadges(t *testing.T) {
g := Goblin(t) g := Goblin(t)
g.Describe("Badges", func() { g.Describe("Badges", func() {
g.It("should svg badges", func() { g.It("should serve svg badges", func() {
for _, test := range badgeTests { for _, test := range badgeTests {
rw := recorder.New() rw := recorder.New()
ctx := &gin.Context{Engine: gin.Default(), Writer: rw} ctx := &gin.Context{Engine: gin.Default(), Writer: rw}

88
server/user_test.go Normal file
View file

@ -0,0 +1,88 @@
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)
})
})
}