Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

api_comment_test.go 7.2KB

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