2019-01-24 16:26:36 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2019-01-11 10:08:51 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-01-22 15:48:23 +00:00
|
|
|
|
2019-01-11 10:08:51 +00:00
|
|
|
"storj.io/storj/internal/testcontext"
|
2019-01-22 15:48:23 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2019-01-11 10:08:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestApiKeysRepository(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
// creating in-memory db and opening connection
|
2019-01-15 13:03:24 +00:00
|
|
|
db, err := NewConsoleDB("sqlite3", "file::memory:?mode=memory&cache=shared")
|
2019-01-11 10:08:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer ctx.Check(db.Close)
|
|
|
|
|
|
|
|
// creating tables
|
|
|
|
err = db.CreateTables()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
projects := db.Projects()
|
|
|
|
apikeys := db.APIKeys()
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
project, err := projects.Insert(ctx, &console.Project{
|
2019-01-11 10:08:51 +00:00
|
|
|
Name: "ProjectName",
|
|
|
|
TermsAccepted: 1,
|
|
|
|
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++ {
|
2019-01-15 13:03:24 +00:00
|
|
|
key, err := console.CreateAPIKey()
|
2019-01-11 10:08:51 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
keyInfo := console.APIKeyInfo{
|
2019-01-11 10:08:51 +00:00
|
|
|
Name: fmt.Sprintf("key %d", i),
|
|
|
|
ProjectID: project.ID,
|
|
|
|
}
|
|
|
|
|
|
|
|
createdKey, err := apikeys.Create(ctx, *key, keyInfo)
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|