You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

api_pull_test.go 4.6 kB

Move macaron to chi (#14293) Use [chi](https://github.com/go-chi/chi) instead of the forked [macaron](https://gitea.com/macaron/macaron). Since macaron and chi have conflicts with session share, this big PR becomes a have-to thing. According my previous idea, we can replace macaron step by step but I'm wrong. :( Below is a list of big changes on this PR. - [x] Define `context.ResponseWriter` interface with an implementation `context.Response`. - [x] Use chi instead of macaron, and also a customize `Route` to wrap chi so that the router usage is similar as before. - [x] Create different routers for `web`, `api`, `internal` and `install` so that the codes will be more clear and no magic . - [x] Use https://github.com/unrolled/render instead of macaron's internal render - [x] Use https://github.com/NYTimes/gziphandler instead of https://gitea.com/macaron/gzip - [x] Use https://gitea.com/go-chi/session which is a modified version of https://gitea.com/macaron/session and removed `nodb` support since it will not be maintained. **BREAK** - [x] Use https://gitea.com/go-chi/captcha which is a modified version of https://gitea.com/macaron/captcha - [x] Use https://gitea.com/go-chi/cache which is a modified version of https://gitea.com/macaron/cache - [x] Use https://gitea.com/go-chi/binding which is a modified version of https://gitea.com/macaron/binding - [x] Use https://github.com/go-chi/cors instead of https://gitea.com/macaron/cors - [x] Dropped https://gitea.com/macaron/i18n and make a new one in `code.gitea.io/gitea/modules/translation` - [x] Move validation form structs from `code.gitea.io/gitea/modules/auth` to `code.gitea.io/gitea/modules/forms` to avoid dependency cycle. - [x] Removed macaron log service because it's not need any more. **BREAK** - [x] All form structs have to be get by `web.GetForm(ctx)` in the route function but not as a function parameter on routes definition. - [x] Move Git HTTP protocol implementation to use routers directly. - [x] Fix the problem that chi routes don't support trailing slash but macaron did. - [x] `/api/v1/swagger` now will be redirect to `/api/swagger` but not render directly so that `APIContext` will not create a html render. Notices: - Chi router don't support request with trailing slash - Integration test `TestUserHeatmap` maybe mysql version related. It's failed on my macOS(mysql 5.7.29 installed via brew) but succeed on CI. Co-authored-by: 6543 <6543@obermui.de>
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. auth "code.gitea.io/gitea/modules/forms"
  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. }