2021-12-12 18:48:20 +03:00
// Copyright 2017 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2021-12-12 18:48:20 +03:00
2022-06-06 11:01:49 +03:00
package repo_test
2021-12-12 18:48:20 +03:00
import (
"testing"
"code.gitea.io/gitea/models/db"
2022-06-06 11:01:49 +03:00
repo_model "code.gitea.io/gitea/models/repo"
2021-12-12 18:48:20 +03:00
"code.gitea.io/gitea/models/unittest"
"github.com/stretchr/testify/assert"
)
func TestLookupRedirect ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2023-10-14 11:37:24 +03:00
repoID , err := repo_model . LookupRedirect ( db . DefaultContext , 2 , "oldrepo1" )
2021-12-12 18:48:20 +03:00
assert . NoError ( t , err )
assert . EqualValues ( t , 1 , repoID )
2023-10-14 11:37:24 +03:00
_ , err = repo_model . LookupRedirect ( db . DefaultContext , unittest . NonexistentID , "doesnotexist" )
2022-06-06 11:01:49 +03:00
assert . True ( t , repo_model . IsErrRedirectNotExist ( err ) )
2021-12-12 18:48:20 +03:00
}
func TestNewRedirect ( t * testing . T ) {
// redirect to a completely new name
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-08-16 05:22:25 +03:00
repo := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 1 } )
2022-06-06 11:01:49 +03:00
assert . NoError ( t , repo_model . NewRedirect ( db . DefaultContext , repo . OwnerID , repo . ID , repo . Name , "newreponame" ) )
2021-12-12 18:48:20 +03:00
2022-06-06 11:01:49 +03:00
unittest . AssertExistsAndLoadBean ( t , & repo_model . Redirect {
2021-12-12 18:48:20 +03:00
OwnerID : repo . OwnerID ,
LowerName : repo . LowerName ,
RedirectRepoID : repo . ID ,
} )
2022-06-06 11:01:49 +03:00
unittest . AssertExistsAndLoadBean ( t , & repo_model . Redirect {
2021-12-12 18:48:20 +03:00
OwnerID : repo . OwnerID ,
LowerName : "oldrepo1" ,
RedirectRepoID : repo . ID ,
} )
}
func TestNewRedirect2 ( t * testing . T ) {
// redirect to previously used name
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-08-16 05:22:25 +03:00
repo := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 1 } )
2022-06-06 11:01:49 +03:00
assert . NoError ( t , repo_model . NewRedirect ( db . DefaultContext , repo . OwnerID , repo . ID , repo . Name , "oldrepo1" ) )
2021-12-12 18:48:20 +03:00
2022-06-06 11:01:49 +03:00
unittest . AssertExistsAndLoadBean ( t , & repo_model . Redirect {
2021-12-12 18:48:20 +03:00
OwnerID : repo . OwnerID ,
LowerName : repo . LowerName ,
RedirectRepoID : repo . ID ,
} )
2022-06-06 11:01:49 +03:00
unittest . AssertNotExistsBean ( t , & repo_model . Redirect {
2021-12-12 18:48:20 +03:00
OwnerID : repo . OwnerID ,
LowerName : "oldrepo1" ,
RedirectRepoID : repo . ID ,
} )
}
func TestNewRedirect3 ( t * testing . T ) {
// redirect for a previously-unredirected repo
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-08-16 05:22:25 +03:00
repo := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 2 } )
2022-06-06 11:01:49 +03:00
assert . NoError ( t , repo_model . NewRedirect ( db . DefaultContext , repo . OwnerID , repo . ID , repo . Name , "newreponame" ) )
2021-12-12 18:48:20 +03:00
2022-06-06 11:01:49 +03:00
unittest . AssertExistsAndLoadBean ( t , & repo_model . Redirect {
2021-12-12 18:48:20 +03:00
OwnerID : repo . OwnerID ,
LowerName : repo . LowerName ,
RedirectRepoID : repo . ID ,
} )
}