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.

58 lines
2.2 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/sdk/gitea"
  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. }