2019-11-15 17:06:11 +09:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-28 03:20:29 +09:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-11-15 17:06:11 +09:00
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2022-08-25 11:31:57 +09:00
|
|
|
activities_model "code.gitea.io/gitea/models/activities"
|
2022-05-11 19:09:36 +09:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-03-29 15:29:02 +09:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
2022-05-11 19:09:36 +09:00
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-12-10 10:27:50 +09:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 23:36:47 +09:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 18:49:20 +09:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-11-15 17:06:11 +09:00
|
|
|
"code.gitea.io/gitea/modules/notification"
|
|
|
|
"code.gitea.io/gitea/modules/notification/action"
|
2020-12-25 18:59:32 +09:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2019-11-15 17:06:11 +09: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 23:36:47 +09:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-11-15 17:06:11 +09:00
|
|
|
|
2022-08-16 11:22:25 +09:00
|
|
|
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})
|
2020-02-01 00:49:04 +09:00
|
|
|
assert.NoError(t, TransferOwnership(doer, doer, repo, nil))
|
2019-11-15 17:06:11 +09:00
|
|
|
|
2022-08-16 11:22:25 +09:00
|
|
|
transferredRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
|
2019-11-15 17:06:11 +09:00
|
|
|
assert.EqualValues(t, 2, transferredRepo.OwnerID)
|
|
|
|
|
2021-12-10 10:27:50 +09:00
|
|
|
exist, err := util.IsExist(repo_model.RepoPath("user3", "repo3"))
|
2020-12-25 18:59:32 +09:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, exist)
|
2021-12-10 10:27:50 +09:00
|
|
|
exist, err = util.IsExist(repo_model.RepoPath("user2", "repo3"))
|
2020-12-25 18:59:32 +09:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, exist)
|
2022-08-25 11:31:57 +09:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &activities_model.Action{
|
|
|
|
OpType: activities_model.ActionTransferRepo,
|
2019-11-15 17:06:11 +09:00
|
|
|
ActUserID: 2,
|
|
|
|
RepoID: 3,
|
|
|
|
Content: "user3/repo3",
|
|
|
|
})
|
|
|
|
|
2022-03-29 15:29:02 +09:00
|
|
|
unittest.CheckConsistencyFor(t, &repo_model.Repository{}, &user_model.User{}, &organization.Team{})
|
2019-11-15 17:06:11 +09:00
|
|
|
}
|
2021-06-15 03:30:35 +09:00
|
|
|
|
|
|
|
func TestStartRepositoryTransferSetPermission(t *testing.T) {
|
2021-11-12 23:36:47 +09:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-06-15 03:30:35 +09:00
|
|
|
|
2022-08-16 11:22:25 +09:00
|
|
|
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 03:30:35 +09:00
|
|
|
|
2022-05-11 19:09:36 +09:00
|
|
|
hasAccess, err := access_model.HasAccess(db.DefaultContext, recipient.ID, repo)
|
2021-06-15 03:30:35 +09:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, hasAccess)
|
|
|
|
|
|
|
|
assert.NoError(t, StartRepositoryTransfer(doer, recipient, repo, nil))
|
|
|
|
|
2022-05-11 19:09:36 +09:00
|
|
|
hasAccess, err = access_model.HasAccess(db.DefaultContext, recipient.ID, repo)
|
2021-06-15 03:30:35 +09:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, hasAccess)
|
|
|
|
|
2022-03-29 15:29:02 +09:00
|
|
|
unittest.CheckConsistencyFor(t, &repo_model.Repository{}, &user_model.User{}, &organization.Team{})
|
2021-06-15 03:30:35 +09:00
|
|
|
}
|