From 3ffedeab03a80e7345823d118042d17a3e3d9b52 Mon Sep 17 00:00:00 2001 From: Ethan Koenig Date: Sat, 24 Jun 2017 19:52:51 -0400 Subject: [PATCH] Fix bug in issue labels API (#2048) --- integrations/api_issue_label_test.go | 64 ++++++++++++++++++++++++++++ integrations/integration_test.go | 4 +- routers/api/v1/repo/issue_label.go | 4 +- 3 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 integrations/api_issue_label_test.go diff --git a/integrations/api_issue_label_test.go b/integrations/api_issue_label_test.go new file mode 100644 index 00000000000..2014d38e7d5 --- /dev/null +++ b/integrations/api_issue_label_test.go @@ -0,0 +1,64 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package integrations + +import ( + "fmt" + "net/http" + "testing" + + "code.gitea.io/gitea/models" + api "code.gitea.io/sdk/gitea" + + "github.com/stretchr/testify/assert" +) + +func TestAPIAddIssueLabels(t *testing.T) { + assert.NoError(t, models.LoadFixtures()) + + repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + issue := models.AssertExistsAndLoadBean(t, &models.Issue{RepoID: repo.ID}).(*models.Issue) + label := models.AssertExistsAndLoadBean(t, &models.Label{RepoID: repo.ID}).(*models.Label) + owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + + urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/labels", + owner.Name, repo.Name, issue.Index) + req := NewRequestWithJSON(t, "POST", urlStr, &api.IssueLabelsOption{ + Labels: []int64{label.ID}, + }) + session := loginUser(t, owner.Name) + resp := session.MakeRequest(t, req) + assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + var apiLabels []*api.Label + DecodeJSON(t, resp, &apiLabels) + assert.Len(t, apiLabels, models.GetCount(t, &models.IssueLabel{IssueID: issue.ID})) + + models.AssertExistsAndLoadBean(t, &models.IssueLabel{IssueID: issue.ID, LabelID: label.ID}) +} + +func TestAPIReplaceIssueLabels(t *testing.T) { + assert.NoError(t, models.LoadFixtures()) + + repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + issue := models.AssertExistsAndLoadBean(t, &models.Issue{RepoID: repo.ID}).(*models.Issue) + label := models.AssertExistsAndLoadBean(t, &models.Label{RepoID: repo.ID}).(*models.Label) + owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + + urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/labels", + owner.Name, repo.Name, issue.Index) + req := NewRequestWithJSON(t, "PUT", urlStr, &api.IssueLabelsOption{ + Labels: []int64{label.ID}, + }) + session := loginUser(t, owner.Name) + resp := session.MakeRequest(t, req) + assert.EqualValues(t, http.StatusOK, resp.HeaderCode) + var apiLabels []*api.Label + DecodeJSON(t, resp, &apiLabels) + assert.Len(t, apiLabels, 1) + assert.EqualValues(t, label.ID, apiLabels[0].ID) + + models.AssertCount(t, &models.IssueLabel{IssueID: issue.ID}, 1) + models.AssertExistsAndLoadBean(t, &models.IssueLabel{IssueID: issue.ID, LabelID: label.ID}) +} diff --git a/integrations/integration_test.go b/integrations/integration_test.go index e935d36f196..84b05cb644b 100644 --- a/integrations/integration_test.go +++ b/integrations/integration_test.go @@ -229,7 +229,9 @@ func NewRequestWithValues(t testing.TB, method, urlStr string, values map[string func NewRequestWithJSON(t testing.TB, method, urlStr string, v interface{}) *http.Request { jsonBytes, err := json.Marshal(v) assert.NoError(t, err) - return NewRequestWithBody(t, method, urlStr, bytes.NewBuffer(jsonBytes)) + req := NewRequestWithBody(t, method, urlStr, bytes.NewBuffer(jsonBytes)) + req.Header.Add("Content-Type", "application/json") + return req } func NewRequestWithBody(t testing.TB, method, urlStr string, body io.Reader) *http.Request { diff --git a/routers/api/v1/repo/issue_label.go b/routers/api/v1/repo/issue_label.go index d94d666714b..2eec743e702 100644 --- a/routers/api/v1/repo/issue_label.go +++ b/routers/api/v1/repo/issue_label.go @@ -66,7 +66,7 @@ func AddIssueLabels(ctx *context.APIContext, form api.IssueLabelsOption) { apiLabels := make([]*api.Label, len(labels)) for i := range labels { - apiLabels[i] = issue.Labels[i].APIFormat() + apiLabels[i] = labels[i].APIFormat() } ctx.JSON(200, &apiLabels) } @@ -142,7 +142,7 @@ func ReplaceIssueLabels(ctx *context.APIContext, form api.IssueLabelsOption) { apiLabels := make([]*api.Label, len(labels)) for i := range labels { - apiLabels[i] = issue.Labels[i].APIFormat() + apiLabels[i] = labels[i].APIFormat() } ctx.JSON(200, &apiLabels) }