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.

72 lines
2.7 KiB

  1. // Copyright 2020 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. "code.gitea.io/gitea/modules/structs"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestAPIIssuesMilestone(t *testing.T) {
  14. defer prepareTestEnv(t)()
  15. milestone := models.AssertExistsAndLoadBean(t, &models.Milestone{ID: 1}).(*models.Milestone)
  16. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: milestone.RepoID}).(*models.Repository)
  17. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  18. assert.Equal(t, int64(1), int64(milestone.NumIssues))
  19. assert.Equal(t, structs.StateOpen, milestone.State())
  20. session := loginUser(t, owner.Name)
  21. token := getTokenForLoggedInUser(t, session)
  22. // update values of issue
  23. milestoneState := "closed"
  24. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/milestones/%d?token=%s", owner.Name, repo.Name, milestone.ID, token)
  25. req := NewRequestWithJSON(t, "PATCH", urlStr, structs.EditMilestoneOption{
  26. State: &milestoneState,
  27. })
  28. resp := session.MakeRequest(t, req, http.StatusOK)
  29. var apiMilestone structs.Milestone
  30. DecodeJSON(t, resp, &apiMilestone)
  31. assert.EqualValues(t, "closed", apiMilestone.State)
  32. req = NewRequest(t, "GET", urlStr)
  33. resp = session.MakeRequest(t, req, http.StatusOK)
  34. var apiMilestone2 structs.Milestone
  35. DecodeJSON(t, resp, &apiMilestone2)
  36. assert.EqualValues(t, "closed", apiMilestone2.State)
  37. req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/milestones?token=%s", owner.Name, repo.Name, token), structs.CreateMilestoneOption{
  38. Title: "wow",
  39. Description: "closed one",
  40. State: "closed",
  41. })
  42. resp = session.MakeRequest(t, req, http.StatusCreated)
  43. DecodeJSON(t, resp, &apiMilestone)
  44. assert.Equal(t, "wow", apiMilestone.Title)
  45. assert.Equal(t, structs.StateClosed, apiMilestone.State)
  46. var apiMilestones []structs.Milestone
  47. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/milestones?state=%s&token=%s", owner.Name, repo.Name, "all", token))
  48. resp = session.MakeRequest(t, req, http.StatusOK)
  49. DecodeJSON(t, resp, &apiMilestones)
  50. assert.Len(t, apiMilestones, 4)
  51. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/milestones?state=%s&name=%s&token=%s", owner.Name, repo.Name, "all", "milestone2", token))
  52. resp = session.MakeRequest(t, req, http.StatusOK)
  53. DecodeJSON(t, resp, &apiMilestones)
  54. assert.Len(t, apiMilestones, 1)
  55. assert.Equal(t, int64(2), apiMilestones[0].ID)
  56. req = NewRequest(t, "DELETE", fmt.Sprintf("/api/v1/repos/%s/%s/milestones/%d?token=%s", owner.Name, repo.Name, apiMilestone.ID, token))
  57. resp = session.MakeRequest(t, req, http.StatusNoContent)
  58. }