2017-06-24 19:52:51 -04:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-06-24 19:52:51 -04:00
|
|
|
|
2022-09-02 15:18:23 -04:00
|
|
|
package integration
|
2017-06-24 19:52:51 -04:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
2020-02-09 15:33:03 +01:00
|
|
|
"strings"
|
2017-06-24 19:52:51 -04:00
|
|
|
"testing"
|
|
|
|
|
|
2023-01-17 16:46:03 -05:00
|
|
|
auth_model "code.gitea.io/gitea/models/auth"
|
2022-06-13 17:37:59 +08:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-12-10 09:27:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-17 20:34:35 +08:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 17:49:20 +08:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-05-11 18:21:34 +08:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2017-06-24 19:52:51 -04:00
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
2020-02-09 15:33:03 +01:00
|
|
|
func TestAPIModifyLabels(t *testing.T) {
|
2021-11-12 22:36:47 +08:00
|
|
|
assert.NoError(t, unittest.LoadFixtures())
|
2020-02-09 15:33:03 +01:00
|
|
|
|
2022-08-16 10:22:25 +08:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
|
|
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
|
2020-02-09 15:33:03 +01:00
|
|
|
session := loginUser(t, owner.Name)
|
2023-06-04 14:57:16 -04:00
|
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
|
2020-02-09 15:33:03 +01:00
|
|
|
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/labels?token=%s", owner.Name, repo.Name, token)
|
|
|
|
|
|
|
|
|
|
// CreateLabel
|
|
|
|
|
req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateLabelOption{
|
|
|
|
|
Name: "TestL 1",
|
|
|
|
|
Color: "abcdef",
|
|
|
|
|
Description: "test label",
|
|
|
|
|
})
|
2022-12-02 11:39:42 +08:00
|
|
|
resp := MakeRequest(t, req, http.StatusCreated)
|
2020-02-09 15:33:03 +01:00
|
|
|
apiLabel := new(api.Label)
|
|
|
|
|
DecodeJSON(t, resp, &apiLabel)
|
2022-08-16 10:22:25 +08:00
|
|
|
dbLabel := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: apiLabel.ID, RepoID: repo.ID})
|
2020-02-09 15:33:03 +01:00
|
|
|
assert.EqualValues(t, dbLabel.Name, apiLabel.Name)
|
|
|
|
|
assert.EqualValues(t, strings.TrimLeft(dbLabel.Color, "#"), apiLabel.Color)
|
|
|
|
|
|
|
|
|
|
req = NewRequestWithJSON(t, "POST", urlStr, &api.CreateLabelOption{
|
|
|
|
|
Name: "TestL 2",
|
|
|
|
|
Color: "#123456",
|
|
|
|
|
Description: "jet another test label",
|
|
|
|
|
})
|
2022-12-02 11:39:42 +08:00
|
|
|
MakeRequest(t, req, http.StatusCreated)
|
2020-02-09 15:33:03 +01:00
|
|
|
req = NewRequestWithJSON(t, "POST", urlStr, &api.CreateLabelOption{
|
|
|
|
|
Name: "WrongTestL",
|
|
|
|
|
Color: "#12345g",
|
|
|
|
|
})
|
2022-12-02 11:39:42 +08:00
|
|
|
MakeRequest(t, req, http.StatusUnprocessableEntity)
|
2020-02-09 15:33:03 +01:00
|
|
|
|
2022-01-20 18:46:10 +01:00
|
|
|
// ListLabels
|
2020-02-09 15:33:03 +01:00
|
|
|
req = NewRequest(t, "GET", urlStr)
|
2022-12-02 11:39:42 +08:00
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2020-02-09 15:33:03 +01:00
|
|
|
var apiLabels []*api.Label
|
|
|
|
|
DecodeJSON(t, resp, &apiLabels)
|
|
|
|
|
assert.Len(t, apiLabels, 2)
|
|
|
|
|
|
2022-01-20 18:46:10 +01:00
|
|
|
// GetLabel
|
2020-02-09 15:33:03 +01:00
|
|
|
singleURLStr := fmt.Sprintf("/api/v1/repos/%s/%s/labels/%d?token=%s", owner.Name, repo.Name, dbLabel.ID, token)
|
|
|
|
|
req = NewRequest(t, "GET", singleURLStr)
|
2022-12-02 11:39:42 +08:00
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2020-02-09 15:33:03 +01:00
|
|
|
DecodeJSON(t, resp, &apiLabel)
|
|
|
|
|
assert.EqualValues(t, strings.TrimLeft(dbLabel.Color, "#"), apiLabel.Color)
|
|
|
|
|
|
2022-01-20 18:46:10 +01:00
|
|
|
// EditLabel
|
2020-02-09 15:33:03 +01:00
|
|
|
newName := "LabelNewName"
|
|
|
|
|
newColor := "09876a"
|
|
|
|
|
newColorWrong := "09g76a"
|
|
|
|
|
req = NewRequestWithJSON(t, "PATCH", singleURLStr, &api.EditLabelOption{
|
|
|
|
|
Name: &newName,
|
|
|
|
|
Color: &newColor,
|
|
|
|
|
})
|
2022-12-02 11:39:42 +08:00
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2020-02-09 15:33:03 +01:00
|
|
|
DecodeJSON(t, resp, &apiLabel)
|
|
|
|
|
assert.EqualValues(t, newColor, apiLabel.Color)
|
|
|
|
|
req = NewRequestWithJSON(t, "PATCH", singleURLStr, &api.EditLabelOption{
|
|
|
|
|
Color: &newColorWrong,
|
|
|
|
|
})
|
2022-12-02 11:39:42 +08:00
|
|
|
MakeRequest(t, req, http.StatusUnprocessableEntity)
|
2020-02-09 15:33:03 +01:00
|
|
|
|
2022-01-20 18:46:10 +01:00
|
|
|
// DeleteLabel
|
2020-02-09 15:33:03 +01:00
|
|
|
req = NewRequest(t, "DELETE", singleURLStr)
|
2022-12-02 11:39:42 +08:00
|
|
|
MakeRequest(t, req, http.StatusNoContent)
|
2020-02-09 15:33:03 +01:00
|
|
|
}
|
|
|
|
|
|
2017-06-24 19:52:51 -04:00
|
|
|
func TestAPIAddIssueLabels(t *testing.T) {
|
2021-11-12 22:36:47 +08:00
|
|
|
assert.NoError(t, unittest.LoadFixtures())
|
2017-06-24 19:52:51 -04:00
|
|
|
|
2022-08-16 10:22:25 +08:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
|
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{RepoID: repo.ID})
|
|
|
|
|
_ = unittest.AssertExistsAndLoadBean(t, &issues_model.Label{RepoID: repo.ID, ID: 2})
|
|
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
|
2017-06-24 19:52:51 -04:00
|
|
|
|
2018-09-11 02:15:52 +10:00
|
|
|
session := loginUser(t, owner.Name)
|
2023-06-04 14:57:16 -04:00
|
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
|
2018-09-11 02:15:52 +10:00
|
|
|
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/labels?token=%s",
|
2021-03-12 17:45:49 +00:00
|
|
|
repo.OwnerName, repo.Name, issue.Index, token)
|
2017-06-24 19:52:51 -04:00
|
|
|
req := NewRequestWithJSON(t, "POST", urlStr, &api.IssueLabelsOption{
|
2021-03-12 17:45:49 +00:00
|
|
|
Labels: []int64{1, 2},
|
2017-06-24 19:52:51 -04:00
|
|
|
})
|
2022-12-02 11:39:42 +08:00
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
2017-06-24 19:52:51 -04:00
|
|
|
var apiLabels []*api.Label
|
|
|
|
|
DecodeJSON(t, resp, &apiLabels)
|
2022-06-13 17:37:59 +08:00
|
|
|
assert.Len(t, apiLabels, unittest.GetCount(t, &issues_model.IssueLabel{IssueID: issue.ID}))
|
2017-06-24 19:52:51 -04:00
|
|
|
|
2022-06-13 17:37:59 +08:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: issue.ID, LabelID: 2})
|
2017-06-24 19:52:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAPIReplaceIssueLabels(t *testing.T) {
|
2021-11-12 22:36:47 +08:00
|
|
|
assert.NoError(t, unittest.LoadFixtures())
|
2017-06-24 19:52:51 -04:00
|
|
|
|
2022-08-16 10:22:25 +08:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
|
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{RepoID: repo.ID})
|
|
|
|
|
label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{RepoID: repo.ID})
|
|
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
|
2017-06-24 19:52:51 -04:00
|
|
|
|
2018-09-11 02:15:52 +10:00
|
|
|
session := loginUser(t, owner.Name)
|
2023-06-04 14:57:16 -04:00
|
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
|
2018-09-11 02:15:52 +10:00
|
|
|
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/labels?token=%s",
|
|
|
|
|
owner.Name, repo.Name, issue.Index, token)
|
2017-06-24 19:52:51 -04:00
|
|
|
req := NewRequestWithJSON(t, "PUT", urlStr, &api.IssueLabelsOption{
|
|
|
|
|
Labels: []int64{label.ID},
|
|
|
|
|
})
|
2022-12-02 11:39:42 +08:00
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
2017-06-24 19:52:51 -04:00
|
|
|
var apiLabels []*api.Label
|
|
|
|
|
DecodeJSON(t, resp, &apiLabels)
|
2017-08-28 11:17:45 +02:00
|
|
|
if assert.Len(t, apiLabels, 1) {
|
|
|
|
|
assert.EqualValues(t, label.ID, apiLabels[0].ID)
|
|
|
|
|
}
|
2017-06-24 19:52:51 -04:00
|
|
|
|
2022-06-13 17:37:59 +08:00
|
|
|
unittest.AssertCount(t, &issues_model.IssueLabel{IssueID: issue.ID}, 1)
|
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: issue.ID, LabelID: label.ID})
|
2017-06-24 19:52:51 -04:00
|
|
|
}
|
2020-04-01 00:14:46 -04:00
|
|
|
|
|
|
|
|
func TestAPIModifyOrgLabels(t *testing.T) {
|
2021-11-12 22:36:47 +08:00
|
|
|
assert.NoError(t, unittest.LoadFixtures())
|
2020-04-01 00:14:46 -04:00
|
|
|
|
2022-08-16 10:22:25 +08:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
|
|
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
|
2020-04-01 00:14:46 -04:00
|
|
|
user := "user1"
|
|
|
|
|
session := loginUser(t, user)
|
2023-06-04 14:57:16 -04:00
|
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteOrganization)
|
2020-04-01 00:14:46 -04:00
|
|
|
urlStr := fmt.Sprintf("/api/v1/orgs/%s/labels?token=%s", owner.Name, token)
|
|
|
|
|
|
|
|
|
|
// CreateLabel
|
|
|
|
|
req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateLabelOption{
|
|
|
|
|
Name: "TestL 1",
|
|
|
|
|
Color: "abcdef",
|
|
|
|
|
Description: "test label",
|
|
|
|
|
})
|
2022-12-02 11:39:42 +08:00
|
|
|
resp := MakeRequest(t, req, http.StatusCreated)
|
2020-04-01 00:14:46 -04:00
|
|
|
apiLabel := new(api.Label)
|
|
|
|
|
DecodeJSON(t, resp, &apiLabel)
|
2022-08-16 10:22:25 +08:00
|
|
|
dbLabel := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: apiLabel.ID, OrgID: owner.ID})
|
2020-04-01 00:14:46 -04:00
|
|
|
assert.EqualValues(t, dbLabel.Name, apiLabel.Name)
|
|
|
|
|
assert.EqualValues(t, strings.TrimLeft(dbLabel.Color, "#"), apiLabel.Color)
|
|
|
|
|
|
|
|
|
|
req = NewRequestWithJSON(t, "POST", urlStr, &api.CreateLabelOption{
|
|
|
|
|
Name: "TestL 2",
|
|
|
|
|
Color: "#123456",
|
|
|
|
|
Description: "jet another test label",
|
|
|
|
|
})
|
2022-12-02 11:39:42 +08:00
|
|
|
MakeRequest(t, req, http.StatusCreated)
|
2020-04-01 00:14:46 -04:00
|
|
|
req = NewRequestWithJSON(t, "POST", urlStr, &api.CreateLabelOption{
|
|
|
|
|
Name: "WrongTestL",
|
|
|
|
|
Color: "#12345g",
|
|
|
|
|
})
|
2022-12-02 11:39:42 +08:00
|
|
|
MakeRequest(t, req, http.StatusUnprocessableEntity)
|
2020-04-01 00:14:46 -04:00
|
|
|
|
2022-01-20 18:46:10 +01:00
|
|
|
// ListLabels
|
2020-04-01 00:14:46 -04:00
|
|
|
req = NewRequest(t, "GET", urlStr)
|
2022-12-02 11:39:42 +08:00
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2020-04-01 00:14:46 -04:00
|
|
|
var apiLabels []*api.Label
|
|
|
|
|
DecodeJSON(t, resp, &apiLabels)
|
|
|
|
|
assert.Len(t, apiLabels, 4)
|
|
|
|
|
|
2022-01-20 18:46:10 +01:00
|
|
|
// GetLabel
|
2020-04-01 00:14:46 -04:00
|
|
|
singleURLStr := fmt.Sprintf("/api/v1/orgs/%s/labels/%d?token=%s", owner.Name, dbLabel.ID, token)
|
|
|
|
|
req = NewRequest(t, "GET", singleURLStr)
|
2022-12-02 11:39:42 +08:00
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2020-04-01 00:14:46 -04:00
|
|
|
DecodeJSON(t, resp, &apiLabel)
|
|
|
|
|
assert.EqualValues(t, strings.TrimLeft(dbLabel.Color, "#"), apiLabel.Color)
|
|
|
|
|
|
2022-01-20 18:46:10 +01:00
|
|
|
// EditLabel
|
2020-04-01 00:14:46 -04:00
|
|
|
newName := "LabelNewName"
|
|
|
|
|
newColor := "09876a"
|
|
|
|
|
newColorWrong := "09g76a"
|
|
|
|
|
req = NewRequestWithJSON(t, "PATCH", singleURLStr, &api.EditLabelOption{
|
|
|
|
|
Name: &newName,
|
|
|
|
|
Color: &newColor,
|
|
|
|
|
})
|
2022-12-02 11:39:42 +08:00
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2020-04-01 00:14:46 -04:00
|
|
|
DecodeJSON(t, resp, &apiLabel)
|
|
|
|
|
assert.EqualValues(t, newColor, apiLabel.Color)
|
|
|
|
|
req = NewRequestWithJSON(t, "PATCH", singleURLStr, &api.EditLabelOption{
|
|
|
|
|
Color: &newColorWrong,
|
|
|
|
|
})
|
2022-12-02 11:39:42 +08:00
|
|
|
MakeRequest(t, req, http.StatusUnprocessableEntity)
|
2020-04-01 00:14:46 -04:00
|
|
|
|
2022-01-20 18:46:10 +01:00
|
|
|
// DeleteLabel
|
2020-04-01 00:14:46 -04:00
|
|
|
req = NewRequest(t, "DELETE", singleURLStr)
|
2022-12-02 11:39:42 +08:00
|
|
|
MakeRequest(t, req, http.StatusNoContent)
|
2020-04-01 00:14:46 -04:00
|
|
|
}
|