harness-drone/pkg/store/builtin/token_test.go

150 lines
3.8 KiB
Go
Raw Normal View History

package builtin
import (
"testing"
"time"
2015-05-22 18:37:40 +00:00
"github.com/drone/drone/Godeps/_workspace/src/github.com/franela/goblin"
2015-06-14 00:51:55 +00:00
"github.com/drone/drone/pkg/types"
)
func TestTokenstore(t *testing.T) {
db := mustConnectTest()
ts := NewTokenstore(db)
defer db.Close()
g := goblin.Goblin(t)
g.Describe("Tokenstore", func() {
// before each test be sure to purge the package
// table data from the database.
2015-04-13 08:22:51 +00:00
g.BeforeEach(func() {
db.Exec("DELETE FROM tokens")
2015-04-13 08:22:51 +00:00
})
g.It("Should Add a new Token", func() {
2015-06-14 00:51:55 +00:00
token := types.Token{
UserID: 1,
Label: "foo",
2015-06-14 00:51:55 +00:00
Kind: types.TokenUser,
Issued: time.Now().Unix(),
Expiry: time.Now().Unix() + 1000,
}
err := ts.AddToken(&token)
g.Assert(err == nil).IsTrue()
g.Assert(token.ID != 0).IsTrue()
2015-04-13 08:22:51 +00:00
})
g.It("Should get a Token", func() {
2015-06-14 00:51:55 +00:00
token := types.Token{
UserID: 1,
Label: "foo",
2015-06-14 00:51:55 +00:00
Kind: types.TokenUser,
Issued: time.Now().Unix(),
Expiry: time.Now().Unix() + 1000,
}
err1 := ts.AddToken(&token)
gettoken, err2 := ts.Token(token.ID)
g.Assert(err1 == nil).IsTrue()
g.Assert(err2 == nil).IsTrue()
g.Assert(token.ID).Equal(gettoken.ID)
g.Assert(token.Label).Equal(gettoken.Label)
g.Assert(token.Kind).Equal(gettoken.Kind)
g.Assert(token.Issued).Equal(gettoken.Issued)
g.Assert(token.Expiry).Equal(gettoken.Expiry)
})
g.It("Should Get a Token By Label", func() {
2015-06-14 00:51:55 +00:00
token := types.Token{
UserID: 1,
Label: "foo",
2015-06-14 00:51:55 +00:00
Kind: types.TokenUser,
Issued: time.Now().Unix(),
Expiry: time.Now().Unix() + 1000,
}
err1 := ts.AddToken(&token)
2015-06-14 00:51:55 +00:00
gettoken, err2 := ts.TokenLabel(&types.User{ID: 1}, "foo")
g.Assert(err1 == nil).IsTrue()
g.Assert(err2 == nil).IsTrue()
g.Assert(token.ID).Equal(gettoken.ID)
g.Assert(token.Label).Equal(gettoken.Label)
g.Assert(token.Kind).Equal(gettoken.Kind)
g.Assert(token.Issued).Equal(gettoken.Issued)
g.Assert(token.Expiry).Equal(gettoken.Expiry)
})
g.It("Should Enforce Unique Token Label", func() {
2015-06-14 00:51:55 +00:00
token1 := types.Token{
UserID: 1,
Label: "foo",
2015-06-14 00:51:55 +00:00
Kind: types.TokenUser,
Issued: time.Now().Unix(),
Expiry: time.Now().Unix() + 1000,
}
2015-06-14 00:51:55 +00:00
token2 := types.Token{
UserID: 1,
Label: "foo",
2015-06-14 00:51:55 +00:00
Kind: types.TokenUser,
Issued: time.Now().Unix(),
Expiry: time.Now().Unix() + 1000,
}
err1 := ts.AddToken(&token1)
err2 := ts.AddToken(&token2)
g.Assert(err1 == nil).IsTrue()
g.Assert(err2 == nil).IsFalse()
})
g.It("Should Get a User Token List", func() {
2015-06-14 00:51:55 +00:00
token1 := types.Token{
UserID: 1,
Label: "bar",
2015-06-14 00:51:55 +00:00
Kind: types.TokenUser,
Issued: time.Now().Unix(),
Expiry: time.Now().Unix() + 1000,
}
2015-06-14 00:51:55 +00:00
token2 := types.Token{
UserID: 1,
Label: "foo",
2015-06-14 00:51:55 +00:00
Kind: types.TokenUser,
Issued: time.Now().Unix(),
Expiry: time.Now().Unix() + 1000,
}
2015-06-14 00:51:55 +00:00
token3 := types.Token{
UserID: 2,
Label: "foo",
2015-06-14 00:51:55 +00:00
Kind: types.TokenUser,
Issued: time.Now().Unix(),
Expiry: time.Now().Unix() + 1000,
}
ts.AddToken(&token1)
ts.AddToken(&token2)
ts.AddToken(&token3)
2015-06-14 00:51:55 +00:00
tokens, err := ts.TokenList(&types.User{ID: 1})
g.Assert(err == nil).IsTrue()
g.Assert(len(tokens)).Equal(2)
g.Assert(tokens[0].ID).Equal(token1.ID)
g.Assert(tokens[0].Label).Equal(token1.Label)
g.Assert(tokens[0].Kind).Equal(token1.Kind)
g.Assert(tokens[0].Issued).Equal(token1.Issued)
g.Assert(tokens[0].Expiry).Equal(token1.Expiry)
})
g.It("Should Del a Token", func() {
2015-06-14 00:51:55 +00:00
token := types.Token{
UserID: 1,
Label: "foo",
2015-06-14 00:51:55 +00:00
Kind: types.TokenUser,
Issued: time.Now().Unix(),
Expiry: time.Now().Unix() + 1000,
}
ts.AddToken(&token)
_, err1 := ts.Token(token.ID)
err2 := ts.DelToken(&token)
_, err3 := ts.Token(token.ID)
g.Assert(err1 == nil).IsTrue()
g.Assert(err2 == nil).IsTrue()
g.Assert(err3 == nil).IsFalse()
})
})
}