package model import ( "testing" "github.com/drone/drone/shared/database" "github.com/franela/goblin" ) func TestKey(t *testing.T) { db := database.Open("sqlite3", ":memory:") defer db.Close() 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() { db.Exec("DELETE FROM keys") }) g.It("Should create a key", func() { key := Key{ RepoID: 1, Public: fakePublicKey, Private: fakePrivateKey, } err := CreateKey(db, &key) g.Assert(err == nil).IsTrue() g.Assert(key.ID != 0).IsTrue() }) g.It("Should update a key", func() { key := Key{ RepoID: 1, Public: fakePublicKey, Private: fakePrivateKey, } err := CreateKey(db, &key) g.Assert(err == nil).IsTrue() g.Assert(key.ID != 0).IsTrue() key.Private = "" key.Public = "" err1 := UpdateKey(db, &key) getkey, err2 := GetKey(db, &Repo{ID: 1}) 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() { key := Key{ RepoID: 1, Public: fakePublicKey, Private: fakePrivateKey, } err := CreateKey(db, &key) g.Assert(err == nil).IsTrue() g.Assert(key.ID != 0).IsTrue() getkey, err := GetKey(db, &Repo{ID: 1}) 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() { key := Key{ RepoID: 1, Public: fakePublicKey, Private: fakePrivateKey, } err1 := CreateKey(db, &key) err2 := DeleteKey(db, &Repo{ID: 1}) g.Assert(err1 == nil).IsTrue() g.Assert(err2 == nil).IsTrue() _, err := GetKey(db, &Repo{ID: 1}) g.Assert(err == nil).IsFalse() }) }) } var fakePublicKey = ` -----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0 FPqri0cb2JZfXJ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