2015-05-18 17:49:26 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
2015-05-19 19:46:19 +00:00
|
|
|
"fmt"
|
2015-05-18 17:49:26 +00:00
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2015-05-22 18:37:40 +00:00
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/dgrijalva/jwt-go"
|
|
|
|
. "github.com/drone/drone/Godeps/_workspace/src/github.com/franela/goblin"
|
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/gin-gonic/gin"
|
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/stretchr/testify/mock"
|
2015-05-28 17:04:50 +00:00
|
|
|
"github.com/drone/drone/pkg/config"
|
2015-05-18 17:49:26 +00:00
|
|
|
"github.com/drone/drone/pkg/server/recorder"
|
|
|
|
"github.com/drone/drone/pkg/server/session"
|
|
|
|
"github.com/drone/drone/pkg/store/mock"
|
|
|
|
"github.com/drone/drone/pkg/types"
|
|
|
|
)
|
|
|
|
|
2015-05-19 19:46:19 +00:00
|
|
|
var createTests = []struct {
|
2015-05-18 18:46:08 +00:00
|
|
|
inLabel string
|
|
|
|
inBody string
|
|
|
|
storeErr error
|
|
|
|
outCode int
|
|
|
|
outKind string
|
2015-05-18 17:49:26 +00:00
|
|
|
}{
|
2015-05-18 18:46:08 +00:00
|
|
|
{"", `{}`, sql.ErrNoRows, 500, ""},
|
|
|
|
{"app1", `{"label": "app1"}`, nil, 200, types.TokenUser},
|
|
|
|
{"app2", `{"label": "app2"}`, nil, 200, types.TokenUser},
|
2015-05-18 17:49:26 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 19:46:19 +00:00
|
|
|
var deleteTests = []struct {
|
|
|
|
inLabel string
|
|
|
|
errTokenLabel error
|
|
|
|
errDelToken error
|
|
|
|
outCode int
|
|
|
|
outToken *types.Token
|
|
|
|
}{
|
|
|
|
{"app1", sql.ErrNoRows, nil, 404, &types.Token{}},
|
|
|
|
{"app2", nil, sql.ErrNoRows, 400, &types.Token{Label: "app2"}},
|
|
|
|
{"app3", nil, nil, 200, &types.Token{Label: "app2"}},
|
|
|
|
}
|
|
|
|
|
2015-05-18 17:49:26 +00:00
|
|
|
func TestToken(t *testing.T) {
|
|
|
|
store := new(mocks.Store)
|
|
|
|
|
|
|
|
g := Goblin(t)
|
|
|
|
g.Describe("Token", func() {
|
2015-05-19 19:46:19 +00:00
|
|
|
|
|
|
|
// POST /api/user/tokens
|
2015-05-18 17:49:26 +00:00
|
|
|
g.It("should create tokens", func() {
|
2015-05-19 19:46:19 +00:00
|
|
|
for _, test := range createTests {
|
2015-05-18 17:49:26 +00:00
|
|
|
rw := recorder.New()
|
|
|
|
ctx := gin.Context{Engine: gin.Default(), Writer: rw}
|
|
|
|
body := bytes.NewBufferString(test.inBody)
|
|
|
|
ctx.Request, _ = http.NewRequest("POST", "/api/user/tokens", body)
|
|
|
|
|
|
|
|
ctx.Set("datastore", store)
|
|
|
|
ctx.Set("user", &types.User{Login: "Freya"})
|
|
|
|
|
2015-05-28 17:04:50 +00:00
|
|
|
conf := &config.Config{}
|
|
|
|
conf.Session.Secret = "Otto"
|
|
|
|
ctx.Set("settings", conf)
|
2015-08-12 07:02:47 +00:00
|
|
|
ctx.Set("session", session.New(conf.Session.Secret))
|
2015-05-18 17:49:26 +00:00
|
|
|
|
2015-05-18 18:46:08 +00:00
|
|
|
// prepare the mock
|
|
|
|
store.On("AddToken", mock.AnythingOfType("*types.Token")).Return(test.storeErr).Once()
|
2015-05-18 17:49:26 +00:00
|
|
|
PostToken(&ctx)
|
|
|
|
|
|
|
|
g.Assert(rw.Code).Equal(test.outCode)
|
|
|
|
if test.outCode != 200 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var respjson map[string]interface{}
|
|
|
|
json.Unmarshal(rw.Body.Bytes(), &respjson)
|
|
|
|
g.Assert(respjson["kind"]).Equal(types.TokenUser)
|
|
|
|
g.Assert(respjson["label"]).Equal(test.inLabel)
|
|
|
|
|
|
|
|
// this is probably going too far... maybe just validate hash is not empty?
|
|
|
|
jwt.Parse(respjson["hash"].(string), func(token *jwt.Token) (interface{}, error) {
|
|
|
|
_, ok := token.Method.(*jwt.SigningMethodHMAC)
|
|
|
|
g.Assert(ok).IsTrue()
|
|
|
|
g.Assert(token.Claims["label"]).Equal(test.inLabel)
|
|
|
|
return nil, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2015-05-19 19:46:19 +00:00
|
|
|
// DELETE /api/user/tokens/:label
|
|
|
|
g.It("should delete tokens", func() {
|
|
|
|
for _, test := range deleteTests {
|
|
|
|
rw := recorder.New()
|
|
|
|
ctx := gin.Context{Engine: gin.Default(), Writer: rw}
|
|
|
|
ctx.Params = append(ctx.Params, gin.Param{Key: "label", Value: test.inLabel})
|
|
|
|
|
|
|
|
ctx.Set("datastore", store)
|
|
|
|
ctx.Set("user", &types.User{Login: "Freya"})
|
|
|
|
|
2015-05-28 17:04:50 +00:00
|
|
|
conf := &config.Config{}
|
|
|
|
conf.Session.Secret = "Otto"
|
|
|
|
ctx.Set("settings", conf)
|
2015-08-12 07:02:47 +00:00
|
|
|
ctx.Set("session", session.New(conf.Session.Secret))
|
2015-05-19 19:46:19 +00:00
|
|
|
|
|
|
|
// prepare the mock
|
|
|
|
store.On("TokenLabel", mock.AnythingOfType("*types.User"), test.inLabel).Return(test.outToken, test.errTokenLabel).Once()
|
|
|
|
|
|
|
|
if test.errTokenLabel == nil {
|
|
|
|
// we don't need this expectation if we error on our first
|
|
|
|
store.On("DelToken", mock.AnythingOfType("*types.Token")).Return(test.errDelToken).Once()
|
|
|
|
}
|
|
|
|
fmt.Println(test)
|
|
|
|
DelToken(&ctx)
|
|
|
|
|
|
|
|
g.Assert(rw.Code).Equal(test.outCode)
|
|
|
|
if test.outCode != 200 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var respjson map[string]interface{}
|
|
|
|
json.Unmarshal(rw.Body.Bytes(), &respjson)
|
|
|
|
fmt.Println(rw.Code, respjson)
|
|
|
|
}
|
|
|
|
})
|
2015-05-18 17:49:26 +00:00
|
|
|
})
|
|
|
|
}
|