2019-01-24 16:26:36 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-20 14:50:47 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-11-29 18:39:27 +00:00
|
|
|
|
2018-11-20 14:50:47 +00:00
|
|
|
"storj.io/storj/internal/testcontext"
|
2019-01-15 13:03:24 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2018-11-20 14:50:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestProjectMembersRepository(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")
|
2018-11-20 14:50:47 +00:00
|
|
|
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)
|
|
|
|
|
2018-12-28 12:07:35 +00:00
|
|
|
projMember3, err := projectMembers.Insert(ctx, createdUsers[3].ID, createdProjects[0].ID)
|
2018-11-20 14:50:47 +00:00
|
|
|
assert.NotNil(t, projMember3)
|
|
|
|
assert.NoError(t, err)
|
2018-12-28 12:07:35 +00:00
|
|
|
|
|
|
|
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)
|
2019-01-11 10:08:51 +00:00
|
|
|
|
|
|
|
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)
|
2018-11-20 14:50:47 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 14:28:58 +00:00
|
|
|
t.Run("Get paged", func(t *testing.T) {
|
2018-12-28 12:07:35 +00:00
|
|
|
// sql injection test. F.E '%SomeText%' = > ''%SomeText%' OR 'x' != '%'' will be true
|
2019-01-15 13:03:24 +00:00
|
|
|
members, err := projectMembers.GetByProjectID(ctx, createdProjects[0].ID, console.Pagination{Limit: 6, Offset: 0, Search: "son%' OR 'x' != '", Order: 2})
|
2018-12-28 12:07:35 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, members)
|
|
|
|
assert.Equal(t, 0, len(members))
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
members, err = projectMembers.GetByProjectID(ctx, createdProjects[0].ID, console.Pagination{Limit: 3, Offset: 0, Search: "", Order: 1})
|
2018-12-17 14:28:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, members)
|
2018-12-28 12:07:35 +00:00
|
|
|
assert.Equal(t, 3, len(members))
|
2018-12-17 14:28:58 +00:00
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
members, err = projectMembers.GetByProjectID(ctx, createdProjects[0].ID, console.Pagination{Limit: 2, Offset: 0, Search: "Liam", Order: 2})
|
2018-12-17 14:28:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, members)
|
|
|
|
assert.Equal(t, 2, len(members))
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
members, err = projectMembers.GetByProjectID(ctx, createdProjects[0].ID, console.Pagination{Limit: 2, Offset: 0, Search: "Liam", Order: 1})
|
2018-12-28 12:07:35 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, members)
|
|
|
|
assert.Equal(t, 2, len(members))
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
members, err = projectMembers.GetByProjectID(ctx, createdProjects[0].ID, console.Pagination{Limit: 6, Offset: 0, Search: "son", Order: 123})
|
2018-12-28 12:07:35 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, members)
|
|
|
|
assert.Equal(t, 5, len(members))
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
members, err = projectMembers.GetByProjectID(ctx, createdProjects[0].ID, console.Pagination{Limit: 6, Offset: 3, Search: "son", Order: 2})
|
2018-12-17 14:28:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, members)
|
2018-12-28 12:07:35 +00:00
|
|
|
assert.Equal(t, 2, len(members))
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
members, err = projectMembers.GetByProjectID(ctx, createdProjects[0].ID, console.Pagination{Limit: -123, Offset: -14, Search: "son", Order: 2})
|
2018-12-28 12:07:35 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, members)
|
|
|
|
assert.Equal(t, 0, len(members))
|
2018-12-17 14:28:58 +00:00
|
|
|
})
|
|
|
|
|
2018-11-20 14:50:47 +00:00
|
|
|
t.Run("Get member by memberID success", func(t *testing.T) {
|
|
|
|
originalMember1 := createdUsers[0]
|
2018-12-27 15:30:15 +00:00
|
|
|
selectedMembers1, err := projectMembers.GetByMemberID(ctx, originalMember1.ID)
|
2018-11-20 14:50:47 +00:00
|
|
|
|
2018-12-27 15:30:15 +00:00
|
|
|
assert.NotNil(t, selectedMembers1)
|
2018-11-20 14:50:47 +00:00
|
|
|
assert.NoError(t, err)
|
2018-12-27 15:30:15 +00:00
|
|
|
assert.Equal(t, originalMember1.ID, selectedMembers1[0].MemberID)
|
2018-11-20 14:50:47 +00:00
|
|
|
|
|
|
|
originalMember2 := createdUsers[1]
|
2018-12-27 15:30:15 +00:00
|
|
|
selectedMembers2, err := projectMembers.GetByMemberID(ctx, originalMember2.ID)
|
2018-11-20 14:50:47 +00:00
|
|
|
|
2018-12-27 15:30:15 +00:00
|
|
|
assert.NotNil(t, selectedMembers2)
|
2018-11-20 14:50:47 +00:00
|
|
|
assert.NoError(t, err)
|
2018-12-27 15:30:15 +00:00
|
|
|
assert.Equal(t, originalMember2.ID, selectedMembers2[0].MemberID)
|
2018-11-20 14:50:47 +00:00
|
|
|
})
|
2019-01-11 10:08:51 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
projMembers, err := projectMembers.GetByProjectID(ctx, createdProjects[0].ID, console.Pagination{
|
2019-01-11 10:08:51 +00:00
|
|
|
Order: 1,
|
|
|
|
Search: "",
|
|
|
|
Offset: 0,
|
|
|
|
Limit: 100,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, projectMembers)
|
|
|
|
assert.Equal(t, len(projMembers), 4)
|
|
|
|
})
|
2018-11-20 14:50:47 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
func prepareUsersAndProjects(ctx context.Context, t *testing.T, users console.Users, projects console.Projects) ([]*console.User, []*console.Project) {
|
|
|
|
usersList := []*console.User{{
|
2018-12-28 12:07:35 +00:00
|
|
|
Email: "2email2@ukr.net",
|
2018-11-20 14:50:47 +00:00
|
|
|
PasswordHash: []byte("some_readable_hash"),
|
2018-12-28 12:07:35 +00:00
|
|
|
LastName: "Liam",
|
|
|
|
FirstName: "Jameson",
|
2018-11-20 14:50:47 +00:00
|
|
|
}, {
|
2018-12-28 12:07:35 +00:00
|
|
|
Email: "1email1@ukr.net",
|
2018-11-20 14:50:47 +00:00
|
|
|
PasswordHash: []byte("some_readable_hash"),
|
2018-12-28 12:07:35 +00:00
|
|
|
LastName: "William",
|
|
|
|
FirstName: "Noahson",
|
2018-11-20 14:50:47 +00:00
|
|
|
}, {
|
|
|
|
Email: "email3@ukr.net",
|
|
|
|
PasswordHash: []byte("some_readable_hash"),
|
2018-12-28 12:07:35 +00:00
|
|
|
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",
|
2018-11-20 14:50:47 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
for i, user := range usersList {
|
|
|
|
usersList[i], err = users.Insert(ctx, user)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
projectList := []*console.Project{
|
2018-11-20 14:50:47 +00:00
|
|
|
{
|
2019-01-28 18:20:33 +00:00
|
|
|
Name: "projName1",
|
|
|
|
Description: "Test project 1",
|
2018-11-20 14:50:47 +00:00
|
|
|
},
|
|
|
|
{
|
2019-01-28 18:20:33 +00:00
|
|
|
Name: "projName2",
|
|
|
|
Description: "Test project 1",
|
2018-11-20 14:50:47 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, project := range projectList {
|
|
|
|
projectList[i], err = projects.Insert(ctx, project)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return usersList, projectList
|
|
|
|
}
|
2018-12-28 12:07:35 +00:00
|
|
|
|
|
|
|
func TestSanitizedOrderColumnName(t *testing.T) {
|
|
|
|
testCases := [...]struct {
|
|
|
|
orderNumber int8
|
|
|
|
orderColumn string
|
|
|
|
}{
|
|
|
|
0: {0, "u.first_name"},
|
|
|
|
1: {1, "u.first_name"},
|
|
|
|
2: {2, "u.email"},
|
|
|
|
3: {3, "u.created_at"},
|
|
|
|
4: {4, "u.first_name"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2019-01-15 13:03:24 +00:00
|
|
|
assert.Equal(t, tc.orderColumn, sanitizedOrderColumnName(console.ProjectMemberOrder(tc.orderNumber)))
|
2018-12-28 12:07:35 +00:00
|
|
|
}
|
|
|
|
}
|