2022-03-29 09:29:02 +03:00
// Copyright 2017 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2022-03-29 09:29:02 +03:00
2022-06-15 10:02:00 +03:00
package organization_test
2022-03-29 09:29:02 +03:00
import (
2024-11-12 06:44:24 +03:00
"slices"
2024-11-11 03:38:30 +03:00
"sort"
2022-03-29 09:29:02 +03:00
"testing"
"code.gitea.io/gitea/models/db"
2022-06-15 10:02:00 +03:00
"code.gitea.io/gitea/models/organization"
2022-03-29 09:29:02 +03:00
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/structs"
"github.com/stretchr/testify/assert"
)
func TestUser_IsOwnedBy ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
for _ , testCase := range [ ] struct {
OrgID int64
UserID int64
ExpectedOwner bool
} {
{ 3 , 2 , true } ,
{ 3 , 1 , false } ,
{ 3 , 3 , false } ,
{ 3 , 4 , false } ,
{ 2 , 2 , false } , // user2 is not an organization
{ 2 , 3 , false } ,
} {
2022-08-16 05:22:25 +03:00
org := unittest . AssertExistsAndLoadBean ( t , & organization . Organization { ID : testCase . OrgID } )
2023-10-03 13:30:41 +03:00
isOwner , err := org . IsOwnedBy ( db . DefaultContext , testCase . UserID )
2022-03-29 09:29:02 +03:00
assert . NoError ( t , err )
assert . Equal ( t , testCase . ExpectedOwner , isOwner )
}
}
func TestUser_IsOrgMember ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
for _ , testCase := range [ ] struct {
OrgID int64
UserID int64
ExpectedMember bool
} {
{ 3 , 2 , true } ,
{ 3 , 4 , true } ,
{ 3 , 1 , false } ,
{ 3 , 3 , false } ,
{ 2 , 2 , false } , // user2 is not an organization
{ 2 , 3 , false } ,
} {
2022-08-16 05:22:25 +03:00
org := unittest . AssertExistsAndLoadBean ( t , & organization . Organization { ID : testCase . OrgID } )
2023-10-03 13:30:41 +03:00
isMember , err := org . IsOrgMember ( db . DefaultContext , testCase . UserID )
2022-03-29 09:29:02 +03:00
assert . NoError ( t , err )
assert . Equal ( t , testCase . ExpectedMember , isMember )
}
}
func TestUser_GetTeam ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-08-16 05:22:25 +03:00
org := unittest . AssertExistsAndLoadBean ( t , & organization . Organization { ID : 3 } )
2023-02-08 09:44:42 +03:00
team , err := org . GetTeam ( db . DefaultContext , "team1" )
2022-03-29 09:29:02 +03:00
assert . NoError ( t , err )
assert . Equal ( t , org . ID , team . OrgID )
assert . Equal ( t , "team1" , team . LowerName )
2023-02-08 09:44:42 +03:00
_ , err = org . GetTeam ( db . DefaultContext , "does not exist" )
2022-06-15 10:02:00 +03:00
assert . True ( t , organization . IsErrTeamNotExist ( err ) )
2022-03-29 09:29:02 +03:00
2022-08-16 05:22:25 +03:00
nonOrg := unittest . AssertExistsAndLoadBean ( t , & organization . Organization { ID : 2 } )
2023-02-08 09:44:42 +03:00
_ , err = nonOrg . GetTeam ( db . DefaultContext , "team" )
2022-06-15 10:02:00 +03:00
assert . True ( t , organization . IsErrTeamNotExist ( err ) )
2022-03-29 09:29:02 +03:00
}
func TestUser_GetOwnerTeam ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-08-16 05:22:25 +03:00
org := unittest . AssertExistsAndLoadBean ( t , & organization . Organization { ID : 3 } )
2023-02-08 09:44:42 +03:00
team , err := org . GetOwnerTeam ( db . DefaultContext )
2022-03-29 09:29:02 +03:00
assert . NoError ( t , err )
assert . Equal ( t , org . ID , team . OrgID )
2022-08-16 05:22:25 +03:00
nonOrg := unittest . AssertExistsAndLoadBean ( t , & organization . Organization { ID : 2 } )
2023-02-08 09:44:42 +03:00
_ , err = nonOrg . GetOwnerTeam ( db . DefaultContext )
2022-06-15 10:02:00 +03:00
assert . True ( t , organization . IsErrTeamNotExist ( err ) )
2022-03-29 09:29:02 +03:00
}
func TestUser_GetTeams ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-08-16 05:22:25 +03:00
org := unittest . AssertExistsAndLoadBean ( t , & organization . Organization { ID : 3 } )
2023-10-03 13:30:41 +03:00
teams , err := org . LoadTeams ( db . DefaultContext )
2022-03-29 09:29:02 +03:00
assert . NoError ( t , err )
2023-01-30 13:12:45 +03:00
if assert . Len ( t , teams , 5 ) {
2022-03-29 09:29:02 +03:00
assert . Equal ( t , int64 ( 1 ) , teams [ 0 ] . ID )
assert . Equal ( t , int64 ( 2 ) , teams [ 1 ] . ID )
assert . Equal ( t , int64 ( 12 ) , teams [ 2 ] . ID )
2023-01-30 13:12:45 +03:00
assert . Equal ( t , int64 ( 14 ) , teams [ 3 ] . ID )
assert . Equal ( t , int64 ( 7 ) , teams [ 4 ] . ID )
2022-03-29 09:29:02 +03:00
}
}
func TestUser_GetMembers ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-08-16 05:22:25 +03:00
org := unittest . AssertExistsAndLoadBean ( t , & organization . Organization { ID : 3 } )
2024-11-11 03:38:30 +03:00
members , _ , err := org . GetMembers ( db . DefaultContext , & user_model . User { IsAdmin : true } )
2022-03-29 09:29:02 +03:00
assert . NoError ( t , err )
if assert . Len ( t , members , 3 ) {
assert . Equal ( t , int64 ( 2 ) , members [ 0 ] . ID )
assert . Equal ( t , int64 ( 28 ) , members [ 1 ] . ID )
assert . Equal ( t , int64 ( 4 ) , members [ 2 ] . ID )
}
}
func TestGetOrgByName ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2023-09-14 05:59:53 +03:00
org , err := organization . GetOrgByName ( db . DefaultContext , "org3" )
2022-03-29 09:29:02 +03:00
assert . NoError ( t , err )
assert . EqualValues ( t , 3 , org . ID )
2023-09-14 05:59:53 +03:00
assert . Equal ( t , "org3" , org . Name )
2022-03-29 09:29:02 +03:00
2023-02-08 09:44:42 +03:00
_ , err = organization . GetOrgByName ( db . DefaultContext , "user2" ) // user2 is an individual
2022-06-15 10:02:00 +03:00
assert . True ( t , organization . IsErrOrgNotExist ( err ) )
2022-03-29 09:29:02 +03:00
2023-02-08 09:44:42 +03:00
_ , err = organization . GetOrgByName ( db . DefaultContext , "" ) // corner case
2022-06-15 10:02:00 +03:00
assert . True ( t , organization . IsErrOrgNotExist ( err ) )
2022-03-29 09:29:02 +03:00
}
func TestIsOrganizationOwner ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
test := func ( orgID , userID int64 , expected bool ) {
2022-06-15 10:02:00 +03:00
isOwner , err := organization . IsOrganizationOwner ( db . DefaultContext , orgID , userID )
2022-03-29 09:29:02 +03:00
assert . NoError ( t , err )
assert . EqualValues ( t , expected , isOwner )
}
test ( 3 , 2 , true )
test ( 3 , 3 , false )
test ( 6 , 5 , true )
test ( 6 , 4 , false )
test ( unittest . NonexistentID , unittest . NonexistentID , false )
}
func TestIsOrganizationMember ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
test := func ( orgID , userID int64 , expected bool ) {
2022-06-15 10:02:00 +03:00
isMember , err := organization . IsOrganizationMember ( db . DefaultContext , orgID , userID )
2022-03-29 09:29:02 +03:00
assert . NoError ( t , err )
assert . EqualValues ( t , expected , isMember )
}
test ( 3 , 2 , true )
test ( 3 , 3 , false )
test ( 3 , 4 , true )
test ( 6 , 5 , true )
test ( 6 , 4 , false )
test ( unittest . NonexistentID , unittest . NonexistentID , false )
}
func TestIsPublicMembership ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
test := func ( orgID , userID int64 , expected bool ) {
2023-10-03 13:30:41 +03:00
isMember , err := organization . IsPublicMembership ( db . DefaultContext , orgID , userID )
2022-03-29 09:29:02 +03:00
assert . NoError ( t , err )
assert . EqualValues ( t , expected , isMember )
}
test ( 3 , 2 , true )
test ( 3 , 3 , false )
test ( 3 , 4 , false )
test ( 6 , 5 , true )
test ( 6 , 4 , false )
test ( unittest . NonexistentID , unittest . NonexistentID , false )
}
2024-11-12 06:44:24 +03:00
func TestRestrictedUserOrgMembers ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
restrictedUser := unittest . AssertExistsAndLoadBean ( t , & user_model . User {
ID : 29 ,
IsRestricted : true ,
} )
if ! assert . True ( t , restrictedUser . IsRestricted ) {
return // ensure fixtures return restricted user
}
testCases := [ ] struct {
name string
opts * organization . FindOrgMembersOpts
expectedUIDs [ ] int64
} {
{
name : "restricted user sees public members and teammates" ,
opts : & organization . FindOrgMembersOpts {
OrgID : 17 , // org17 where user29 is in team9
Doer : restrictedUser ,
IsDoerMember : true ,
} ,
expectedUIDs : [ ] int64 { 2 , 15 , 20 , 29 } , // Public members (2) + teammates in team9 (15, 20, 29)
} ,
{
name : "restricted user sees only public members when not member" ,
opts : & organization . FindOrgMembersOpts {
OrgID : 3 , // org3 where user29 is not a member
Doer : restrictedUser ,
} ,
expectedUIDs : [ ] int64 { 2 , 28 } , // Only public members
} ,
{
name : "non logged in only shows public members" ,
opts : & organization . FindOrgMembersOpts {
OrgID : 3 ,
} ,
expectedUIDs : [ ] int64 { 2 , 28 } , // Only public members
} ,
{
name : "non restricted user sees all members" ,
opts : & organization . FindOrgMembersOpts {
OrgID : 17 ,
Doer : unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 15 } ) ,
IsDoerMember : true ,
} ,
expectedUIDs : [ ] int64 { 2 , 15 , 18 , 20 , 29 } , // All members
} ,
}
for _ , tc := range testCases {
t . Run ( tc . name , func ( t * testing . T ) {
count , err := organization . CountOrgMembers ( db . DefaultContext , tc . opts )
assert . NoError ( t , err )
assert . EqualValues ( t , len ( tc . expectedUIDs ) , count )
members , err := organization . GetOrgUsersByOrgID ( db . DefaultContext , tc . opts )
assert . NoError ( t , err )
memberUIDs := make ( [ ] int64 , 0 , len ( members ) )
for _ , member := range members {
memberUIDs = append ( memberUIDs , member . UID )
}
slices . Sort ( memberUIDs )
assert . EqualValues ( t , tc . expectedUIDs , memberUIDs )
} )
}
}
2022-03-29 09:29:02 +03:00
func TestGetOrgUsersByOrgID ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2024-11-11 03:38:30 +03:00
opts := & organization . FindOrgMembersOpts {
Doer : & user_model . User { IsAdmin : true } ,
OrgID : 3 ,
}
assert . False ( t , opts . PublicOnly ( ) )
orgUsers , err := organization . GetOrgUsersByOrgID ( db . DefaultContext , opts )
assert . NoError ( t , err )
sort . Slice ( orgUsers , func ( i , j int ) bool {
return orgUsers [ i ] . ID < orgUsers [ j ] . ID
2022-03-29 09:29:02 +03:00
} )
2024-11-11 03:38:30 +03:00
assert . EqualValues ( t , [ ] * organization . OrgUser { {
ID : 1 ,
OrgID : 3 ,
UID : 2 ,
IsPublic : true ,
} , {
ID : 2 ,
OrgID : 3 ,
UID : 4 ,
IsPublic : false ,
} , {
ID : 9 ,
OrgID : 3 ,
UID : 28 ,
IsPublic : true ,
} } , orgUsers )
opts = & organization . FindOrgMembersOpts { OrgID : 3 }
assert . True ( t , opts . PublicOnly ( ) )
orgUsers , err = organization . GetOrgUsersByOrgID ( db . DefaultContext , opts )
2022-03-29 09:29:02 +03:00
assert . NoError ( t , err )
2024-11-11 03:38:30 +03:00
assert . Len ( t , orgUsers , 2 )
2022-03-29 09:29:02 +03:00
2022-06-15 10:02:00 +03:00
orgUsers , err = organization . GetOrgUsersByOrgID ( db . DefaultContext , & organization . FindOrgMembersOpts {
2022-03-29 09:29:02 +03:00
ListOptions : db . ListOptions { } ,
OrgID : unittest . NonexistentID ,
} )
assert . NoError ( t , err )
assert . Len ( t , orgUsers , 0 )
}
func TestChangeOrgUserStatus ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
testSuccess := func ( orgID , userID int64 , public bool ) {
2023-10-03 13:30:41 +03:00
assert . NoError ( t , organization . ChangeOrgUserStatus ( db . DefaultContext , orgID , userID , public ) )
2022-08-16 05:22:25 +03:00
orgUser := unittest . AssertExistsAndLoadBean ( t , & organization . OrgUser { OrgID : orgID , UID : userID } )
2022-03-29 09:29:02 +03:00
assert . Equal ( t , public , orgUser . IsPublic )
}
testSuccess ( 3 , 2 , false )
testSuccess ( 3 , 2 , false )
testSuccess ( 3 , 4 , true )
2023-10-03 13:30:41 +03:00
assert . NoError ( t , organization . ChangeOrgUserStatus ( db . DefaultContext , unittest . NonexistentID , unittest . NonexistentID , true ) )
2022-03-29 09:29:02 +03:00
}
func TestUser_GetUserTeamIDs ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-08-16 05:22:25 +03:00
org := unittest . AssertExistsAndLoadBean ( t , & organization . Organization { ID : 3 } )
2022-03-29 09:29:02 +03:00
testSuccess := func ( userID int64 , expected [ ] int64 ) {
2023-10-03 13:30:41 +03:00
teamIDs , err := org . GetUserTeamIDs ( db . DefaultContext , userID )
2022-03-29 09:29:02 +03:00
assert . NoError ( t , err )
assert . Equal ( t , expected , teamIDs )
}
2023-01-30 13:12:45 +03:00
testSuccess ( 2 , [ ] int64 { 1 , 2 , 14 } )
2022-03-29 09:29:02 +03:00
testSuccess ( 4 , [ ] int64 { 2 } )
testSuccess ( unittest . NonexistentID , [ ] int64 { } )
}
func TestAccessibleReposEnv_CountRepos ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-08-16 05:22:25 +03:00
org := unittest . AssertExistsAndLoadBean ( t , & organization . Organization { ID : 3 } )
2022-03-29 09:29:02 +03:00
testSuccess := func ( userID , expectedCount int64 ) {
2022-06-15 10:02:00 +03:00
env , err := organization . AccessibleReposEnv ( db . DefaultContext , org , userID )
2022-03-29 09:29:02 +03:00
assert . NoError ( t , err )
count , err := env . CountRepos ( )
assert . NoError ( t , err )
assert . EqualValues ( t , expectedCount , count )
}
testSuccess ( 2 , 3 )
testSuccess ( 4 , 2 )
}
func TestAccessibleReposEnv_RepoIDs ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-08-16 05:22:25 +03:00
org := unittest . AssertExistsAndLoadBean ( t , & organization . Organization { ID : 3 } )
2024-04-29 11:47:56 +03:00
testSuccess := func ( userID int64 , expectedRepoIDs [ ] int64 ) {
2022-06-15 10:02:00 +03:00
env , err := organization . AccessibleReposEnv ( db . DefaultContext , org , userID )
2022-03-29 09:29:02 +03:00
assert . NoError ( t , err )
repoIDs , err := env . RepoIDs ( 1 , 100 )
assert . NoError ( t , err )
assert . Equal ( t , expectedRepoIDs , repoIDs )
}
2024-04-29 11:47:56 +03:00
testSuccess ( 2 , [ ] int64 { 3 , 5 , 32 } )
testSuccess ( 4 , [ ] int64 { 3 , 32 } )
2022-03-29 09:29:02 +03:00
}
func TestAccessibleReposEnv_Repos ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-08-16 05:22:25 +03:00
org := unittest . AssertExistsAndLoadBean ( t , & organization . Organization { ID : 3 } )
2022-03-29 09:29:02 +03:00
testSuccess := func ( userID int64 , expectedRepoIDs [ ] int64 ) {
2022-06-15 10:02:00 +03:00
env , err := organization . AccessibleReposEnv ( db . DefaultContext , org , userID )
2022-03-29 09:29:02 +03:00
assert . NoError ( t , err )
repos , err := env . Repos ( 1 , 100 )
assert . NoError ( t , err )
2023-06-05 10:25:47 +03:00
expectedRepos := make ( repo_model . RepositoryList , len ( expectedRepoIDs ) )
2022-03-29 09:29:02 +03:00
for i , repoID := range expectedRepoIDs {
expectedRepos [ i ] = unittest . AssertExistsAndLoadBean ( t ,
2022-08-16 05:22:25 +03:00
& repo_model . Repository { ID : repoID } )
2022-03-29 09:29:02 +03:00
}
assert . Equal ( t , expectedRepos , repos )
}
testSuccess ( 2 , [ ] int64 { 3 , 5 , 32 } )
testSuccess ( 4 , [ ] int64 { 3 , 32 } )
}
func TestAccessibleReposEnv_MirrorRepos ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-08-16 05:22:25 +03:00
org := unittest . AssertExistsAndLoadBean ( t , & organization . Organization { ID : 3 } )
2022-03-29 09:29:02 +03:00
testSuccess := func ( userID int64 , expectedRepoIDs [ ] int64 ) {
2022-06-15 10:02:00 +03:00
env , err := organization . AccessibleReposEnv ( db . DefaultContext , org , userID )
2022-03-29 09:29:02 +03:00
assert . NoError ( t , err )
repos , err := env . MirrorRepos ( )
assert . NoError ( t , err )
2023-06-05 10:25:47 +03:00
expectedRepos := make ( repo_model . RepositoryList , len ( expectedRepoIDs ) )
2022-03-29 09:29:02 +03:00
for i , repoID := range expectedRepoIDs {
expectedRepos [ i ] = unittest . AssertExistsAndLoadBean ( t ,
2022-08-16 05:22:25 +03:00
& repo_model . Repository { ID : repoID } )
2022-03-29 09:29:02 +03:00
}
assert . Equal ( t , expectedRepos , repos )
}
testSuccess ( 2 , [ ] int64 { 5 } )
testSuccess ( 4 , [ ] int64 { } )
}
func TestHasOrgVisibleTypePublic ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-08-16 05:22:25 +03:00
owner := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 2 } )
2023-09-14 05:59:53 +03:00
org3 := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 3 } )
2022-03-29 09:29:02 +03:00
const newOrgName = "test-org-public"
2022-06-15 10:02:00 +03:00
org := & organization . Organization {
2022-03-29 09:29:02 +03:00
Name : newOrgName ,
Visibility : structs . VisibleTypePublic ,
}
unittest . AssertNotExistsBean ( t , & user_model . User { Name : org . Name , Type : user_model . UserTypeOrganization } )
2023-10-03 13:30:41 +03:00
assert . NoError ( t , organization . CreateOrganization ( db . DefaultContext , org , owner ) )
2022-03-29 09:29:02 +03:00
org = unittest . AssertExistsAndLoadBean ( t ,
2022-08-16 05:22:25 +03:00
& organization . Organization { Name : org . Name , Type : user_model . UserTypeOrganization } )
2022-06-15 10:02:00 +03:00
test1 := organization . HasOrgOrUserVisible ( db . DefaultContext , org . AsUser ( ) , owner )
2023-09-14 05:59:53 +03:00
test2 := organization . HasOrgOrUserVisible ( db . DefaultContext , org . AsUser ( ) , org3 )
2022-06-15 10:02:00 +03:00
test3 := organization . HasOrgOrUserVisible ( db . DefaultContext , org . AsUser ( ) , nil )
2022-03-29 09:29:02 +03:00
assert . True ( t , test1 ) // owner of org
assert . True ( t , test2 ) // user not a part of org
assert . True ( t , test3 ) // logged out user
}
func TestHasOrgVisibleTypeLimited ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-08-16 05:22:25 +03:00
owner := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 2 } )
2023-09-14 05:59:53 +03:00
org3 := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 3 } )
2022-03-29 09:29:02 +03:00
const newOrgName = "test-org-limited"
2022-06-15 10:02:00 +03:00
org := & organization . Organization {
2022-03-29 09:29:02 +03:00
Name : newOrgName ,
Visibility : structs . VisibleTypeLimited ,
}
unittest . AssertNotExistsBean ( t , & user_model . User { Name : org . Name , Type : user_model . UserTypeOrganization } )
2023-10-03 13:30:41 +03:00
assert . NoError ( t , organization . CreateOrganization ( db . DefaultContext , org , owner ) )
2022-03-29 09:29:02 +03:00
org = unittest . AssertExistsAndLoadBean ( t ,
2022-08-16 05:22:25 +03:00
& organization . Organization { Name : org . Name , Type : user_model . UserTypeOrganization } )
2022-06-15 10:02:00 +03:00
test1 := organization . HasOrgOrUserVisible ( db . DefaultContext , org . AsUser ( ) , owner )
2023-09-14 05:59:53 +03:00
test2 := organization . HasOrgOrUserVisible ( db . DefaultContext , org . AsUser ( ) , org3 )
2022-06-15 10:02:00 +03:00
test3 := organization . HasOrgOrUserVisible ( db . DefaultContext , org . AsUser ( ) , nil )
2022-03-29 09:29:02 +03:00
assert . True ( t , test1 ) // owner of org
assert . True ( t , test2 ) // user not a part of org
assert . False ( t , test3 ) // logged out user
}
func TestHasOrgVisibleTypePrivate ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-08-16 05:22:25 +03:00
owner := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 2 } )
2023-09-14 05:59:53 +03:00
org3 := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 3 } )
2022-03-29 09:29:02 +03:00
const newOrgName = "test-org-private"
2022-06-15 10:02:00 +03:00
org := & organization . Organization {
2022-03-29 09:29:02 +03:00
Name : newOrgName ,
Visibility : structs . VisibleTypePrivate ,
}
unittest . AssertNotExistsBean ( t , & user_model . User { Name : org . Name , Type : user_model . UserTypeOrganization } )
2023-10-03 13:30:41 +03:00
assert . NoError ( t , organization . CreateOrganization ( db . DefaultContext , org , owner ) )
2022-03-29 09:29:02 +03:00
org = unittest . AssertExistsAndLoadBean ( t ,
2022-08-16 05:22:25 +03:00
& organization . Organization { Name : org . Name , Type : user_model . UserTypeOrganization } )
2022-06-15 10:02:00 +03:00
test1 := organization . HasOrgOrUserVisible ( db . DefaultContext , org . AsUser ( ) , owner )
2023-09-14 05:59:53 +03:00
test2 := organization . HasOrgOrUserVisible ( db . DefaultContext , org . AsUser ( ) , org3 )
2022-06-15 10:02:00 +03:00
test3 := organization . HasOrgOrUserVisible ( db . DefaultContext , org . AsUser ( ) , nil )
2022-03-29 09:29:02 +03:00
assert . True ( t , test1 ) // owner of org
assert . False ( t , test2 ) // user not a part of org
assert . False ( t , test3 ) // logged out user
}
func TestGetUsersWhoCanCreateOrgRepo ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-06-15 10:02:00 +03:00
users , err := organization . GetUsersWhoCanCreateOrgRepo ( db . DefaultContext , 3 )
2022-03-29 09:29:02 +03:00
assert . NoError ( t , err )
assert . Len ( t , users , 2 )
var ids [ ] int64
for i := range users {
ids = append ( ids , users [ i ] . ID )
}
assert . ElementsMatch ( t , ids , [ ] int64 { 2 , 28 } )
2022-06-15 10:02:00 +03:00
users , err = organization . GetUsersWhoCanCreateOrgRepo ( db . DefaultContext , 7 )
2022-03-29 09:29:02 +03:00
assert . NoError ( t , err )
assert . Len ( t , users , 1 )
2023-01-30 13:12:45 +03:00
assert . NotNil ( t , users [ 5 ] )
2022-03-29 09:29:02 +03:00
}
2022-06-15 10:02:00 +03:00
func TestUser_RemoveOrgRepo ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-08-16 05:22:25 +03:00
org := unittest . AssertExistsAndLoadBean ( t , & organization . Organization { ID : 3 } )
repo := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { OwnerID : org . ID } )
2022-06-15 10:02:00 +03:00
// remove a repo that does belong to org
unittest . AssertExistsAndLoadBean ( t , & organization . TeamRepo { RepoID : repo . ID , OrgID : org . ID } )
assert . NoError ( t , organization . RemoveOrgRepo ( db . DefaultContext , org . ID , repo . ID ) )
unittest . AssertNotExistsBean ( t , & organization . TeamRepo { RepoID : repo . ID , OrgID : org . ID } )
unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : repo . ID } ) // repo should still exist
// remove a repo that does not belong to org
assert . NoError ( t , organization . RemoveOrgRepo ( db . DefaultContext , org . ID , repo . ID ) )
unittest . AssertNotExistsBean ( t , & organization . TeamRepo { RepoID : repo . ID , OrgID : org . ID } )
assert . NoError ( t , organization . RemoveOrgRepo ( db . DefaultContext , org . ID , unittest . NonexistentID ) )
unittest . CheckConsistencyFor ( t ,
& user_model . User { ID : org . ID } ,
& organization . Team { OrgID : org . ID } ,
& repo_model . Repository { ID : repo . ID } )
}
func TestCreateOrganization ( t * testing . T ) {
// successful creation of org
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-08-16 05:22:25 +03:00
owner := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 2 } )
2022-06-15 10:02:00 +03:00
const newOrgName = "neworg"
org := & organization . Organization {
Name : newOrgName ,
}
unittest . AssertNotExistsBean ( t , & user_model . User { Name : newOrgName , Type : user_model . UserTypeOrganization } )
2023-10-03 13:30:41 +03:00
assert . NoError ( t , organization . CreateOrganization ( db . DefaultContext , org , owner ) )
2022-06-15 10:02:00 +03:00
org = unittest . AssertExistsAndLoadBean ( t ,
2022-08-16 05:22:25 +03:00
& organization . Organization { Name : newOrgName , Type : user_model . UserTypeOrganization } )
2022-06-15 10:02:00 +03:00
ownerTeam := unittest . AssertExistsAndLoadBean ( t ,
2022-08-16 05:22:25 +03:00
& organization . Team { Name : organization . OwnerTeamName , OrgID : org . ID } )
2022-06-15 10:02:00 +03:00
unittest . AssertExistsAndLoadBean ( t , & organization . TeamUser { UID : owner . ID , TeamID : ownerTeam . ID } )
unittest . CheckConsistencyFor ( t , & user_model . User { } , & organization . Team { } )
}
func TestCreateOrganization2 ( t * testing . T ) {
// unauthorized creation of org
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-08-16 05:22:25 +03:00
owner := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 5 } )
2022-06-15 10:02:00 +03:00
const newOrgName = "neworg"
org := & organization . Organization {
Name : newOrgName ,
}
unittest . AssertNotExistsBean ( t , & organization . Organization { Name : newOrgName , Type : user_model . UserTypeOrganization } )
2023-10-03 13:30:41 +03:00
err := organization . CreateOrganization ( db . DefaultContext , org , owner )
2022-06-15 10:02:00 +03:00
assert . Error ( t , err )
assert . True ( t , organization . IsErrUserNotAllowedCreateOrg ( err ) )
unittest . AssertNotExistsBean ( t , & organization . Organization { Name : newOrgName , Type : user_model . UserTypeOrganization } )
unittest . CheckConsistencyFor ( t , & organization . Organization { } , & organization . Team { } )
}
func TestCreateOrganization3 ( t * testing . T ) {
// create org with same name as existent org
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-08-16 05:22:25 +03:00
owner := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 2 } )
2023-09-14 05:59:53 +03:00
org := & organization . Organization { Name : "org3" } // should already exist
2022-06-15 10:02:00 +03:00
unittest . AssertExistsAndLoadBean ( t , & user_model . User { Name : org . Name } ) // sanity check
2023-10-03 13:30:41 +03:00
err := organization . CreateOrganization ( db . DefaultContext , org , owner )
2022-06-15 10:02:00 +03:00
assert . Error ( t , err )
assert . True ( t , user_model . IsErrUserAlreadyExist ( err ) )
unittest . CheckConsistencyFor ( t , & user_model . User { } , & organization . Team { } )
}
func TestCreateOrganization4 ( t * testing . T ) {
// create org with unusable name
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-08-16 05:22:25 +03:00
owner := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 2 } )
2023-10-03 13:30:41 +03:00
err := organization . CreateOrganization ( db . DefaultContext , & organization . Organization { Name : "assets" } , owner )
2022-06-15 10:02:00 +03:00
assert . Error ( t , err )
assert . True ( t , db . IsErrNameReserved ( err ) )
unittest . CheckConsistencyFor ( t , & organization . Organization { } , & organization . Team { } )
}