2021-03-01 01:47:30 +01:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-12-10 09:27:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 22:36:47 +08:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 17:49:20 +08:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-17 20:34:35 +08:00
|
|
|
|
2021-03-01 01:47:30 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRepositoryTransfer(t *testing.T) {
|
2021-11-12 22:36:47 +08:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-03-01 01:47:30 +01:00
|
|
|
|
2021-11-24 17:49:20 +08:00
|
|
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}).(*user_model.User)
|
2021-12-10 09:27:50 +08:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}).(*repo_model.Repository)
|
2021-03-01 01:47:30 +01:00
|
|
|
|
|
|
|
transfer, err := GetPendingRepositoryTransfer(repo)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, transfer)
|
|
|
|
|
|
|
|
// Cancel transfer
|
|
|
|
assert.NoError(t, CancelRepositoryTransfer(repo))
|
|
|
|
|
|
|
|
transfer, err = GetPendingRepositoryTransfer(repo)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, transfer)
|
|
|
|
assert.True(t, IsErrNoPendingTransfer(err))
|
|
|
|
|
2021-11-24 17:49:20 +08:00
|
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
2021-03-01 01:47:30 +01:00
|
|
|
|
|
|
|
assert.NoError(t, CreatePendingRepositoryTransfer(doer, user2, repo.ID, nil))
|
|
|
|
|
|
|
|
transfer, err = GetPendingRepositoryTransfer(repo)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NoError(t, transfer.LoadAttributes())
|
|
|
|
assert.Equal(t, "user2", transfer.Recipient.Name)
|
|
|
|
|
2021-11-24 17:49:20 +08:00
|
|
|
user6 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
2021-03-01 01:47:30 +01:00
|
|
|
|
|
|
|
// Only transfer can be started at any given time
|
|
|
|
err = CreatePendingRepositoryTransfer(doer, user6, repo.ID, nil)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrRepoTransferInProgress(err))
|
|
|
|
|
|
|
|
// Unknown user
|
2021-11-24 17:49:20 +08:00
|
|
|
err = CreatePendingRepositoryTransfer(doer, &user_model.User{ID: 1000, LowerName: "user1000"}, repo.ID, nil)
|
2021-03-01 01:47:30 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
// Cancel transfer
|
|
|
|
assert.NoError(t, CancelRepositoryTransfer(repo))
|
|
|
|
}
|