code.gitea.io/gitea@v1.22.3/tests/integration/api_gitignore_templates_test.go (about) 1 // Copyright 2023 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package integration 5 6 import ( 7 "fmt" 8 "net/http" 9 "testing" 10 11 "code.gitea.io/gitea/modules/options" 12 repo_module "code.gitea.io/gitea/modules/repository" 13 api "code.gitea.io/gitea/modules/structs" 14 "code.gitea.io/gitea/tests" 15 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func TestAPIListGitignoresTemplates(t *testing.T) { 20 defer tests.PrepareTestEnv(t)() 21 22 req := NewRequest(t, "GET", "/api/v1/gitignore/templates") 23 resp := MakeRequest(t, req, http.StatusOK) 24 25 // This tests if the API returns a list of strings 26 var gitignoreList []string 27 DecodeJSON(t, resp, &gitignoreList) 28 } 29 30 func TestAPIGetGitignoreTemplateInfo(t *testing.T) { 31 defer tests.PrepareTestEnv(t)() 32 33 // If Gitea has for some reason no Gitignore templates, we need to skip this test 34 if len(repo_module.Gitignores) == 0 { 35 return 36 } 37 38 // Use the first template for the test 39 templateName := repo_module.Gitignores[0] 40 41 urlStr := fmt.Sprintf("/api/v1/gitignore/templates/%s", templateName) 42 req := NewRequest(t, "GET", urlStr) 43 resp := MakeRequest(t, req, http.StatusOK) 44 45 var templateInfo api.GitignoreTemplateInfo 46 DecodeJSON(t, resp, &templateInfo) 47 48 // We get the text of the template here 49 text, _ := options.Gitignore(templateName) 50 51 assert.Equal(t, templateInfo.Name, templateName) 52 assert.Equal(t, templateInfo.Source, string(text)) 53 }