internal/database: write the rest of the tests

This commit is contained in:
Cadey Ratio 2018-01-21 12:19:54 -08:00
parent 0088a7dc09
commit 220b2a8994
5 changed files with 180 additions and 14 deletions

View File

@ -189,6 +189,7 @@ func (b *boltTokenStorage) Put(ctx context.Context, t Token) (Token, error) {
t.ID = uuid.New() t.ID = uuid.New()
t.CreatedAt = time.Now() t.CreatedAt = time.Now()
t.Active = true t.Active = true
t.Body = uuid.New()
} }
err := b.db.Save(&t) err := b.db.Save(&t)

View File

@ -5,7 +5,6 @@ import (
"os" "os"
"testing" "testing"
"git.xeserv.us/xena/route/internal/routecrypto"
"github.com/Xe/uuid" "github.com/Xe/uuid"
) )
@ -57,21 +56,9 @@ func testCerts(ctx context.Context, t *testing.T, c Certs) {
} }
func TestBoltDBCertStorage(t *testing.T) { func TestBoltDBCertStorage(t *testing.T) {
k, err := routecrypto.ParseKey(cryptoKey) st, p, ctx, cancel := newTestBoltStorage(t)
if err != nil {
t.Fatal(err)
}
p := uuid.New()
defer os.RemoveAll(p) defer os.RemoveAll(p)
st, err := NewBoltStorage(p, k)
if err != nil {
t.Fatal(err)
}
defer st.Close() defer st.Close()
ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
testCerts(ctx, t, st.Certs()) testCerts(ctx, t, st.Certs())

View File

@ -0,0 +1,27 @@
package database
import (
"testing"
"git.xeserv.us/xena/route/internal/routecrypto"
"github.com/Xe/uuid"
"golang.org/x/net/context"
)
func newTestBoltStorage(t *testing.T) (Storage, string, context.Context, context.CancelFunc) {
k, err := routecrypto.ParseKey(cryptoKey)
if err != nil {
t.Fatal(err)
}
p := uuid.New()
st, err := NewBoltStorage(p, k)
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
return st, p, ctx, cancel
}

View File

@ -0,0 +1,77 @@
package database
import (
"os"
"testing"
"github.com/Xe/uuid"
"golang.org/x/net/context"
)
const (
creator = "shachi"
hostBase = "shachi.loves."
)
func testRoutes(ctx context.Context, t *testing.T, r Routes) {
hostname := hostBase + uuid.New()
id := uuid.New()
t.Run("put", func(t *testing.T) {
rt := Route{
ID: id,
Creator: creator,
Hostname: hostname,
}
_, err := r.Put(ctx, rt)
if err != nil {
t.Fatal(err)
}
})
t.Run("get", func(t *testing.T) {
_, err := r.Get(ctx, id)
if err != nil {
t.Fatal(err)
}
})
t.Run("getHost", func(t *testing.T) {
_, err := r.GetHost(ctx, hostname)
if err != nil {
t.Fatal(err)
}
})
t.Run("getAll", func(t *testing.T) {
res, err := r.GetAll(ctx, creator)
if err != nil {
t.Fatal(err)
}
if len(res) == 0 {
t.Fatal("didn't get any routes back for shachi")
}
})
t.Run("delete", func(t *testing.T) {
_, err := r.Delete(ctx, Route{ID: id})
if err != nil {
t.Fatal(err)
}
_, err = r.Get(ctx, id)
if err == nil {
t.Fatal("able to fetch a deleted item")
}
})
}
func TestBoltDBRouteStorage(t *testing.T) {
st, p, ctx, cancel := newTestBoltStorage(t)
defer os.RemoveAll(p)
defer st.Close()
defer cancel()
testRoutes(ctx, t, st.Routes())
}

View File

@ -0,0 +1,74 @@
package database
import (
"os"
"testing"
"golang.org/x/net/context"
)
func testTokens(ctx context.Context, t *testing.T, tk Tokens) {
var (
tokenID string
tokenBody string
)
t.Run("put", func(t *testing.T) {
tok := Token{
Owner: creator,
}
tok2, err := tk.Put(ctx, tok)
if err != nil {
t.Fatal(err)
}
tokenID = tok2.ID
tokenBody = tok2.Body
})
t.Run("get", func(t *testing.T) {
_, err := tk.Get(ctx, tokenID)
if err != nil {
t.Fatal(err)
}
})
t.Run("getBody", func(t *testing.T) {
_, err := tk.GetBody(ctx, tokenBody)
if err != nil {
t.Fatal(err)
}
})
t.Run("getAll", func(t *testing.T) {
lis, err := tk.GetAll(ctx, creator)
if err != nil {
t.Fatal(err)
}
if len(lis) == 0 {
t.Fatal("expected results but got none")
}
})
t.Run("delete", func(t *testing.T) {
_, err := tk.Delete(ctx, tokenID)
if err != nil {
t.Fatal(err)
}
_, err = tk.Get(ctx, tokenID)
if err == nil {
t.Fatal("can fetch a deleted resource")
}
})
}
func TestBoltDBTokenStorage(t *testing.T) {
st, p, ctx, cancel := newTestBoltStorage(t)
defer os.RemoveAll(p)
defer st.Close()
defer cancel()
testTokens(ctx, t, st.Tokens())
}