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.

api_comment_test.go 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. "net/url"
  9. "testing"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/models/unittest"
  12. user_model "code.gitea.io/gitea/models/user"
  13. "code.gitea.io/gitea/modules/convert"
  14. api "code.gitea.io/gitea/modules/structs"
  15. "github.com/stretchr/testify/assert"
  16. )
  17. func TestAPIListRepoComments(t *testing.T) {
  18. defer prepareTestEnv(t)()
  19. comment := unittest.AssertExistsAndLoadBean(t, &models.Comment{},
  20. unittest.Cond("type = ?", models.CommentTypeComment)).(*models.Comment)
  21. issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: comment.IssueID}).(*models.Issue)
  22. repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository)
  23. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}).(*user_model.User)
  24. session := loginUser(t, repoOwner.Name)
  25. link, _ := url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments", repoOwner.Name, repo.Name))
  26. req := NewRequest(t, "GET", link.String())
  27. resp := session.MakeRequest(t, req, http.StatusOK)
  28. var apiComments []*api.Comment
  29. DecodeJSON(t, resp, &apiComments)
  30. assert.Len(t, apiComments, 2)
  31. for _, apiComment := range apiComments {
  32. c := &models.Comment{ID: apiComment.ID}
  33. unittest.AssertExistsAndLoadBean(t, c,
  34. unittest.Cond("type = ?", models.CommentTypeComment))
  35. unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: c.IssueID, RepoID: repo.ID})
  36. }
  37. //test before and since filters
  38. query := url.Values{}
  39. before := "2000-01-01T00:00:11+00:00" //unix: 946684811
  40. since := "2000-01-01T00:00:12+00:00" //unix: 946684812
  41. query.Add("before", before)
  42. link.RawQuery = query.Encode()
  43. req = NewRequest(t, "GET", link.String())
  44. resp = session.MakeRequest(t, req, http.StatusOK)
  45. DecodeJSON(t, resp, &apiComments)
  46. assert.Len(t, apiComments, 1)
  47. assert.EqualValues(t, 2, apiComments[0].ID)
  48. query.Del("before")
  49. query.Add("since", since)
  50. link.RawQuery = query.Encode()
  51. req = NewRequest(t, "GET", link.String())
  52. resp = session.MakeRequest(t, req, http.StatusOK)
  53. DecodeJSON(t, resp, &apiComments)
  54. assert.Len(t, apiComments, 1)
  55. assert.EqualValues(t, 3, apiComments[0].ID)
  56. }
  57. func TestAPIListIssueComments(t *testing.T) {
  58. defer prepareTestEnv(t)()
  59. comment := unittest.AssertExistsAndLoadBean(t, &models.Comment{},
  60. unittest.Cond("type = ?", models.CommentTypeComment)).(*models.Comment)
  61. issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: comment.IssueID}).(*models.Issue)
  62. repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository)
  63. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}).(*user_model.User)
  64. session := loginUser(t, repoOwner.Name)
  65. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/%d/comments",
  66. repoOwner.Name, repo.Name, issue.Index)
  67. resp := session.MakeRequest(t, req, http.StatusOK)
  68. var comments []*api.Comment
  69. DecodeJSON(t, resp, &comments)
  70. expectedCount := unittest.GetCount(t, &models.Comment{IssueID: issue.ID},
  71. unittest.Cond("type = ?", models.CommentTypeComment))
  72. assert.EqualValues(t, expectedCount, len(comments))
  73. }
  74. func TestAPICreateComment(t *testing.T) {
  75. defer prepareTestEnv(t)()
  76. const commentBody = "Comment body"
  77. issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{}).(*models.Issue)
  78. repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository)
  79. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}).(*user_model.User)
  80. session := loginUser(t, repoOwner.Name)
  81. token := getTokenForLoggedInUser(t, session)
  82. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/comments?token=%s",
  83. repoOwner.Name, repo.Name, issue.Index, token)
  84. req := NewRequestWithValues(t, "POST", urlStr, map[string]string{
  85. "body": commentBody,
  86. })
  87. resp := session.MakeRequest(t, req, http.StatusCreated)
  88. var updatedComment api.Comment
  89. DecodeJSON(t, resp, &updatedComment)
  90. assert.EqualValues(t, commentBody, updatedComment.Body)
  91. unittest.AssertExistsAndLoadBean(t, &models.Comment{ID: updatedComment.ID, IssueID: issue.ID, Content: commentBody})
  92. }
  93. func TestAPIGetComment(t *testing.T) {
  94. defer prepareTestEnv(t)()
  95. comment := unittest.AssertExistsAndLoadBean(t, &models.Comment{ID: 2}).(*models.Comment)
  96. assert.NoError(t, comment.LoadIssue())
  97. repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: comment.Issue.RepoID}).(*models.Repository)
  98. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}).(*user_model.User)
  99. session := loginUser(t, repoOwner.Name)
  100. token := getTokenForLoggedInUser(t, session)
  101. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/comments/%d", repoOwner.Name, repo.Name, comment.ID)
  102. session.MakeRequest(t, req, http.StatusOK)
  103. req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/comments/%d?token=%s", repoOwner.Name, repo.Name, comment.ID, token)
  104. resp := session.MakeRequest(t, req, http.StatusOK)
  105. var apiComment api.Comment
  106. DecodeJSON(t, resp, &apiComment)
  107. assert.NoError(t, comment.LoadPoster())
  108. expect := convert.ToComment(comment)
  109. assert.Equal(t, expect.ID, apiComment.ID)
  110. assert.Equal(t, expect.Poster.FullName, apiComment.Poster.FullName)
  111. assert.Equal(t, expect.Body, apiComment.Body)
  112. assert.Equal(t, expect.Created.Unix(), apiComment.Created.Unix())
  113. }
  114. func TestAPIEditComment(t *testing.T) {
  115. defer prepareTestEnv(t)()
  116. const newCommentBody = "This is the new comment body"
  117. comment := unittest.AssertExistsAndLoadBean(t, &models.Comment{},
  118. unittest.Cond("type = ?", models.CommentTypeComment)).(*models.Comment)
  119. issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: comment.IssueID}).(*models.Issue)
  120. repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository)
  121. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}).(*user_model.User)
  122. session := loginUser(t, repoOwner.Name)
  123. token := getTokenForLoggedInUser(t, session)
  124. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d?token=%s",
  125. repoOwner.Name, repo.Name, comment.ID, token)
  126. req := NewRequestWithValues(t, "PATCH", urlStr, map[string]string{
  127. "body": newCommentBody,
  128. })
  129. resp := session.MakeRequest(t, req, http.StatusOK)
  130. var updatedComment api.Comment
  131. DecodeJSON(t, resp, &updatedComment)
  132. assert.EqualValues(t, comment.ID, updatedComment.ID)
  133. assert.EqualValues(t, newCommentBody, updatedComment.Body)
  134. unittest.AssertExistsAndLoadBean(t, &models.Comment{ID: comment.ID, IssueID: issue.ID, Content: newCommentBody})
  135. }
  136. func TestAPIDeleteComment(t *testing.T) {
  137. defer prepareTestEnv(t)()
  138. comment := unittest.AssertExistsAndLoadBean(t, &models.Comment{},
  139. unittest.Cond("type = ?", models.CommentTypeComment)).(*models.Comment)
  140. issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: comment.IssueID}).(*models.Issue)
  141. repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository)
  142. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}).(*user_model.User)
  143. session := loginUser(t, repoOwner.Name)
  144. token := getTokenForLoggedInUser(t, session)
  145. req := NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/comments/%d?token=%s",
  146. repoOwner.Name, repo.Name, comment.ID, token)
  147. session.MakeRequest(t, req, http.StatusNoContent)
  148. unittest.AssertNotExistsBean(t, &models.Comment{ID: comment.ID})
  149. }