2023-03-28 21:22:07 +03:00
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package integration
import (
"fmt"
"net/http"
"testing"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/tests"
"github.com/stretchr/testify/assert"
2023-09-28 15:16:40 +03:00
"gopkg.in/yaml.v3"
2023-03-28 21:22:07 +03:00
)
2023-09-28 15:16:40 +03:00
func createIssueConfig ( t * testing . T , user * user_model . User , repo * repo_model . Repository , issueConfig map [ string ] any ) {
config , err := yaml . Marshal ( issueConfig )
assert . NoError ( t , err )
2023-03-28 21:22:07 +03:00
2023-09-28 15:16:40 +03:00
err = createOrReplaceFileInBranch ( user , repo , ".gitea/ISSUE_TEMPLATE/config.yaml" , repo . DefaultBranch , string ( config ) )
assert . NoError ( t , err )
}
2023-03-28 21:22:07 +03:00
2023-09-28 15:16:40 +03:00
func getIssueConfig ( t * testing . T , owner , repo string ) api . IssueConfig {
urlStr := fmt . Sprintf ( "/api/v1/repos/%s/%s/issue_config" , owner , repo )
2023-03-28 21:22:07 +03:00
req := NewRequest ( t , "GET" , urlStr )
resp := MakeRequest ( t , req , http . StatusOK )
var issueConfig api . IssueConfig
DecodeJSON ( t , resp , & issueConfig )
2023-09-28 15:16:40 +03:00
return issueConfig
}
func TestAPIRepoGetIssueConfig ( t * testing . T ) {
defer tests . PrepareTestEnv ( t ) ( )
repo := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 49 } )
owner := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : repo . OwnerID } )
t . Run ( "Default" , func ( t * testing . T ) {
issueConfig := getIssueConfig ( t , owner . Name , repo . Name )
assert . True ( t , issueConfig . BlankIssuesEnabled )
assert . Len ( t , issueConfig . ContactLinks , 0 )
} )
t . Run ( "DisableBlankIssues" , func ( t * testing . T ) {
config := make ( map [ string ] any )
config [ "blank_issues_enabled" ] = false
createIssueConfig ( t , owner , repo , config )
issueConfig := getIssueConfig ( t , owner . Name , repo . Name )
assert . False ( t , issueConfig . BlankIssuesEnabled )
assert . Len ( t , issueConfig . ContactLinks , 0 )
} )
t . Run ( "ContactLinks" , func ( t * testing . T ) {
contactLink := make ( map [ string ] string )
contactLink [ "name" ] = "TestName"
contactLink [ "url" ] = "https://example.com"
contactLink [ "about" ] = "TestAbout"
config := make ( map [ string ] any )
config [ "contact_links" ] = [ ] map [ string ] string { contactLink }
createIssueConfig ( t , owner , repo , config )
issueConfig := getIssueConfig ( t , owner . Name , repo . Name )
assert . True ( t , issueConfig . BlankIssuesEnabled )
assert . Len ( t , issueConfig . ContactLinks , 1 )
assert . Equal ( t , "TestName" , issueConfig . ContactLinks [ 0 ] . Name )
assert . Equal ( t , "https://example.com" , issueConfig . ContactLinks [ 0 ] . URL )
assert . Equal ( t , "TestAbout" , issueConfig . ContactLinks [ 0 ] . About )
} )
t . Run ( "Full" , func ( t * testing . T ) {
contactLink := make ( map [ string ] string )
contactLink [ "name" ] = "TestName"
contactLink [ "url" ] = "https://example.com"
contactLink [ "about" ] = "TestAbout"
config := make ( map [ string ] any )
config [ "blank_issues_enabled" ] = false
config [ "contact_links" ] = [ ] map [ string ] string { contactLink }
createIssueConfig ( t , owner , repo , config )
issueConfig := getIssueConfig ( t , owner . Name , repo . Name )
assert . False ( t , issueConfig . BlankIssuesEnabled )
assert . Len ( t , issueConfig . ContactLinks , 1 )
assert . Equal ( t , "TestName" , issueConfig . ContactLinks [ 0 ] . Name )
assert . Equal ( t , "https://example.com" , issueConfig . ContactLinks [ 0 ] . URL )
assert . Equal ( t , "TestAbout" , issueConfig . ContactLinks [ 0 ] . About )
} )
2023-03-28 21:22:07 +03:00
}
2023-09-28 15:16:40 +03:00
func TestAPIRepoIssueConfigPaths ( t * testing . T ) {
2023-03-28 21:22:07 +03:00
defer tests . PrepareTestEnv ( t ) ( )
2023-09-28 15:16:40 +03:00
repo := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 49 } )
owner := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : repo . OwnerID } )
templateConfigCandidates := [ ] string {
".gitea/ISSUE_TEMPLATE/config" ,
".gitea/issue_template/config" ,
".github/ISSUE_TEMPLATE/config" ,
".github/issue_template/config" ,
}
2024-04-27 11:03:49 +03:00
for _ , candidate := range templateConfigCandidates {
2023-09-28 15:16:40 +03:00
for _ , extension := range [ ] string { ".yaml" , ".yml" } {
2024-04-27 11:03:49 +03:00
fullPath := candidate + extension
2023-09-28 15:16:40 +03:00
t . Run ( fullPath , func ( t * testing . T ) {
configMap := make ( map [ string ] any )
configMap [ "blank_issues_enabled" ] = false
configData , err := yaml . Marshal ( configMap )
assert . NoError ( t , err )
_ , err = createFileInBranch ( owner , repo , fullPath , repo . DefaultBranch , string ( configData ) )
assert . NoError ( t , err )
issueConfig := getIssueConfig ( t , owner . Name , repo . Name )
assert . False ( t , issueConfig . BlankIssuesEnabled )
assert . Len ( t , issueConfig . ContactLinks , 0 )
_ , err = deleteFileInBranch ( owner , repo , fullPath , repo . DefaultBranch )
assert . NoError ( t , err )
} )
}
}
}
func TestAPIRepoValidateIssueConfig ( t * testing . T ) {
defer tests . PrepareTestEnv ( t ) ( )
repo := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 49 } )
2023-03-28 21:22:07 +03:00
owner := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : repo . OwnerID } )
urlStr := fmt . Sprintf ( "/api/v1/repos/%s/%s/issue_config/validate" , owner . Name , repo . Name )
2023-09-28 15:16:40 +03:00
t . Run ( "Valid" , func ( t * testing . T ) {
req := NewRequest ( t , "GET" , urlStr )
resp := MakeRequest ( t , req , http . StatusOK )
var issueConfigValidation api . IssueConfigValidation
DecodeJSON ( t , resp , & issueConfigValidation )
assert . True ( t , issueConfigValidation . Valid )
assert . Empty ( t , issueConfigValidation . Message )
} )
t . Run ( "Invalid" , func ( t * testing . T ) {
config := make ( map [ string ] any )
config [ "blank_issues_enabled" ] = "Test"
createIssueConfig ( t , owner , repo , config )
req := NewRequest ( t , "GET" , urlStr )
resp := MakeRequest ( t , req , http . StatusOK )
var issueConfigValidation api . IssueConfigValidation
DecodeJSON ( t , resp , & issueConfigValidation )
2023-03-28 21:22:07 +03:00
2023-09-28 15:16:40 +03:00
assert . False ( t , issueConfigValidation . Valid )
assert . NotEmpty ( t , issueConfigValidation . Message )
} )
2023-03-28 21:22:07 +03:00
}