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.

128 lines
5.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. api "code.gitea.io/sdk/gitea"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestAPIListRepoComments(t *testing.T) {
  14. prepareTestEnv(t)
  15. comment := models.AssertExistsAndLoadBean(t, &models.Comment{},
  16. models.Cond("type = ?", models.CommentTypeComment)).(*models.Comment)
  17. issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: comment.IssueID}).(*models.Issue)
  18. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository)
  19. repoOwner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  20. session := loginUser(t, repoOwner.Name)
  21. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/comments",
  22. repoOwner.Name, repo.Name)
  23. resp := session.MakeRequest(t, req, http.StatusOK)
  24. var apiComments []*api.Comment
  25. DecodeJSON(t, resp, &apiComments)
  26. for _, apiComment := range apiComments {
  27. c := &models.Comment{ID: apiComment.ID}
  28. models.AssertExistsAndLoadBean(t, c,
  29. models.Cond("type = ?", models.CommentTypeComment))
  30. models.AssertExistsAndLoadBean(t, &models.Issue{ID: c.IssueID, RepoID: repo.ID})
  31. }
  32. }
  33. func TestAPIListIssueComments(t *testing.T) {
  34. prepareTestEnv(t)
  35. comment := models.AssertExistsAndLoadBean(t, &models.Comment{},
  36. models.Cond("type = ?", models.CommentTypeComment)).(*models.Comment)
  37. issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: comment.IssueID}).(*models.Issue)
  38. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository)
  39. repoOwner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  40. session := loginUser(t, repoOwner.Name)
  41. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/%d/comments",
  42. repoOwner.Name, repo.Name, issue.Index)
  43. resp := session.MakeRequest(t, req, http.StatusOK)
  44. var comments []*api.Comment
  45. DecodeJSON(t, resp, &comments)
  46. expectedCount := models.GetCount(t, &models.Comment{IssueID: issue.ID},
  47. models.Cond("type = ?", models.CommentTypeComment))
  48. assert.EqualValues(t, expectedCount, len(comments))
  49. }
  50. func TestAPICreateComment(t *testing.T) {
  51. prepareTestEnv(t)
  52. const commentBody = "Comment body"
  53. issue := models.AssertExistsAndLoadBean(t, &models.Issue{}).(*models.Issue)
  54. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository)
  55. repoOwner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  56. session := loginUser(t, repoOwner.Name)
  57. token := getTokenForLoggedInUser(t, session)
  58. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/comments?token=%s",
  59. repoOwner.Name, repo.Name, issue.Index, token)
  60. req := NewRequestWithValues(t, "POST", urlStr, map[string]string{
  61. "body": commentBody,
  62. })
  63. resp := session.MakeRequest(t, req, http.StatusCreated)
  64. var updatedComment api.Comment
  65. DecodeJSON(t, resp, &updatedComment)
  66. assert.EqualValues(t, commentBody, updatedComment.Body)
  67. models.AssertExistsAndLoadBean(t, &models.Comment{ID: updatedComment.ID, IssueID: issue.ID, Content: commentBody})
  68. }
  69. func TestAPIEditComment(t *testing.T) {
  70. prepareTestEnv(t)
  71. const newCommentBody = "This is the new comment body"
  72. comment := models.AssertExistsAndLoadBean(t, &models.Comment{},
  73. models.Cond("type = ?", models.CommentTypeComment)).(*models.Comment)
  74. issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: comment.IssueID}).(*models.Issue)
  75. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository)
  76. repoOwner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  77. session := loginUser(t, repoOwner.Name)
  78. token := getTokenForLoggedInUser(t, session)
  79. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d?token=%s",
  80. repoOwner.Name, repo.Name, comment.ID, token)
  81. req := NewRequestWithValues(t, "PATCH", urlStr, map[string]string{
  82. "body": newCommentBody,
  83. })
  84. resp := session.MakeRequest(t, req, http.StatusOK)
  85. var updatedComment api.Comment
  86. DecodeJSON(t, resp, &updatedComment)
  87. assert.EqualValues(t, comment.ID, updatedComment.ID)
  88. assert.EqualValues(t, newCommentBody, updatedComment.Body)
  89. models.AssertExistsAndLoadBean(t, &models.Comment{ID: comment.ID, IssueID: issue.ID, Content: newCommentBody})
  90. }
  91. func TestAPIDeleteComment(t *testing.T) {
  92. prepareTestEnv(t)
  93. comment := models.AssertExistsAndLoadBean(t, &models.Comment{},
  94. models.Cond("type = ?", models.CommentTypeComment)).(*models.Comment)
  95. issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: comment.IssueID}).(*models.Issue)
  96. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository)
  97. repoOwner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  98. session := loginUser(t, repoOwner.Name)
  99. token := getTokenForLoggedInUser(t, session)
  100. req := NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/comments/%d?token=%s",
  101. repoOwner.Name, repo.Name, comment.ID, token)
  102. session.MakeRequest(t, req, http.StatusNoContent)
  103. models.AssertNotExistsBean(t, &models.Comment{ID: comment.ID})
  104. }