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