gitea源码

api_gitignore_templates_test.go 1.3KB

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