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