You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.4 KiB

  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)
  26. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  27. var apiLabels []*api.Label
  28. DecodeJSON(t, resp, &apiLabels)
  29. assert.Len(t, apiLabels, models.GetCount(t, &models.IssueLabel{IssueID: issue.ID}))
  30. models.AssertExistsAndLoadBean(t, &models.IssueLabel{IssueID: issue.ID, LabelID: label.ID})
  31. }
  32. func TestAPIReplaceIssueLabels(t *testing.T) {
  33. assert.NoError(t, models.LoadFixtures())
  34. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  35. issue := models.AssertExistsAndLoadBean(t, &models.Issue{RepoID: repo.ID}).(*models.Issue)
  36. label := models.AssertExistsAndLoadBean(t, &models.Label{RepoID: repo.ID}).(*models.Label)
  37. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  38. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/labels",
  39. owner.Name, repo.Name, issue.Index)
  40. req := NewRequestWithJSON(t, "PUT", urlStr, &api.IssueLabelsOption{
  41. Labels: []int64{label.ID},
  42. })
  43. session := loginUser(t, owner.Name)
  44. resp := session.MakeRequest(t, req)
  45. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  46. var apiLabels []*api.Label
  47. DecodeJSON(t, resp, &apiLabels)
  48. assert.Len(t, apiLabels, 1)
  49. assert.EqualValues(t, label.ID, apiLabels[0].ID)
  50. models.AssertCount(t, &models.IssueLabel{IssueID: issue.ID}, 1)
  51. models.AssertExistsAndLoadBean(t, &models.IssueLabel{IssueID: issue.ID, LabelID: label.ID})
  52. }