2017-01-24 11:16:36 -05:00
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package models
import (
"testing"
2021-09-19 19:49:59 +08:00
"code.gitea.io/gitea/models/db"
2022-03-29 14:29:02 +08:00
"code.gitea.io/gitea/models/organization"
2021-12-10 09:27:50 +08:00
repo_model "code.gitea.io/gitea/models/repo"
2021-11-12 22:36:47 +08:00
"code.gitea.io/gitea/models/unittest"
2021-11-24 17:49:20 +08:00
user_model "code.gitea.io/gitea/models/user"
2019-08-24 09:28:59 -03:00
"code.gitea.io/gitea/modules/setting"
2019-02-18 17:00:27 +01:00
2017-01-24 11:16:36 -05:00
"github.com/stretchr/testify/assert"
)
func TestUser_RemoveMember ( t * testing . T ) {
2021-11-12 22:36:47 +08:00
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-03-29 14:29:02 +08:00
org := unittest . AssertExistsAndLoadBean ( t , & organization . Organization { ID : 3 } ) . ( * organization . Organization )
2017-01-24 11:16:36 -05:00
// remove a user that is a member
2022-03-29 14:29:02 +08:00
unittest . AssertExistsAndLoadBean ( t , & organization . OrgUser { UID : 4 , OrgID : 3 } )
2017-01-24 11:16:36 -05:00
prevNumMembers := org . NumMembers
2022-03-29 14:29:02 +08:00
assert . NoError ( t , RemoveOrgUser ( org . ID , 4 ) )
unittest . AssertNotExistsBean ( t , & organization . OrgUser { UID : 4 , OrgID : 3 } )
org = unittest . AssertExistsAndLoadBean ( t , & organization . Organization { ID : 3 } ) . ( * organization . Organization )
2017-01-24 11:16:36 -05:00
assert . Equal ( t , prevNumMembers - 1 , org . NumMembers )
// remove a user that is not a member
2022-03-29 14:29:02 +08:00
unittest . AssertNotExistsBean ( t , & organization . OrgUser { UID : 5 , OrgID : 3 } )
2017-01-24 11:16:36 -05:00
prevNumMembers = org . NumMembers
2022-03-29 14:29:02 +08:00
assert . NoError ( t , RemoveOrgUser ( org . ID , 5 ) )
unittest . AssertNotExistsBean ( t , & organization . OrgUser { UID : 5 , OrgID : 3 } )
org = unittest . AssertExistsAndLoadBean ( t , & organization . Organization { ID : 3 } ) . ( * organization . Organization )
2017-01-24 11:16:36 -05:00
assert . Equal ( t , prevNumMembers , org . NumMembers )
2017-02-07 06:47:55 -05:00
2022-03-29 14:29:02 +08:00
unittest . CheckConsistencyFor ( t , & user_model . User { } , & organization . Team { } )
}
func TestRemoveOrgUser ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
testSuccess := func ( orgID , userID int64 ) {
org := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : orgID } ) . ( * user_model . User )
expectedNumMembers := org . NumMembers
if unittest . BeanExists ( t , & organization . OrgUser { OrgID : orgID , UID : userID } ) {
expectedNumMembers --
}
assert . NoError ( t , RemoveOrgUser ( orgID , userID ) )
unittest . AssertNotExistsBean ( t , & organization . OrgUser { OrgID : orgID , UID : userID } )
org = unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : orgID } ) . ( * user_model . User )
assert . EqualValues ( t , expectedNumMembers , org . NumMembers )
}
testSuccess ( 3 , 4 )
testSuccess ( 3 , 4 )
err := RemoveOrgUser ( 7 , 5 )
assert . Error ( t , err )
assert . True ( t , organization . IsErrLastOrgOwner ( err ) )
unittest . AssertExistsAndLoadBean ( t , & organization . OrgUser { OrgID : 7 , UID : 5 } )
unittest . CheckConsistencyFor ( t , & user_model . User { } , & organization . Team { } )
2017-01-24 11:16:36 -05:00
}
func TestUser_RemoveOrgRepo ( t * testing . T ) {
2021-11-12 22:36:47 +08:00
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-03-29 14:29:02 +08:00
org := unittest . AssertExistsAndLoadBean ( t , & organization . Organization { ID : 3 } ) . ( * organization . Organization )
2021-12-10 09:27:50 +08:00
repo := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { OwnerID : org . ID } ) . ( * repo_model . Repository )
2017-01-24 11:16:36 -05:00
// remove a repo that does belong to org
2022-03-29 14:29:02 +08:00
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 } )
2021-12-10 09:27:50 +08:00
unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : repo . ID } ) // repo should still exist
2017-01-24 11:16:36 -05:00
// remove a repo that does not belong to org
2022-03-29 14:29:02 +08:00
assert . NoError ( t , organization . RemoveOrgRepo ( db . DefaultContext , org . ID , repo . ID ) )
unittest . AssertNotExistsBean ( t , & organization . TeamRepo { RepoID : repo . ID , OrgID : org . ID } )
2017-01-24 11:16:36 -05:00
2022-03-29 14:29:02 +08:00
assert . NoError ( t , organization . RemoveOrgRepo ( db . DefaultContext , org . ID , unittest . NonexistentID ) )
2017-02-07 06:47:55 -05:00
2021-11-16 16:53:21 +08:00
unittest . CheckConsistencyFor ( t ,
2021-11-24 17:49:20 +08:00
& user_model . User { ID : org . ID } ,
2022-03-29 14:29:02 +08:00
& organization . Team { OrgID : org . ID } ,
2021-12-10 09:27:50 +08:00
& repo_model . Repository { ID : repo . ID } )
2017-01-24 11:16:36 -05:00
}
func TestCreateOrganization ( t * testing . T ) {
// successful creation of org
2021-11-12 22:36:47 +08:00
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2017-01-24 11:16:36 -05:00
2021-11-24 17:49:20 +08:00
owner := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 2 } ) . ( * user_model . User )
2017-01-24 11:16:36 -05:00
const newOrgName = "neworg"
2022-03-29 14:29:02 +08:00
org := & organization . Organization {
2017-01-24 11:16:36 -05:00
Name : newOrgName ,
}
2021-11-24 17:49:20 +08:00
unittest . AssertNotExistsBean ( t , & user_model . User { Name : newOrgName , Type : user_model . UserTypeOrganization } )
2022-03-29 14:29:02 +08:00
assert . NoError ( t , organization . CreateOrganization ( org , owner ) )
2021-11-16 16:53:21 +08:00
org = unittest . AssertExistsAndLoadBean ( t ,
2022-03-29 14:29:02 +08:00
& organization . Organization { Name : newOrgName , Type : user_model . UserTypeOrganization } ) . ( * organization . Organization )
2021-11-16 16:53:21 +08:00
ownerTeam := unittest . AssertExistsAndLoadBean ( t ,
2022-03-29 14:29:02 +08:00
& organization . Team { Name : organization . OwnerTeamName , OrgID : org . ID } ) . ( * organization . Team )
unittest . AssertExistsAndLoadBean ( t , & organization . TeamUser { UID : owner . ID , TeamID : ownerTeam . ID } )
unittest . CheckConsistencyFor ( t , & user_model . User { } , & organization . Team { } )
2017-01-24 11:16:36 -05:00
}
func TestCreateOrganization2 ( t * testing . T ) {
// unauthorized creation of org
2021-11-12 22:36:47 +08:00
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2017-01-24 11:16:36 -05:00
2021-11-24 17:49:20 +08:00
owner := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 5 } ) . ( * user_model . User )
2017-01-24 11:16:36 -05:00
const newOrgName = "neworg"
2022-03-29 14:29:02 +08:00
org := & organization . Organization {
2017-01-24 11:16:36 -05:00
Name : newOrgName ,
}
2022-03-29 14:29:02 +08:00
unittest . AssertNotExistsBean ( t , & organization . Organization { Name : newOrgName , Type : user_model . UserTypeOrganization } )
err := organization . CreateOrganization ( org , owner )
2017-01-24 11:16:36 -05:00
assert . Error ( t , err )
2022-03-29 14:29:02 +08:00
assert . True ( t , organization . IsErrUserNotAllowedCreateOrg ( err ) )
unittest . AssertNotExistsBean ( t , & organization . Organization { Name : newOrgName , Type : user_model . UserTypeOrganization } )
unittest . CheckConsistencyFor ( t , & organization . Organization { } , & organization . Team { } )
2017-01-24 11:16:36 -05:00
}
func TestCreateOrganization3 ( t * testing . T ) {
// create org with same name as existent org
2021-11-12 22:36:47 +08:00
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2017-01-24 11:16:36 -05:00
2021-11-24 17:49:20 +08:00
owner := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 2 } ) . ( * user_model . User )
2022-03-29 14:29:02 +08:00
org := & organization . Organization { Name : "user3" } // should already exist
2021-11-24 17:49:20 +08:00
unittest . AssertExistsAndLoadBean ( t , & user_model . User { Name : org . Name } ) // sanity check
2022-03-29 14:29:02 +08:00
err := organization . CreateOrganization ( org , owner )
2017-01-24 11:16:36 -05:00
assert . Error ( t , err )
2021-11-24 17:49:20 +08:00
assert . True ( t , user_model . IsErrUserAlreadyExist ( err ) )
2022-03-29 14:29:02 +08:00
unittest . CheckConsistencyFor ( t , & user_model . User { } , & organization . Team { } )
2017-01-24 11:16:36 -05:00
}
func TestCreateOrganization4 ( t * testing . T ) {
// create org with unusable name
2021-11-12 22:36:47 +08:00
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2017-01-24 11:16:36 -05:00
2021-11-24 17:49:20 +08:00
owner := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 2 } ) . ( * user_model . User )
2022-03-29 14:29:02 +08:00
err := organization . CreateOrganization ( & organization . Organization { Name : "assets" } , owner )
2017-01-24 11:16:36 -05:00
assert . Error ( t , err )
2021-11-24 17:49:20 +08:00
assert . True ( t , db . IsErrNameReserved ( err ) )
2022-03-29 14:29:02 +08:00
unittest . CheckConsistencyFor ( t , & organization . Organization { } , & organization . Team { } )
2017-01-24 11:16:36 -05:00
}
func TestAddOrgUser ( t * testing . T ) {
2021-11-12 22:36:47 +08:00
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2019-08-24 09:28:59 -03:00
testSuccess := func ( orgID , userID int64 , isPublic bool ) {
2021-11-24 17:49:20 +08:00
org := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : orgID } ) . ( * user_model . User )
2017-01-24 11:16:36 -05:00
expectedNumMembers := org . NumMembers
2022-03-29 14:29:02 +08:00
if ! unittest . BeanExists ( t , & organization . OrgUser { OrgID : orgID , UID : userID } ) {
2017-01-24 11:16:36 -05:00
expectedNumMembers ++
}
2022-03-29 14:29:02 +08:00
assert . NoError ( t , organization . AddOrgUser ( orgID , userID ) )
ou := & organization . OrgUser { OrgID : orgID , UID : userID }
2021-11-16 16:53:21 +08:00
unittest . AssertExistsAndLoadBean ( t , ou )
2021-06-07 07:27:09 +02:00
assert . Equal ( t , isPublic , ou . IsPublic )
2021-11-24 17:49:20 +08:00
org = unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : orgID } ) . ( * user_model . User )
2017-01-24 11:16:36 -05:00
assert . EqualValues ( t , expectedNumMembers , org . NumMembers )
}
2019-08-24 09:28:59 -03:00
setting . Service . DefaultOrgMemberVisible = false
testSuccess ( 3 , 5 , false )
testSuccess ( 3 , 5 , false )
testSuccess ( 6 , 2 , false )
setting . Service . DefaultOrgMemberVisible = true
testSuccess ( 6 , 3 , true )
2022-03-29 14:29:02 +08:00
unittest . CheckConsistencyFor ( t , & user_model . User { } , & organization . Team { } )
2021-03-01 01:47:30 +01:00
}