Skip to content

Commit 3c3d498

Browse files
authored
Remove dependent on session auth for api/v1 routers (#19321)
* Remove dependent on session auth for api/v1 routers * Remove unnecessary session on API context * remove missed header * fix test * fix missed api/v1
1 parent 75f8534 commit 3c3d498

22 files changed

+219
-161
lines changed

integrations/api_issue_test.go

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -168,30 +168,30 @@ func TestAPIEditIssue(t *testing.T) {
168168
func TestAPISearchIssues(t *testing.T) {
169169
defer prepareTestEnv(t)()
170170

171-
session := loginUser(t, "user2")
172-
token := getTokenForLoggedInUser(t, session)
171+
token := getUserToken(t, "user2")
173172

174173
link, _ := url.Parse("/api/v1/repos/issues/search")
175-
req := NewRequest(t, "GET", link.String())
176-
resp := session.MakeRequest(t, req, http.StatusOK)
174+
req := NewRequest(t, "GET", link.String()+"?token="+token)
175+
resp := MakeRequest(t, req, http.StatusOK)
177176
var apiIssues []*api.Issue
178177
DecodeJSON(t, resp, &apiIssues)
179178
assert.Len(t, apiIssues, 10)
180179

181180
query := url.Values{"token": {token}}
182181
link.RawQuery = query.Encode()
183182
req = NewRequest(t, "GET", link.String())
184-
resp = session.MakeRequest(t, req, http.StatusOK)
183+
resp = MakeRequest(t, req, http.StatusOK)
185184
DecodeJSON(t, resp, &apiIssues)
186185
assert.Len(t, apiIssues, 10)
187186

188187
since := "2000-01-01T00%3A50%3A01%2B00%3A00" // 946687801
189188
before := time.Unix(999307200, 0).Format(time.RFC3339)
190189
query.Add("since", since)
191190
query.Add("before", before)
191+
query.Add("token", token)
192192
link.RawQuery = query.Encode()
193193
req = NewRequest(t, "GET", link.String())
194-
resp = session.MakeRequest(t, req, http.StatusOK)
194+
resp = MakeRequest(t, req, http.StatusOK)
195195
DecodeJSON(t, resp, &apiIssues)
196196
assert.Len(t, apiIssues, 8)
197197
query.Del("since")
@@ -200,77 +200,76 @@ func TestAPISearchIssues(t *testing.T) {
200200
query.Add("state", "closed")
201201
link.RawQuery = query.Encode()
202202
req = NewRequest(t, "GET", link.String())
203-
resp = session.MakeRequest(t, req, http.StatusOK)
203+
resp = MakeRequest(t, req, http.StatusOK)
204204
DecodeJSON(t, resp, &apiIssues)
205205
assert.Len(t, apiIssues, 2)
206206

207207
query.Set("state", "all")
208208
link.RawQuery = query.Encode()
209209
req = NewRequest(t, "GET", link.String())
210-
resp = session.MakeRequest(t, req, http.StatusOK)
210+
resp = MakeRequest(t, req, http.StatusOK)
211211
DecodeJSON(t, resp, &apiIssues)
212212
assert.EqualValues(t, "15", resp.Header().Get("X-Total-Count"))
213213
assert.Len(t, apiIssues, 10) // there are more but 10 is page item limit
214214

215215
query.Add("limit", "20")
216216
link.RawQuery = query.Encode()
217217
req = NewRequest(t, "GET", link.String())
218-
resp = session.MakeRequest(t, req, http.StatusOK)
218+
resp = MakeRequest(t, req, http.StatusOK)
219219
DecodeJSON(t, resp, &apiIssues)
220220
assert.Len(t, apiIssues, 15)
221221

222-
query = url.Values{"assigned": {"true"}, "state": {"all"}}
222+
query = url.Values{"assigned": {"true"}, "state": {"all"}, "token": {token}}
223223
link.RawQuery = query.Encode()
224224
req = NewRequest(t, "GET", link.String())
225-
resp = session.MakeRequest(t, req, http.StatusOK)
225+
resp = MakeRequest(t, req, http.StatusOK)
226226
DecodeJSON(t, resp, &apiIssues)
227227
assert.Len(t, apiIssues, 1)
228228

229-
query = url.Values{"milestones": {"milestone1"}, "state": {"all"}}
229+
query = url.Values{"milestones": {"milestone1"}, "state": {"all"}, "token": {token}}
230230
link.RawQuery = query.Encode()
231231
req = NewRequest(t, "GET", link.String())
232-
resp = session.MakeRequest(t, req, http.StatusOK)
232+
resp = MakeRequest(t, req, http.StatusOK)
233233
DecodeJSON(t, resp, &apiIssues)
234234
assert.Len(t, apiIssues, 1)
235235

236-
query = url.Values{"milestones": {"milestone1,milestone3"}, "state": {"all"}}
236+
query = url.Values{"milestones": {"milestone1,milestone3"}, "state": {"all"}, "token": {token}}
237237
link.RawQuery = query.Encode()
238238
req = NewRequest(t, "GET", link.String())
239-
resp = session.MakeRequest(t, req, http.StatusOK)
239+
resp = MakeRequest(t, req, http.StatusOK)
240240
DecodeJSON(t, resp, &apiIssues)
241241
assert.Len(t, apiIssues, 2)
242242

243-
query = url.Values{"owner": {"user2"}} // user
243+
query = url.Values{"owner": {"user2"}, "token": {token}} // user
244244
link.RawQuery = query.Encode()
245245
req = NewRequest(t, "GET", link.String())
246-
resp = session.MakeRequest(t, req, http.StatusOK)
246+
resp = MakeRequest(t, req, http.StatusOK)
247247
DecodeJSON(t, resp, &apiIssues)
248248
assert.Len(t, apiIssues, 6)
249249

250-
query = url.Values{"owner": {"user3"}} // organization
250+
query = url.Values{"owner": {"user3"}, "token": {token}} // organization
251251
link.RawQuery = query.Encode()
252252
req = NewRequest(t, "GET", link.String())
253-
resp = session.MakeRequest(t, req, http.StatusOK)
253+
resp = MakeRequest(t, req, http.StatusOK)
254254
DecodeJSON(t, resp, &apiIssues)
255255
assert.Len(t, apiIssues, 3)
256256

257-
query = url.Values{"owner": {"user3"}, "team": {"team1"}} // organization + team
257+
query = url.Values{"owner": {"user3"}, "team": {"team1"}, "token": {token}} // organization + team
258258
link.RawQuery = query.Encode()
259259
req = NewRequest(t, "GET", link.String())
260-
resp = session.MakeRequest(t, req, http.StatusOK)
260+
resp = MakeRequest(t, req, http.StatusOK)
261261
DecodeJSON(t, resp, &apiIssues)
262262
assert.Len(t, apiIssues, 2)
263263
}
264264

265265
func TestAPISearchIssuesWithLabels(t *testing.T) {
266266
defer prepareTestEnv(t)()
267267

268-
session := loginUser(t, "user1")
269-
token := getTokenForLoggedInUser(t, session)
268+
token := getUserToken(t, "user1")
270269

271270
link, _ := url.Parse("/api/v1/repos/issues/search")
272-
req := NewRequest(t, "GET", link.String())
273-
resp := session.MakeRequest(t, req, http.StatusOK)
271+
req := NewRequest(t, "GET", link.String()+"?token="+token)
272+
resp := MakeRequest(t, req, http.StatusOK)
274273
var apiIssues []*api.Issue
275274
DecodeJSON(t, resp, &apiIssues)
276275

@@ -280,30 +279,30 @@ func TestAPISearchIssuesWithLabels(t *testing.T) {
280279
query.Add("token", token)
281280
link.RawQuery = query.Encode()
282281
req = NewRequest(t, "GET", link.String())
283-
resp = session.MakeRequest(t, req, http.StatusOK)
282+
resp = MakeRequest(t, req, http.StatusOK)
284283
DecodeJSON(t, resp, &apiIssues)
285284
assert.Len(t, apiIssues, 10)
286285

287286
query.Add("labels", "label1")
288287
link.RawQuery = query.Encode()
289288
req = NewRequest(t, "GET", link.String())
290-
resp = session.MakeRequest(t, req, http.StatusOK)
289+
resp = MakeRequest(t, req, http.StatusOK)
291290
DecodeJSON(t, resp, &apiIssues)
292291
assert.Len(t, apiIssues, 2)
293292

294293
// multiple labels
295294
query.Set("labels", "label1,label2")
296295
link.RawQuery = query.Encode()
297296
req = NewRequest(t, "GET", link.String())
298-
resp = session.MakeRequest(t, req, http.StatusOK)
297+
resp = MakeRequest(t, req, http.StatusOK)
299298
DecodeJSON(t, resp, &apiIssues)
300299
assert.Len(t, apiIssues, 2)
301300

302301
// an org label
303302
query.Set("labels", "orglabel4")
304303
link.RawQuery = query.Encode()
305304
req = NewRequest(t, "GET", link.String())
306-
resp = session.MakeRequest(t, req, http.StatusOK)
305+
resp = MakeRequest(t, req, http.StatusOK)
307306
DecodeJSON(t, resp, &apiIssues)
308307
assert.Len(t, apiIssues, 1)
309308

@@ -312,15 +311,15 @@ func TestAPISearchIssuesWithLabels(t *testing.T) {
312311
query.Add("state", "all")
313312
link.RawQuery = query.Encode()
314313
req = NewRequest(t, "GET", link.String())
315-
resp = session.MakeRequest(t, req, http.StatusOK)
314+
resp = MakeRequest(t, req, http.StatusOK)
316315
DecodeJSON(t, resp, &apiIssues)
317316
assert.Len(t, apiIssues, 2)
318317

319318
// org and repo label which share the same issue
320319
query.Set("labels", "label1,orglabel4")
321320
link.RawQuery = query.Encode()
322321
req = NewRequest(t, "GET", link.String())
323-
resp = session.MakeRequest(t, req, http.StatusOK)
322+
resp = MakeRequest(t, req, http.StatusOK)
324323
DecodeJSON(t, resp, &apiIssues)
325324
assert.Len(t, apiIssues, 2)
326325
}

integrations/api_org_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ import (
2020

2121
func TestAPIOrgCreate(t *testing.T) {
2222
onGiteaRun(t, func(*testing.T, *url.URL) {
23-
session := loginUser(t, "user1")
23+
token := getUserToken(t, "user1")
2424

25-
token := getTokenForLoggedInUser(t, session)
2625
org := api.CreateOrgOption{
2726
UserName: "user1_org",
2827
FullName: "User1's organization",
@@ -32,7 +31,7 @@ func TestAPIOrgCreate(t *testing.T) {
3231
Visibility: "limited",
3332
}
3433
req := NewRequestWithJSON(t, "POST", "/api/v1/orgs?token="+token, &org)
35-
resp := session.MakeRequest(t, req, http.StatusCreated)
34+
resp := MakeRequest(t, req, http.StatusCreated)
3635

3736
var apiOrg api.Organization
3837
DecodeJSON(t, resp, &apiOrg)
@@ -50,22 +49,22 @@ func TestAPIOrgCreate(t *testing.T) {
5049
FullName: org.FullName,
5150
})
5251

53-
req = NewRequestf(t, "GET", "/api/v1/orgs/%s", org.UserName)
54-
resp = session.MakeRequest(t, req, http.StatusOK)
52+
req = NewRequestf(t, "GET", "/api/v1/orgs/%s?token=%s", org.UserName, token)
53+
resp = MakeRequest(t, req, http.StatusOK)
5554
DecodeJSON(t, resp, &apiOrg)
5655
assert.EqualValues(t, org.UserName, apiOrg.UserName)
5756

58-
req = NewRequestf(t, "GET", "/api/v1/orgs/%s/repos", org.UserName)
59-
resp = session.MakeRequest(t, req, http.StatusOK)
57+
req = NewRequestf(t, "GET", "/api/v1/orgs/%s/repos?token=%s", org.UserName, token)
58+
resp = MakeRequest(t, req, http.StatusOK)
6059

6160
var repos []*api.Repository
6261
DecodeJSON(t, resp, &repos)
6362
for _, repo := range repos {
6463
assert.False(t, repo.Private)
6564
}
6665

67-
req = NewRequestf(t, "GET", "/api/v1/orgs/%s/members", org.UserName)
68-
resp = session.MakeRequest(t, req, http.StatusOK)
66+
req = NewRequestf(t, "GET", "/api/v1/orgs/%s/members?token=%s", org.UserName, token)
67+
resp = MakeRequest(t, req, http.StatusOK)
6968

7069
// user1 on this org is public
7170
var users []*api.User

integrations/api_releases_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ func TestAPIListReleases(t *testing.T) {
2525

2626
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
2727
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
28-
session := loginUser(t, user2.LowerName)
29-
token := getTokenForLoggedInUser(t, session)
28+
token := getUserToken(t, user2.LowerName)
3029

3130
link, _ := url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/releases", user2.Name, repo.Name))
3231
link.RawQuery = url.Values{"token": {token}}.Encode()
33-
resp := session.MakeRequest(t, NewRequest(t, "GET", link.String()), http.StatusOK)
32+
resp := MakeRequest(t, NewRequest(t, "GET", link.String()), http.StatusOK)
3433
var apiReleases []*api.Release
3534
DecodeJSON(t, resp, &apiReleases)
3635
if assert.Len(t, apiReleases, 3) {
@@ -53,13 +52,11 @@ func TestAPIListReleases(t *testing.T) {
5352

5453
// test filter
5554
testFilterByLen := func(auth bool, query url.Values, expectedLength int, msgAndArgs ...string) {
56-
link.RawQuery = query.Encode()
5755
if auth {
5856
query.Set("token", token)
59-
resp = session.MakeRequest(t, NewRequest(t, "GET", link.String()), http.StatusOK)
60-
} else {
61-
resp = MakeRequest(t, NewRequest(t, "GET", link.String()), http.StatusOK)
6257
}
58+
link.RawQuery = query.Encode()
59+
resp = MakeRequest(t, NewRequest(t, "GET", link.String()), http.StatusOK)
6360
DecodeJSON(t, resp, &apiReleases)
6461
assert.Len(t, apiReleases, expectedLength, msgAndArgs)
6562
}

integrations/api_repo_topic_test.go

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,36 +59,34 @@ func TestAPIRepoTopic(t *testing.T) {
5959
repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}).(*repo_model.Repository)
6060

6161
// Get user2's token
62-
session := loginUser(t, user2.Name)
63-
token2 := getTokenForLoggedInUser(t, session)
62+
token2 := getUserToken(t, user2.Name)
6463

6564
// Test read topics using login
6665
url := fmt.Sprintf("/api/v1/repos/%s/%s/topics", user2.Name, repo2.Name)
67-
req := NewRequest(t, "GET", url)
68-
res := session.MakeRequest(t, req, http.StatusOK)
66+
req := NewRequest(t, "GET", url+"?token="+token2)
67+
res := MakeRequest(t, req, http.StatusOK)
6968
var topics *api.TopicName
7069
DecodeJSON(t, res, &topics)
7170
assert.ElementsMatch(t, []string{"topicname1", "topicname2"}, topics.TopicNames)
7271

7372
// Log out user2
74-
session = emptyTestSession(t)
7573
url = fmt.Sprintf("/api/v1/repos/%s/%s/topics?token=%s", user2.Name, repo2.Name, token2)
7674

7775
// Test delete a topic
7876
req = NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "Topicname1", token2)
79-
session.MakeRequest(t, req, http.StatusNoContent)
77+
MakeRequest(t, req, http.StatusNoContent)
8078

8179
// Test add an existing topic
8280
req = NewRequestf(t, "PUT", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "Golang", token2)
83-
session.MakeRequest(t, req, http.StatusNoContent)
81+
MakeRequest(t, req, http.StatusNoContent)
8482

8583
// Test add a topic
8684
req = NewRequestf(t, "PUT", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "topicName3", token2)
87-
session.MakeRequest(t, req, http.StatusNoContent)
85+
MakeRequest(t, req, http.StatusNoContent)
8886

8987
// Test read topics using token
9088
req = NewRequest(t, "GET", url)
91-
res = session.MakeRequest(t, req, http.StatusOK)
89+
res = MakeRequest(t, req, http.StatusOK)
9290
DecodeJSON(t, res, &topics)
9391
assert.ElementsMatch(t, []string{"topicname2", "golang", "topicname3"}, topics.TopicNames)
9492

@@ -97,9 +95,9 @@ func TestAPIRepoTopic(t *testing.T) {
9795
req = NewRequestWithJSON(t, "PUT", url, &api.RepoTopicOptions{
9896
Topics: newTopics,
9997
})
100-
session.MakeRequest(t, req, http.StatusNoContent)
98+
MakeRequest(t, req, http.StatusNoContent)
10199
req = NewRequest(t, "GET", url)
102-
res = session.MakeRequest(t, req, http.StatusOK)
100+
res = MakeRequest(t, req, http.StatusOK)
103101
DecodeJSON(t, res, &topics)
104102
assert.ElementsMatch(t, []string{"windows", "mac"}, topics.TopicNames)
105103

@@ -108,9 +106,9 @@ func TestAPIRepoTopic(t *testing.T) {
108106
req = NewRequestWithJSON(t, "PUT", url, &api.RepoTopicOptions{
109107
Topics: newTopics,
110108
})
111-
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
109+
MakeRequest(t, req, http.StatusUnprocessableEntity)
112110
req = NewRequest(t, "GET", url)
113-
res = session.MakeRequest(t, req, http.StatusOK)
111+
res = MakeRequest(t, req, http.StatusOK)
114112
DecodeJSON(t, res, &topics)
115113
assert.ElementsMatch(t, []string{"windows", "mac"}, topics.TopicNames)
116114

@@ -119,9 +117,9 @@ func TestAPIRepoTopic(t *testing.T) {
119117
req = NewRequestWithJSON(t, "PUT", url, &api.RepoTopicOptions{
120118
Topics: newTopics,
121119
})
122-
session.MakeRequest(t, req, http.StatusNoContent)
120+
MakeRequest(t, req, http.StatusNoContent)
123121
req = NewRequest(t, "GET", url)
124-
res = session.MakeRequest(t, req, http.StatusOK)
122+
res = MakeRequest(t, req, http.StatusOK)
125123
DecodeJSON(t, res, &topics)
126124
assert.Len(t, topics.TopicNames, 25)
127125

@@ -130,29 +128,27 @@ func TestAPIRepoTopic(t *testing.T) {
130128
req = NewRequestWithJSON(t, "PUT", url, &api.RepoTopicOptions{
131129
Topics: newTopics,
132130
})
133-
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
131+
MakeRequest(t, req, http.StatusUnprocessableEntity)
134132

135133
// Test add a topic when there is already maximum
136134
req = NewRequestf(t, "PUT", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "t26", token2)
137-
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
135+
MakeRequest(t, req, http.StatusUnprocessableEntity)
138136

139137
// Test delete a topic that repo doesn't have
140138
req = NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/topics/%s?token=%s", user2.Name, repo2.Name, "Topicname1", token2)
141-
session.MakeRequest(t, req, http.StatusNotFound)
139+
MakeRequest(t, req, http.StatusNotFound)
142140

143141
// Get user4's token
144-
session = loginUser(t, user4.Name)
145-
token4 := getTokenForLoggedInUser(t, session)
146-
session = emptyTestSession(t)
142+
token4 := getUserToken(t, user4.Name)
147143

148144
// Test read topics with write access
149145
url = fmt.Sprintf("/api/v1/repos/%s/%s/topics?token=%s", user3.Name, repo3.Name, token4)
150146
req = NewRequest(t, "GET", url)
151-
res = session.MakeRequest(t, req, http.StatusOK)
147+
res = MakeRequest(t, req, http.StatusOK)
152148
DecodeJSON(t, res, &topics)
153149
assert.Empty(t, topics.TopicNames)
154150

155151
// Test add a topic to repo with write access (requires repo admin access)
156152
req = NewRequestf(t, "PUT", "/api/v1/repos/%s/%s/topics/%s?token=%s", user3.Name, repo3.Name, "topicName", token4)
157-
session.MakeRequest(t, req, http.StatusForbidden)
153+
MakeRequest(t, req, http.StatusForbidden)
158154
}

0 commit comments

Comments
 (0)