2021-12-10 09:27:50 +08:00
// Copyright 2017 The Gitea Authors. All rights reserved.
2022-11-27 13:20:29 -05:00
// SPDX-License-Identifier: MIT
2021-12-10 09:27:50 +08:00
2024-11-16 16:41:44 +08:00
package repo
2021-12-10 09:27:50 +08:00
import (
"testing"
"code.gitea.io/gitea/models/db"
2022-06-15 15:02:00 +08:00
"code.gitea.io/gitea/models/unit"
2021-12-10 09:27:50 +08:00
"code.gitea.io/gitea/models/unittest"
2022-06-15 15:02:00 +08:00
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/markup"
2024-03-02 16:42:31 +01:00
"code.gitea.io/gitea/modules/optional"
2023-12-15 14:18:12 +08:00
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/test"
2021-12-10 09:27:50 +08:00
"github.com/stretchr/testify/assert"
)
2022-05-20 22:08:52 +08:00
var (
2024-11-16 16:41:44 +08:00
countRepospts = CountRepositoryOptions { OwnerID : 10 }
countReposptsPublic = CountRepositoryOptions { OwnerID : 10 , Private : optional . Some ( false ) }
countReposptsPrivate = CountRepositoryOptions { OwnerID : 10 , Private : optional . Some ( true ) }
2022-05-20 22:08:52 +08:00
)
2021-12-10 09:27:50 +08:00
func TestGetRepositoryCount ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-05-20 22:08:52 +08:00
ctx := db . DefaultContext
2024-11-16 16:41:44 +08:00
count , err1 := CountRepositories ( ctx , countRepospts )
privateCount , err2 := CountRepositories ( ctx , countReposptsPrivate )
publicCount , err3 := CountRepositories ( ctx , countReposptsPublic )
2021-12-10 09:27:50 +08:00
assert . NoError ( t , err1 )
assert . NoError ( t , err2 )
assert . NoError ( t , err3 )
assert . Equal ( t , int64 ( 3 ) , count )
assert . Equal ( t , privateCount + publicCount , count )
}
func TestGetPublicRepositoryCount ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2024-11-16 16:41:44 +08:00
count , err := CountRepositories ( db . DefaultContext , countReposptsPublic )
2021-12-10 09:27:50 +08:00
assert . NoError ( t , err )
assert . Equal ( t , int64 ( 1 ) , count )
}
func TestGetPrivateRepositoryCount ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2024-11-16 16:41:44 +08:00
count , err := CountRepositories ( db . DefaultContext , countReposptsPrivate )
2021-12-10 09:27:50 +08:00
assert . NoError ( t , err )
assert . Equal ( t , int64 ( 2 ) , count )
}
2021-12-12 23:48:20 +08:00
func TestRepoAPIURL ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2024-11-16 16:41:44 +08:00
repo := unittest . AssertExistsAndLoadBean ( t , & Repository { ID : 10 } )
2021-12-12 23:48:20 +08:00
assert . Equal ( t , "https://try.gitea.io/api/v1/repos/user12/repo10" , repo . APIURL ( ) )
}
2022-06-15 15:02:00 +08:00
func TestWatchRepo ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2024-11-16 16:41:44 +08:00
repo := unittest . AssertExistsAndLoadBean ( t , & Repository { ID : 3 } )
2024-03-04 09:16:03 +01:00
user := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 2 } )
2022-06-15 15:02:00 +08:00
2024-11-16 16:41:44 +08:00
assert . NoError ( t , WatchRepo ( db . DefaultContext , user , repo , true ) )
unittest . AssertExistsAndLoadBean ( t , & Watch { RepoID : repo . ID , UserID : user . ID } )
unittest . CheckConsistencyFor ( t , & Repository { ID : repo . ID } )
2024-03-04 09:16:03 +01:00
2024-11-16 16:41:44 +08:00
assert . NoError ( t , WatchRepo ( db . DefaultContext , user , repo , false ) )
unittest . AssertNotExistsBean ( t , & Watch { RepoID : repo . ID , UserID : user . ID } )
unittest . CheckConsistencyFor ( t , & Repository { ID : repo . ID } )
2022-06-15 15:02:00 +08:00
}
func TestMetas ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2024-11-16 16:41:44 +08:00
repo := & Repository { Name : "testRepo" }
2022-06-15 15:02:00 +08:00
repo . Owner = & user_model . User { Name : "testOwner" }
repo . OwnerName = repo . Owner . Name
repo . Units = nil
2023-10-11 06:24:07 +02:00
metas := repo . ComposeMetas ( db . DefaultContext )
2022-06-15 15:02:00 +08:00
assert . Equal ( t , "testRepo" , metas [ "repo" ] )
assert . Equal ( t , "testOwner" , metas [ "user" ] )
2024-11-16 16:41:44 +08:00
externalTracker := RepoUnit {
2022-06-15 15:02:00 +08:00
Type : unit . TypeExternalTracker ,
2024-11-16 16:41:44 +08:00
Config : & ExternalTrackerConfig {
2022-06-15 15:02:00 +08:00
ExternalTrackerFormat : "https://someurl.com/{user}/{repo}/{issue}" ,
} ,
}
testSuccess := func ( expectedStyle string ) {
2024-11-16 16:41:44 +08:00
repo . Units = [ ] * RepoUnit { & externalTracker }
repo . commonRenderingMetas = nil
2023-10-11 06:24:07 +02:00
metas := repo . ComposeMetas ( db . DefaultContext )
2022-06-15 15:02:00 +08:00
assert . Equal ( t , expectedStyle , metas [ "style" ] )
assert . Equal ( t , "testRepo" , metas [ "repo" ] )
assert . Equal ( t , "testOwner" , metas [ "user" ] )
assert . Equal ( t , "https://someurl.com/{user}/{repo}/{issue}" , metas [ "format" ] )
}
testSuccess ( markup . IssueNameStyleNumeric )
externalTracker . ExternalTrackerConfig ( ) . ExternalTrackerStyle = markup . IssueNameStyleAlphanumeric
testSuccess ( markup . IssueNameStyleAlphanumeric )
externalTracker . ExternalTrackerConfig ( ) . ExternalTrackerStyle = markup . IssueNameStyleNumeric
testSuccess ( markup . IssueNameStyleNumeric )
externalTracker . ExternalTrackerConfig ( ) . ExternalTrackerStyle = markup . IssueNameStyleRegexp
testSuccess ( markup . IssueNameStyleRegexp )
2024-11-16 16:41:44 +08:00
repo , err := GetRepositoryByID ( db . DefaultContext , 3 )
2022-06-15 15:02:00 +08:00
assert . NoError ( t , err )
2023-10-11 06:24:07 +02:00
metas = repo . ComposeMetas ( db . DefaultContext )
2022-06-15 15:02:00 +08:00
assert . Contains ( t , metas , "org" )
assert . Contains ( t , metas , "teams" )
2023-09-14 10:59:53 +08:00
assert . Equal ( t , "org3" , metas [ "org" ] )
2022-06-15 15:02:00 +08:00
assert . Equal ( t , ",owners,team1," , metas [ "teams" ] )
}
2023-03-28 19:55:03 +02:00
func TestGetRepositoryByURL ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
t . Run ( "InvalidPath" , func ( t * testing . T ) {
2024-11-16 16:41:44 +08:00
repo , err := GetRepositoryByURL ( db . DefaultContext , "something" )
2023-03-28 19:55:03 +02:00
assert . Nil ( t , repo )
assert . Error ( t , err )
} )
t . Run ( "ValidHttpURL" , func ( t * testing . T ) {
test := func ( t * testing . T , url string ) {
2024-11-16 16:41:44 +08:00
repo , err := GetRepositoryByURL ( db . DefaultContext , url )
2023-03-28 19:55:03 +02:00
assert . NotNil ( t , repo )
assert . NoError ( t , err )
assert . Equal ( t , repo . ID , int64 ( 2 ) )
assert . Equal ( t , repo . OwnerID , int64 ( 2 ) )
}
test ( t , "https://try.gitea.io/user2/repo2" )
test ( t , "https://try.gitea.io/user2/repo2.git" )
} )
t . Run ( "ValidGitSshURL" , func ( t * testing . T ) {
test := func ( t * testing . T , url string ) {
2024-11-16 16:41:44 +08:00
repo , err := GetRepositoryByURL ( db . DefaultContext , url )
2023-03-28 19:55:03 +02:00
assert . NotNil ( t , repo )
assert . NoError ( t , err )
assert . Equal ( t , repo . ID , int64 ( 2 ) )
assert . Equal ( t , repo . OwnerID , int64 ( 2 ) )
}
test ( t , "git+ssh://sshuser@try.gitea.io/user2/repo2" )
test ( t , "git+ssh://sshuser@try.gitea.io/user2/repo2.git" )
test ( t , "git+ssh://try.gitea.io/user2/repo2" )
test ( t , "git+ssh://try.gitea.io/user2/repo2.git" )
} )
t . Run ( "ValidImplicitSshURL" , func ( t * testing . T ) {
test := func ( t * testing . T , url string ) {
2024-11-16 16:41:44 +08:00
repo , err := GetRepositoryByURL ( db . DefaultContext , url )
2023-03-28 19:55:03 +02:00
assert . NotNil ( t , repo )
assert . NoError ( t , err )
assert . Equal ( t , repo . ID , int64 ( 2 ) )
assert . Equal ( t , repo . OwnerID , int64 ( 2 ) )
}
test ( t , "sshuser@try.gitea.io:user2/repo2" )
test ( t , "sshuser@try.gitea.io:user2/repo2.git" )
test ( t , "try.gitea.io:user2/repo2" )
test ( t , "try.gitea.io:user2/repo2.git" )
} )
}
2023-12-15 14:18:12 +08:00
func TestComposeSSHCloneURL ( t * testing . T ) {
defer test . MockVariableValue ( & setting . SSH , setting . SSH ) ( )
defer test . MockVariableValue ( & setting . Repository , setting . Repository ) ( )
setting . SSH . User = "git"
// test SSH_DOMAIN
setting . SSH . Domain = "domain"
setting . SSH . Port = 22
setting . Repository . UseCompatSSHURI = false
2024-11-16 16:41:44 +08:00
assert . Equal ( t , "git@domain:user/repo.git" , ComposeSSHCloneURL ( "user" , "repo" ) )
2023-12-15 14:18:12 +08:00
setting . Repository . UseCompatSSHURI = true
2024-11-16 16:41:44 +08:00
assert . Equal ( t , "ssh://git@domain/user/repo.git" , ComposeSSHCloneURL ( "user" , "repo" ) )
2023-12-15 14:18:12 +08:00
// test SSH_DOMAIN while use non-standard SSH port
setting . SSH . Port = 123
setting . Repository . UseCompatSSHURI = false
2024-11-16 16:41:44 +08:00
assert . Equal ( t , "ssh://git@domain:123/user/repo.git" , ComposeSSHCloneURL ( "user" , "repo" ) )
2023-12-15 14:18:12 +08:00
setting . Repository . UseCompatSSHURI = true
2024-11-16 16:41:44 +08:00
assert . Equal ( t , "ssh://git@domain:123/user/repo.git" , ComposeSSHCloneURL ( "user" , "repo" ) )
2023-12-15 14:18:12 +08:00
// test IPv6 SSH_DOMAIN
setting . Repository . UseCompatSSHURI = false
setting . SSH . Domain = "::1"
setting . SSH . Port = 22
2024-11-16 16:41:44 +08:00
assert . Equal ( t , "git@[::1]:user/repo.git" , ComposeSSHCloneURL ( "user" , "repo" ) )
2023-12-15 14:18:12 +08:00
setting . SSH . Port = 123
2024-11-16 16:41:44 +08:00
assert . Equal ( t , "ssh://git@[::1]:123/user/repo.git" , ComposeSSHCloneURL ( "user" , "repo" ) )
2023-12-15 14:18:12 +08:00
}