gitea源码

api_label_templates_test.go 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "net/http"
  6. "net/url"
  7. "strings"
  8. "testing"
  9. repo_module "code.gitea.io/gitea/modules/repository"
  10. api "code.gitea.io/gitea/modules/structs"
  11. "code.gitea.io/gitea/tests"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func TestAPIListLabelTemplates(t *testing.T) {
  15. defer tests.PrepareTestEnv(t)()
  16. req := NewRequest(t, "GET", "/api/v1/label/templates")
  17. resp := MakeRequest(t, req, http.StatusOK)
  18. var templateList []string
  19. DecodeJSON(t, resp, &templateList)
  20. for i := range repo_module.LabelTemplateFiles {
  21. assert.Equal(t, repo_module.LabelTemplateFiles[i].DisplayName, templateList[i])
  22. }
  23. }
  24. func TestAPIGetLabelTemplateInfo(t *testing.T) {
  25. defer tests.PrepareTestEnv(t)()
  26. // If Gitea has for some reason no Label templates, we need to skip this test
  27. if len(repo_module.LabelTemplateFiles) == 0 {
  28. return
  29. }
  30. // Use the first template for the test
  31. templateName := repo_module.LabelTemplateFiles[0].DisplayName
  32. urlStr := "/api/v1/label/templates/" + url.PathEscape(templateName)
  33. req := NewRequest(t, "GET", urlStr)
  34. resp := MakeRequest(t, req, http.StatusOK)
  35. var templateInfo []api.LabelTemplate
  36. DecodeJSON(t, resp, &templateInfo)
  37. labels, err := repo_module.LoadTemplateLabelsByDisplayName(templateName)
  38. assert.NoError(t, err)
  39. for i := range labels {
  40. assert.Equal(t, strings.TrimLeft(labels[i].Color, "#"), templateInfo[i].Color)
  41. assert.Equal(t, labels[i].Description, templateInfo[i].Description)
  42. assert.Equal(t, labels[i].Exclusive, templateInfo[i].Exclusive)
  43. assert.Equal(t, labels[i].Name, templateInfo[i].Name)
  44. }
  45. }