2021-03-01 00:47:30 +00:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-03-01 00:47:30 +00:00
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-12-10 02:46:31 +00:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-10 01:27:50 +00:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 14:36:47 +00:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 09:49:20 +00:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-17 12:34:35 +00:00
|
|
|
|
2021-03-01 00:47:30 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRepositoryTransfer(t *testing.T) {
|
2021-11-12 14:36:47 +00:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-03-01 00:47:30 +00:00
|
|
|
|
2022-08-16 02:22:25 +00:00
|
|
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
|
2021-03-01 00:47:30 +00:00
|
|
|
|
2022-12-10 02:46:31 +00:00
|
|
|
transfer, err := GetPendingRepositoryTransfer(db.DefaultContext, repo)
|
2021-03-01 00:47:30 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, transfer)
|
|
|
|
|
|
|
|
// Cancel transfer
|
2023-09-16 14:39:12 +00:00
|
|
|
assert.NoError(t, CancelRepositoryTransfer(db.DefaultContext, repo))
|
2021-03-01 00:47:30 +00:00
|
|
|
|
2022-12-10 02:46:31 +00:00
|
|
|
transfer, err = GetPendingRepositoryTransfer(db.DefaultContext, repo)
|
2021-03-01 00:47:30 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, transfer)
|
|
|
|
assert.True(t, IsErrNoPendingTransfer(err))
|
|
|
|
|
2022-08-16 02:22:25 +00:00
|
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2021-03-01 00:47:30 +00:00
|
|
|
|
2022-12-10 02:46:31 +00:00
|
|
|
assert.NoError(t, CreatePendingRepositoryTransfer(db.DefaultContext, doer, user2, repo.ID, nil))
|
2021-03-01 00:47:30 +00:00
|
|
|
|
2022-12-10 02:46:31 +00:00
|
|
|
transfer, err = GetPendingRepositoryTransfer(db.DefaultContext, repo)
|
2021-03-01 00:47:30 +00:00
|
|
|
assert.Nil(t, err)
|
2022-12-10 02:46:31 +00:00
|
|
|
assert.NoError(t, transfer.LoadAttributes(db.DefaultContext))
|
2021-03-01 00:47:30 +00:00
|
|
|
assert.Equal(t, "user2", transfer.Recipient.Name)
|
|
|
|
|
2023-09-14 02:59:53 +00:00
|
|
|
org6 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2021-03-01 00:47:30 +00:00
|
|
|
|
|
|
|
// Only transfer can be started at any given time
|
2023-09-14 02:59:53 +00:00
|
|
|
err = CreatePendingRepositoryTransfer(db.DefaultContext, doer, org6, repo.ID, nil)
|
2021-03-01 00:47:30 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrRepoTransferInProgress(err))
|
|
|
|
|
|
|
|
// Unknown user
|
2022-12-10 02:46:31 +00:00
|
|
|
err = CreatePendingRepositoryTransfer(db.DefaultContext, doer, &user_model.User{ID: 1000, LowerName: "user1000"}, repo.ID, nil)
|
2021-03-01 00:47:30 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
// Cancel transfer
|
2023-09-16 14:39:12 +00:00
|
|
|
assert.NoError(t, CancelRepositoryTransfer(db.DefaultContext, repo))
|
2021-03-01 00:47:30 +00:00
|
|
|
}
|