storj/satellite/console/apikeys_test.go

115 lines
2.8 KiB
Go
Raw Normal View History

2019-01-31 13:01:13 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package console_test
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"storj.io/storj/internal/testcontext"
"storj.io/storj/pkg/macaroon"
2019-01-31 13:01:13 +00:00
"storj.io/storj/satellite"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/satellitedb/satellitedbtest"
)
func TestApiKeysRepository(t *testing.T) {
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
ctx := testcontext.New(t)
defer ctx.Cleanup()
projects := db.Console().Projects()
apikeys := db.Console().APIKeys()
project, err := projects.Insert(ctx, &console.Project{
Name: "ProjectName",
Description: "projects description",
})
assert.NotNil(t, project)
assert.NoError(t, err)
t.Run("Creation success", func(t *testing.T) {
for i := 0; i < 10; i++ {
key, err := macaroon.NewAPIKey([]byte("testSecret"))
2019-01-31 13:01:13 +00:00
assert.NoError(t, err)
keyInfo := console.APIKeyInfo{
Name: fmt.Sprintf("key %d", i),
ProjectID: project.ID,
Secret: []byte("testSecret"),
2019-01-31 13:01:13 +00:00
}
createdKey, err := apikeys.Create(ctx, key.Head(), keyInfo)
2019-01-31 13:01:13 +00:00
assert.NotNil(t, createdKey)
assert.NoError(t, err)
}
})
t.Run("GetByProjectID success", func(t *testing.T) {
keys, err := apikeys.GetByProjectID(ctx, project.ID)
assert.NotNil(t, keys)
assert.Equal(t, len(keys), 10)
assert.NoError(t, err)
})
t.Run("Get By ID success", func(t *testing.T) {
keys, err := apikeys.GetByProjectID(ctx, project.ID)
assert.NotNil(t, keys)
assert.Equal(t, len(keys), 10)
assert.NoError(t, err)
key, err := apikeys.Get(ctx, keys[0].ID)
assert.NotNil(t, key)
assert.Equal(t, keys[0].ID, key.ID)
assert.NoError(t, err)
})
t.Run("Update success", func(t *testing.T) {
keys, err := apikeys.GetByProjectID(ctx, project.ID)
assert.NotNil(t, keys)
assert.Equal(t, len(keys), 10)
assert.NoError(t, err)
key, err := apikeys.Get(ctx, keys[0].ID)
assert.NotNil(t, key)
assert.Equal(t, keys[0].ID, key.ID)
assert.NoError(t, err)
key.Name = "some new name"
err = apikeys.Update(ctx, *key)
assert.NoError(t, err)
updatedKey, err := apikeys.Get(ctx, keys[0].ID)
assert.NotNil(t, key)
assert.Equal(t, key.Name, updatedKey.Name)
assert.NoError(t, err)
})
t.Run("Delete success", func(t *testing.T) {
keys, err := apikeys.GetByProjectID(ctx, project.ID)
assert.NotNil(t, keys)
assert.Equal(t, len(keys), 10)
assert.NoError(t, err)
key, err := apikeys.Get(ctx, keys[0].ID)
assert.NotNil(t, key)
assert.Equal(t, keys[0].ID, key.ID)
assert.NoError(t, err)
key.Name = "some new name"
err = apikeys.Delete(ctx, key.ID)
assert.NoError(t, err)
keys, err = apikeys.GetByProjectID(ctx, project.ID)
assert.NotNil(t, keys)
assert.Equal(t, len(keys), 9)
assert.NoError(t, err)
})
})
}