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.

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