code.gitea.io/gitea@v1.21.7/tests/integration/api_issue_label_test.go (about)

     1  // Copyright 2017 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package integration
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  	"strings"
    10  	"testing"
    11  
    12  	auth_model "code.gitea.io/gitea/models/auth"
    13  	issues_model "code.gitea.io/gitea/models/issues"
    14  	repo_model "code.gitea.io/gitea/models/repo"
    15  	"code.gitea.io/gitea/models/unittest"
    16  	user_model "code.gitea.io/gitea/models/user"
    17  	api "code.gitea.io/gitea/modules/structs"
    18  
    19  	"github.com/stretchr/testify/assert"
    20  )
    21  
    22  func TestAPIModifyLabels(t *testing.T) {
    23  	assert.NoError(t, unittest.LoadFixtures())
    24  
    25  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
    26  	owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
    27  	session := loginUser(t, owner.Name)
    28  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
    29  	urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/labels?token=%s", owner.Name, repo.Name, token)
    30  
    31  	// CreateLabel
    32  	req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateLabelOption{
    33  		Name:        "TestL 1",
    34  		Color:       "abcdef",
    35  		Description: "test label",
    36  	})
    37  	resp := MakeRequest(t, req, http.StatusCreated)
    38  	apiLabel := new(api.Label)
    39  	DecodeJSON(t, resp, &apiLabel)
    40  	dbLabel := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: apiLabel.ID, RepoID: repo.ID})
    41  	assert.EqualValues(t, dbLabel.Name, apiLabel.Name)
    42  	assert.EqualValues(t, strings.TrimLeft(dbLabel.Color, "#"), apiLabel.Color)
    43  
    44  	req = NewRequestWithJSON(t, "POST", urlStr, &api.CreateLabelOption{
    45  		Name:        "TestL 2",
    46  		Color:       "#123456",
    47  		Description: "jet another test label",
    48  	})
    49  	MakeRequest(t, req, http.StatusCreated)
    50  	req = NewRequestWithJSON(t, "POST", urlStr, &api.CreateLabelOption{
    51  		Name:  "WrongTestL",
    52  		Color: "#12345g",
    53  	})
    54  	MakeRequest(t, req, http.StatusUnprocessableEntity)
    55  
    56  	// ListLabels
    57  	req = NewRequest(t, "GET", urlStr)
    58  	resp = MakeRequest(t, req, http.StatusOK)
    59  	var apiLabels []*api.Label
    60  	DecodeJSON(t, resp, &apiLabels)
    61  	assert.Len(t, apiLabels, 2)
    62  
    63  	// GetLabel
    64  	singleURLStr := fmt.Sprintf("/api/v1/repos/%s/%s/labels/%d?token=%s", owner.Name, repo.Name, dbLabel.ID, token)
    65  	req = NewRequest(t, "GET", singleURLStr)
    66  	resp = MakeRequest(t, req, http.StatusOK)
    67  	DecodeJSON(t, resp, &apiLabel)
    68  	assert.EqualValues(t, strings.TrimLeft(dbLabel.Color, "#"), apiLabel.Color)
    69  
    70  	// EditLabel
    71  	newName := "LabelNewName"
    72  	newColor := "09876a"
    73  	newColorWrong := "09g76a"
    74  	req = NewRequestWithJSON(t, "PATCH", singleURLStr, &api.EditLabelOption{
    75  		Name:  &newName,
    76  		Color: &newColor,
    77  	})
    78  	resp = MakeRequest(t, req, http.StatusOK)
    79  	DecodeJSON(t, resp, &apiLabel)
    80  	assert.EqualValues(t, newColor, apiLabel.Color)
    81  	req = NewRequestWithJSON(t, "PATCH", singleURLStr, &api.EditLabelOption{
    82  		Color: &newColorWrong,
    83  	})
    84  	MakeRequest(t, req, http.StatusUnprocessableEntity)
    85  
    86  	// DeleteLabel
    87  	req = NewRequest(t, "DELETE", singleURLStr)
    88  	MakeRequest(t, req, http.StatusNoContent)
    89  }
    90  
    91  func TestAPIAddIssueLabels(t *testing.T) {
    92  	assert.NoError(t, unittest.LoadFixtures())
    93  
    94  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
    95  	issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{RepoID: repo.ID})
    96  	_ = unittest.AssertExistsAndLoadBean(t, &issues_model.Label{RepoID: repo.ID, ID: 2})
    97  	owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
    98  
    99  	session := loginUser(t, owner.Name)
   100  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
   101  	urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/labels?token=%s",
   102  		repo.OwnerName, repo.Name, issue.Index, token)
   103  	req := NewRequestWithJSON(t, "POST", urlStr, &api.IssueLabelsOption{
   104  		Labels: []int64{1, 2},
   105  	})
   106  	resp := MakeRequest(t, req, http.StatusOK)
   107  	var apiLabels []*api.Label
   108  	DecodeJSON(t, resp, &apiLabels)
   109  	assert.Len(t, apiLabels, unittest.GetCount(t, &issues_model.IssueLabel{IssueID: issue.ID}))
   110  
   111  	unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: issue.ID, LabelID: 2})
   112  }
   113  
   114  func TestAPIReplaceIssueLabels(t *testing.T) {
   115  	assert.NoError(t, unittest.LoadFixtures())
   116  
   117  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
   118  	issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{RepoID: repo.ID})
   119  	label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{RepoID: repo.ID})
   120  	owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
   121  
   122  	session := loginUser(t, owner.Name)
   123  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
   124  	urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/labels?token=%s",
   125  		owner.Name, repo.Name, issue.Index, token)
   126  	req := NewRequestWithJSON(t, "PUT", urlStr, &api.IssueLabelsOption{
   127  		Labels: []int64{label.ID},
   128  	})
   129  	resp := MakeRequest(t, req, http.StatusOK)
   130  	var apiLabels []*api.Label
   131  	DecodeJSON(t, resp, &apiLabels)
   132  	if assert.Len(t, apiLabels, 1) {
   133  		assert.EqualValues(t, label.ID, apiLabels[0].ID)
   134  	}
   135  
   136  	unittest.AssertCount(t, &issues_model.IssueLabel{IssueID: issue.ID}, 1)
   137  	unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: issue.ID, LabelID: label.ID})
   138  }
   139  
   140  func TestAPIModifyOrgLabels(t *testing.T) {
   141  	assert.NoError(t, unittest.LoadFixtures())
   142  
   143  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
   144  	owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
   145  	user := "user1"
   146  	session := loginUser(t, user)
   147  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteOrganization)
   148  	urlStr := fmt.Sprintf("/api/v1/orgs/%s/labels?token=%s", owner.Name, token)
   149  
   150  	// CreateLabel
   151  	req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateLabelOption{
   152  		Name:        "TestL 1",
   153  		Color:       "abcdef",
   154  		Description: "test label",
   155  	})
   156  	resp := MakeRequest(t, req, http.StatusCreated)
   157  	apiLabel := new(api.Label)
   158  	DecodeJSON(t, resp, &apiLabel)
   159  	dbLabel := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: apiLabel.ID, OrgID: owner.ID})
   160  	assert.EqualValues(t, dbLabel.Name, apiLabel.Name)
   161  	assert.EqualValues(t, strings.TrimLeft(dbLabel.Color, "#"), apiLabel.Color)
   162  
   163  	req = NewRequestWithJSON(t, "POST", urlStr, &api.CreateLabelOption{
   164  		Name:        "TestL 2",
   165  		Color:       "#123456",
   166  		Description: "jet another test label",
   167  	})
   168  	MakeRequest(t, req, http.StatusCreated)
   169  	req = NewRequestWithJSON(t, "POST", urlStr, &api.CreateLabelOption{
   170  		Name:  "WrongTestL",
   171  		Color: "#12345g",
   172  	})
   173  	MakeRequest(t, req, http.StatusUnprocessableEntity)
   174  
   175  	// ListLabels
   176  	req = NewRequest(t, "GET", urlStr)
   177  	resp = MakeRequest(t, req, http.StatusOK)
   178  	var apiLabels []*api.Label
   179  	DecodeJSON(t, resp, &apiLabels)
   180  	assert.Len(t, apiLabels, 4)
   181  
   182  	// GetLabel
   183  	singleURLStr := fmt.Sprintf("/api/v1/orgs/%s/labels/%d?token=%s", owner.Name, dbLabel.ID, token)
   184  	req = NewRequest(t, "GET", singleURLStr)
   185  	resp = MakeRequest(t, req, http.StatusOK)
   186  	DecodeJSON(t, resp, &apiLabel)
   187  	assert.EqualValues(t, strings.TrimLeft(dbLabel.Color, "#"), apiLabel.Color)
   188  
   189  	// EditLabel
   190  	newName := "LabelNewName"
   191  	newColor := "09876a"
   192  	newColorWrong := "09g76a"
   193  	req = NewRequestWithJSON(t, "PATCH", singleURLStr, &api.EditLabelOption{
   194  		Name:  &newName,
   195  		Color: &newColor,
   196  	})
   197  	resp = MakeRequest(t, req, http.StatusOK)
   198  	DecodeJSON(t, resp, &apiLabel)
   199  	assert.EqualValues(t, newColor, apiLabel.Color)
   200  	req = NewRequestWithJSON(t, "PATCH", singleURLStr, &api.EditLabelOption{
   201  		Color: &newColorWrong,
   202  	})
   203  	MakeRequest(t, req, http.StatusUnprocessableEntity)
   204  
   205  	// DeleteLabel
   206  	req = NewRequest(t, "DELETE", singleURLStr)
   207  	MakeRequest(t, req, http.StatusNoContent)
   208  }