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