code.gitea.io/gitea@v1.21.7/tests/integration/api_label_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  	"strings"
    11  	"testing"
    12  
    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 TestAPIListLabelTemplates(t *testing.T) {
    21  	defer tests.PrepareTestEnv(t)()
    22  
    23  	req := NewRequest(t, "GET", "/api/v1/label/templates")
    24  	resp := MakeRequest(t, req, http.StatusOK)
    25  
    26  	var templateList []string
    27  	DecodeJSON(t, resp, &templateList)
    28  
    29  	for i := range repo_module.LabelTemplateFiles {
    30  		assert.Equal(t, repo_module.LabelTemplateFiles[i].DisplayName, templateList[i])
    31  	}
    32  }
    33  
    34  func TestAPIGetLabelTemplateInfo(t *testing.T) {
    35  	defer tests.PrepareTestEnv(t)()
    36  
    37  	// If Gitea has for some reason no Label templates, we need to skip this test
    38  	if len(repo_module.LabelTemplateFiles) == 0 {
    39  		return
    40  	}
    41  
    42  	// Use the first template for the test
    43  	templateName := repo_module.LabelTemplateFiles[0].DisplayName
    44  
    45  	urlStr := fmt.Sprintf("/api/v1/label/templates/%s", url.PathEscape(templateName))
    46  	req := NewRequest(t, "GET", urlStr)
    47  	resp := MakeRequest(t, req, http.StatusOK)
    48  
    49  	var templateInfo []api.LabelTemplate
    50  	DecodeJSON(t, resp, &templateInfo)
    51  
    52  	labels, err := repo_module.LoadTemplateLabelsByDisplayName(templateName)
    53  	assert.NoError(t, err)
    54  
    55  	for i := range labels {
    56  		assert.Equal(t, strings.TrimLeft(labels[i].Color, "#"), templateInfo[i].Color)
    57  		assert.Equal(t, labels[i].Description, templateInfo[i].Description)
    58  		assert.Equal(t, labels[i].Exclusive, templateInfo[i].Exclusive)
    59  		assert.Equal(t, labels[i].Name, templateInfo[i].Name)
    60  	}
    61  }