2019-01-31 13:01:13 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package console_test
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
2019-06-28 14:04:50 +01:00
"github.com/stretchr/testify/require"
2019-01-31 13:01:13 +00:00
"storj.io/storj/internal/testcontext"
2019-06-26 11:38:51 +01:00
"storj.io/storj/internal/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 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 ) {
2019-06-26 11:38:51 +01:00
missingUserID := testrand . UUID ( )
2019-01-31 13:01:13 +00:00
2019-06-26 11:38:51 +01:00
projMember , err := projectMembers . Insert ( ctx , missingUserID , createdProjects [ 0 ] . ID )
2019-01-31 13:01:13 +00:00
assert . Nil ( t , projMember )
assert . Error ( t , err )
} )
t . Run ( "Can't insert projectMember without projectID" , func ( t * testing . T ) {
2019-06-26 11:38:51 +01:00
missingProjectID := testrand . UUID ( )
2019-01-31 13:01:13 +00:00
2019-06-26 11:38:51 +01:00
projMember , err := projectMembers . Insert ( ctx , createdUsers [ 0 ] . ID , missingProjectID )
2019-01-31 13:01:13 +00:00
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
2019-08-12 11:22:32 +01:00
members , err := projectMembers . GetPagedByProjectID ( ctx , createdProjects [ 0 ] . ID , console . ProjectMembersCursor { Limit : 6 , Search : "son%' OR 'x' != '" , Order : 2 , Page : 1 } )
2019-01-31 13:01:13 +00:00
assert . NoError ( t , err )
2019-08-12 11:22:32 +01:00
assert . NotNil ( t , members )
assert . Equal ( t , uint64 ( 0 ) , members . TotalCount )
assert . Equal ( t , uint ( 0 ) , members . CurrentPage )
assert . Equal ( t , uint ( 0 ) , members . PageCount )
assert . Equal ( t , 0 , len ( members . ProjectMembers ) )
2019-01-31 13:01:13 +00:00
2019-08-12 11:22:32 +01:00
members , err = projectMembers . GetPagedByProjectID ( ctx , createdProjects [ 0 ] . ID , console . ProjectMembersCursor { Limit : 3 , Search : "" , Order : 1 , Page : 1 } )
2019-01-31 13:01:13 +00:00
assert . NoError ( t , err )
assert . NotNil ( t , members )
2019-08-12 11:22:32 +01:00
assert . Equal ( t , uint64 ( 5 ) , members . TotalCount )
assert . Equal ( t , uint ( 1 ) , members . CurrentPage )
assert . Equal ( t , uint ( 2 ) , members . PageCount )
assert . Equal ( t , 3 , len ( members . ProjectMembers ) )
2019-01-31 13:01:13 +00:00
2019-08-12 11:22:32 +01:00
members , err = projectMembers . GetPagedByProjectID ( ctx , createdProjects [ 0 ] . ID , console . ProjectMembersCursor { Limit : 2 , Search : "iam" , Order : 2 , Page : 1 } ) // TODO: fix case sensitity issues and change back to "Liam"
2019-01-31 13:01:13 +00:00
assert . NoError ( t , err )
assert . NotNil ( t , members )
2019-08-12 11:22:32 +01:00
assert . Equal ( t , uint64 ( 2 ) , members . TotalCount )
assert . Equal ( t , 2 , len ( members . ProjectMembers ) )
2019-01-31 13:01:13 +00:00
2019-08-12 11:22:32 +01:00
members , err = projectMembers . GetPagedByProjectID ( ctx , createdProjects [ 0 ] . ID , console . ProjectMembersCursor { Limit : 2 , Search : "iam" , Order : 1 , Page : 1 } ) // TODO: fix case sensitity issues and change back to "Liam"
2019-01-31 13:01:13 +00:00
assert . NoError ( t , err )
assert . NotNil ( t , members )
2019-08-12 11:22:32 +01:00
assert . Equal ( t , uint64 ( 2 ) , members . TotalCount )
assert . Equal ( t , 2 , len ( members . ProjectMembers ) )
2019-01-31 13:01:13 +00:00
2019-08-12 11:22:32 +01:00
members , err = projectMembers . GetPagedByProjectID ( ctx , createdProjects [ 0 ] . ID , console . ProjectMembersCursor { Limit : 6 , Search : "son" , Order : 123 , Page : 1 } )
2019-01-31 13:01:13 +00:00
assert . NoError ( t , err )
assert . NotNil ( t , members )
2019-08-12 11:22:32 +01:00
assert . Equal ( t , uint64 ( 5 ) , members . TotalCount )
assert . Equal ( t , 5 , len ( members . ProjectMembers ) )
2019-01-31 13:01:13 +00:00
2019-08-12 11:22:32 +01:00
members , err = projectMembers . GetPagedByProjectID ( ctx , createdProjects [ 0 ] . ID , console . ProjectMembersCursor { Limit : 6 , Search : "son" , Order : 2 , Page : 1 } )
2019-01-31 13:01:13 +00:00
assert . NoError ( t , err )
assert . NotNil ( t , members )
2019-08-12 11:22:32 +01:00
assert . Equal ( t , uint64 ( 5 ) , members . TotalCount )
assert . Equal ( t , 5 , len ( members . ProjectMembers ) )
2019-01-31 13:01:13 +00:00
} )
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 )
2019-08-12 11:22:32 +01:00
projMembers , err := projectMembers . GetPagedByProjectID ( ctx , createdProjects [ 0 ] . ID , console . ProjectMembersCursor {
2019-01-31 13:01:13 +00:00
Order : 1 ,
Search : "" ,
Limit : 100 ,
2019-08-12 11:22:32 +01:00
Page : 1 ,
2019-01-31 13:01:13 +00:00
} )
assert . NoError ( t , err )
assert . NotNil ( t , projectMembers )
2019-08-12 11:22:32 +01:00
assert . Equal ( t , len ( projMembers . ProjectMembers ) , 4 )
2019-01-31 13:01:13 +00:00
} )
} )
}
func prepareUsersAndProjects ( ctx context . Context , t * testing . T , users console . Users , projects console . Projects ) ( [ ] * console . User , [ ] * console . Project ) {
usersList := [ ] * console . User { {
2019-06-18 01:28:40 +01:00
Email : "2email2@mail.test" ,
2019-01-31 13:01:13 +00:00
PasswordHash : [ ] byte ( "some_readable_hash" ) ,
2019-03-27 12:33:32 +00:00
ShortName : "Liam" ,
FullName : "Liam Jameson" ,
2019-01-31 13:01:13 +00:00
} , {
2019-06-18 01:28:40 +01:00
Email : "1email1@mail.test" ,
2019-01-31 13:01:13 +00:00
PasswordHash : [ ] byte ( "some_readable_hash" ) ,
2019-03-27 12:33:32 +00:00
ShortName : "William" ,
FullName : "Noahson William" ,
2019-01-31 13:01:13 +00:00
} , {
2019-06-18 01:28:40 +01:00
Email : "email3@mail.test" ,
2019-01-31 13:01:13 +00:00
PasswordHash : [ ] byte ( "some_readable_hash" ) ,
2019-03-27 12:33:32 +00:00
ShortName : "Mason" ,
FullName : "Mason Elijahson" ,
2019-01-31 13:01:13 +00:00
} , {
2019-06-18 01:28:40 +01:00
Email : "email4@mail.test" ,
2019-01-31 13:01:13 +00:00
PasswordHash : [ ] byte ( "some_readable_hash" ) ,
2019-03-27 12:33:32 +00:00
ShortName : "Oliver" ,
FullName : "Oliver Jacobson" ,
2019-01-31 13:01:13 +00:00
} , {
2019-06-18 01:28:40 +01:00
Email : "email5@mail.test" ,
2019-01-31 13:01:13 +00:00
PasswordHash : [ ] byte ( "some_readable_hash" ) ,
2019-03-27 12:33:32 +00:00
ShortName : "Lucas" ,
FullName : "Michaelson Lucas" ,
2019-01-31 13:01:13 +00:00
} , {
2019-06-18 01:28:40 +01:00
Email : "email6@mail.test" ,
2019-01-31 13:01:13 +00:00
PasswordHash : [ ] byte ( "some_readable_hash" ) ,
2019-03-27 12:33:32 +00:00
ShortName : "Alexander" ,
FullName : "Alexander Ethanson" ,
2019-01-31 13:01:13 +00:00
} ,
}
var err error
for i , user := range usersList {
usersList [ i ] , err = users . Insert ( ctx , user )
2019-06-28 14:04:50 +01:00
require . NoError ( t , err )
2019-01-31 13:01:13 +00:00
}
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 )
2019-06-28 14:04:50 +01:00
require . NoError ( t , err )
2019-01-31 13:01:13 +00:00
}
return usersList , projectList
}