2017-01-24 09:16:36 -07: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"
|
|
|
|
|
2022-03-29 00:29:02 -06:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
2021-11-12 07:36:47 -07:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 02:49:20 -07:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-02-18 09:00:27 -07:00
|
|
|
|
2017-01-24 09:16:36 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUser_RemoveMember(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 20:22:25 -06:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
|
2017-01-24 09:16:36 -07:00
|
|
|
|
|
|
|
// remove a user that is a member
|
2022-03-29 00:29:02 -06:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &organization.OrgUser{UID: 4, OrgID: 3})
|
2017-01-24 09:16:36 -07:00
|
|
|
prevNumMembers := org.NumMembers
|
2022-03-29 00:29:02 -06:00
|
|
|
assert.NoError(t, RemoveOrgUser(org.ID, 4))
|
|
|
|
unittest.AssertNotExistsBean(t, &organization.OrgUser{UID: 4, OrgID: 3})
|
2022-08-15 20:22:25 -06:00
|
|
|
org = unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
|
2017-01-24 09:16:36 -07:00
|
|
|
assert.Equal(t, prevNumMembers-1, org.NumMembers)
|
|
|
|
|
|
|
|
// remove a user that is not a member
|
2022-03-29 00:29:02 -06:00
|
|
|
unittest.AssertNotExistsBean(t, &organization.OrgUser{UID: 5, OrgID: 3})
|
2017-01-24 09:16:36 -07:00
|
|
|
prevNumMembers = org.NumMembers
|
2022-03-29 00:29:02 -06:00
|
|
|
assert.NoError(t, RemoveOrgUser(org.ID, 5))
|
|
|
|
unittest.AssertNotExistsBean(t, &organization.OrgUser{UID: 5, OrgID: 3})
|
2022-08-15 20:22:25 -06:00
|
|
|
org = unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
|
2017-01-24 09:16:36 -07:00
|
|
|
assert.Equal(t, prevNumMembers, org.NumMembers)
|
2017-02-07 04:47:55 -07:00
|
|
|
|
2022-03-29 00:29:02 -06:00
|
|
|
unittest.CheckConsistencyFor(t, &user_model.User{}, &organization.Team{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveOrgUser(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
testSuccess := func(orgID, userID int64) {
|
2022-08-15 20:22:25 -06:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: orgID})
|
2022-03-29 00:29:02 -06:00
|
|
|
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})
|
2022-08-15 20:22:25 -06:00
|
|
|
org = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: orgID})
|
2022-03-29 00:29:02 -06:00
|
|
|
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 09:16:36 -07:00
|
|
|
}
|