2019-11-15 13:36:11 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2019-11-15 13:36:11 +05:30
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2022-08-25 08:01:57 +05:30
|
|
|
activities_model "code.gitea.io/gitea/models/activities"
|
2022-05-11 15:39:36 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-03-29 11:59:02 +05:30
|
|
|
"code.gitea.io/gitea/models/organization"
|
2022-05-11 15:39:36 +05:30
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 20:06:47 +05:30
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-11-15 13:36:11 +05:30
|
|
|
"code.gitea.io/gitea/modules/notification"
|
2020-12-25 15:29:32 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2023-09-05 18:30:52 +05:30
|
|
|
"code.gitea.io/gitea/services/feed"
|
2019-11-15 13:36:11 +05:30
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
var notifySync sync.Once
|
|
|
|
|
|
|
|
func registerNotifier() {
|
|
|
|
notifySync.Do(func() {
|
2023-09-05 18:30:52 +05:30
|
|
|
notification.RegisterNotifier(feed.NewNotifier())
|
2019-11-15 13:36:11 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTransferOwnership(t *testing.T) {
|
|
|
|
registerNotifier()
|
|
|
|
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-11-15 13:36:11 +05:30
|
|
|
|
2022-08-16 07:52:25 +05:30
|
|
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
|
|
|
|
repo.Owner = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
|
2022-12-10 08:16:31 +05:30
|
|
|
assert.NoError(t, TransferOwnership(db.DefaultContext, doer, doer, repo, nil))
|
2019-11-15 13:36:11 +05:30
|
|
|
|
2022-08-16 07:52:25 +05:30
|
|
|
transferredRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
|
2019-11-15 13:36:11 +05:30
|
|
|
assert.EqualValues(t, 2, transferredRepo.OwnerID)
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
exist, err := util.IsExist(repo_model.RepoPath("user3", "repo3"))
|
2020-12-25 15:29:32 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, exist)
|
2021-12-10 06:57:50 +05:30
|
|
|
exist, err = util.IsExist(repo_model.RepoPath("user2", "repo3"))
|
2020-12-25 15:29:32 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, exist)
|
2022-08-25 08:01:57 +05:30
|
|
|
unittest.AssertExistsAndLoadBean(t, &activities_model.Action{
|
|
|
|
OpType: activities_model.ActionTransferRepo,
|
2019-11-15 13:36:11 +05:30
|
|
|
ActUserID: 2,
|
|
|
|
RepoID: 3,
|
|
|
|
Content: "user3/repo3",
|
|
|
|
})
|
|
|
|
|
2022-03-29 11:59:02 +05:30
|
|
|
unittest.CheckConsistencyFor(t, &repo_model.Repository{}, &user_model.User{}, &organization.Team{})
|
2019-11-15 13:36:11 +05:30
|
|
|
}
|
2021-06-15 00:00:35 +05:30
|
|
|
|
|
|
|
func TestStartRepositoryTransferSetPermission(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-06-15 00:00:35 +05:30
|
|
|
|
2022-08-16 07:52:25 +05:30
|
|
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
|
|
|
|
recipient := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5})
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
|
|
|
|
repo.Owner = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
|
2021-06-15 00:00:35 +05:30
|
|
|
|
2022-05-11 15:39:36 +05:30
|
|
|
hasAccess, err := access_model.HasAccess(db.DefaultContext, recipient.ID, repo)
|
2021-06-15 00:00:35 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, hasAccess)
|
|
|
|
|
2022-12-10 08:16:31 +05:30
|
|
|
assert.NoError(t, StartRepositoryTransfer(db.DefaultContext, doer, recipient, repo, nil))
|
2021-06-15 00:00:35 +05:30
|
|
|
|
2022-05-11 15:39:36 +05:30
|
|
|
hasAccess, err = access_model.HasAccess(db.DefaultContext, recipient.ID, repo)
|
2021-06-15 00:00:35 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, hasAccess)
|
|
|
|
|
2022-03-29 11:59:02 +05:30
|
|
|
unittest.CheckConsistencyFor(t, &repo_model.Repository{}, &user_model.User{}, &organization.Team{})
|
2021-06-15 00:00:35 +05:30
|
|
|
}
|