2021-03-01 03:47:30 +03: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-11-12 17:36:47 +03:00
"code.gitea.io/gitea/models/unittest"
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
2021-11-16 11:53:21 +03:00
doer := unittest . AssertExistsAndLoadBean ( t , & User { ID : 3 } ) . ( * User )
repo := unittest . AssertExistsAndLoadBean ( t , & Repository { ID : 3 } ) . ( * Repository )
2021-03-01 03:47:30 +03: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-16 11:53:21 +03:00
user2 := unittest . AssertExistsAndLoadBean ( t , & User { ID : 2 } ) . ( * User )
2021-03-01 03:47:30 +03: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-16 11:53:21 +03:00
user6 := unittest . AssertExistsAndLoadBean ( t , & User { ID : 2 } ) . ( * User )
2021-03-01 03:47:30 +03: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
err = CreatePendingRepositoryTransfer ( doer , & User { ID : 1000 , LowerName : "user1000" } , repo . ID , nil )
assert . Error ( t , err )
// Cancel transfer
assert . NoError ( t , CancelRepositoryTransfer ( repo ) )
}