storj/satellite/console/apikeys_test.go

164 lines
4.0 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/pkg/macaroon"
"storj.io/storj/private/testcontext"
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("GetPagedByProjectID success", func(t *testing.T) {
cursor := console.APIKeyCursor{
Page: 1,
Limit: 10,
Search: "",
}
page, err := apikeys.GetPagedByProjectID(ctx, project.ID, cursor)
assert.NotNil(t, page)
assert.Equal(t, len(page.APIKeys), 10)
assert.NoError(t, err)
})
t.Run("GetPagedByProjectID with limit success", func(t *testing.T) {
cursor := console.APIKeyCursor{
Page: 1,
Limit: 2,
Search: "",
}
page, err := apikeys.GetPagedByProjectID(ctx, project.ID, cursor)
assert.NotNil(t, page)
assert.Equal(t, len(page.APIKeys), 2)
assert.Equal(t, page.PageCount, uint(5))
2019-01-31 13:01:13 +00:00
assert.NoError(t, err)
})
t.Run("Get By ID success", func(t *testing.T) {
cursor := console.APIKeyCursor{
Page: 1,
Limit: 10,
Search: "",
}
page, err := apikeys.GetPagedByProjectID(ctx, project.ID, cursor)
assert.NotNil(t, page)
assert.Equal(t, len(page.APIKeys), 10)
2019-01-31 13:01:13 +00:00
assert.NoError(t, err)
key, err := apikeys.Get(ctx, page.APIKeys[0].ID)
2019-01-31 13:01:13 +00:00
assert.NotNil(t, key)
assert.Equal(t, page.APIKeys[0].ID, key.ID)
2019-01-31 13:01:13 +00:00
assert.NoError(t, err)
})
t.Run("Update success", func(t *testing.T) {
cursor := console.APIKeyCursor{
Page: 1,
Limit: 10,
Search: "",
}
page, err := apikeys.GetPagedByProjectID(ctx, project.ID, cursor)
assert.NotNil(t, page)
assert.Equal(t, len(page.APIKeys), 10)
2019-01-31 13:01:13 +00:00
assert.NoError(t, err)
key, err := apikeys.Get(ctx, page.APIKeys[0].ID)
2019-01-31 13:01:13 +00:00
assert.NotNil(t, key)
assert.Equal(t, page.APIKeys[0].ID, key.ID)
2019-01-31 13:01:13 +00:00
assert.NoError(t, err)
key.Name = "some new name"
err = apikeys.Update(ctx, *key)
assert.NoError(t, err)
updatedKey, err := apikeys.Get(ctx, page.APIKeys[0].ID)
2019-01-31 13:01:13 +00:00
assert.NotNil(t, key)
assert.Equal(t, key.Name, updatedKey.Name)
assert.NoError(t, err)
})
t.Run("Delete success", func(t *testing.T) {
cursor := console.APIKeyCursor{
Page: 1,
Limit: 10,
Search: "",
}
page, err := apikeys.GetPagedByProjectID(ctx, project.ID, cursor)
assert.NotNil(t, page)
assert.Equal(t, len(page.APIKeys), 10)
2019-01-31 13:01:13 +00:00
assert.NoError(t, err)
key, err := apikeys.Get(ctx, page.APIKeys[0].ID)
2019-01-31 13:01:13 +00:00
assert.NotNil(t, key)
assert.Equal(t, page.APIKeys[0].ID, key.ID)
2019-01-31 13:01:13 +00:00
assert.NoError(t, err)
key.Name = "some new name"
err = apikeys.Delete(ctx, key.ID)
assert.NoError(t, err)
page, err = apikeys.GetPagedByProjectID(ctx, project.ID, cursor)
assert.NotNil(t, page)
assert.Equal(t, len(page.APIKeys), 9)
2019-01-31 13:01:13 +00:00
assert.NoError(t, err)
})
t.Run("GetPageByProjectID with 0 page error", func(t *testing.T) {
cursor := console.APIKeyCursor{
Page: 0,
Limit: 10,
Search: "",
}
page, err := apikeys.GetPagedByProjectID(ctx, project.ID, cursor)
assert.Nil(t, page)
assert.Error(t, err)
})
2019-01-31 13:01:13 +00:00
})
}