本站源代码
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

96 行
3.8KB

  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. 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. 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 TestAPICreatePullSuccess1(t *testing.T) {
  49. 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 TestAPICreatePullSuccess2(t *testing.T) {
  66. 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. session.MakeRequest(t, req, 201)
  77. }
上海开阖软件有限公司 沪ICP备12045867号-1