gitea源码

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. "testing"
  8. "code.gitea.io/gitea/modules/options"
  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 TestAPIListLicenseTemplates(t *testing.T) {
  15. defer tests.PrepareTestEnv(t)()
  16. req := NewRequest(t, "GET", "/api/v1/licenses")
  17. resp := MakeRequest(t, req, http.StatusOK)
  18. // This tests if the API returns a list of strings
  19. var licenseList []api.LicensesTemplateListEntry
  20. DecodeJSON(t, resp, &licenseList)
  21. }
  22. func TestAPIGetLicenseTemplateInfo(t *testing.T) {
  23. defer tests.PrepareTestEnv(t)()
  24. // If Gitea has for some reason no License templates, we need to skip this test
  25. if len(repo_module.Licenses) == 0 {
  26. return
  27. }
  28. // Use the first template for the test
  29. licenseName := repo_module.Licenses[0]
  30. urlStr := "/api/v1/licenses/" + url.PathEscape(licenseName)
  31. req := NewRequest(t, "GET", urlStr)
  32. resp := MakeRequest(t, req, http.StatusOK)
  33. var licenseInfo api.LicenseTemplateInfo
  34. DecodeJSON(t, resp, &licenseInfo)
  35. // We get the text of the template here
  36. text, _ := options.License(licenseName)
  37. assert.Equal(t, licenseInfo.Key, licenseName)
  38. assert.Equal(t, licenseInfo.Name, licenseName)
  39. assert.Equal(t, licenseInfo.Body, string(text))
  40. }