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.

155 lines
6.4 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/gitea/modules/structs"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestAPIListRepoComments(t *testing.T) {
  14. defer 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. defer 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. defer 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 TestAPIGetComment(t *testing.T) {
  70. defer prepareTestEnv(t)()
  71. comment := models.AssertExistsAndLoadBean(t, &models.Comment{ID: 2}).(*models.Comment)
  72. assert.NoError(t, comment.LoadIssue())
  73. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: comment.Issue.RepoID}).(*models.Repository)
  74. repoOwner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  75. session := loginUser(t, repoOwner.Name)
  76. token := getTokenForLoggedInUser(t, session)
  77. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/comments/%d", repoOwner.Name, repo.Name, comment.ID)
  78. resp := session.MakeRequest(t, req, http.StatusOK)
  79. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/comments/%d?token=%s", repoOwner.Name, repo.Name, comment.ID, token)
  80. resp = session.MakeRequest(t, req, http.StatusOK)
  81. var apiComment api.Comment
  82. DecodeJSON(t, resp, &apiComment)
  83. assert.NoError(t, comment.LoadPoster())
  84. expect := comment.APIFormat()
  85. assert.Equal(t, expect.ID, apiComment.ID)
  86. assert.Equal(t, expect.Poster.FullName, apiComment.Poster.FullName)
  87. assert.Equal(t, expect.Body, apiComment.Body)
  88. assert.Equal(t, expect.Created.Unix(), apiComment.Created.Unix())
  89. }
  90. func TestAPIEditComment(t *testing.T) {
  91. defer prepareTestEnv(t)()
  92. const newCommentBody = "This is the new comment body"
  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. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d?token=%s",
  101. repoOwner.Name, repo.Name, comment.ID, token)
  102. req := NewRequestWithValues(t, "PATCH", urlStr, map[string]string{
  103. "body": newCommentBody,
  104. })
  105. resp := session.MakeRequest(t, req, http.StatusOK)
  106. var updatedComment api.Comment
  107. DecodeJSON(t, resp, &updatedComment)
  108. assert.EqualValues(t, comment.ID, updatedComment.ID)
  109. assert.EqualValues(t, newCommentBody, updatedComment.Body)
  110. models.AssertExistsAndLoadBean(t, &models.Comment{ID: comment.ID, IssueID: issue.ID, Content: newCommentBody})
  111. }
  112. func TestAPIDeleteComment(t *testing.T) {
  113. defer prepareTestEnv(t)()
  114. comment := models.AssertExistsAndLoadBean(t, &models.Comment{},
  115. models.Cond("type = ?", models.CommentTypeComment)).(*models.Comment)
  116. issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: comment.IssueID}).(*models.Issue)
  117. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository)
  118. repoOwner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  119. session := loginUser(t, repoOwner.Name)
  120. token := getTokenForLoggedInUser(t, session)
  121. req := NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/comments/%d?token=%s",
  122. repoOwner.Name, repo.Name, comment.ID, token)
  123. session.MakeRequest(t, req, http.StatusNoContent)
  124. models.AssertNotExistsBean(t, &models.Comment{ID: comment.ID})
  125. }