code.gitea.io/gitea@v1.21.7/tests/integration/api_license_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  	"net/url"
    10  	"testing"
    11  
    12  	"code.gitea.io/gitea/modules/options"
    13  	repo_module "code.gitea.io/gitea/modules/repository"
    14  	api "code.gitea.io/gitea/modules/structs"
    15  	"code.gitea.io/gitea/tests"
    16  
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  func TestAPIListLicenseTemplates(t *testing.T) {
    21  	defer tests.PrepareTestEnv(t)()
    22  
    23  	req := NewRequest(t, "GET", "/api/v1/licenses")
    24  	resp := MakeRequest(t, req, http.StatusOK)
    25  
    26  	// This tests if the API returns a list of strings
    27  	var licenseList []api.LicensesTemplateListEntry
    28  	DecodeJSON(t, resp, &licenseList)
    29  }
    30  
    31  func TestAPIGetLicenseTemplateInfo(t *testing.T) {
    32  	defer tests.PrepareTestEnv(t)()
    33  
    34  	// If Gitea has for some reason no License templates, we need to skip this test
    35  	if len(repo_module.Licenses) == 0 {
    36  		return
    37  	}
    38  
    39  	// Use the first template for the test
    40  	licenseName := repo_module.Licenses[0]
    41  
    42  	urlStr := fmt.Sprintf("/api/v1/licenses/%s", url.PathEscape(licenseName))
    43  	req := NewRequest(t, "GET", urlStr)
    44  	resp := MakeRequest(t, req, http.StatusOK)
    45  
    46  	var licenseInfo api.LicenseTemplateInfo
    47  	DecodeJSON(t, resp, &licenseInfo)
    48  
    49  	// We get the text of the template here
    50  	text, _ := options.License(licenseName)
    51  
    52  	assert.Equal(t, licenseInfo.Key, licenseName)
    53  	assert.Equal(t, licenseInfo.Name, licenseName)
    54  	assert.Equal(t, licenseInfo.Body, string(text))
    55  }