2017-01-24 16:16:36 +00:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-01-24 16:16:36 +00:00
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-10-15 15:46:06 +00:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-03-29 06:29:02 +00:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
2021-11-12 14:36:47 +00:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 09:49:20 +00:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-02-18 16:00:27 +00:00
|
|
|
|
2017-01-24 16:16:36 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUser_RemoveMember(t *testing.T) {
|
2021-11-12 14:36:47 +00:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2024-03-04 08:16:03 +00:00
|
|
|
|
2022-08-16 02:22:25 +00:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
|
2024-03-04 08:16:03 +00:00
|
|
|
user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4})
|
|
|
|
user5 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5})
|
2017-01-24 16:16:36 +00:00
|
|
|
|
|
|
|
// remove a user that is a member
|
2024-03-04 08:16:03 +00:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.OrgUser{UID: user4.ID, OrgID: org.ID})
|
2017-01-24 16:16:36 +00:00
|
|
|
prevNumMembers := org.NumMembers
|
2024-03-04 08:16:03 +00:00
|
|
|
assert.NoError(t, RemoveOrgUser(db.DefaultContext, org, user4))
|
|
|
|
unittest.AssertNotExistsBean(t, &organization.OrgUser{UID: user4.ID, OrgID: org.ID})
|
|
|
|
|
|
|
|
org = unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: org.ID})
|
2017-01-24 16:16:36 +00:00
|
|
|
assert.Equal(t, prevNumMembers-1, org.NumMembers)
|
|
|
|
|
|
|
|
// remove a user that is not a member
|
2024-03-04 08:16:03 +00:00
|
|
|
unittest.AssertNotExistsBean(t, &organization.OrgUser{UID: user5.ID, OrgID: org.ID})
|
2017-01-24 16:16:36 +00:00
|
|
|
prevNumMembers = org.NumMembers
|
2024-03-04 08:16:03 +00:00
|
|
|
assert.NoError(t, RemoveOrgUser(db.DefaultContext, org, user5))
|
|
|
|
unittest.AssertNotExistsBean(t, &organization.OrgUser{UID: user5.ID, OrgID: org.ID})
|
|
|
|
|
|
|
|
org = unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: org.ID})
|
2017-01-24 16:16:36 +00:00
|
|
|
assert.Equal(t, prevNumMembers, org.NumMembers)
|
2017-02-07 11:47:55 +00:00
|
|
|
|
2022-03-29 06:29:02 +00:00
|
|
|
unittest.CheckConsistencyFor(t, &user_model.User{}, &organization.Team{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveOrgUser(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2024-03-04 08:16:03 +00:00
|
|
|
|
|
|
|
testSuccess := func(org *organization.Organization, user *user_model.User) {
|
2022-03-29 06:29:02 +00:00
|
|
|
expectedNumMembers := org.NumMembers
|
2024-03-04 08:16:03 +00:00
|
|
|
if unittest.BeanExists(t, &organization.OrgUser{OrgID: org.ID, UID: user.ID}) {
|
2022-03-29 06:29:02 +00:00
|
|
|
expectedNumMembers--
|
|
|
|
}
|
2024-03-04 08:16:03 +00:00
|
|
|
assert.NoError(t, RemoveOrgUser(db.DefaultContext, org, user))
|
|
|
|
unittest.AssertNotExistsBean(t, &organization.OrgUser{OrgID: org.ID, UID: user.ID})
|
|
|
|
org = unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: org.ID})
|
2022-03-29 06:29:02 +00:00
|
|
|
assert.EqualValues(t, expectedNumMembers, org.NumMembers)
|
|
|
|
}
|
|
|
|
|
2024-03-04 08:16:03 +00:00
|
|
|
org3 := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
|
|
|
|
org7 := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 7})
|
|
|
|
user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4})
|
|
|
|
user5 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5})
|
|
|
|
|
|
|
|
testSuccess(org3, user4)
|
|
|
|
|
|
|
|
org3 = unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
|
|
|
|
testSuccess(org3, user4)
|
|
|
|
|
|
|
|
err := RemoveOrgUser(db.DefaultContext, org7, user5)
|
2022-03-29 06:29:02 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, organization.IsErrLastOrgOwner(err))
|
2024-03-04 08:16:03 +00:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.OrgUser{OrgID: org7.ID, UID: user5.ID})
|
2022-03-29 06:29:02 +00:00
|
|
|
unittest.CheckConsistencyFor(t, &user_model.User{}, &organization.Team{})
|
2017-01-24 16:16:36 +00:00
|
|
|
}
|