gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "fmt"
  6. "net/http"
  7. "testing"
  8. auth_model "code.gitea.io/gitea/models/auth"
  9. issues_model "code.gitea.io/gitea/models/issues"
  10. repo_model "code.gitea.io/gitea/models/repo"
  11. "code.gitea.io/gitea/models/unittest"
  12. user_model "code.gitea.io/gitea/models/user"
  13. api "code.gitea.io/gitea/modules/structs"
  14. "code.gitea.io/gitea/tests"
  15. "github.com/stretchr/testify/assert"
  16. )
  17. func TestAPIPinIssue(t *testing.T) {
  18. defer tests.PrepareTestEnv(t)()
  19. assert.NoError(t, unittest.LoadFixtures())
  20. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
  21. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{RepoID: repo.ID})
  22. owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  23. session := loginUser(t, owner.Name)
  24. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
  25. // Pin the Issue
  26. req := NewRequest(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/pin", repo.OwnerName, repo.Name, issue.Index)).
  27. AddTokenAuth(token)
  28. MakeRequest(t, req, http.StatusNoContent)
  29. // Check if the Issue is pinned
  30. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d", repo.OwnerName, repo.Name, issue.Index))
  31. resp := MakeRequest(t, req, http.StatusOK)
  32. var issueAPI api.Issue
  33. DecodeJSON(t, resp, &issueAPI)
  34. assert.Equal(t, 1, issueAPI.PinOrder)
  35. }
  36. func TestAPIUnpinIssue(t *testing.T) {
  37. defer tests.PrepareTestEnv(t)()
  38. assert.NoError(t, unittest.LoadFixtures())
  39. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
  40. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{RepoID: repo.ID})
  41. owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  42. session := loginUser(t, owner.Name)
  43. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
  44. // Pin the Issue
  45. req := NewRequest(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/pin", repo.OwnerName, repo.Name, issue.Index)).
  46. AddTokenAuth(token)
  47. MakeRequest(t, req, http.StatusNoContent)
  48. // Check if the Issue is pinned
  49. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d", repo.OwnerName, repo.Name, issue.Index))
  50. resp := MakeRequest(t, req, http.StatusOK)
  51. var issueAPI api.Issue
  52. DecodeJSON(t, resp, &issueAPI)
  53. assert.Equal(t, 1, issueAPI.PinOrder)
  54. // Unpin the Issue
  55. req = NewRequest(t, "DELETE", fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/pin", repo.OwnerName, repo.Name, issue.Index)).
  56. AddTokenAuth(token)
  57. MakeRequest(t, req, http.StatusNoContent)
  58. // Check if the Issue is no longer pinned
  59. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d", repo.OwnerName, repo.Name, issue.Index))
  60. resp = MakeRequest(t, req, http.StatusOK)
  61. DecodeJSON(t, resp, &issueAPI)
  62. assert.Equal(t, 0, issueAPI.PinOrder)
  63. }
  64. func TestAPIMoveIssuePin(t *testing.T) {
  65. defer tests.PrepareTestEnv(t)()
  66. assert.NoError(t, unittest.LoadFixtures())
  67. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
  68. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{RepoID: repo.ID})
  69. issue2 := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2, RepoID: repo.ID})
  70. owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  71. session := loginUser(t, owner.Name)
  72. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
  73. // Pin the first Issue
  74. req := NewRequest(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/pin", repo.OwnerName, repo.Name, issue.Index)).
  75. AddTokenAuth(token)
  76. MakeRequest(t, req, http.StatusNoContent)
  77. // Check if the first Issue is pinned at position 1
  78. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d", repo.OwnerName, repo.Name, issue.Index))
  79. resp := MakeRequest(t, req, http.StatusOK)
  80. var issueAPI api.Issue
  81. DecodeJSON(t, resp, &issueAPI)
  82. assert.Equal(t, 1, issueAPI.PinOrder)
  83. // Pin the second Issue
  84. req = NewRequest(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/pin", repo.OwnerName, repo.Name, issue2.Index)).
  85. AddTokenAuth(token)
  86. MakeRequest(t, req, http.StatusNoContent)
  87. // Move the first Issue to position 2
  88. req = NewRequest(t, "PATCH", fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/pin/2", repo.OwnerName, repo.Name, issue.Index)).
  89. AddTokenAuth(token)
  90. MakeRequest(t, req, http.StatusNoContent)
  91. // Check if the first Issue is pinned at position 2
  92. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d", repo.OwnerName, repo.Name, issue.Index))
  93. resp = MakeRequest(t, req, http.StatusOK)
  94. var issueAPI3 api.Issue
  95. DecodeJSON(t, resp, &issueAPI3)
  96. assert.Equal(t, 2, issueAPI3.PinOrder)
  97. // Check if the second Issue is pinned at position 1
  98. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d", repo.OwnerName, repo.Name, issue2.Index))
  99. resp = MakeRequest(t, req, http.StatusOK)
  100. var issueAPI4 api.Issue
  101. DecodeJSON(t, resp, &issueAPI4)
  102. assert.Equal(t, 1, issueAPI4.PinOrder)
  103. }
  104. func TestAPIListPinnedIssues(t *testing.T) {
  105. defer tests.PrepareTestEnv(t)()
  106. assert.NoError(t, unittest.LoadFixtures())
  107. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
  108. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{RepoID: repo.ID})
  109. owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  110. session := loginUser(t, owner.Name)
  111. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
  112. // Pin the Issue
  113. req := NewRequest(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/pin", repo.OwnerName, repo.Name, issue.Index)).
  114. AddTokenAuth(token)
  115. MakeRequest(t, req, http.StatusNoContent)
  116. // Check if the Issue is in the List
  117. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/issues/pinned", repo.OwnerName, repo.Name))
  118. resp := MakeRequest(t, req, http.StatusOK)
  119. var issueList []api.Issue
  120. DecodeJSON(t, resp, &issueList)
  121. assert.Len(t, issueList, 1)
  122. assert.Equal(t, issue.ID, issueList[0].ID)
  123. }
  124. func TestAPIListPinnedPullrequests(t *testing.T) {
  125. defer tests.PrepareTestEnv(t)()
  126. assert.NoError(t, unittest.LoadFixtures())
  127. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
  128. req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/pulls/pinned", repo.OwnerName, repo.Name))
  129. resp := MakeRequest(t, req, http.StatusOK)
  130. var prList []api.PullRequest
  131. DecodeJSON(t, resp, &prList)
  132. assert.Empty(t, prList)
  133. }
  134. func TestAPINewPinAllowed(t *testing.T) {
  135. defer tests.PrepareTestEnv(t)()
  136. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
  137. owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  138. req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/new_pin_allowed", owner.Name, repo.Name))
  139. resp := MakeRequest(t, req, http.StatusOK)
  140. var newPinsAllowed api.NewIssuePinsAllowed
  141. DecodeJSON(t, resp, &newPinsAllowed)
  142. assert.True(t, newPinsAllowed.Issues)
  143. assert.True(t, newPinsAllowed.PullRequests)
  144. }