2015-05-01 04:08:42 +00:00
|
|
|
package builtin
|
2015-04-07 08:20:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2015-05-11 07:45:31 +00:00
|
|
|
"time"
|
2015-04-07 08:20:55 +00:00
|
|
|
|
2015-04-13 08:22:51 +00:00
|
|
|
"github.com/drone/drone/common"
|
2015-05-11 07:45:31 +00:00
|
|
|
"github.com/franela/goblin"
|
2015-04-07 08:20:55 +00:00
|
|
|
)
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
func TestTokenstore(t *testing.T) {
|
|
|
|
db := mustConnectTest()
|
|
|
|
ts := NewTokenstore(db)
|
|
|
|
defer db.Close()
|
2015-04-07 08:20:55 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
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() {
|
2015-05-11 07:45:31 +00:00
|
|
|
db.Exec("DELETE FROM tokens")
|
2015-04-13 08:22:51 +00:00
|
|
|
})
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
g.It("Should Add a new Token", func() {
|
|
|
|
token := common.Token{
|
|
|
|
UserID: 1,
|
|
|
|
Label: "foo",
|
|
|
|
Kind: common.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
|
|
|
})
|
2015-04-15 05:04:38 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
g.It("Should get a Token", func() {
|
|
|
|
token := common.Token{
|
|
|
|
UserID: 1,
|
|
|
|
Label: "foo",
|
|
|
|
Kind: common.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)
|
2015-04-15 05:04:38 +00:00
|
|
|
})
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
g.It("Should Get a Token By Label", func() {
|
|
|
|
token := common.Token{
|
|
|
|
UserID: 1,
|
|
|
|
Label: "foo",
|
|
|
|
Kind: common.TokenUser,
|
|
|
|
Issued: time.Now().Unix(),
|
|
|
|
Expiry: time.Now().Unix() + 1000,
|
|
|
|
}
|
|
|
|
err1 := ts.AddToken(&token)
|
|
|
|
gettoken, err2 := ts.TokenLabel(&common.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)
|
|
|
|
})
|
2015-04-15 05:04:38 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
g.It("Should Enforce Unique Token Label", func() {
|
|
|
|
token1 := common.Token{
|
|
|
|
UserID: 1,
|
|
|
|
Label: "foo",
|
|
|
|
Kind: common.TokenUser,
|
|
|
|
Issued: time.Now().Unix(),
|
|
|
|
Expiry: time.Now().Unix() + 1000,
|
|
|
|
}
|
|
|
|
token2 := common.Token{
|
|
|
|
UserID: 1,
|
|
|
|
Label: "foo",
|
|
|
|
Kind: common.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()
|
|
|
|
})
|
2015-04-15 05:04:38 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
g.It("Should Get a User Token List", func() {
|
|
|
|
token1 := common.Token{
|
|
|
|
UserID: 1,
|
|
|
|
Label: "bar",
|
|
|
|
Kind: common.TokenUser,
|
|
|
|
Issued: time.Now().Unix(),
|
|
|
|
Expiry: time.Now().Unix() + 1000,
|
|
|
|
}
|
|
|
|
token2 := common.Token{
|
|
|
|
UserID: 1,
|
|
|
|
Label: "foo",
|
|
|
|
Kind: common.TokenUser,
|
|
|
|
Issued: time.Now().Unix(),
|
|
|
|
Expiry: time.Now().Unix() + 1000,
|
|
|
|
}
|
|
|
|
token3 := common.Token{
|
|
|
|
UserID: 2,
|
|
|
|
Label: "foo",
|
|
|
|
Kind: common.TokenUser,
|
|
|
|
Issued: time.Now().Unix(),
|
|
|
|
Expiry: time.Now().Unix() + 1000,
|
|
|
|
}
|
|
|
|
ts.AddToken(&token1)
|
|
|
|
ts.AddToken(&token2)
|
|
|
|
ts.AddToken(&token3)
|
|
|
|
tokens, err := ts.TokenList(&common.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)
|
|
|
|
})
|
2015-04-15 05:04:38 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
g.It("Should Del a Token", func() {
|
|
|
|
token := common.Token{
|
|
|
|
UserID: 1,
|
|
|
|
Label: "foo",
|
|
|
|
Kind: common.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()
|
2015-04-15 05:04:38 +00:00
|
|
|
})
|
2015-04-07 08:20:55 +00:00
|
|
|
})
|
|
|
|
}
|