2015-10-21 23:14:02 +00:00
|
|
|
package datastore
|
2015-09-30 01:21:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2015-10-21 23:14:02 +00:00
|
|
|
"github.com/drone/drone/model"
|
2015-09-30 01:21:17 +00:00
|
|
|
"github.com/franela/goblin"
|
|
|
|
)
|
|
|
|
|
2016-03-25 19:54:16 +00:00
|
|
|
func TestKeys(t *testing.T) {
|
2015-10-21 23:14:02 +00:00
|
|
|
db := openTest()
|
2015-09-30 01:21:17 +00:00
|
|
|
defer db.Close()
|
|
|
|
|
2015-10-21 23:14:02 +00:00
|
|
|
s := From(db)
|
2015-09-30 01:21:17 +00:00
|
|
|
g := goblin.Goblin(t)
|
|
|
|
g.Describe("Keys", func() {
|
|
|
|
|
|
|
|
// before each test be sure to purge the package
|
|
|
|
// table data from the database.
|
|
|
|
g.BeforeEach(func() {
|
2015-10-21 23:14:02 +00:00
|
|
|
db.Exec(rebind("DELETE FROM `keys`"))
|
2015-09-30 01:21:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should create a key", func() {
|
2015-10-21 23:14:02 +00:00
|
|
|
key := model.Key{
|
2015-09-30 01:21:17 +00:00
|
|
|
RepoID: 1,
|
|
|
|
Public: fakePublicKey,
|
|
|
|
Private: fakePrivateKey,
|
|
|
|
}
|
2016-03-25 19:54:16 +00:00
|
|
|
err := s.CreateKey(&key)
|
2015-09-30 01:21:17 +00:00
|
|
|
g.Assert(err == nil).IsTrue()
|
|
|
|
g.Assert(key.ID != 0).IsTrue()
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should update a key", func() {
|
2015-10-21 23:14:02 +00:00
|
|
|
key := model.Key{
|
2015-09-30 01:21:17 +00:00
|
|
|
RepoID: 1,
|
|
|
|
Public: fakePublicKey,
|
|
|
|
Private: fakePrivateKey,
|
|
|
|
}
|
2016-03-25 19:54:16 +00:00
|
|
|
err := s.CreateKey(&key)
|
2015-09-30 01:21:17 +00:00
|
|
|
g.Assert(err == nil).IsTrue()
|
|
|
|
g.Assert(key.ID != 0).IsTrue()
|
|
|
|
|
|
|
|
key.Private = ""
|
|
|
|
key.Public = ""
|
|
|
|
|
2016-03-25 19:54:16 +00:00
|
|
|
err1 := s.UpdateKey(&key)
|
|
|
|
getkey, err2 := s.GetKey(&model.Repo{ID: 1})
|
2015-09-30 01:21:17 +00:00
|
|
|
g.Assert(err1 == nil).IsTrue()
|
|
|
|
g.Assert(err2 == nil).IsTrue()
|
|
|
|
g.Assert(key.ID).Equal(getkey.ID)
|
|
|
|
g.Assert(key.Public).Equal(getkey.Public)
|
|
|
|
g.Assert(key.Private).Equal(getkey.Private)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should get a key", func() {
|
2015-10-21 23:14:02 +00:00
|
|
|
key := model.Key{
|
2015-09-30 01:21:17 +00:00
|
|
|
RepoID: 1,
|
|
|
|
Public: fakePublicKey,
|
|
|
|
Private: fakePrivateKey,
|
|
|
|
}
|
2016-03-25 19:54:16 +00:00
|
|
|
err := s.CreateKey(&key)
|
2015-09-30 01:21:17 +00:00
|
|
|
g.Assert(err == nil).IsTrue()
|
|
|
|
g.Assert(key.ID != 0).IsTrue()
|
|
|
|
|
2016-03-25 19:54:16 +00:00
|
|
|
getkey, err := s.GetKey(&model.Repo{ID: 1})
|
2015-09-30 01:21:17 +00:00
|
|
|
g.Assert(err == nil).IsTrue()
|
|
|
|
g.Assert(key.ID).Equal(getkey.ID)
|
|
|
|
g.Assert(key.Public).Equal(getkey.Public)
|
|
|
|
g.Assert(key.Private).Equal(getkey.Private)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should delete a key", func() {
|
2015-10-21 23:14:02 +00:00
|
|
|
key := model.Key{
|
2015-09-30 01:21:17 +00:00
|
|
|
RepoID: 1,
|
|
|
|
Public: fakePublicKey,
|
|
|
|
Private: fakePrivateKey,
|
|
|
|
}
|
2016-03-25 19:54:16 +00:00
|
|
|
err1 := s.CreateKey(&key)
|
|
|
|
err2 := s.DeleteKey(&key)
|
2015-09-30 01:21:17 +00:00
|
|
|
g.Assert(err1 == nil).IsTrue()
|
|
|
|
g.Assert(err2 == nil).IsTrue()
|
|
|
|
|
2016-03-25 19:54:16 +00:00
|
|
|
_, err := s.GetKey(&model.Repo{ID: 1})
|
2015-09-30 01:21:17 +00:00
|
|
|
g.Assert(err == nil).IsFalse()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var fakePublicKey = `
|
|
|
|
-----BEGIN PUBLIC KEY-----
|
|
|
|
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0
|
|
|
|
FPqri0cb2JZfXJ/DgYSF6vUpwmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/
|
|
|
|
3j+skZ6UtW+5u09lHNsj6tQ51s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQAB
|
|
|
|
-----END PUBLIC KEY-----
|
|
|
|
`
|
|
|
|
|
|
|
|
var fakePrivateKey = `
|
|
|
|
|
|
|
|
-----BEGIN RSA PRIVATE KEY-----
|
|
|
|
MIICXAIBAAKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0FPqri0cb2JZfXJ/DgYSF6vUp
|
|
|
|
wmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ5
|
|
|
|
1s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQABAoGAFijko56+qGyN8M0RVyaRAXz++xTqHBLh
|
|
|
|
3tx4VgMtrQ+WEgCjhoTwo23KMBAuJGSYnRmoBZM3lMfTKevIkAidPExvYCdm5dYq3XToLkkLv5L2
|
|
|
|
pIIVOFMDG+KESnAFV7l2c+cnzRMW0+b6f8mR1CJzZuxVLL6Q02fvLi55/mbSYxECQQDeAw6f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