2019-11-15 03:06:11 -05:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-11-15 03:06:11 -05:00
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2024-02-05 01:17:23 -05:00
|
|
|
"code.gitea.io/gitea/models"
|
2022-08-24 22:31:57 -04:00
|
|
|
activities_model "code.gitea.io/gitea/models/activities"
|
2022-05-11 06:09:36 -04:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-03-29 02:29:02 -04:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
2022-05-11 06:09:36 -04:00
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-12-09 20:27:50 -05:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 09:36:47 -05:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-12-25 04:59:32 -05:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2023-09-05 09:00:52 -04:00
|
|
|
"code.gitea.io/gitea/services/feed"
|
2023-09-05 14:37:47 -04:00
|
|
|
notify_service "code.gitea.io/gitea/services/notify"
|
2019-11-15 03:06:11 -05:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
var notifySync sync.Once
|
|
|
|
|
|
|
|
func registerNotifier() {
|
|
|
|
notifySync.Do(func() {
|
2023-09-05 14:37:47 -04:00
|
|
|
notify_service.RegisterNotifier(feed.NewNotifier())
|
2019-11-15 03:06:11 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTransferOwnership(t *testing.T) {
|
|
|
|
registerNotifier()
|
|
|
|
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-11-15 03:06:11 -05:00
|
|
|
|
2022-08-15 22:22:25 -04: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})
|
2022-12-09 21:46:31 -05:00
|
|
|
assert.NoError(t, TransferOwnership(db.DefaultContext, doer, doer, repo, nil))
|
2019-11-15 03:06:11 -05:00
|
|
|
|
2022-08-15 22:22:25 -04:00
|
|
|
transferredRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
|
2019-11-15 03:06:11 -05:00
|
|
|
assert.EqualValues(t, 2, transferredRepo.OwnerID)
|
|
|
|
|
2023-09-13 22:59:53 -04:00
|
|
|
exist, err := util.IsExist(repo_model.RepoPath("org3", "repo3"))
|
2020-12-25 04:59:32 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, exist)
|
2021-12-09 20:27:50 -05:00
|
|
|
exist, err = util.IsExist(repo_model.RepoPath("user2", "repo3"))
|
2020-12-25 04:59:32 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, exist)
|
2022-08-24 22:31:57 -04:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &activities_model.Action{
|
|
|
|
OpType: activities_model.ActionTransferRepo,
|
2019-11-15 03:06:11 -05:00
|
|
|
ActUserID: 2,
|
|
|
|
RepoID: 3,
|
2023-09-13 22:59:53 -04:00
|
|
|
Content: "org3/repo3",
|
2019-11-15 03:06:11 -05:00
|
|
|
})
|
|
|
|
|
2022-03-29 02:29:02 -04:00
|
|
|
unittest.CheckConsistencyFor(t, &repo_model.Repository{}, &user_model.User{}, &organization.Team{})
|
2019-11-15 03:06:11 -05:00
|
|
|
}
|
2021-06-14 14:30:35 -04:00
|
|
|
|
|
|
|
func TestStartRepositoryTransferSetPermission(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-06-14 14:30:35 -04:00
|
|
|
|
2022-08-15 22:22:25 -04: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-14 14:30:35 -04:00
|
|
|
|
2024-04-19 23:15:04 -04:00
|
|
|
hasAccess, err := access_model.HasAnyUnitAccess(db.DefaultContext, recipient.ID, repo)
|
2021-06-14 14:30:35 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, hasAccess)
|
|
|
|
|
2022-12-09 21:46:31 -05:00
|
|
|
assert.NoError(t, StartRepositoryTransfer(db.DefaultContext, doer, recipient, repo, nil))
|
2021-06-14 14:30:35 -04:00
|
|
|
|
2024-04-19 23:15:04 -04:00
|
|
|
hasAccess, err = access_model.HasAnyUnitAccess(db.DefaultContext, recipient.ID, repo)
|
2021-06-14 14:30:35 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, hasAccess)
|
|
|
|
|
2022-03-29 02:29:02 -04:00
|
|
|
unittest.CheckConsistencyFor(t, &repo_model.Repository{}, &user_model.User{}, &organization.Team{})
|
2021-06-14 14:30:35 -04:00
|
|
|
}
|
2024-02-05 01:17:23 -05:00
|
|
|
|
|
|
|
func TestRepositoryTransfer(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
|
|
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
|
|
|
|
|
|
|
|
transfer, err := models.GetPendingRepositoryTransfer(db.DefaultContext, repo)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, transfer)
|
|
|
|
|
|
|
|
// Cancel transfer
|
|
|
|
assert.NoError(t, CancelRepositoryTransfer(db.DefaultContext, repo))
|
|
|
|
|
|
|
|
transfer, err = models.GetPendingRepositoryTransfer(db.DefaultContext, repo)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, transfer)
|
|
|
|
assert.True(t, models.IsErrNoPendingTransfer(err))
|
|
|
|
|
|
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
|
|
|
|
assert.NoError(t, models.CreatePendingRepositoryTransfer(db.DefaultContext, doer, user2, repo.ID, nil))
|
|
|
|
|
|
|
|
transfer, err = models.GetPendingRepositoryTransfer(db.DefaultContext, repo)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NoError(t, transfer.LoadAttributes(db.DefaultContext))
|
|
|
|
assert.Equal(t, "user2", transfer.Recipient.Name)
|
|
|
|
|
|
|
|
org6 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
|
|
|
|
// Only transfer can be started at any given time
|
|
|
|
err = models.CreatePendingRepositoryTransfer(db.DefaultContext, doer, org6, repo.ID, nil)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, models.IsErrRepoTransferInProgress(err))
|
|
|
|
|
|
|
|
// Unknown user
|
|
|
|
err = models.CreatePendingRepositoryTransfer(db.DefaultContext, doer, &user_model.User{ID: 1000, LowerName: "user1000"}, repo.ID, nil)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
// Cancel transfer
|
|
|
|
assert.NoError(t, CancelRepositoryTransfer(db.DefaultContext, repo))
|
|
|
|
}
|