2021-11-18 17:42:27 +00:00
|
|
|
// Copyright 2021 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 user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-03-29 06:29:02 +00:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
2021-12-10 01:27:50 +00:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-18 17:42:27 +00:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 09:49:20 +00:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-18 17:42:27 +00:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2022-04-14 13:58:21 +00:00
|
|
|
unittest.MainTest(m, &unittest.TestOptions{
|
|
|
|
GiteaRootPath: filepath.Join("..", ".."),
|
|
|
|
})
|
2021-11-18 17:42:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteUser(t *testing.T) {
|
|
|
|
test := func(userID int64) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-24 09:49:20 +00:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: userID}).(*user_model.User)
|
2021-11-18 17:42:27 +00:00
|
|
|
|
2021-12-10 01:27:50 +00:00
|
|
|
ownedRepos := make([]*repo_model.Repository, 0, 10)
|
|
|
|
assert.NoError(t, db.GetEngine(db.DefaultContext).Find(&ownedRepos, &repo_model.Repository{OwnerID: userID}))
|
2021-11-18 17:42:27 +00:00
|
|
|
if len(ownedRepos) > 0 {
|
2022-07-14 07:22:09 +00:00
|
|
|
err := DeleteUser(db.DefaultContext, user, false)
|
2021-11-18 17:42:27 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, models.IsErrUserOwnRepos(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-29 06:29:02 +00:00
|
|
|
orgUsers := make([]*organization.OrgUser, 0, 10)
|
|
|
|
assert.NoError(t, db.GetEngine(db.DefaultContext).Find(&orgUsers, &organization.OrgUser{UID: userID}))
|
2021-11-18 17:42:27 +00:00
|
|
|
for _, orgUser := range orgUsers {
|
|
|
|
if err := models.RemoveOrgUser(orgUser.OrgID, orgUser.UID); err != nil {
|
2022-03-29 06:29:02 +00:00
|
|
|
assert.True(t, organization.IsErrLastOrgOwner(err))
|
2021-11-18 17:42:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-07-14 07:22:09 +00:00
|
|
|
assert.NoError(t, DeleteUser(db.DefaultContext, user, false))
|
2021-11-24 09:49:20 +00:00
|
|
|
unittest.AssertNotExistsBean(t, &user_model.User{ID: userID})
|
2021-12-10 01:27:50 +00:00
|
|
|
unittest.CheckConsistencyFor(t, &user_model.User{}, &repo_model.Repository{})
|
2021-11-18 17:42:27 +00:00
|
|
|
}
|
|
|
|
test(2)
|
|
|
|
test(4)
|
|
|
|
test(8)
|
|
|
|
test(11)
|
|
|
|
|
2021-11-24 09:49:20 +00:00
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}).(*user_model.User)
|
2022-07-14 07:22:09 +00:00
|
|
|
assert.Error(t, DeleteUser(db.DefaultContext, org, false))
|
2021-11-18 17:42:27 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 21:31:22 +00:00
|
|
|
func TestPurgeUser(t *testing.T) {
|
|
|
|
test := func(userID int64) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: userID}).(*user_model.User)
|
|
|
|
|
|
|
|
err := DeleteUser(db.DefaultContext, user, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
unittest.AssertNotExistsBean(t, &user_model.User{ID: userID})
|
|
|
|
unittest.CheckConsistencyFor(t, &user_model.User{}, &repo_model.Repository{})
|
|
|
|
}
|
|
|
|
test(2)
|
|
|
|
test(4)
|
|
|
|
test(8)
|
|
|
|
test(11)
|
|
|
|
|
|
|
|
org := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}).(*user_model.User)
|
|
|
|
assert.Error(t, DeleteUser(db.DefaultContext, org, false))
|
|
|
|
}
|
|
|
|
|
2021-11-18 17:42:27 +00:00
|
|
|
func TestCreateUser(t *testing.T) {
|
2021-11-24 09:49:20 +00:00
|
|
|
user := &user_model.User{
|
2021-11-18 17:42:27 +00:00
|
|
|
Name: "GiteaBot",
|
|
|
|
Email: "GiteaBot@gitea.io",
|
|
|
|
Passwd: ";p['////..-++']",
|
|
|
|
IsAdmin: false,
|
|
|
|
Theme: setting.UI.DefaultTheme,
|
|
|
|
MustChangePassword: false,
|
|
|
|
}
|
|
|
|
|
2021-11-24 09:49:20 +00:00
|
|
|
assert.NoError(t, user_model.CreateUser(user))
|
2021-11-18 17:42:27 +00:00
|
|
|
|
2022-07-14 07:22:09 +00:00
|
|
|
assert.NoError(t, DeleteUser(db.DefaultContext, user, false))
|
2021-11-18 17:42:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateUser_Issue5882(t *testing.T) {
|
|
|
|
// Init settings
|
|
|
|
_ = setting.Admin
|
|
|
|
|
|
|
|
passwd := ".//.;1;;//.,-=_"
|
|
|
|
|
|
|
|
tt := []struct {
|
2021-11-24 09:49:20 +00:00
|
|
|
user *user_model.User
|
2021-11-18 17:42:27 +00:00
|
|
|
disableOrgCreation bool
|
|
|
|
}{
|
2021-11-24 09:49:20 +00:00
|
|
|
{&user_model.User{Name: "GiteaBot", Email: "GiteaBot@gitea.io", Passwd: passwd, MustChangePassword: false}, false},
|
|
|
|
{&user_model.User{Name: "GiteaBot2", Email: "GiteaBot2@gitea.io", Passwd: passwd, MustChangePassword: false}, true},
|
2021-11-18 17:42:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setting.Service.DefaultAllowCreateOrganization = true
|
|
|
|
|
|
|
|
for _, v := range tt {
|
|
|
|
setting.Admin.DisableRegularOrgCreation = v.disableOrgCreation
|
|
|
|
|
2021-11-24 09:49:20 +00:00
|
|
|
assert.NoError(t, user_model.CreateUser(v.user))
|
2021-11-18 17:42:27 +00:00
|
|
|
|
2021-11-24 09:49:20 +00:00
|
|
|
u, err := user_model.GetUserByEmail(v.user.Email)
|
2021-11-18 17:42:27 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, !u.AllowCreateOrganization, v.disableOrgCreation)
|
|
|
|
|
2022-07-14 07:22:09 +00:00
|
|
|
assert.NoError(t, DeleteUser(db.DefaultContext, v.user, false))
|
2021-11-18 17:42:27 +00:00
|
|
|
}
|
|
|
|
}
|