@ -69,40 +69,41 @@ func TestAPISearchRepo(t *testing.T) {
name , requestURL string
expectedResults
} {
{ name : "RepositoriesMax50" , requestURL : "/api/v1/repos/search?limit=50" , expectedResults : expectedResults {
{ name : "RepositoriesMax50" , requestURL : "/api/v1/repos/search?limit=50&private=false" , expectedResults : expectedResults {
nil : { count : 21 } ,
user : { count : 21 } ,
user2 : { count : 21 } } ,
} ,
{ name : "RepositoriesMax10" , requestURL : "/api/v1/repos/search?limit=10" , expectedResults : expectedResults {
{ name : "RepositoriesMax10" , requestURL : "/api/v1/repos/search?limit=10&private=false" , expectedResults : expectedResults {
nil : { count : 10 } ,
user : { count : 10 } ,
user2 : { count : 10 } } ,
} ,
{ name : "RepositoriesDefaultMax10" , requestURL : "/api/v1/repos/search?default" , expectedResults : expectedResults {
{ name : "RepositoriesDefaultMax10" , requestURL : "/api/v1/repos/search?default&private=false" , expectedResults : expectedResults {
nil : { count : 10 } ,
user : { count : 10 } ,
user2 : { count : 10 } } ,
} ,
{ name : "RepositoriesByName" , requestURL : fmt . Sprintf ( "/api/v1/repos/search?q=%s" , "big_test_" ) , expectedResults : expectedResults {
{ name : "RepositoriesByName" , requestURL : fmt . Sprintf ( "/api/v1/repos/search?q=%s&private=false" , "big_test_" ) , expectedResults : expectedResults {
nil : { count : 7 , repoName : "big_test_" } ,
user : { count : 7 , repoName : "big_test_" } ,
user2 : { count : 7 , repoName : "big_test_" } } ,
} ,
{ name : "RepositoriesAccessibleAndRelatedToUser" , requestURL : fmt . Sprintf ( "/api/v1/repos/search?uid=%d" , user . ID ) , expectedResults : expectedResults {
nil : { count : 4 } ,
user : { count : 8 , includesPrivate : true } ,
user2 : { count : 4 } } ,
nil : { count : 5 } ,
user : { count : 9 , includesPrivate : true } ,
user2 : { count : 5 , includesPrivate : true } } ,
} ,
{ name : "RepositoriesAccessibleAndRelatedToUser2" , requestURL : fmt . Sprintf ( "/api/v1/repos/search?uid=%d" , user2 . ID ) , expectedResults : expectedResults {
nil : { count : 1 } ,
user : { count : 1 } ,
user2 : { count : 2 , includesPrivate : true } } ,
user : { count : 2 , includesPrivate : true } ,
user2 : { count : 2 , includesPrivate : true } ,
user4 : { count : 1 } } ,
} ,
{ name : "RepositoriesAccessibleAndRelatedToUser3" , requestURL : fmt . Sprintf ( "/api/v1/repos/search?uid=%d" , user3 . ID ) , expectedResults : expectedResults {
nil : { count : 1 } ,
user : { count : 1 } ,
user2 : { count : 1 } ,
user : { count : 4 , includesPrivate : true } ,
user2 : { count : 2 , includesPrivate : true } ,
user3 : { count : 4 , includesPrivate : true } } ,
} ,
{ name : "RepositoriesOwnedByOrganization" , requestURL : fmt . Sprintf ( "/api/v1/repos/search?uid=%d" , orgUser . ID ) , expectedResults : expectedResults {
@ -112,12 +113,12 @@ func TestAPISearchRepo(t *testing.T) {
} ,
{ name : "RepositoriesAccessibleAndRelatedToUser4" , requestURL : fmt . Sprintf ( "/api/v1/repos/search?uid=%d" , user4 . ID ) , expectedResults : expectedResults {
nil : { count : 3 } ,
user : { count : 3 } ,
user4 : { count : 6 , includesPrivate : true } } } ,
user : { count : 4 , includesPrivate : true } ,
user4 : { count : 7 , includesPrivate : true } } } ,
{ name : "RepositoriesAccessibleAndRelatedToUser4/SearchModeSource" , requestURL : fmt . Sprintf ( "/api/v1/repos/search?uid=%d&mode=%s" , user4 . ID , "source" ) , expectedResults : expectedResults {
nil : { count : 0 } ,
user : { count : 0 } ,
user4 : { count : 0 , includesPrivate : true } } } ,
user : { count : 1 , includesPrivate : true } ,
user4 : { count : 1 , includesPrivate : true } } } ,
{ name : "RepositoriesAccessibleAndRelatedToUser4/SearchModeFork" , requestURL : fmt . Sprintf ( "/api/v1/repos/search?uid=%d&mode=%s" , user4 . ID , "fork" ) , expectedResults : expectedResults {
nil : { count : 1 } ,
user : { count : 1 } ,
@ -136,8 +137,8 @@ func TestAPISearchRepo(t *testing.T) {
user4 : { count : 2 , includesPrivate : true } } } ,
{ name : "RepositoriesAccessibleAndRelatedToUser4/SearchModeCollaborative" , requestURL : fmt . Sprintf ( "/api/v1/repos/search?uid=%d&mode=%s" , user4 . ID , "collaborative" ) , expectedResults : expectedResults {
nil : { count : 0 } ,
user : { count : 0 } ,
user4 : { count : 0 , includesPrivate : true } } } ,
user : { count : 1 , includesPrivate : true } ,
user4 : { count : 1 , includesPrivate : true } } } ,
}
for _ , testCase := range testCases {
@ -164,14 +165,19 @@ func TestAPISearchRepo(t *testing.T) {
var body api . SearchResults
DecodeJSON ( t , response , & body )
assert . Len ( t , body . Data , expected . count )
repoNames := make ( [ ] string , 0 , len ( body . Data ) )
for _ , repo := range body . Data {
repoNames = append ( repoNames , fmt . Sprintf ( "%d:%s:%t" , repo . ID , repo . FullName , repo . Private ) )
}
assert . Len ( t , repoNames , expected . count )
for _ , repo := range body . Data {
r := getRepo ( t , repo . ID )
hasAccess , err := models . HasAccess ( userID , r )
assert . NoError ( t , err )
assert . True ( t , hasAccess )
assert . NoError ( t , err , "Error when checking if User: %d has access to %s: %v" , userID , repo . FullName , err )
assert . True ( t , hasAccess , "User: %d does not have access to %s" , userID , repo . FullName )
assert . NotEmpty ( t , repo . Name )
assert . Equal ( t , repo . Name , r . Name )
if len ( expected . repoName ) > 0 {
assert . Contains ( t , repo . Name , expected . repoName )
@ -182,7 +188,7 @@ func TestAPISearchRepo(t *testing.T) {
}
if ! expected . includesPrivate {
assert . False ( t , repo . Private )
assert . False ( t , repo . Private , "User: %d not expecting private repository: %s" , userID , repo . FullName )
}
}
} )