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.

125 lines
5.0 KiB

  1. // Copyright 2020 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. api "code.gitea.io/gitea/modules/structs"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestAPIPullReview(t *testing.T) {
  14. defer prepareTestEnv(t)()
  15. pullIssue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 3}).(*models.Issue)
  16. assert.NoError(t, pullIssue.LoadAttributes())
  17. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: pullIssue.RepoID}).(*models.Repository)
  18. // test ListPullReviews
  19. session := loginUser(t, "user2")
  20. token := getTokenForLoggedInUser(t, session)
  21. req := NewRequestf(t, http.MethodGet, "/api/v1/repos/%s/%s/pulls/%d/reviews?token=%s", repo.OwnerName, repo.Name, pullIssue.Index, token)
  22. resp := session.MakeRequest(t, req, http.StatusOK)
  23. var reviews []*api.PullReview
  24. DecodeJSON(t, resp, &reviews)
  25. if !assert.Len(t, reviews, 6) {
  26. return
  27. }
  28. for _, r := range reviews {
  29. assert.EqualValues(t, pullIssue.HTMLURL(), r.HTMLPullURL)
  30. }
  31. assert.EqualValues(t, 8, reviews[3].ID)
  32. assert.EqualValues(t, "APPROVED", reviews[3].State)
  33. assert.EqualValues(t, 0, reviews[3].CodeCommentsCount)
  34. assert.EqualValues(t, true, reviews[3].Stale)
  35. assert.EqualValues(t, false, reviews[3].Official)
  36. assert.EqualValues(t, 10, reviews[5].ID)
  37. assert.EqualValues(t, "REQUEST_CHANGES", reviews[5].State)
  38. assert.EqualValues(t, 1, reviews[5].CodeCommentsCount)
  39. assert.EqualValues(t, 0, reviews[5].Reviewer.ID) // ghost user
  40. assert.EqualValues(t, false, reviews[5].Stale)
  41. assert.EqualValues(t, true, reviews[5].Official)
  42. // test GetPullReview
  43. req = NewRequestf(t, http.MethodGet, "/api/v1/repos/%s/%s/pulls/%d/reviews/%d?token=%s", repo.OwnerName, repo.Name, pullIssue.Index, reviews[3].ID, token)
  44. resp = session.MakeRequest(t, req, http.StatusOK)
  45. var review api.PullReview
  46. DecodeJSON(t, resp, &review)
  47. assert.EqualValues(t, *reviews[3], review)
  48. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/pulls/%d/reviews/%d?token=%s", repo.OwnerName, repo.Name, pullIssue.Index, reviews[5].ID, token)
  49. resp = session.MakeRequest(t, req, http.StatusOK)
  50. DecodeJSON(t, resp, &review)
  51. assert.EqualValues(t, *reviews[5], review)
  52. // test GetPullReviewComments
  53. comment := models.AssertExistsAndLoadBean(t, &models.Comment{ID: 7}).(*models.Comment)
  54. req = NewRequestf(t, http.MethodGet, "/api/v1/repos/%s/%s/pulls/%d/reviews/%d/comments?token=%s", repo.OwnerName, repo.Name, pullIssue.Index, 10, token)
  55. resp = session.MakeRequest(t, req, http.StatusOK)
  56. var reviewComments []*api.PullReviewComment
  57. DecodeJSON(t, resp, &reviewComments)
  58. assert.Len(t, reviewComments, 1)
  59. assert.EqualValues(t, "Ghost", reviewComments[0].Reviewer.UserName)
  60. assert.EqualValues(t, "a review from a deleted user", reviewComments[0].Body)
  61. assert.EqualValues(t, comment.ID, reviewComments[0].ID)
  62. assert.EqualValues(t, comment.UpdatedUnix, reviewComments[0].Updated.Unix())
  63. assert.EqualValues(t, comment.HTMLURL(), reviewComments[0].HTMLURL)
  64. // test CreatePullReview
  65. req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/reviews?token=%s", repo.OwnerName, repo.Name, pullIssue.Index, token), &api.CreatePullReviewOptions{
  66. Body: "body1",
  67. // Event: "" # will result in PENDING
  68. Comments: []api.CreatePullReviewComment{{
  69. Path: "README.md",
  70. Body: "first new line",
  71. OldLineNum: 0,
  72. NewLineNum: 1,
  73. }, {
  74. Path: "README.md",
  75. Body: "first old line",
  76. OldLineNum: 1,
  77. NewLineNum: 0,
  78. }, {
  79. Path: "iso-8859-1.txt",
  80. Body: "this line contains a non-utf-8 character",
  81. OldLineNum: 0,
  82. NewLineNum: 1,
  83. },
  84. },
  85. })
  86. resp = session.MakeRequest(t, req, http.StatusOK)
  87. DecodeJSON(t, resp, &review)
  88. assert.EqualValues(t, 6, review.ID)
  89. assert.EqualValues(t, "PENDING", review.State)
  90. assert.EqualValues(t, 3, review.CodeCommentsCount)
  91. // test SubmitPullReview
  92. req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/reviews/%d?token=%s", repo.OwnerName, repo.Name, pullIssue.Index, review.ID, token), &api.SubmitPullReviewOptions{
  93. Event: "APPROVED",
  94. Body: "just two nits",
  95. })
  96. resp = session.MakeRequest(t, req, http.StatusOK)
  97. DecodeJSON(t, resp, &review)
  98. assert.EqualValues(t, 6, review.ID)
  99. assert.EqualValues(t, "APPROVED", review.State)
  100. assert.EqualValues(t, 3, review.CodeCommentsCount)
  101. // test DeletePullReview
  102. req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/reviews?token=%s", repo.OwnerName, repo.Name, pullIssue.Index, token), &api.CreatePullReviewOptions{
  103. Body: "just a comment",
  104. Event: "COMMENT",
  105. })
  106. resp = session.MakeRequest(t, req, http.StatusOK)
  107. DecodeJSON(t, resp, &review)
  108. assert.EqualValues(t, "COMMENT", review.State)
  109. assert.EqualValues(t, 0, review.CodeCommentsCount)
  110. req = NewRequestf(t, http.MethodDelete, "/api/v1/repos/%s/%s/pulls/%d/reviews/%d?token=%s", repo.OwnerName, repo.Name, pullIssue.Index, review.ID, token)
  111. resp = session.MakeRequest(t, req, http.StatusNoContent)
  112. }