Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

api_comment_test.go 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/comments",
  58. repoOwner.Name, repo.Name, issue.Index)
  59. req := NewRequestWithValues(t, "POST", urlStr, map[string]string{
  60. "body": commentBody,
  61. })
  62. resp := session.MakeRequest(t, req, http.StatusCreated)
  63. var updatedComment api.Comment
  64. DecodeJSON(t, resp, &updatedComment)
  65. assert.EqualValues(t, commentBody, updatedComment.Body)
  66. models.AssertExistsAndLoadBean(t, &models.Comment{ID: updatedComment.ID, IssueID: issue.ID, Content: commentBody})
  67. }
  68. func TestAPIEditComment(t *testing.T) {
  69. prepareTestEnv(t)
  70. const newCommentBody = "This is the new comment body"
  71. comment := models.AssertExistsAndLoadBean(t, &models.Comment{},
  72. models.Cond("type = ?", models.CommentTypeComment)).(*models.Comment)
  73. issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: comment.IssueID}).(*models.Issue)
  74. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository)
  75. repoOwner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  76. session := loginUser(t, repoOwner.Name)
  77. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d",
  78. repoOwner.Name, repo.Name, comment.ID)
  79. req := NewRequestWithValues(t, "PATCH", urlStr, map[string]string{
  80. "body": newCommentBody,
  81. })
  82. resp := session.MakeRequest(t, req, http.StatusOK)
  83. var updatedComment api.Comment
  84. DecodeJSON(t, resp, &updatedComment)
  85. assert.EqualValues(t, comment.ID, updatedComment.ID)
  86. assert.EqualValues(t, newCommentBody, updatedComment.Body)
  87. models.AssertExistsAndLoadBean(t, &models.Comment{ID: comment.ID, IssueID: issue.ID, Content: newCommentBody})
  88. }
  89. func TestAPIDeleteComment(t *testing.T) {
  90. prepareTestEnv(t)
  91. comment := models.AssertExistsAndLoadBean(t, &models.Comment{},
  92. models.Cond("type = ?", models.CommentTypeComment)).(*models.Comment)
  93. issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: comment.IssueID}).(*models.Issue)
  94. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository)
  95. repoOwner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  96. session := loginUser(t, repoOwner.Name)
  97. req := NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/comments/%d",
  98. repoOwner.Name, repo.Name, comment.ID)
  99. session.MakeRequest(t, req, http.StatusNoContent)
  100. models.AssertNotExistsBean(t, &models.Comment{ID: comment.ID})
  101. }