2018-11-13 08:27:42 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-11-29 18:39:27 +00:00
|
|
|
|
2018-11-13 08:27:42 +00:00
|
|
|
"storj.io/storj/internal/testcontext"
|
|
|
|
"storj.io/storj/pkg/satellite"
|
2018-11-30 13:40:13 +00:00
|
|
|
"storj.io/storj/pkg/satellite/satellitedb/dbx"
|
2018-11-13 08:27:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestProjectsRepository(t *testing.T) {
|
|
|
|
//testing constants
|
|
|
|
const (
|
|
|
|
// for user
|
|
|
|
lastName = "lastName"
|
|
|
|
email = "email@ukr.net"
|
|
|
|
pass = "123456"
|
|
|
|
userName = "name"
|
|
|
|
|
|
|
|
// for project
|
2018-11-14 12:45:49 +00:00
|
|
|
name = "Project"
|
2018-11-13 08:27:42 +00:00
|
|
|
description = "some description"
|
|
|
|
|
|
|
|
// updated project values
|
2018-11-14 12:45:49 +00:00
|
|
|
newName = "NewProject"
|
2018-11-13 08:27:42 +00:00
|
|
|
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 := New("sqlite3", "file::memory:?mode=memory&cache=shared")
|
|
|
|
if err != nil {
|
2018-11-20 14:50:47 +00:00
|
|
|
t.Fatal(err)
|
2018-11-13 08:27:42 +00:00
|
|
|
}
|
|
|
|
defer ctx.Check(db.Close)
|
|
|
|
|
|
|
|
// creating tables
|
|
|
|
err = db.CreateTables()
|
|
|
|
if err != nil {
|
2018-11-20 14:50:47 +00:00
|
|
|
t.Fatal(err)
|
2018-11-13 08:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// repositories
|
|
|
|
users := db.Users()
|
|
|
|
projects := db.Projects()
|
|
|
|
|
2018-11-14 12:45:49 +00:00
|
|
|
var owner *satellite.User
|
2018-11-13 08:27:42 +00:00
|
|
|
|
2018-11-14 12:45:49 +00:00
|
|
|
t.Run("Can insert project without owner", func(t *testing.T) {
|
2018-11-13 08:27:42 +00:00
|
|
|
project := &satellite.Project{
|
2018-11-20 14:50:47 +00:00
|
|
|
OwnerID: nil,
|
|
|
|
Name: name,
|
|
|
|
Description: description,
|
|
|
|
TermsAccepted: 1,
|
2018-11-13 08:27:42 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 12:45:49 +00:00
|
|
|
createdProject, err := projects.Insert(ctx, project)
|
2018-11-13 08:27:42 +00:00
|
|
|
|
2018-11-14 12:45:49 +00:00
|
|
|
assert.NotNil(t, createdProject)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NoError(t, err)
|
2018-11-13 08:27:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Insert project successfully", func(t *testing.T) {
|
2018-11-14 12:45:49 +00:00
|
|
|
owner, err = users.Insert(ctx, &satellite.User{
|
2018-11-13 08:27:42 +00:00
|
|
|
FirstName: userName,
|
|
|
|
LastName: lastName,
|
|
|
|
Email: email,
|
|
|
|
PasswordHash: []byte(pass),
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2018-11-14 12:45:49 +00:00
|
|
|
assert.NotNil(t, owner)
|
2018-11-13 08:27:42 +00:00
|
|
|
|
|
|
|
project := &satellite.Project{
|
2018-11-14 12:45:49 +00:00
|
|
|
OwnerID: &owner.ID,
|
2018-11-13 08:27:42 +00:00
|
|
|
|
2018-11-20 14:50:47 +00:00
|
|
|
Name: name,
|
|
|
|
Description: description,
|
|
|
|
TermsAccepted: 1,
|
2018-11-13 08:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
createdProject, err := projects.Insert(ctx, project)
|
|
|
|
|
|
|
|
assert.NotNil(t, createdProject)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Get project success", func(t *testing.T) {
|
2018-11-14 12:45:49 +00:00
|
|
|
projectsByOwnerID, err := projects.GetByOwnerID(ctx, owner.ID)
|
2018-11-13 08:27:42 +00:00
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NoError(t, err)
|
2018-11-14 12:45:49 +00:00
|
|
|
assert.Equal(t, len(projectsByOwnerID), 1)
|
2018-11-13 08:27:42 +00:00
|
|
|
|
2018-11-14 12:45:49 +00:00
|
|
|
assert.Equal(t, projectsByOwnerID[0].OwnerID, &owner.ID)
|
|
|
|
assert.Equal(t, projectsByOwnerID[0].Name, name)
|
|
|
|
assert.Equal(t, projectsByOwnerID[0].Description, description)
|
2018-11-20 14:50:47 +00:00
|
|
|
assert.Equal(t, projectsByOwnerID[0].TermsAccepted, 1)
|
2018-11-13 08:27:42 +00:00
|
|
|
|
2018-11-14 12:45:49 +00:00
|
|
|
projectByID, err := projects.Get(ctx, projectsByOwnerID[0].ID)
|
2018-11-13 08:27:42 +00:00
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-11-14 12:45:49 +00:00
|
|
|
assert.Equal(t, projectByID.ID, projectsByOwnerID[0].ID)
|
|
|
|
assert.Equal(t, projectByID.OwnerID, &owner.ID)
|
2018-11-13 08:27:42 +00:00
|
|
|
assert.Equal(t, projectByID.Name, name)
|
|
|
|
assert.Equal(t, projectByID.Description, description)
|
2018-11-20 14:50:47 +00:00
|
|
|
assert.Equal(t, projectByID.TermsAccepted, 1)
|
2018-11-13 08:27:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Update project success", func(t *testing.T) {
|
2018-11-14 12:45:49 +00:00
|
|
|
oldProjects, err := projects.GetByOwnerID(ctx, owner.ID)
|
2018-11-13 08:27:42 +00:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2018-11-14 12:45:49 +00:00
|
|
|
assert.NotNil(t, oldProjects)
|
|
|
|
assert.Equal(t, len(oldProjects), 1)
|
2018-11-13 08:27:42 +00:00
|
|
|
|
2018-11-14 12:45:49 +00:00
|
|
|
// creating new project with updated values
|
2018-11-13 08:27:42 +00:00
|
|
|
newProject := &satellite.Project{
|
2018-11-20 14:50:47 +00:00
|
|
|
ID: oldProjects[0].ID,
|
|
|
|
OwnerID: &owner.ID,
|
|
|
|
Name: newName,
|
|
|
|
Description: newDescription,
|
|
|
|
TermsAccepted: 1,
|
2018-11-13 08:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = projects.Update(ctx, newProject)
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// fetching updated project from db
|
2018-11-14 12:45:49 +00:00
|
|
|
newProject, err = projects.Get(ctx, oldProjects[0].ID)
|
2018-11-13 08:27:42 +00:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-11-14 12:45:49 +00:00
|
|
|
assert.Equal(t, newProject.ID, oldProjects[0].ID)
|
|
|
|
assert.Equal(t, newProject.OwnerID, &owner.ID)
|
2018-11-13 08:27:42 +00:00
|
|
|
assert.Equal(t, newProject.Name, newName)
|
|
|
|
assert.Equal(t, newProject.Description, newDescription)
|
2018-11-20 14:50:47 +00:00
|
|
|
assert.Equal(t, newProject.TermsAccepted, 1)
|
2018-11-13 08:27:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Delete project success", func(t *testing.T) {
|
2018-11-14 12:45:49 +00:00
|
|
|
oldProjects, err := projects.GetByOwnerID(ctx, owner.ID)
|
2018-11-13 08:27:42 +00:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2018-11-14 12:45:49 +00:00
|
|
|
assert.NotNil(t, oldProjects)
|
2018-11-13 08:27:42 +00:00
|
|
|
|
2018-11-14 12:45:49 +00:00
|
|
|
err = projects.Delete(ctx, oldProjects[0].ID)
|
2018-11-13 08:27:42 +00:00
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-11-14 12:45:49 +00:00
|
|
|
_, err = projects.Get(ctx, oldProjects[0].ID)
|
2018-11-13 08:27:42 +00:00
|
|
|
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("GetAll success", func(t *testing.T) {
|
|
|
|
allProjects, err := projects.GetAll(ctx)
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NoError(t, err)
|
2018-11-14 12:45:49 +00:00
|
|
|
assert.Equal(t, len(allProjects), 1)
|
2018-11-13 08:27:42 +00:00
|
|
|
|
|
|
|
newProject := &satellite.Project{
|
2018-11-20 14:50:47 +00:00
|
|
|
OwnerID: &owner.ID,
|
|
|
|
Description: description,
|
|
|
|
Name: name,
|
|
|
|
TermsAccepted: 1,
|
2018-11-13 08:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = projects.Insert(ctx, newProject)
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
allProjects, err = projects.GetAll(ctx)
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NoError(t, err)
|
2018-11-14 12:45:49 +00:00
|
|
|
assert.Equal(t, len(allProjects), 2)
|
2018-11-13 08:27:42 +00:00
|
|
|
|
|
|
|
newProject2 := &satellite.Project{
|
2018-11-20 14:50:47 +00:00
|
|
|
OwnerID: &owner.ID,
|
|
|
|
Description: description,
|
|
|
|
Name: name,
|
|
|
|
TermsAccepted: 1,
|
2018-11-13 08:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = projects.Insert(ctx, newProject2)
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
allProjects, err = projects.GetAll(ctx)
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NoError(t, err)
|
2018-11-14 12:45:49 +00:00
|
|
|
assert.Equal(t, len(allProjects), 3)
|
2018-11-13 08:27:42 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProjectFromDbx(t *testing.T) {
|
|
|
|
t.Run("can't create dbo from nil dbx model", func(t *testing.T) {
|
2018-11-14 12:45:49 +00:00
|
|
|
project, err := projectFromDBX(nil)
|
2018-11-13 08:27:42 +00:00
|
|
|
|
2018-11-14 12:45:49 +00:00
|
|
|
assert.Nil(t, project)
|
2018-11-13 08:27:42 +00:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("can't create dbo from dbx model with invalid ID", func(t *testing.T) {
|
|
|
|
dbxProject := dbx.Project{
|
|
|
|
Id: []byte("qweqwe"),
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err := projectFromDBX(&dbxProject)
|
|
|
|
|
|
|
|
assert.Nil(t, project)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
2018-11-14 12:45:49 +00:00
|
|
|
t.Run("can't create dbo from dbx model with invalid OwnerID", func(t *testing.T) {
|
2018-11-13 08:27:42 +00:00
|
|
|
projectID, err := uuid.New()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
dbxProject := dbx.Project{
|
2018-11-14 12:45:49 +00:00
|
|
|
Id: projectID[:],
|
|
|
|
OwnerId: []byte("qweqwe"),
|
2018-11-13 08:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
project, err := projectFromDBX(&dbxProject)
|
|
|
|
|
|
|
|
assert.Nil(t, project)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|