THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

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.

66 lines
2.5KB

  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/gitea/modules/structs"
  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. session := loginUser(t, owner.Name)
  20. token := getTokenForLoggedInUser(t, session)
  21. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/labels?token=%s",
  22. owner.Name, repo.Name, issue.Index, token)
  23. req := NewRequestWithJSON(t, "POST", urlStr, &api.IssueLabelsOption{
  24. Labels: []int64{label.ID},
  25. })
  26. resp := session.MakeRequest(t, req, http.StatusOK)
  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. session := loginUser(t, owner.Name)
  39. token := getTokenForLoggedInUser(t, session)
  40. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/labels?token=%s",
  41. owner.Name, repo.Name, issue.Index, token)
  42. req := NewRequestWithJSON(t, "PUT", urlStr, &api.IssueLabelsOption{
  43. Labels: []int64{label.ID},
  44. })
  45. resp := session.MakeRequest(t, req, http.StatusOK)
  46. var apiLabels []*api.Label
  47. DecodeJSON(t, resp, &apiLabels)
  48. if assert.Len(t, apiLabels, 1) {
  49. assert.EqualValues(t, label.ID, apiLabels[0].ID)
  50. }
  51. models.AssertCount(t, &models.IssueLabel{IssueID: issue.ID}, 1)
  52. models.AssertExistsAndLoadBean(t, &models.IssueLabel{IssueID: issue.ID, LabelID: label.ID})
  53. }