@ -7,16 +7,18 @@ import (
"fmt"
"fmt"
"net/http"
"net/http"
"net/http/httptest"
"net/http/httptest"
"strings"
"testing"
"testing"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/tests"
"code.gitea.io/gitea/tests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
)
)
func testRepoGenerate ( t * testing . T , session * TestSession , templateOwnerName , templateRepoName , generateOwnerName , generateRepoName string ) * httptest . ResponseRecorder {
func testRepoGenerate ( t * testing . T , session * TestSession , templateID , template OwnerName , templateRepoName , generateOwnerName , generateRepoName string ) * httptest . ResponseRecorder {
generateOwner := unittest . AssertExistsAndLoadBean ( t , & user_model . User { Name : generateOwnerName } )
generateOwner := unittest . AssertExistsAndLoadBean ( t , & user_model . User { Name : generateOwnerName } )
// Step0: check the existence of the generated repo
// Step0: check the existence of the generated repo
@ -41,16 +43,38 @@ func testRepoGenerate(t *testing.T, session *TestSession, templateOwnerName, tem
_ , exists = htmlDoc . doc . Find ( fmt . Sprintf ( ".owner.dropdown .item[data-value=\"%d\"]" , generateOwner . ID ) ) . Attr ( "data-value" )
_ , exists = htmlDoc . doc . Find ( fmt . Sprintf ( ".owner.dropdown .item[data-value=\"%d\"]" , generateOwner . ID ) ) . Attr ( "data-value" )
assert . True ( t , exists , fmt . Sprintf ( "Generate owner '%s' is not present in select box" , generateOwnerName ) )
assert . True ( t , exists , fmt . Sprintf ( "Generate owner '%s' is not present in select box" , generateOwnerName ) )
req = NewRequestWithValues ( t , "POST" , link , map [ string ] string {
req = NewRequestWithValues ( t , "POST" , link , map [ string ] string {
"_csrf" : htmlDoc . GetCSRF ( ) ,
"_csrf" : htmlDoc . GetCSRF ( ) ,
"uid" : fmt . Sprintf ( "%d" , generateOwner . ID ) ,
"uid" : fmt . Sprintf ( "%d" , generateOwner . ID ) ,
"repo_name" : generateRepoName ,
"repo_name" : generateRepoName ,
"git_content" : "true" ,
"repo_template" : templateID ,
"git_content" : "true" ,
} )
} )
session . MakeRequest ( t , req , http . StatusSeeOther )
session . MakeRequest ( t , req , http . StatusSeeOther )
// Step4: check the existence of the generated repo
// Step4: check the existence of the generated repo
req = NewRequestf ( t , "GET" , "/%s/%s" , generateOwnerName , generateRepoName )
req = NewRequestf ( t , "GET" , "/%s/%s" , generateOwnerName , generateRepoName )
session . MakeRequest ( t , req , http . StatusOK )
// Step5: check substituted values in Readme
req = NewRequestf ( t , "GET" , "/%s/%s/raw/branch/master/README.md" , generateOwnerName , generateRepoName )
resp = session . MakeRequest ( t , req , http . StatusOK )
body := fmt . Sprintf ( ` # % s Readme
Owner : % s
Link : / % s / % s
Clone URL : % s % s / % s . git ` ,
generateRepoName ,
strings . ToUpper ( generateOwnerName ) ,
generateOwnerName ,
generateRepoName ,
setting . AppURL ,
generateOwnerName ,
generateRepoName )
assert . Equal ( t , body , resp . Body . String ( ) )
// Step6: check substituted values in substituted file path ${REPO_NAME}
req = NewRequestf ( t , "GET" , "/%s/%s/raw/branch/master/%s.log" , generateOwnerName , generateRepoName , generateRepoName )
resp = session . MakeRequest ( t , req , http . StatusOK )
resp = session . MakeRequest ( t , req , http . StatusOK )
assert . Equal ( t , generateRepoName , resp . Body . String ( ) )
return resp
return resp
}
}
@ -58,11 +82,11 @@ func testRepoGenerate(t *testing.T, session *TestSession, templateOwnerName, tem
func TestRepoGenerate ( t * testing . T ) {
func TestRepoGenerate ( t * testing . T ) {
defer tests . PrepareTestEnv ( t ) ( )
defer tests . PrepareTestEnv ( t ) ( )
session := loginUser ( t , "user1" )
session := loginUser ( t , "user1" )
testRepoGenerate ( t , session , "user27" , "template1" , "user1" , "generated1" )
testRepoGenerate ( t , session , "44" , " user27" , "template1" , "user1" , "generated1" )
}
}
func TestRepoGenerateToOrg ( t * testing . T ) {
func TestRepoGenerateToOrg ( t * testing . T ) {
defer tests . PrepareTestEnv ( t ) ( )
defer tests . PrepareTestEnv ( t ) ( )
session := loginUser ( t , "user2" )
session := loginUser ( t , "user2" )
testRepoGenerate ( t , session , "user27" , "template1" , "user2" , "generated2" )
testRepoGenerate ( t , session , "44" , " user27" , "template1" , "user2" , "generated2" )
}
}