2019-11-15 16:06:11 +08:00
|
|
|
// Copyright 2019 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 repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2021-12-10 09:27:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 22:36:47 +08:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 17:49:20 +08:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-11-15 16:06:11 +08:00
|
|
|
"code.gitea.io/gitea/modules/notification"
|
|
|
|
"code.gitea.io/gitea/modules/notification/action"
|
2020-12-25 17:59:32 +08:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2019-11-15 16:06:11 +08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
var notifySync sync.Once
|
|
|
|
|
|
|
|
func registerNotifier() {
|
|
|
|
notifySync.Do(func() {
|
|
|
|
notification.RegisterNotifier(action.NewNotifier())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTransferOwnership(t *testing.T) {
|
|
|
|
registerNotifier()
|
|
|
|
|
2021-11-12 22:36:47 +08:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-11-15 16:06:11 +08:00
|
|
|
|
2021-11-24 17:49:20 +08:00
|
|
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
2021-12-10 09:27:50 +08:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}).(*repo_model.Repository)
|
2021-11-24 17:49:20 +08:00
|
|
|
repo.Owner = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}).(*user_model.User)
|
2020-01-31 23:49:04 +08:00
|
|
|
assert.NoError(t, TransferOwnership(doer, doer, repo, nil))
|
2019-11-15 16:06:11 +08:00
|
|
|
|
2021-12-10 09:27:50 +08:00
|
|
|
transferredRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}).(*repo_model.Repository)
|
2019-11-15 16:06:11 +08:00
|
|
|
assert.EqualValues(t, 2, transferredRepo.OwnerID)
|
|
|
|
|
2021-12-10 09:27:50 +08:00
|
|
|
exist, err := util.IsExist(repo_model.RepoPath("user3", "repo3"))
|
2020-12-25 17:59:32 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, exist)
|
2021-12-10 09:27:50 +08:00
|
|
|
exist, err = util.IsExist(repo_model.RepoPath("user2", "repo3"))
|
2020-12-25 17:59:32 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, exist)
|
2021-11-16 16:53:21 +08:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &models.Action{
|
2019-11-15 16:06:11 +08:00
|
|
|
OpType: models.ActionTransferRepo,
|
|
|
|
ActUserID: 2,
|
|
|
|
RepoID: 3,
|
|
|
|
Content: "user3/repo3",
|
|
|
|
})
|
|
|
|
|
2021-12-10 09:27:50 +08:00
|
|
|
unittest.CheckConsistencyFor(t, &repo_model.Repository{}, &user_model.User{}, &models.Team{})
|
2019-11-15 16:06:11 +08:00
|
|
|
}
|
2021-06-15 02:30:35 +08:00
|
|
|
|
|
|
|
func TestStartRepositoryTransferSetPermission(t *testing.T) {
|
2021-11-12 22:36:47 +08:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-06-15 02:30:35 +08:00
|
|
|
|
2021-11-24 17:49:20 +08:00
|
|
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}).(*user_model.User)
|
|
|
|
recipient := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5}).(*user_model.User)
|
2021-12-10 09:27:50 +08:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}).(*repo_model.Repository)
|
2021-11-24 17:49:20 +08:00
|
|
|
repo.Owner = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}).(*user_model.User)
|
2021-06-15 02:30:35 +08:00
|
|
|
|
|
|
|
hasAccess, err := models.HasAccess(recipient.ID, repo)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, hasAccess)
|
|
|
|
|
|
|
|
assert.NoError(t, StartRepositoryTransfer(doer, recipient, repo, nil))
|
|
|
|
|
|
|
|
hasAccess, err = models.HasAccess(recipient.ID, repo)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, hasAccess)
|
|
|
|
|
2021-12-10 09:27:50 +08:00
|
|
|
unittest.CheckConsistencyFor(t, &repo_model.Repository{}, &user_model.User{}, &models.Team{})
|
2021-06-15 02:30:35 +08:00
|
|
|
}
|