2015-04-07 08:20:55 +00:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
2015-04-13 08:22:51 +00:00
|
|
|
"os"
|
2015-04-07 08:20:55 +00:00
|
|
|
"testing"
|
|
|
|
|
2015-04-13 08:22:51 +00:00
|
|
|
"github.com/drone/drone/common"
|
2015-04-07 08:20:55 +00:00
|
|
|
. "github.com/franela/goblin"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestToken(t *testing.T) {
|
|
|
|
g := Goblin(t)
|
|
|
|
g.Describe("Tokens", func() {
|
2015-04-13 08:22:51 +00:00
|
|
|
var db *DB // temporary database
|
2015-04-07 08:20:55 +00:00
|
|
|
|
2015-04-13 08:22:51 +00:00
|
|
|
// create a new database before each unit
|
|
|
|
// test and destroy afterwards.
|
|
|
|
g.BeforeEach(func() {
|
|
|
|
db = Must("/tmp/drone.test.db")
|
|
|
|
})
|
|
|
|
g.AfterEach(func() {
|
|
|
|
os.Remove(db.Path())
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should list for user", func() {
|
2015-04-15 05:04:38 +00:00
|
|
|
db.SetUserNotExists(&common.User{Login: "octocat"})
|
|
|
|
err1 := db.SetToken(&common.Token{Login: "octocat", Label: "gist"})
|
|
|
|
err2 := db.SetToken(&common.Token{Login: "octocat", Label: "github"})
|
2015-04-13 08:22:51 +00:00
|
|
|
g.Assert(err1).Equal(nil)
|
|
|
|
g.Assert(err2).Equal(nil)
|
|
|
|
|
2015-04-15 05:04:38 +00:00
|
|
|
list, err := db.TokenList("octocat")
|
2015-04-13 08:22:51 +00:00
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
g.Assert(len(list)).Equal(2)
|
|
|
|
})
|
2015-04-15 05:04:38 +00:00
|
|
|
|
|
|
|
g.It("Should insert", func() {
|
|
|
|
db.SetUserNotExists(&common.User{Login: "octocat"})
|
|
|
|
err := db.SetToken(&common.Token{Login: "octocat", Label: "gist"})
|
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
|
|
|
|
token, err := db.Token("octocat", "gist")
|
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
g.Assert(token.Label).Equal("gist")
|
|
|
|
g.Assert(token.Login).Equal("octocat")
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should delete", func() {
|
|
|
|
db.SetUserNotExists(&common.User{Login: "octocat"})
|
|
|
|
err := db.SetToken(&common.Token{Login: "octocat", Label: "gist"})
|
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
|
|
|
|
token, err := db.Token("octocat", "gist")
|
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
g.Assert(token.Label).Equal("gist")
|
|
|
|
g.Assert(token.Login).Equal("octocat")
|
|
|
|
|
|
|
|
err = db.DelToken(token)
|
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
|
|
|
|
token, err = db.Token("octocat", "gist")
|
|
|
|
g.Assert(err != nil).IsTrue()
|
|
|
|
})
|
2015-04-07 08:20:55 +00:00
|
|
|
})
|
|
|
|
}
|