選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

api_license_templates_test.go 1.4KB

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