Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

api_issue_label_test.go 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package integrations
  5. import (
  6. "fmt"
  7. "net/http"
  8. "testing"
  9. "code.gitea.io/gitea/models"
  10. api "code.gitea.io/sdk/gitea"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestAPIAddIssueLabels(t *testing.T) {
  14. assert.NoError(t, models.LoadFixtures())
  15. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  16. issue := models.AssertExistsAndLoadBean(t, &models.Issue{RepoID: repo.ID}).(*models.Issue)
  17. label := models.AssertExistsAndLoadBean(t, &models.Label{RepoID: repo.ID}).(*models.Label)
  18. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  19. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/labels",
  20. owner.Name, repo.Name, issue.Index)
  21. req := NewRequestWithJSON(t, "POST", urlStr, &api.IssueLabelsOption{
  22. Labels: []int64{label.ID},
  23. })
  24. session := loginUser(t, owner.Name)
  25. resp := session.MakeRequest(t, req, http.StatusOK)
  26. var apiLabels []*api.Label
  27. DecodeJSON(t, resp, &apiLabels)
  28. assert.Len(t, apiLabels, models.GetCount(t, &models.IssueLabel{IssueID: issue.ID}))
  29. models.AssertExistsAndLoadBean(t, &models.IssueLabel{IssueID: issue.ID, LabelID: label.ID})
  30. }
  31. func TestAPIReplaceIssueLabels(t *testing.T) {
  32. assert.NoError(t, models.LoadFixtures())
  33. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  34. issue := models.AssertExistsAndLoadBean(t, &models.Issue{RepoID: repo.ID}).(*models.Issue)
  35. label := models.AssertExistsAndLoadBean(t, &models.Label{RepoID: repo.ID}).(*models.Label)
  36. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  37. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/labels",
  38. owner.Name, repo.Name, issue.Index)
  39. req := NewRequestWithJSON(t, "PUT", urlStr, &api.IssueLabelsOption{
  40. Labels: []int64{label.ID},
  41. })
  42. session := loginUser(t, owner.Name)
  43. resp := session.MakeRequest(t, req, http.StatusOK)
  44. var apiLabels []*api.Label
  45. DecodeJSON(t, resp, &apiLabels)
  46. if assert.Len(t, apiLabels, 1) {
  47. assert.EqualValues(t, label.ID, apiLabels[0].ID)
  48. }
  49. models.AssertCount(t, &models.IssueLabel{IssueID: issue.ID}, 1)
  50. models.AssertExistsAndLoadBean(t, &models.IssueLabel{IssueID: issue.ID, LabelID: label.ID})
  51. }