From 51e995e0a1ce64948359939560148c383cb67df7 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Thu, 30 Apr 2015 22:48:15 -0700 Subject: [PATCH] more mock examples --- datastore/mock/{datastore.go => mock.go} | 0 server/badge_test.go | 2 +- server/user_test.go | 88 ++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) rename datastore/mock/{datastore.go => mock.go} (100%) create mode 100644 server/user_test.go diff --git a/datastore/mock/datastore.go b/datastore/mock/mock.go similarity index 100% rename from datastore/mock/datastore.go rename to datastore/mock/mock.go diff --git a/server/badge_test.go b/server/badge_test.go index cc6cf6581..84b5c88e0 100644 --- a/server/badge_test.go +++ b/server/badge_test.go @@ -37,7 +37,7 @@ func TestBadges(t *testing.T) { g := Goblin(t) g.Describe("Badges", func() { - g.It("should svg badges", func() { + g.It("should serve svg badges", func() { for _, test := range badgeTests { rw := recorder.New() ctx := &gin.Context{Engine: gin.Default(), Writer: rw} diff --git a/server/user_test.go b/server/user_test.go new file mode 100644 index 000000000..2b678ec5a --- /dev/null +++ b/server/user_test.go @@ -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) + }) + }) +}