Console postgres tests (#1188)
This commit is contained in:
parent
21c6cbccdd
commit
5e27d6ec59
112
satellite/console/apikeys_test.go
Normal file
112
satellite/console/apikeys_test.go
Normal file
@ -0,0 +1,112 @@
|
||||
// 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/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 := console.CreateAPIKey()
|
||||
assert.NoError(t, err)
|
||||
|
||||
keyInfo := console.APIKeyInfo{
|
||||
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)
|
||||
})
|
||||
})
|
||||
}
|
@ -12,7 +12,7 @@ import (
|
||||
"github.com/graphql-go/graphql"
|
||||
"github.com/skyrings/skyring-common/tools/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zaptest"
|
||||
|
||||
"storj.io/storj/internal/testcontext"
|
||||
"storj.io/storj/pkg/auth"
|
||||
@ -28,7 +28,7 @@ func TestGrapqhlMutation(t *testing.T) {
|
||||
ctx := testcontext.New(t)
|
||||
defer ctx.Cleanup()
|
||||
|
||||
log := zap.NewExample()
|
||||
log := zaptest.NewLogger(t)
|
||||
|
||||
service, err := console.NewService(
|
||||
log,
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
|
||||
"github.com/graphql-go/graphql"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zaptest"
|
||||
|
||||
"storj.io/storj/internal/testcontext"
|
||||
"storj.io/storj/pkg/auth"
|
||||
@ -26,7 +26,7 @@ func TestGraphqlQuery(t *testing.T) {
|
||||
ctx := testcontext.New(t)
|
||||
defer ctx.Cleanup()
|
||||
|
||||
log := zap.NewExample()
|
||||
log := zaptest.NewLogger(t)
|
||||
|
||||
service, err := console.NewService(
|
||||
log,
|
||||
|
218
satellite/console/projectmembers_test.go
Normal file
218
satellite/console/projectmembers_test.go
Normal file
@ -0,0 +1,218 @@
|
||||
// Copyright (C) 2019 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
package console_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/skyrings/skyring-common/tools/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"storj.io/storj/internal/testcontext"
|
||||
"storj.io/storj/satellite"
|
||||
"storj.io/storj/satellite/console"
|
||||
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
||||
)
|
||||
|
||||
func TestProjectMembersRepository(t *testing.T) {
|
||||
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
|
||||
ctx := testcontext.New(t)
|
||||
defer ctx.Cleanup()
|
||||
|
||||
// repositories
|
||||
users := db.Console().Users()
|
||||
projects := db.Console().Projects()
|
||||
projectMembers := db.Console().ProjectMembers()
|
||||
|
||||
createdUsers, createdProjects := prepareUsersAndProjects(ctx, t, users, projects)
|
||||
|
||||
t.Run("Can't insert projectMember without memberID", func(t *testing.T) {
|
||||
unexistingUserID, err := uuid.New()
|
||||
assert.NoError(t, err)
|
||||
|
||||
projMember, err := projectMembers.Insert(ctx, *unexistingUserID, createdProjects[0].ID)
|
||||
assert.Nil(t, projMember)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("Can't insert projectMember without projectID", func(t *testing.T) {
|
||||
unexistingProjectID, err := uuid.New()
|
||||
assert.NoError(t, err)
|
||||
|
||||
projMember, err := projectMembers.Insert(ctx, createdUsers[0].ID, *unexistingProjectID)
|
||||
assert.Nil(t, projMember)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("Insert success", func(t *testing.T) {
|
||||
projMember1, err := projectMembers.Insert(ctx, createdUsers[0].ID, createdProjects[0].ID)
|
||||
assert.NotNil(t, projMember1)
|
||||
assert.NoError(t, err)
|
||||
|
||||
projMember2, err := projectMembers.Insert(ctx, createdUsers[1].ID, createdProjects[0].ID)
|
||||
assert.NotNil(t, projMember2)
|
||||
assert.NoError(t, err)
|
||||
|
||||
projMember3, err := projectMembers.Insert(ctx, createdUsers[3].ID, createdProjects[0].ID)
|
||||
assert.NotNil(t, projMember3)
|
||||
assert.NoError(t, err)
|
||||
|
||||
projMember4, err := projectMembers.Insert(ctx, createdUsers[4].ID, createdProjects[0].ID)
|
||||
assert.NotNil(t, projMember4)
|
||||
assert.NoError(t, err)
|
||||
|
||||
projMember5, err := projectMembers.Insert(ctx, createdUsers[5].ID, createdProjects[0].ID)
|
||||
assert.NotNil(t, projMember5)
|
||||
assert.NoError(t, err)
|
||||
|
||||
projMember6, err := projectMembers.Insert(ctx, createdUsers[2].ID, createdProjects[1].ID)
|
||||
assert.NotNil(t, projMember6)
|
||||
assert.NoError(t, err)
|
||||
|
||||
projMember7, err := projectMembers.Insert(ctx, createdUsers[0].ID, createdProjects[1].ID)
|
||||
assert.NotNil(t, projMember7)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("Get projects by userID", func(t *testing.T) {
|
||||
projects, err := projects.GetByUserID(ctx, createdUsers[0].ID)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, projects)
|
||||
assert.Equal(t, len(projects), 2)
|
||||
})
|
||||
|
||||
t.Run("Get paged", func(t *testing.T) {
|
||||
// sql injection test. F.E '%SomeText%' = > ''%SomeText%' OR 'x' != '%'' will be true
|
||||
members, err := projectMembers.GetByProjectID(ctx, createdProjects[0].ID, console.Pagination{Limit: 6, Offset: 0, Search: "son%' OR 'x' != '", Order: 2})
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, members)
|
||||
assert.Equal(t, 0, len(members))
|
||||
|
||||
members, err = projectMembers.GetByProjectID(ctx, createdProjects[0].ID, console.Pagination{Limit: 3, Offset: 0, Search: "", Order: 1})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, members)
|
||||
assert.Equal(t, 3, len(members))
|
||||
|
||||
members, err = projectMembers.GetByProjectID(ctx, createdProjects[0].ID, console.Pagination{Limit: 2, Offset: 0, Search: "iam", Order: 2}) // TODO: fix case sensitity issues and change back to "Liam"
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, members)
|
||||
assert.Equal(t, 2, len(members))
|
||||
|
||||
members, err = projectMembers.GetByProjectID(ctx, createdProjects[0].ID, console.Pagination{Limit: 2, Offset: 0, Search: "iam", Order: 1}) // TODO: fix case sensitity issues and change back to "Liam"
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, members)
|
||||
assert.Equal(t, 2, len(members))
|
||||
|
||||
members, err = projectMembers.GetByProjectID(ctx, createdProjects[0].ID, console.Pagination{Limit: 6, Offset: 0, Search: "son", Order: 123})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, members)
|
||||
assert.Equal(t, 5, len(members))
|
||||
|
||||
members, err = projectMembers.GetByProjectID(ctx, createdProjects[0].ID, console.Pagination{Limit: 6, Offset: 3, Search: "son", Order: 2})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, members)
|
||||
assert.Equal(t, 2, len(members))
|
||||
|
||||
members, err = projectMembers.GetByProjectID(ctx, createdProjects[0].ID, console.Pagination{Limit: -123, Offset: -14, Search: "son", Order: 2})
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, members)
|
||||
assert.Equal(t, 0, len(members))
|
||||
})
|
||||
|
||||
t.Run("Get member by memberID success", func(t *testing.T) {
|
||||
originalMember1 := createdUsers[0]
|
||||
selectedMembers1, err := projectMembers.GetByMemberID(ctx, originalMember1.ID)
|
||||
|
||||
assert.NotNil(t, selectedMembers1)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, originalMember1.ID, selectedMembers1[0].MemberID)
|
||||
|
||||
originalMember2 := createdUsers[1]
|
||||
selectedMembers2, err := projectMembers.GetByMemberID(ctx, originalMember2.ID)
|
||||
|
||||
assert.NotNil(t, selectedMembers2)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, originalMember2.ID, selectedMembers2[0].MemberID)
|
||||
})
|
||||
|
||||
t.Run("Delete member by memberID and projectID success", func(t *testing.T) {
|
||||
err := projectMembers.Delete(ctx, createdUsers[0].ID, createdProjects[0].ID)
|
||||
assert.NoError(t, err)
|
||||
|
||||
projMembers, err := projectMembers.GetByProjectID(ctx, createdProjects[0].ID, console.Pagination{
|
||||
Order: 1,
|
||||
Search: "",
|
||||
Offset: 0,
|
||||
Limit: 100,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, projectMembers)
|
||||
assert.Equal(t, len(projMembers), 4)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func prepareUsersAndProjects(ctx context.Context, t *testing.T, users console.Users, projects console.Projects) ([]*console.User, []*console.Project) {
|
||||
usersList := []*console.User{{
|
||||
Email: "2email2@ukr.net",
|
||||
PasswordHash: []byte("some_readable_hash"),
|
||||
LastName: "Liam",
|
||||
FirstName: "Jameson",
|
||||
}, {
|
||||
Email: "1email1@ukr.net",
|
||||
PasswordHash: []byte("some_readable_hash"),
|
||||
LastName: "William",
|
||||
FirstName: "Noahson",
|
||||
}, {
|
||||
Email: "email3@ukr.net",
|
||||
PasswordHash: []byte("some_readable_hash"),
|
||||
LastName: "Mason",
|
||||
FirstName: "Elijahson",
|
||||
}, {
|
||||
Email: "email4@ukr.net",
|
||||
PasswordHash: []byte("some_readable_hash"),
|
||||
LastName: "Oliver",
|
||||
FirstName: "Jacobson",
|
||||
}, {
|
||||
Email: "email5@ukr.net",
|
||||
PasswordHash: []byte("some_readable_hash"),
|
||||
LastName: "Lucas",
|
||||
FirstName: "Michaelson",
|
||||
}, {
|
||||
Email: "email6@ukr.net",
|
||||
PasswordHash: []byte("some_readable_hash"),
|
||||
LastName: "Alexander",
|
||||
FirstName: "Ethanson",
|
||||
},
|
||||
}
|
||||
|
||||
var err error
|
||||
for i, user := range usersList {
|
||||
usersList[i], err = users.Insert(ctx, user)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
projectList := []*console.Project{
|
||||
{
|
||||
Name: "projName1",
|
||||
Description: "Test project 1",
|
||||
},
|
||||
{
|
||||
Name: "projName2",
|
||||
Description: "Test project 1",
|
||||
},
|
||||
}
|
||||
|
||||
for i, project := range projectList {
|
||||
projectList[i], err = projects.Insert(ctx, project)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
return usersList, projectList
|
||||
}
|
144
satellite/console/projects_test.go
Normal file
144
satellite/console/projects_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
// Copyright (C) 2019 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
package console_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"storj.io/storj/internal/testcontext"
|
||||
"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
|
||||
lastName = "lastName"
|
||||
email = "email@ukr.net"
|
||||
pass = "123456"
|
||||
userName = "name"
|
||||
|
||||
// for project
|
||||
name = "Project"
|
||||
description = "some description"
|
||||
|
||||
// updated project values
|
||||
newDescription = "some new description"
|
||||
)
|
||||
|
||||
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
|
||||
ctx := testcontext.New(t)
|
||||
defer ctx.Cleanup()
|
||||
|
||||
// repositories
|
||||
users := db.Console().Users()
|
||||
projects := db.Console().Projects()
|
||||
var project *console.Project
|
||||
var owner *console.User
|
||||
|
||||
t.Run("Insert project successfully", func(t *testing.T) {
|
||||
var err error
|
||||
owner, err = users.Insert(ctx, &console.User{
|
||||
FirstName: userName,
|
||||
LastName: lastName,
|
||||
Email: email,
|
||||
PasswordHash: []byte(pass),
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, owner)
|
||||
|
||||
project = &console.Project{
|
||||
Name: name,
|
||||
Description: description,
|
||||
}
|
||||
|
||||
project, err = projects.Insert(ctx, project)
|
||||
assert.NotNil(t, project)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
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.Description, description)
|
||||
})
|
||||
|
||||
t.Run("Get by projectID 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.Description, description)
|
||||
})
|
||||
|
||||
t.Run("Update project success", func(t *testing.T) {
|
||||
oldProject, err := projects.Get(ctx, project.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, oldProject)
|
||||
|
||||
// creating new project with updated values
|
||||
newProject := &console.Project{
|
||||
ID: oldProject.ID,
|
||||
Description: newDescription,
|
||||
}
|
||||
|
||||
err = projects.Update(ctx, newProject)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// fetching updated project from db
|
||||
newProject, err = projects.Get(ctx, oldProject.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, newProject.ID, oldProject.ID)
|
||||
assert.Equal(t, newProject.Description, newDescription)
|
||||
})
|
||||
|
||||
t.Run("Delete project success", func(t *testing.T) {
|
||||
oldProject, err := projects.Get(ctx, project.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, oldProject)
|
||||
|
||||
err = projects.Delete(ctx, oldProject.ID)
|
||||
assert.NoError(t, err)
|
||||
|
||||
_, err = projects.Get(ctx, oldProject.ID)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("GetAll success", func(t *testing.T) {
|
||||
allProjects, err := projects.GetAll(ctx)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, len(allProjects), 0)
|
||||
|
||||
newProject := &console.Project{
|
||||
Description: description,
|
||||
Name: name,
|
||||
}
|
||||
|
||||
_, err = projects.Insert(ctx, newProject)
|
||||
assert.NoError(t, err)
|
||||
|
||||
allProjects, err = projects.GetAll(ctx)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, len(allProjects), 1)
|
||||
|
||||
newProject2 := &console.Project{
|
||||
Description: description,
|
||||
Name: name,
|
||||
}
|
||||
|
||||
_, err = projects.Insert(ctx, newProject2)
|
||||
assert.NoError(t, err)
|
||||
|
||||
allProjects, err = projects.GetAll(ctx)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, len(allProjects), 2)
|
||||
})
|
||||
})
|
||||
}
|
123
satellite/console/users_test.go
Normal file
123
satellite/console/users_test.go
Normal file
@ -0,0 +1,123 @@
|
||||
// Copyright (C) 2019 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
package console_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/skyrings/skyring-common/tools/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"storj.io/storj/internal/testcontext"
|
||||
"storj.io/storj/satellite"
|
||||
"storj.io/storj/satellite/console"
|
||||
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
||||
)
|
||||
|
||||
func TestUserRepository(t *testing.T) {
|
||||
//testing constants
|
||||
const (
|
||||
lastName = "lastName"
|
||||
email = "email@ukr.net"
|
||||
passValid = "123456"
|
||||
name = "name"
|
||||
newName = "newName"
|
||||
newLastName = "newLastName"
|
||||
newEmail = "newEmail@ukr.net"
|
||||
newPass = "newPass1234567890123456789012345"
|
||||
)
|
||||
|
||||
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
|
||||
ctx := testcontext.New(t)
|
||||
defer ctx.Cleanup()
|
||||
|
||||
repository := db.Console().Users()
|
||||
|
||||
t.Run("User insertion success", func(t *testing.T) {
|
||||
id, err := uuid.New()
|
||||
assert.NoError(t, err)
|
||||
|
||||
user := &console.User{
|
||||
ID: *id,
|
||||
FirstName: name,
|
||||
LastName: lastName,
|
||||
Email: email,
|
||||
PasswordHash: []byte(passValid),
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
_, err = repository.Insert(ctx, user)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("Can't insert user with same email twice", func(t *testing.T) {
|
||||
user := &console.User{
|
||||
FirstName: name,
|
||||
LastName: lastName,
|
||||
Email: email,
|
||||
PasswordHash: []byte(passValid),
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
_, err := repository.Insert(ctx, user)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("Get user success", func(t *testing.T) {
|
||||
userByEmail, err := repository.GetByEmail(ctx, email)
|
||||
assert.Equal(t, userByEmail.FirstName, name)
|
||||
assert.Equal(t, userByEmail.LastName, lastName)
|
||||
assert.NoError(t, err)
|
||||
|
||||
userByID, err := repository.Get(ctx, userByEmail.ID)
|
||||
assert.Equal(t, userByID.FirstName, name)
|
||||
assert.Equal(t, userByID.LastName, lastName)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, userByID.ID, userByEmail.ID)
|
||||
assert.Equal(t, userByID.FirstName, userByEmail.FirstName)
|
||||
assert.Equal(t, userByID.LastName, userByEmail.LastName)
|
||||
assert.Equal(t, userByID.Email, userByEmail.Email)
|
||||
assert.Equal(t, userByID.PasswordHash, userByEmail.PasswordHash)
|
||||
assert.Equal(t, userByID.CreatedAt, userByEmail.CreatedAt)
|
||||
})
|
||||
|
||||
t.Run("Update user success", func(t *testing.T) {
|
||||
oldUser, err := repository.GetByEmail(ctx, email)
|
||||
assert.NoError(t, err)
|
||||
|
||||
newUser := &console.User{
|
||||
ID: oldUser.ID,
|
||||
FirstName: newName,
|
||||
LastName: newLastName,
|
||||
Email: newEmail,
|
||||
PasswordHash: []byte(newPass),
|
||||
}
|
||||
|
||||
err = repository.Update(ctx, newUser)
|
||||
assert.NoError(t, err)
|
||||
|
||||
newUser, err = repository.Get(ctx, oldUser.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, newUser.ID, oldUser.ID)
|
||||
assert.Equal(t, newUser.FirstName, newName)
|
||||
assert.Equal(t, newUser.LastName, newLastName)
|
||||
assert.Equal(t, newUser.Email, newEmail)
|
||||
assert.Equal(t, newUser.PasswordHash, []byte(newPass))
|
||||
assert.Equal(t, newUser.CreatedAt, oldUser.CreatedAt)
|
||||
})
|
||||
|
||||
t.Run("Delete user success", func(t *testing.T) {
|
||||
oldUser, err := repository.GetByEmail(ctx, newEmail)
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = repository.Delete(ctx, oldUser.ID)
|
||||
assert.NoError(t, err)
|
||||
|
||||
_, err = repository.Get(ctx, oldUser.ID)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
})
|
||||
}
|
@ -1,121 +0,0 @@
|
||||
// Copyright (C) 2019 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
package satellitedb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"storj.io/storj/internal/testcontext"
|
||||
"storj.io/storj/satellite/console"
|
||||
)
|
||||
|
||||
func TestApiKeysRepository(t *testing.T) {
|
||||
ctx := testcontext.New(t)
|
||||
defer ctx.Cleanup()
|
||||
|
||||
// creating in-memory db and opening connection
|
||||
db, err := NewConsoleDB("sqlite3", "file::memory:?mode=memory&cache=shared")
|
||||
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()
|
||||
|
||||
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 := console.CreateAPIKey()
|
||||
assert.NoError(t, err)
|
||||
|
||||
keyInfo := console.APIKeyInfo{
|
||||
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)
|
||||
})
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
// Copyright (C) 2019 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
package satellitedb
|
||||
package satellitedb_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
@ -9,75 +9,41 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"storj.io/storj/internal/testcontext"
|
||||
"storj.io/storj/satellite"
|
||||
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
||||
)
|
||||
|
||||
func TestDatabase(t *testing.T) {
|
||||
func TestConsoleTx(t *testing.T) {
|
||||
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
|
||||
ctx := testcontext.New(t)
|
||||
defer ctx.Cleanup()
|
||||
|
||||
t.Run("BeginTx return err when db is nil", func(t *testing.T) {
|
||||
db := &ConsoleDB{
|
||||
db: nil,
|
||||
}
|
||||
console := db.Console()
|
||||
|
||||
transaction, err := db.BeginTx(ctx)
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, transaction)
|
||||
t.Run("BeginTx, Commit, Rollback", func(t *testing.T) {
|
||||
tx, err := console.BeginTx(ctx)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, tx)
|
||||
|
||||
// TODO: add something into database
|
||||
|
||||
assert.NoError(t, tx.Commit())
|
||||
assert.Error(t, tx.Rollback())
|
||||
|
||||
// TODO: check whether it has been committed
|
||||
})
|
||||
|
||||
t.Run("BeginTx and Commit success", func(t *testing.T) {
|
||||
db, err := NewConsoleDB("sqlite3", "file::memory:?mode=memory&cache=shared")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
transaction, err := db.BeginTx(ctx)
|
||||
t.Run("BeginTx, Rollback, Commit", func(t *testing.T) {
|
||||
tx, err := console.BeginTx(ctx)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, transaction)
|
||||
assert.NotNil(t, tx)
|
||||
|
||||
err = transaction.Commit()
|
||||
assert.NoError(t, err)
|
||||
// TODO: add something into database
|
||||
|
||||
err = db.Close()
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, tx.Rollback())
|
||||
assert.Error(t, tx.Commit())
|
||||
|
||||
// TODO: check whether it has been rolled back
|
||||
})
|
||||
|
||||
t.Run("BeginTx and Rollback success", func(t *testing.T) {
|
||||
db, err := NewConsoleDB("sqlite3", "file::memory:?mode=memory&cache=shared")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
transaction, err := db.BeginTx(ctx)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, transaction)
|
||||
|
||||
err = transaction.Rollback()
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = db.Close()
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("Commit fails", func(t *testing.T) {
|
||||
transaction := &DBTx{
|
||||
ConsoleDB: &ConsoleDB{
|
||||
tx: nil,
|
||||
},
|
||||
}
|
||||
|
||||
err := transaction.Commit()
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("Rollback fails", func(t *testing.T) {
|
||||
transaction := &DBTx{
|
||||
ConsoleDB: &ConsoleDB{
|
||||
tx: nil,
|
||||
},
|
||||
}
|
||||
|
||||
err := transaction.Rollback()
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
@ -4,6 +4,8 @@
|
||||
package satellitedb
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/zeebo/errs"
|
||||
|
||||
"storj.io/storj/internal/migrate"
|
||||
@ -29,6 +31,7 @@ var (
|
||||
// DB contains access to different database tables
|
||||
type DB struct {
|
||||
db *dbx.DB
|
||||
driver string
|
||||
}
|
||||
|
||||
// New creates instance of database (supports: postgres, sqlite3)
|
||||
@ -44,7 +47,7 @@ func New(databaseURL string) (satellite.DB, error) {
|
||||
driver, source, err)
|
||||
}
|
||||
|
||||
core := &DB{db: db}
|
||||
core := &DB{db: db, driver: driver}
|
||||
if driver == "sqlite3" {
|
||||
return newLocked(core), nil
|
||||
}
|
||||
@ -56,6 +59,50 @@ func NewInMemory() (satellite.DB, error) {
|
||||
return New("sqlite3://file::memory:?mode=memory")
|
||||
}
|
||||
|
||||
// DROP_ALL_TABLES drops all tables in the database.
|
||||
// Deprected: do not use in production.
|
||||
func DROP_ALL_TABLES(dbAny satellite.DB) (err error) { //nolint: ignore all caps requirement for dangerous function
|
||||
var db *DB
|
||||
switch temp := dbAny.(type) {
|
||||
case *DB:
|
||||
db = temp
|
||||
case *locked:
|
||||
return DROP_ALL_TABLES(temp.db)
|
||||
default:
|
||||
return errors.New("unsupported implementation")
|
||||
}
|
||||
|
||||
switch db.driver {
|
||||
case "postgres":
|
||||
rows, err := db.db.Query(`select tablename from pg_tables where schemaname = 'public';`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { err = errs.Combine(err, rows.Close()) }()
|
||||
|
||||
for rows.Next() {
|
||||
var tablename string
|
||||
err := rows.Scan(&tablename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = db.db.Exec(`drop table "` + tablename + `" cascade;`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// BandwidthAgreement is a getter for bandwidth agreement repository
|
||||
func (db *DB) BandwidthAgreement() bwagreement.DB {
|
||||
return &bandwidthagreement{db: db.db}
|
||||
|
@ -40,7 +40,8 @@ func (pm *projectMembers) GetByProjectID(ctx context.Context, projectID uuid.UUI
|
||||
searchSubQuery := "%" + pagination.Search + "%"
|
||||
//`+getOrder(pagination.Order)+`
|
||||
|
||||
rebindedQuery := pm.db.Rebind(`
|
||||
// TODO: LIKE is case-sensitive postgres, however this should be case-insensitive and possibly allow typos
|
||||
reboundQuery := pm.db.Rebind(`
|
||||
SELECT pm.*
|
||||
FROM project_members pm
|
||||
INNER JOIN users u ON pm.member_id = u.id
|
||||
@ -52,7 +53,7 @@ func (pm *projectMembers) GetByProjectID(ctx context.Context, projectID uuid.UUI
|
||||
LIMIT ? OFFSET ?
|
||||
`)
|
||||
|
||||
rows, err := pm.db.Query(rebindedQuery, projectID[:], searchSubQuery, searchSubQuery, searchSubQuery, pagination.Limit, pagination.Offset)
|
||||
rows, err := pm.db.Query(reboundQuery, projectID[:], searchSubQuery, searchSubQuery, searchSubQuery, pagination.Limit, pagination.Offset)
|
||||
|
||||
defer func() {
|
||||
err = errs.Combine(err, rows.Close())
|
||||
|
@ -4,228 +4,13 @@
|
||||
package satellitedb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/skyrings/skyring-common/tools/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"storj.io/storj/internal/testcontext"
|
||||
"storj.io/storj/satellite/console"
|
||||
)
|
||||
|
||||
func TestProjectMembersRepository(t *testing.T) {
|
||||
ctx := testcontext.New(t)
|
||||
defer ctx.Cleanup()
|
||||
|
||||
// creating in-memory db and opening connection
|
||||
db, err := NewConsoleDB("sqlite3", "file::memory:?mode=memory&cache=shared")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer ctx.Check(db.Close)
|
||||
|
||||
// creating tables
|
||||
err = db.CreateTables()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// repositories
|
||||
users := db.Users()
|
||||
projects := db.Projects()
|
||||
projectMembers := db.ProjectMembers()
|
||||
|
||||
createdUsers, createdProjects := prepareUsersAndProjects(ctx, t, users, projects)
|
||||
|
||||
t.Run("Can't insert projectMember without memberID", func(t *testing.T) {
|
||||
unexistingUserID, err := uuid.New()
|
||||
assert.NoError(t, err)
|
||||
|
||||
projMember, err := projectMembers.Insert(ctx, *unexistingUserID, createdProjects[0].ID)
|
||||
assert.Nil(t, projMember)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("Can't insert projectMember without projectID", func(t *testing.T) {
|
||||
unexistingProjectID, err := uuid.New()
|
||||
assert.NoError(t, err)
|
||||
|
||||
projMember, err := projectMembers.Insert(ctx, createdUsers[0].ID, *unexistingProjectID)
|
||||
assert.Nil(t, projMember)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("Insert success", func(t *testing.T) {
|
||||
projMember1, err := projectMembers.Insert(ctx, createdUsers[0].ID, createdProjects[0].ID)
|
||||
assert.NotNil(t, projMember1)
|
||||
assert.NoError(t, err)
|
||||
|
||||
projMember2, err := projectMembers.Insert(ctx, createdUsers[1].ID, createdProjects[0].ID)
|
||||
assert.NotNil(t, projMember2)
|
||||
assert.NoError(t, err)
|
||||
|
||||
projMember3, err := projectMembers.Insert(ctx, createdUsers[3].ID, createdProjects[0].ID)
|
||||
assert.NotNil(t, projMember3)
|
||||
assert.NoError(t, err)
|
||||
|
||||
projMember4, err := projectMembers.Insert(ctx, createdUsers[4].ID, createdProjects[0].ID)
|
||||
assert.NotNil(t, projMember4)
|
||||
assert.NoError(t, err)
|
||||
|
||||
projMember5, err := projectMembers.Insert(ctx, createdUsers[5].ID, createdProjects[0].ID)
|
||||
assert.NotNil(t, projMember5)
|
||||
assert.NoError(t, err)
|
||||
|
||||
projMember6, err := projectMembers.Insert(ctx, createdUsers[2].ID, createdProjects[1].ID)
|
||||
assert.NotNil(t, projMember6)
|
||||
assert.NoError(t, err)
|
||||
|
||||
projMember7, err := projectMembers.Insert(ctx, createdUsers[0].ID, createdProjects[1].ID)
|
||||
assert.NotNil(t, projMember7)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("Get projects by userID", func(t *testing.T) {
|
||||
projects, err := projects.GetByUserID(ctx, createdUsers[0].ID)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, projects)
|
||||
assert.Equal(t, len(projects), 2)
|
||||
})
|
||||
|
||||
t.Run("Get paged", func(t *testing.T) {
|
||||
// sql injection test. F.E '%SomeText%' = > ''%SomeText%' OR 'x' != '%'' will be true
|
||||
members, err := projectMembers.GetByProjectID(ctx, createdProjects[0].ID, console.Pagination{Limit: 6, Offset: 0, Search: "son%' OR 'x' != '", Order: 2})
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, members)
|
||||
assert.Equal(t, 0, len(members))
|
||||
|
||||
members, err = projectMembers.GetByProjectID(ctx, createdProjects[0].ID, console.Pagination{Limit: 3, Offset: 0, Search: "", Order: 1})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, members)
|
||||
assert.Equal(t, 3, len(members))
|
||||
|
||||
members, err = projectMembers.GetByProjectID(ctx, createdProjects[0].ID, console.Pagination{Limit: 2, Offset: 0, Search: "Liam", Order: 2})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, members)
|
||||
assert.Equal(t, 2, len(members))
|
||||
|
||||
members, err = projectMembers.GetByProjectID(ctx, createdProjects[0].ID, console.Pagination{Limit: 2, Offset: 0, Search: "Liam", Order: 1})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, members)
|
||||
assert.Equal(t, 2, len(members))
|
||||
|
||||
members, err = projectMembers.GetByProjectID(ctx, createdProjects[0].ID, console.Pagination{Limit: 6, Offset: 0, Search: "son", Order: 123})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, members)
|
||||
assert.Equal(t, 5, len(members))
|
||||
|
||||
members, err = projectMembers.GetByProjectID(ctx, createdProjects[0].ID, console.Pagination{Limit: 6, Offset: 3, Search: "son", Order: 2})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, members)
|
||||
assert.Equal(t, 2, len(members))
|
||||
|
||||
members, err = projectMembers.GetByProjectID(ctx, createdProjects[0].ID, console.Pagination{Limit: -123, Offset: -14, Search: "son", Order: 2})
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, members)
|
||||
assert.Equal(t, 0, len(members))
|
||||
})
|
||||
|
||||
t.Run("Get member by memberID success", func(t *testing.T) {
|
||||
originalMember1 := createdUsers[0]
|
||||
selectedMembers1, err := projectMembers.GetByMemberID(ctx, originalMember1.ID)
|
||||
|
||||
assert.NotNil(t, selectedMembers1)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, originalMember1.ID, selectedMembers1[0].MemberID)
|
||||
|
||||
originalMember2 := createdUsers[1]
|
||||
selectedMembers2, err := projectMembers.GetByMemberID(ctx, originalMember2.ID)
|
||||
|
||||
assert.NotNil(t, selectedMembers2)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, originalMember2.ID, selectedMembers2[0].MemberID)
|
||||
})
|
||||
|
||||
t.Run("Delete member by memberID and projectID success", func(t *testing.T) {
|
||||
err := projectMembers.Delete(ctx, createdUsers[0].ID, createdProjects[0].ID)
|
||||
assert.NoError(t, err)
|
||||
|
||||
projMembers, err := projectMembers.GetByProjectID(ctx, createdProjects[0].ID, console.Pagination{
|
||||
Order: 1,
|
||||
Search: "",
|
||||
Offset: 0,
|
||||
Limit: 100,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, projectMembers)
|
||||
assert.Equal(t, len(projMembers), 4)
|
||||
})
|
||||
}
|
||||
|
||||
func prepareUsersAndProjects(ctx context.Context, t *testing.T, users console.Users, projects console.Projects) ([]*console.User, []*console.Project) {
|
||||
usersList := []*console.User{{
|
||||
Email: "2email2@ukr.net",
|
||||
PasswordHash: []byte("some_readable_hash"),
|
||||
LastName: "Liam",
|
||||
FirstName: "Jameson",
|
||||
}, {
|
||||
Email: "1email1@ukr.net",
|
||||
PasswordHash: []byte("some_readable_hash"),
|
||||
LastName: "William",
|
||||
FirstName: "Noahson",
|
||||
}, {
|
||||
Email: "email3@ukr.net",
|
||||
PasswordHash: []byte("some_readable_hash"),
|
||||
LastName: "Mason",
|
||||
FirstName: "Elijahson",
|
||||
}, {
|
||||
Email: "email4@ukr.net",
|
||||
PasswordHash: []byte("some_readable_hash"),
|
||||
LastName: "Oliver",
|
||||
FirstName: "Jacobson",
|
||||
}, {
|
||||
Email: "email5@ukr.net",
|
||||
PasswordHash: []byte("some_readable_hash"),
|
||||
LastName: "Lucas",
|
||||
FirstName: "Michaelson",
|
||||
}, {
|
||||
Email: "email6@ukr.net",
|
||||
PasswordHash: []byte("some_readable_hash"),
|
||||
LastName: "Alexander",
|
||||
FirstName: "Ethanson",
|
||||
},
|
||||
}
|
||||
|
||||
var err error
|
||||
for i, user := range usersList {
|
||||
usersList[i], err = users.Insert(ctx, user)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
projectList := []*console.Project{
|
||||
{
|
||||
Name: "projName1",
|
||||
Description: "Test project 1",
|
||||
},
|
||||
{
|
||||
Name: "projName2",
|
||||
Description: "Test project 1",
|
||||
},
|
||||
}
|
||||
|
||||
for i, project := range projectList {
|
||||
projectList[i], err = projects.Insert(ctx, project)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
return usersList, projectList
|
||||
}
|
||||
|
||||
func TestSanitizedOrderColumnName(t *testing.T) {
|
||||
testCases := [...]struct {
|
||||
orderNumber int8
|
||||
|
@ -8,151 +8,9 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"storj.io/storj/internal/testcontext"
|
||||
"storj.io/storj/satellite/console"
|
||||
dbx "storj.io/storj/satellite/satellitedb/dbx"
|
||||
)
|
||||
|
||||
func TestProjectsRepository(t *testing.T) {
|
||||
//testing constants
|
||||
const (
|
||||
// for user
|
||||
lastName = "lastName"
|
||||
email = "email@ukr.net"
|
||||
pass = "123456"
|
||||
userName = "name"
|
||||
|
||||
// for project
|
||||
name = "Project"
|
||||
description = "some description"
|
||||
|
||||
// updated project values
|
||||
newDescription = "some new description"
|
||||
)
|
||||
|
||||
ctx := testcontext.New(t)
|
||||
defer ctx.Cleanup()
|
||||
|
||||
// creating in-memory db and opening connection
|
||||
// to test with real db3 file use this connection string - "../db/accountdb.db3"
|
||||
db, err := NewConsoleDB("sqlite3", "file::memory:?mode=memory&cache=shared")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer ctx.Check(db.Close)
|
||||
|
||||
// creating tables
|
||||
err = db.CreateTables()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// repositories
|
||||
users := db.Users()
|
||||
projects := db.Projects()
|
||||
var project *console.Project
|
||||
var owner *console.User
|
||||
|
||||
t.Run("Insert project successfully", func(t *testing.T) {
|
||||
owner, err = users.Insert(ctx, &console.User{
|
||||
FirstName: userName,
|
||||
LastName: lastName,
|
||||
Email: email,
|
||||
PasswordHash: []byte(pass),
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, owner)
|
||||
|
||||
project = &console.Project{
|
||||
Name: name,
|
||||
Description: description,
|
||||
}
|
||||
|
||||
project, err = projects.Insert(ctx, project)
|
||||
assert.NotNil(t, project)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
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.Description, description)
|
||||
})
|
||||
|
||||
t.Run("Get by projectID 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.Description, description)
|
||||
})
|
||||
|
||||
t.Run("Update project success", func(t *testing.T) {
|
||||
oldProject, err := projects.Get(ctx, project.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, oldProject)
|
||||
|
||||
// creating new project with updated values
|
||||
newProject := &console.Project{
|
||||
ID: oldProject.ID,
|
||||
Description: newDescription,
|
||||
}
|
||||
|
||||
err = projects.Update(ctx, newProject)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// fetching updated project from db
|
||||
newProject, err = projects.Get(ctx, oldProject.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, newProject.ID, oldProject.ID)
|
||||
assert.Equal(t, newProject.Description, newDescription)
|
||||
})
|
||||
|
||||
t.Run("Delete project success", func(t *testing.T) {
|
||||
oldProject, err := projects.Get(ctx, project.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, oldProject)
|
||||
|
||||
err = projects.Delete(ctx, oldProject.ID)
|
||||
assert.NoError(t, err)
|
||||
|
||||
_, err = projects.Get(ctx, oldProject.ID)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("GetAll success", func(t *testing.T) {
|
||||
allProjects, err := projects.GetAll(ctx)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, len(allProjects), 0)
|
||||
|
||||
newProject := &console.Project{
|
||||
Description: description,
|
||||
Name: name,
|
||||
}
|
||||
|
||||
_, err = projects.Insert(ctx, newProject)
|
||||
assert.NoError(t, err)
|
||||
|
||||
allProjects, err = projects.GetAll(ctx)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, len(allProjects), 1)
|
||||
|
||||
newProject2 := &console.Project{
|
||||
Description: description,
|
||||
Name: name,
|
||||
}
|
||||
|
||||
_, err = projects.Insert(ctx, newProject2)
|
||||
assert.NoError(t, err)
|
||||
|
||||
allProjects, err = projects.GetAll(ctx)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, len(allProjects), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestProjectFromDbx(t *testing.T) {
|
||||
t.Run("can't create dbo from nil dbx model", func(t *testing.T) {
|
||||
project, err := projectFromDBX(nil)
|
||||
|
@ -52,6 +52,12 @@ func Run(t *testing.T, test func(t *testing.T, db satellite.DB)) {
|
||||
}
|
||||
}()
|
||||
|
||||
// TODO: figure out a better way to do this safely
|
||||
err = satellitedb.DROP_ALL_TABLES(db) //nolint ignore warning
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = db.CreateTables()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
@ -1,7 +1,7 @@
|
||||
// Copyright (C) 2019 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
|
||||
package satellitedb_test
|
||||
package satellitedbtest_test
|
||||
|
||||
import (
|
||||
"testing"
|
@ -7,132 +7,11 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/skyrings/skyring-common/tools/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"storj.io/storj/internal/testcontext"
|
||||
"storj.io/storj/satellite/console"
|
||||
dbx "storj.io/storj/satellite/satellitedb/dbx"
|
||||
)
|
||||
|
||||
func TestUserRepository(t *testing.T) {
|
||||
//testing constants
|
||||
const (
|
||||
lastName = "lastName"
|
||||
email = "email@ukr.net"
|
||||
passValid = "123456"
|
||||
name = "name"
|
||||
newName = "newName"
|
||||
newLastName = "newLastName"
|
||||
newEmail = "newEmail@ukr.net"
|
||||
newPass = "newPass1234567890123456789012345"
|
||||
)
|
||||
|
||||
ctx := testcontext.New(t)
|
||||
defer ctx.Cleanup()
|
||||
|
||||
// creating in-memory db and opens connection
|
||||
// to test with real db3 file use this connection string - "../db/accountdb.db3"
|
||||
db, err := NewConsoleDB("sqlite3", "file::memory:?mode=memory&cache=shared")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer ctx.Check(db.Close)
|
||||
|
||||
// creating tables
|
||||
err = db.CreateTables()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
repository := db.Users()
|
||||
|
||||
t.Run("User insertion success", func(t *testing.T) {
|
||||
id, err := uuid.New()
|
||||
assert.NoError(t, err)
|
||||
|
||||
user := &console.User{
|
||||
ID: *id,
|
||||
FirstName: name,
|
||||
LastName: lastName,
|
||||
Email: email,
|
||||
PasswordHash: []byte(passValid),
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
_, err = repository.Insert(ctx, user)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("Can't insert user with same email twice", func(t *testing.T) {
|
||||
user := &console.User{
|
||||
FirstName: name,
|
||||
LastName: lastName,
|
||||
Email: email,
|
||||
PasswordHash: []byte(passValid),
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
_, err = repository.Insert(ctx, user)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("Get user success", func(t *testing.T) {
|
||||
userByEmail, err := repository.GetByEmail(ctx, email)
|
||||
assert.Equal(t, userByEmail.FirstName, name)
|
||||
assert.Equal(t, userByEmail.LastName, lastName)
|
||||
assert.NoError(t, err)
|
||||
|
||||
userByID, err := repository.Get(ctx, userByEmail.ID)
|
||||
assert.Equal(t, userByID.FirstName, name)
|
||||
assert.Equal(t, userByID.LastName, lastName)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, userByID.ID, userByEmail.ID)
|
||||
assert.Equal(t, userByID.FirstName, userByEmail.FirstName)
|
||||
assert.Equal(t, userByID.LastName, userByEmail.LastName)
|
||||
assert.Equal(t, userByID.Email, userByEmail.Email)
|
||||
assert.Equal(t, userByID.PasswordHash, userByEmail.PasswordHash)
|
||||
assert.Equal(t, userByID.CreatedAt, userByEmail.CreatedAt)
|
||||
})
|
||||
|
||||
t.Run("Update user success", func(t *testing.T) {
|
||||
oldUser, err := repository.GetByEmail(ctx, email)
|
||||
assert.NoError(t, err)
|
||||
|
||||
newUser := &console.User{
|
||||
ID: oldUser.ID,
|
||||
FirstName: newName,
|
||||
LastName: newLastName,
|
||||
Email: newEmail,
|
||||
PasswordHash: []byte(newPass),
|
||||
}
|
||||
|
||||
err = repository.Update(ctx, newUser)
|
||||
assert.NoError(t, err)
|
||||
|
||||
newUser, err = repository.Get(ctx, oldUser.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, newUser.ID, oldUser.ID)
|
||||
assert.Equal(t, newUser.FirstName, newName)
|
||||
assert.Equal(t, newUser.LastName, newLastName)
|
||||
assert.Equal(t, newUser.Email, newEmail)
|
||||
assert.Equal(t, newUser.PasswordHash, []byte(newPass))
|
||||
assert.Equal(t, newUser.CreatedAt, oldUser.CreatedAt)
|
||||
})
|
||||
|
||||
t.Run("Delete user success", func(t *testing.T) {
|
||||
oldUser, err := repository.GetByEmail(ctx, newEmail)
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = repository.Delete(ctx, oldUser.ID)
|
||||
assert.NoError(t, err)
|
||||
|
||||
_, err = repository.Get(ctx, oldUser.ID)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestUserFromDbx(t *testing.T) {
|
||||
t.Run("can't create dbo from nil dbx model", func(t *testing.T) {
|
||||
user, err := userFromDBX(nil)
|
||||
|
Loading…
Reference in New Issue
Block a user