From b2c24a71fdf6b21d2ac045592c58c3ba642079bc Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 11 Jan 2025 00:01:47 -0800 Subject: [PATCH] Fix test --- models/repo/transfer.go | 14 ++++++++++++++ services/repository/transfer_test.go | 16 +++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/models/repo/transfer.go b/models/repo/transfer.go index 36fbd42377319..a499acb3d8d24 100644 --- a/models/repo/transfer.go +++ b/models/repo/transfer.go @@ -206,11 +206,25 @@ func CreatePendingRepositoryTransfer(ctx context.Context, doer, newOwner *user_m return err } + if _, err := user_model.GetUserByID(ctx, newOwner.ID); err != nil { + return err + } + // Make sure repo is ready to transfer if err := TestRepositoryReadyForTransfer(repo.Status); err != nil { return err } + _, err = GetPendingRepositoryTransfer(ctx, repo) + if err == nil { + return ErrRepoTransferInProgress{ + Uname: repo.Owner.LowerName, + Name: repo.Name, + } + } else if !IsErrNoPendingTransfer(err) { + return err + } + repo.Status = RepositoryPendingTransfer if err := UpdateRepositoryCols(ctx, repo, "status"); err != nil { return err diff --git a/services/repository/transfer_test.go b/services/repository/transfer_test.go index 9441ac105b170..ccdff0ff3ad4a 100644 --- a/services/repository/transfer_test.go +++ b/services/repository/transfer_test.go @@ -64,10 +64,10 @@ func TestTransferOwnership(t *testing.T) { func TestStartRepositoryTransferSetPermission(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}) + doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) 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}) + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}) + assert.NoError(t, repo.LoadOwner(db.DefaultContext)) hasAccess, err := access_model.HasAnyUnitAccess(db.DefaultContext, recipient.ID, repo) assert.NoError(t, err) @@ -85,7 +85,7 @@ func TestStartRepositoryTransferSetPermission(t *testing.T) { func TestRepositoryTransfer(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}) + doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}) transfer, err := repo_model.GetPendingRepositoryTransfer(db.DefaultContext, repo) @@ -116,10 +116,12 @@ func TestRepositoryTransfer(t *testing.T) { assert.Error(t, err) assert.True(t, repo_model.IsErrRepoTransferInProgress(err)) - // Unknown user - err = repo_model.CreatePendingRepositoryTransfer(db.DefaultContext, doer, &user_model.User{ID: 1000, LowerName: "user1000"}, repo.ID, nil) + repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}) + // Unknown user, transfer non-existent transfer repo id = 2 + err = repo_model.CreatePendingRepositoryTransfer(db.DefaultContext, doer, &user_model.User{ID: 1000, LowerName: "user1000"}, repo2.ID, nil) assert.Error(t, err) // Cancel transfer - assert.NoError(t, RejectRepositoryTransfer(db.DefaultContext, repo, doer)) + err = RejectRepositoryTransfer(db.DefaultContext, repo2, doer) + assert.True(t, repo_model.IsErrNoPendingTransfer(err)) }