2019-01-31 13:01:13 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package console_test
|
|
|
|
|
|
|
|
import (
|
2020-09-10 10:32:35 +01:00
|
|
|
"math/rand"
|
2020-04-30 11:48:29 +01:00
|
|
|
"sort"
|
2019-01-31 13:01:13 +00:00
|
|
|
"testing"
|
2020-02-21 16:51:28 +00:00
|
|
|
"time"
|
2019-01-31 13:01:13 +00:00
|
|
|
|
2020-04-30 11:48:29 +01:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2020-01-29 00:57:15 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-01-17 15:01:36 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-01-31 13:01:13 +00:00
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
2019-01-31 13:01:13 +00:00
|
|
|
"storj.io/storj/satellite"
|
|
|
|
"storj.io/storj/satellite/console"
|
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestProjectsRepository(t *testing.T) {
|
|
|
|
//testing constants
|
|
|
|
const (
|
|
|
|
// for user
|
2019-03-27 12:33:32 +00:00
|
|
|
shortName = "lastName"
|
2019-06-18 01:28:40 +01:00
|
|
|
email = "email@mail.test"
|
2019-03-27 12:33:32 +00:00
|
|
|
pass = "123456"
|
|
|
|
userFullName = "name"
|
2019-01-31 13:01:13 +00:00
|
|
|
|
|
|
|
// for project
|
|
|
|
name = "Project"
|
|
|
|
description = "some description"
|
|
|
|
|
|
|
|
// updated project values
|
2020-09-10 10:32:35 +01:00
|
|
|
newName = "newProjectName"
|
2019-01-31 13:01:13 +00:00
|
|
|
newDescription = "some new description"
|
|
|
|
)
|
|
|
|
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) { // repositories
|
2019-01-31 13:01:13 +00:00
|
|
|
users := db.Console().Users()
|
|
|
|
projects := db.Console().Projects()
|
|
|
|
var project *console.Project
|
|
|
|
var owner *console.User
|
|
|
|
|
2020-01-17 15:01:36 +00:00
|
|
|
rateLimit := 100
|
2019-01-31 13:01:13 +00:00
|
|
|
t.Run("Insert project successfully", func(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
owner, err = users.Insert(ctx, &console.User{
|
2020-04-02 15:18:08 +01:00
|
|
|
ID: testrand.UUID(),
|
2019-03-27 12:33:32 +00:00
|
|
|
FullName: userFullName,
|
|
|
|
ShortName: shortName,
|
2019-01-31 13:01:13 +00:00
|
|
|
Email: email,
|
|
|
|
PasswordHash: []byte(pass),
|
|
|
|
})
|
2020-01-17 15:01:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, owner)
|
2020-01-29 00:57:15 +00:00
|
|
|
owner, err := users.Insert(ctx, &console.User{
|
2020-04-02 15:18:08 +01:00
|
|
|
ID: testrand.UUID(),
|
2020-01-29 00:57:15 +00:00
|
|
|
FullName: userFullName,
|
|
|
|
ShortName: shortName,
|
|
|
|
Email: email,
|
|
|
|
PasswordHash: []byte(pass),
|
|
|
|
})
|
2020-01-17 15:01:36 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-29 00:57:15 +00:00
|
|
|
require.NotNil(t, owner)
|
2020-01-17 15:01:36 +00:00
|
|
|
|
2020-01-29 00:57:15 +00:00
|
|
|
t.Run("Insert project successfully", func(t *testing.T) {
|
|
|
|
project = &console.Project{
|
|
|
|
Name: name,
|
|
|
|
Description: description,
|
|
|
|
OwnerID: owner.ID,
|
|
|
|
RateLimit: &rateLimit,
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err = projects.Insert(ctx, project)
|
|
|
|
assert.NotNil(t, project)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
2019-01-31 13:01:13 +00:00
|
|
|
|
2020-01-29 00:57:15 +00:00
|
|
|
t.Run("Get project success", func(t *testing.T) {
|
|
|
|
projectByID, err := projects.Get(ctx, project.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, projectByID.ID, project.ID)
|
|
|
|
assert.Equal(t, projectByID.Name, name)
|
|
|
|
assert.Equal(t, projectByID.OwnerID, owner.ID)
|
|
|
|
assert.Equal(t, projectByID.Description, description)
|
|
|
|
require.NotNil(t, project)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
2019-01-31 13:01:13 +00:00
|
|
|
|
2020-01-29 00:57:15 +00:00
|
|
|
t.Run("Get by projectID success", func(t *testing.T) {
|
|
|
|
projectByID, err := projects.Get(ctx, project.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, project.ID, projectByID.ID)
|
|
|
|
require.Equal(t, name, projectByID.Name)
|
|
|
|
require.Equal(t, owner.ID, projectByID.OwnerID)
|
|
|
|
require.Equal(t, description, projectByID.Description)
|
|
|
|
require.Equal(t, rateLimit, *projectByID.RateLimit)
|
|
|
|
})
|
2019-01-31 13:01:13 +00:00
|
|
|
|
2020-01-29 00:57:15 +00:00
|
|
|
t.Run("Update project success", func(t *testing.T) {
|
|
|
|
oldProject, err := projects.Get(ctx, project.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, oldProject)
|
|
|
|
|
|
|
|
newRateLimit := 1000
|
|
|
|
|
2020-09-10 10:32:35 +01:00
|
|
|
// creating new project with updated values.
|
2020-01-29 00:57:15 +00:00
|
|
|
newProject := &console.Project{
|
|
|
|
ID: oldProject.ID,
|
2020-09-10 10:32:35 +01:00
|
|
|
Name: newName,
|
2020-01-29 00:57:15 +00:00
|
|
|
Description: newDescription,
|
|
|
|
RateLimit: &newRateLimit,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = projects.Update(ctx, newProject)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// fetching updated project from db
|
|
|
|
newProject, err = projects.Get(ctx, oldProject.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, oldProject.ID, newProject.ID)
|
2020-09-10 10:32:35 +01:00
|
|
|
require.Equal(t, newName, newProject.Name)
|
2020-01-29 00:57:15 +00:00
|
|
|
require.Equal(t, newDescription, newProject.Description)
|
|
|
|
require.Equal(t, newRateLimit, *newProject.RateLimit)
|
|
|
|
})
|
2019-01-31 13:01:13 +00:00
|
|
|
|
2020-01-29 00:57:15 +00:00
|
|
|
t.Run("Delete project success", func(t *testing.T) {
|
|
|
|
oldProject, err := projects.Get(ctx, project.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, oldProject)
|
2019-01-31 13:01:13 +00:00
|
|
|
|
2020-01-29 00:57:15 +00:00
|
|
|
err = projects.Delete(ctx, oldProject.ID)
|
|
|
|
require.NoError(t, err)
|
2019-01-31 13:01:13 +00:00
|
|
|
|
2020-01-29 00:57:15 +00:00
|
|
|
_, err = projects.Get(ctx, oldProject.ID)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
2019-01-31 13:01:13 +00:00
|
|
|
|
2020-01-29 00:57:15 +00:00
|
|
|
t.Run("GetAll success", func(t *testing.T) {
|
|
|
|
allProjects, err := projects.GetAll(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(allProjects))
|
2019-01-31 13:01:13 +00:00
|
|
|
|
2020-01-29 00:57:15 +00:00
|
|
|
newProject := &console.Project{
|
|
|
|
Description: description,
|
|
|
|
Name: name,
|
|
|
|
}
|
2019-01-31 13:01:13 +00:00
|
|
|
|
2020-01-29 00:57:15 +00:00
|
|
|
_, err = projects.Insert(ctx, newProject)
|
|
|
|
require.NoError(t, err)
|
2019-01-31 13:01:13 +00:00
|
|
|
|
2020-01-29 00:57:15 +00:00
|
|
|
allProjects, err = projects.GetAll(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(allProjects))
|
2019-01-31 13:01:13 +00:00
|
|
|
|
2020-01-29 00:57:15 +00:00
|
|
|
newProject2 := &console.Project{
|
|
|
|
Description: description,
|
|
|
|
Name: name + "2",
|
|
|
|
}
|
2019-01-31 13:01:13 +00:00
|
|
|
|
2020-01-29 00:57:15 +00:00
|
|
|
_, err = projects.Insert(ctx, newProject2)
|
|
|
|
require.NoError(t, err)
|
2019-01-31 13:01:13 +00:00
|
|
|
|
2020-01-29 00:57:15 +00:00
|
|
|
allProjects, err = projects.GetAll(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 2, len(allProjects))
|
|
|
|
})
|
2019-01-31 13:01:13 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2020-02-21 16:51:28 +00:00
|
|
|
|
|
|
|
func TestProjectsList(t *testing.T) {
|
|
|
|
const (
|
|
|
|
limit = 5
|
|
|
|
length = limit * 4
|
|
|
|
)
|
|
|
|
|
|
|
|
rateLimit := 100
|
|
|
|
|
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) { // repositories
|
|
|
|
// create owner
|
|
|
|
owner, err := db.Console().Users().Insert(ctx,
|
|
|
|
&console.User{
|
2020-04-02 15:18:08 +01:00
|
|
|
ID: testrand.UUID(),
|
2020-02-21 16:51:28 +00:00
|
|
|
FullName: "Billy H",
|
|
|
|
Email: "billyh@example.com",
|
|
|
|
PasswordHash: []byte("example_password"),
|
|
|
|
Status: 1,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
projectsDB := db.Console().Projects()
|
|
|
|
|
|
|
|
//create projects
|
|
|
|
var projects []console.Project
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
proj, err := projectsDB.Insert(ctx,
|
|
|
|
&console.Project{
|
|
|
|
Name: "example",
|
|
|
|
Description: "example",
|
|
|
|
OwnerID: owner.ID,
|
|
|
|
RateLimit: &rateLimit,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
projects = append(projects, *proj)
|
|
|
|
}
|
|
|
|
|
2020-04-30 11:48:29 +01:00
|
|
|
now := time.Now().Add(time.Second)
|
2020-02-21 16:51:28 +00:00
|
|
|
|
|
|
|
projsPage, err := projectsDB.List(ctx, 0, limit, now)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
projectsList := projsPage.Projects
|
|
|
|
|
|
|
|
for projsPage.Next {
|
|
|
|
projsPage, err = projectsDB.List(ctx, projsPage.NextOffset, limit, now)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
projectsList = append(projectsList, projsPage.Projects...)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.False(t, projsPage.Next)
|
|
|
|
require.Equal(t, int64(0), projsPage.NextOffset)
|
|
|
|
require.Equal(t, length, len(projectsList))
|
2020-04-30 11:48:29 +01:00
|
|
|
require.Empty(t, cmp.Diff(projects[0], projectsList[0],
|
|
|
|
cmp.Transformer("Sort", func(xs []console.Project) []console.Project {
|
|
|
|
rs := append([]console.Project{}, xs...)
|
|
|
|
sort.Slice(rs, func(i, k int) bool {
|
|
|
|
return rs[i].ID.String() < rs[k].ID.String()
|
|
|
|
})
|
|
|
|
return rs
|
|
|
|
})))
|
2020-02-21 16:51:28 +00:00
|
|
|
})
|
|
|
|
}
|
2020-06-30 22:49:29 +01:00
|
|
|
|
|
|
|
func TestGetMaxBuckets(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
|
|
|
maxCount := 100
|
|
|
|
consoleDB := db.Console()
|
|
|
|
project, err := consoleDB.Projects().Insert(ctx, &console.Project{Name: "testproject1", MaxBuckets: maxCount})
|
|
|
|
require.NoError(t, err)
|
|
|
|
projectsDB := db.Console().Projects()
|
|
|
|
max, err := projectsDB.GetMaxBuckets(ctx, project.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, maxCount, max)
|
|
|
|
})
|
|
|
|
}
|
2020-09-10 10:32:35 +01:00
|
|
|
|
|
|
|
func TestValidateNameAndDescription(t *testing.T) {
|
|
|
|
t.Run("Project name and description validation test", func(t *testing.T) {
|
|
|
|
validDescription := randString(100)
|
|
|
|
|
|
|
|
// update project with empty name.
|
|
|
|
err := console.ValidateNameAndDescription("", validDescription)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
notValidName := randString(21)
|
|
|
|
|
|
|
|
// update project with too long name.
|
|
|
|
err = console.ValidateNameAndDescription(notValidName, validDescription)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
validName := randString(15)
|
|
|
|
notValidDescription := randString(101)
|
|
|
|
|
|
|
|
// update project with too long description.
|
|
|
|
err = console.ValidateNameAndDescription(validName, notValidDescription)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// update project with valid name and description.
|
|
|
|
err = console.ValidateNameAndDescription(validName, validDescription)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func randString(n int) string {
|
|
|
|
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
|
|
|
|
|
|
|
b := make([]rune, n)
|
|
|
|
for i := range b {
|
|
|
|
b[i] = letters[rand.Intn(len(letters))]
|
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|