code.gitea.io/gitea@v1.21.7/services/repository/transfer_test.go (about)

     1  // Copyright 2019 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package repository
     5  
     6  import (
     7  	"sync"
     8  	"testing"
     9  
    10  	activities_model "code.gitea.io/gitea/models/activities"
    11  	"code.gitea.io/gitea/models/db"
    12  	"code.gitea.io/gitea/models/organization"
    13  	access_model "code.gitea.io/gitea/models/perm/access"
    14  	repo_model "code.gitea.io/gitea/models/repo"
    15  	"code.gitea.io/gitea/models/unittest"
    16  	user_model "code.gitea.io/gitea/models/user"
    17  	"code.gitea.io/gitea/modules/util"
    18  	"code.gitea.io/gitea/services/feed"
    19  	notify_service "code.gitea.io/gitea/services/notify"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  )
    23  
    24  var notifySync sync.Once
    25  
    26  func registerNotifier() {
    27  	notifySync.Do(func() {
    28  		notify_service.RegisterNotifier(feed.NewNotifier())
    29  	})
    30  }
    31  
    32  func TestTransferOwnership(t *testing.T) {
    33  	registerNotifier()
    34  
    35  	assert.NoError(t, unittest.PrepareTestDatabase())
    36  
    37  	doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
    38  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
    39  	repo.Owner = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
    40  	assert.NoError(t, TransferOwnership(db.DefaultContext, doer, doer, repo, nil))
    41  
    42  	transferredRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
    43  	assert.EqualValues(t, 2, transferredRepo.OwnerID)
    44  
    45  	exist, err := util.IsExist(repo_model.RepoPath("org3", "repo3"))
    46  	assert.NoError(t, err)
    47  	assert.False(t, exist)
    48  	exist, err = util.IsExist(repo_model.RepoPath("user2", "repo3"))
    49  	assert.NoError(t, err)
    50  	assert.True(t, exist)
    51  	unittest.AssertExistsAndLoadBean(t, &activities_model.Action{
    52  		OpType:    activities_model.ActionTransferRepo,
    53  		ActUserID: 2,
    54  		RepoID:    3,
    55  		Content:   "org3/repo3",
    56  	})
    57  
    58  	unittest.CheckConsistencyFor(t, &repo_model.Repository{}, &user_model.User{}, &organization.Team{})
    59  }
    60  
    61  func TestStartRepositoryTransferSetPermission(t *testing.T) {
    62  	assert.NoError(t, unittest.PrepareTestDatabase())
    63  
    64  	doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
    65  	recipient := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5})
    66  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
    67  	repo.Owner = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
    68  
    69  	hasAccess, err := access_model.HasAccess(db.DefaultContext, recipient.ID, repo)
    70  	assert.NoError(t, err)
    71  	assert.False(t, hasAccess)
    72  
    73  	assert.NoError(t, StartRepositoryTransfer(db.DefaultContext, doer, recipient, repo, nil))
    74  
    75  	hasAccess, err = access_model.HasAccess(db.DefaultContext, recipient.ID, repo)
    76  	assert.NoError(t, err)
    77  	assert.True(t, hasAccess)
    78  
    79  	unittest.CheckConsistencyFor(t, &repo_model.Repository{}, &user_model.User{}, &organization.Team{})
    80  }