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