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.

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