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.

110 lines
4.5 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. "code.gitea.io/gitea/modules/auth"
  11. "code.gitea.io/gitea/modules/setting"
  12. api "code.gitea.io/gitea/modules/structs"
  13. issue_service "code.gitea.io/gitea/services/issue"
  14. "github.com/stretchr/testify/assert"
  15. )
  16. func TestAPIViewPulls(t *testing.T) {
  17. defer prepareTestEnv(t)()
  18. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  19. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  20. session := loginUser(t, "user2")
  21. token := getTokenForLoggedInUser(t, session)
  22. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/pulls?state=all&token="+token, owner.Name, repo.Name)
  23. resp := session.MakeRequest(t, req, http.StatusOK)
  24. var pulls []*api.PullRequest
  25. DecodeJSON(t, resp, &pulls)
  26. expectedLen := models.GetCount(t, &models.Issue{RepoID: repo.ID}, models.Cond("is_pull = ?", true))
  27. assert.Len(t, pulls, expectedLen)
  28. }
  29. // TestAPIMergePullWIP ensures that we can't merge a WIP pull request
  30. func TestAPIMergePullWIP(t *testing.T) {
  31. defer prepareTestEnv(t)()
  32. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  33. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  34. pr := models.AssertExistsAndLoadBean(t, &models.PullRequest{Status: models.PullRequestStatusMergeable}, models.Cond("has_merged = ?", false)).(*models.PullRequest)
  35. pr.LoadIssue()
  36. issue_service.ChangeTitle(pr.Issue, owner, setting.Repository.PullRequest.WorkInProgressPrefixes[0]+" "+pr.Issue.Title)
  37. // force reload
  38. pr.LoadAttributes()
  39. assert.Contains(t, pr.Issue.Title, setting.Repository.PullRequest.WorkInProgressPrefixes[0])
  40. session := loginUser(t, owner.Name)
  41. token := getTokenForLoggedInUser(t, session)
  42. req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge?token=%s", owner.Name, repo.Name, pr.Index, token), &auth.MergePullRequestForm{
  43. MergeMessageField: pr.Issue.Title,
  44. Do: string(models.MergeStyleMerge),
  45. })
  46. session.MakeRequest(t, req, http.StatusMethodNotAllowed)
  47. }
  48. func TestAPICreatePullSuccess(t *testing.T) {
  49. defer prepareTestEnv(t)()
  50. repo10 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 10}).(*models.Repository)
  51. // repo10 have code, pulls units.
  52. repo11 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 11}).(*models.Repository)
  53. // repo11 only have code unit but should still create pulls
  54. owner10 := models.AssertExistsAndLoadBean(t, &models.User{ID: repo10.OwnerID}).(*models.User)
  55. owner11 := models.AssertExistsAndLoadBean(t, &models.User{ID: repo11.OwnerID}).(*models.User)
  56. session := loginUser(t, owner11.Name)
  57. token := getTokenForLoggedInUser(t, session)
  58. req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls?token=%s", owner10.Name, repo10.Name, token), &api.CreatePullRequestOption{
  59. Head: fmt.Sprintf("%s:master", owner11.Name),
  60. Base: "master",
  61. Title: "create a failure pr",
  62. })
  63. session.MakeRequest(t, req, 201)
  64. }
  65. func TestAPIEditPull(t *testing.T) {
  66. defer prepareTestEnv(t)()
  67. repo10 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 10}).(*models.Repository)
  68. owner10 := models.AssertExistsAndLoadBean(t, &models.User{ID: repo10.OwnerID}).(*models.User)
  69. session := loginUser(t, owner10.Name)
  70. token := getTokenForLoggedInUser(t, session)
  71. req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls?token=%s", owner10.Name, repo10.Name, token), &api.CreatePullRequestOption{
  72. Head: "develop",
  73. Base: "master",
  74. Title: "create a success pr",
  75. })
  76. pull := new(api.PullRequest)
  77. resp := session.MakeRequest(t, req, 201)
  78. DecodeJSON(t, resp, pull)
  79. assert.EqualValues(t, "master", pull.Base.Name)
  80. req = NewRequestWithJSON(t, http.MethodPatch, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d?token=%s", owner10.Name, repo10.Name, pull.Index, token), &api.EditPullRequestOption{
  81. Base: "feature/1",
  82. Title: "edit a this pr",
  83. })
  84. resp = session.MakeRequest(t, req, 201)
  85. DecodeJSON(t, resp, pull)
  86. assert.EqualValues(t, "feature/1", pull.Base.Name)
  87. req = NewRequestWithJSON(t, http.MethodPatch, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d?token=%s", owner10.Name, repo10.Name, pull.Index, token), &api.EditPullRequestOption{
  88. Base: "not-exist",
  89. })
  90. session.MakeRequest(t, req, 404)
  91. }