Skip to content

Commit 838db2f

Browse files
authored
Convert to url auth to header auth in tests (#28484)
Related #28390
1 parent 04b235d commit 838db2f

File tree

102 files changed

+1714
-1522
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+1714
-1522
lines changed

tests/integration/api_actions_artifact_test.go

+43-46
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ func TestActionsArtifactUploadSingleFile(t *testing.T) {
3030
req := NewRequestWithJSON(t, "POST", "/api/actions_pipeline/_apis/pipelines/workflows/791/artifacts", getUploadArtifactRequest{
3131
Type: "actions_storage",
3232
Name: "artifact",
33-
})
34-
req = addTokenAuthHeader(req, "Bearer 8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
33+
}).AddTokenAuth("8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
3534
resp := MakeRequest(t, req, http.StatusOK)
3635
var uploadResp uploadArtifactResponse
3736
DecodeJSON(t, resp, &uploadResp)
@@ -43,18 +42,18 @@ func TestActionsArtifactUploadSingleFile(t *testing.T) {
4342

4443
// upload artifact chunk
4544
body := strings.Repeat("A", 1024)
46-
req = NewRequestWithBody(t, "PUT", url, strings.NewReader(body))
47-
req = addTokenAuthHeader(req, "Bearer 8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
48-
req.Header.Add("Content-Range", "bytes 0-1023/1024")
49-
req.Header.Add("x-tfs-filelength", "1024")
50-
req.Header.Add("x-actions-results-md5", "1HsSe8LeLWh93ILaw1TEFQ==") // base64(md5(body))
45+
req = NewRequestWithBody(t, "PUT", url, strings.NewReader(body)).
46+
AddTokenAuth("8061e833a55f6fc0157c98b883e91fcfeeb1a71a").
47+
SetHeader("Content-Range", "bytes 0-1023/1024").
48+
SetHeader("x-tfs-filelength", "1024").
49+
SetHeader("x-actions-results-md5", "1HsSe8LeLWh93ILaw1TEFQ==") // base64(md5(body))
5150
MakeRequest(t, req, http.StatusOK)
5251

5352
t.Logf("Create artifact confirm")
5453

5554
// confirm artifact upload
56-
req = NewRequest(t, "PATCH", "/api/actions_pipeline/_apis/pipelines/workflows/791/artifacts?artifactName=artifact")
57-
req = addTokenAuthHeader(req, "Bearer 8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
55+
req = NewRequest(t, "PATCH", "/api/actions_pipeline/_apis/pipelines/workflows/791/artifacts?artifactName=artifact").
56+
AddTokenAuth("8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
5857
MakeRequest(t, req, http.StatusOK)
5958
}
6059

@@ -64,20 +63,20 @@ func TestActionsArtifactUploadInvalidHash(t *testing.T) {
6463
// artifact id 54321 not exist
6564
url := "/api/actions_pipeline/_apis/pipelines/workflows/791/artifacts/8e5b948a454515dbabfc7eb718ddddddd/upload?itemPath=artifact/abc.txt"
6665
body := strings.Repeat("A", 1024)
67-
req := NewRequestWithBody(t, "PUT", url, strings.NewReader(body))
68-
req = addTokenAuthHeader(req, "Bearer 8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
69-
req.Header.Add("Content-Range", "bytes 0-1023/1024")
70-
req.Header.Add("x-tfs-filelength", "1024")
71-
req.Header.Add("x-actions-results-md5", "1HsSe8LeLWh93ILaw1TEFQ==") // base64(md5(body))
66+
req := NewRequestWithBody(t, "PUT", url, strings.NewReader(body)).
67+
AddTokenAuth("8061e833a55f6fc0157c98b883e91fcfeeb1a71a").
68+
SetHeader("Content-Range", "bytes 0-1023/1024").
69+
SetHeader("x-tfs-filelength", "1024").
70+
SetHeader("x-actions-results-md5", "1HsSe8LeLWh93ILaw1TEFQ==") // base64(md5(body))
7271
resp := MakeRequest(t, req, http.StatusBadRequest)
7372
assert.Contains(t, resp.Body.String(), "Invalid artifact hash")
7473
}
7574

7675
func TestActionsArtifactConfirmUploadWithoutName(t *testing.T) {
7776
defer tests.PrepareTestEnv(t)()
7877

79-
req := NewRequest(t, "PATCH", "/api/actions_pipeline/_apis/pipelines/workflows/791/artifacts")
80-
req = addTokenAuthHeader(req, "Bearer 8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
78+
req := NewRequest(t, "PATCH", "/api/actions_pipeline/_apis/pipelines/workflows/791/artifacts").
79+
AddTokenAuth("8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
8180
resp := MakeRequest(t, req, http.StatusBadRequest)
8281
assert.Contains(t, resp.Body.String(), "artifact name is empty")
8382
}
@@ -111,8 +110,8 @@ type (
111110
func TestActionsArtifactDownload(t *testing.T) {
112111
defer tests.PrepareTestEnv(t)()
113112

114-
req := NewRequest(t, "GET", "/api/actions_pipeline/_apis/pipelines/workflows/791/artifacts")
115-
req = addTokenAuthHeader(req, "Bearer 8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
113+
req := NewRequest(t, "GET", "/api/actions_pipeline/_apis/pipelines/workflows/791/artifacts").
114+
AddTokenAuth("8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
116115
resp := MakeRequest(t, req, http.StatusOK)
117116
var listResp listArtifactsResponse
118117
DecodeJSON(t, resp, &listResp)
@@ -122,8 +121,8 @@ func TestActionsArtifactDownload(t *testing.T) {
122121

123122
idx := strings.Index(listResp.Value[0].FileContainerResourceURL, "/api/actions_pipeline/_apis/pipelines/")
124123
url := listResp.Value[0].FileContainerResourceURL[idx+1:] + "?itemPath=artifact"
125-
req = NewRequest(t, "GET", url)
126-
req = addTokenAuthHeader(req, "Bearer 8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
124+
req = NewRequest(t, "GET", url).
125+
AddTokenAuth("8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
127126
resp = MakeRequest(t, req, http.StatusOK)
128127
var downloadResp downloadArtifactResponse
129128
DecodeJSON(t, resp, &downloadResp)
@@ -134,8 +133,8 @@ func TestActionsArtifactDownload(t *testing.T) {
134133

135134
idx = strings.Index(downloadResp.Value[0].ContentLocation, "/api/actions_pipeline/_apis/pipelines/")
136135
url = downloadResp.Value[0].ContentLocation[idx:]
137-
req = NewRequest(t, "GET", url)
138-
req = addTokenAuthHeader(req, "Bearer 8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
136+
req = NewRequest(t, "GET", url).
137+
AddTokenAuth("8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
139138
resp = MakeRequest(t, req, http.StatusOK)
140139
body := strings.Repeat("A", 1024)
141140
assert.Equal(t, resp.Body.String(), body)
@@ -150,8 +149,7 @@ func TestActionsArtifactUploadMultipleFile(t *testing.T) {
150149
req := NewRequestWithJSON(t, "POST", "/api/actions_pipeline/_apis/pipelines/workflows/791/artifacts", getUploadArtifactRequest{
151150
Type: "actions_storage",
152151
Name: testArtifactName,
153-
})
154-
req = addTokenAuthHeader(req, "Bearer 8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
152+
}).AddTokenAuth("8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
155153
resp := MakeRequest(t, req, http.StatusOK)
156154
var uploadResp uploadArtifactResponse
157155
DecodeJSON(t, resp, &uploadResp)
@@ -182,19 +180,19 @@ func TestActionsArtifactUploadMultipleFile(t *testing.T) {
182180
url := uploadResp.FileContainerResourceURL[idx:] + "?itemPath=" + testArtifactName + "/" + f.Path
183181

184182
// upload artifact chunk
185-
req = NewRequestWithBody(t, "PUT", url, strings.NewReader(f.Content))
186-
req = addTokenAuthHeader(req, "Bearer 8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
187-
req.Header.Add("Content-Range", "bytes 0-1023/1024")
188-
req.Header.Add("x-tfs-filelength", "1024")
189-
req.Header.Add("x-actions-results-md5", f.MD5) // base64(md5(body))
183+
req = NewRequestWithBody(t, "PUT", url, strings.NewReader(f.Content)).
184+
AddTokenAuth("8061e833a55f6fc0157c98b883e91fcfeeb1a71a").
185+
SetHeader("Content-Range", "bytes 0-1023/1024").
186+
SetHeader("x-tfs-filelength", "1024").
187+
SetHeader("x-actions-results-md5", f.MD5) // base64(md5(body))
190188
MakeRequest(t, req, http.StatusOK)
191189
}
192190

193191
t.Logf("Create artifact confirm")
194192

195193
// confirm artifact upload
196-
req = NewRequest(t, "PATCH", "/api/actions_pipeline/_apis/pipelines/workflows/791/artifacts?artifactName="+testArtifactName)
197-
req = addTokenAuthHeader(req, "Bearer 8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
194+
req = NewRequest(t, "PATCH", "/api/actions_pipeline/_apis/pipelines/workflows/791/artifacts?artifactName="+testArtifactName).
195+
AddTokenAuth("8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
198196
MakeRequest(t, req, http.StatusOK)
199197
}
200198

@@ -203,8 +201,8 @@ func TestActionsArtifactDownloadMultiFiles(t *testing.T) {
203201

204202
const testArtifactName = "multi-files"
205203

206-
req := NewRequest(t, "GET", "/api/actions_pipeline/_apis/pipelines/workflows/791/artifacts")
207-
req = addTokenAuthHeader(req, "Bearer 8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
204+
req := NewRequest(t, "GET", "/api/actions_pipeline/_apis/pipelines/workflows/791/artifacts").
205+
AddTokenAuth("8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
208206
resp := MakeRequest(t, req, http.StatusOK)
209207
var listResp listArtifactsResponse
210208
DecodeJSON(t, resp, &listResp)
@@ -221,8 +219,8 @@ func TestActionsArtifactDownloadMultiFiles(t *testing.T) {
221219

222220
idx := strings.Index(fileContainerResourceURL, "/api/actions_pipeline/_apis/pipelines/")
223221
url := fileContainerResourceURL[idx+1:] + "?itemPath=" + testArtifactName
224-
req = NewRequest(t, "GET", url)
225-
req = addTokenAuthHeader(req, "Bearer 8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
222+
req = NewRequest(t, "GET", url).
223+
AddTokenAuth("8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
226224
resp = MakeRequest(t, req, http.StatusOK)
227225
var downloadResp downloadArtifactResponse
228226
DecodeJSON(t, resp, &downloadResp)
@@ -246,8 +244,8 @@ func TestActionsArtifactDownloadMultiFiles(t *testing.T) {
246244

247245
idx = strings.Index(value.ContentLocation, "/api/actions_pipeline/_apis/pipelines/")
248246
url = value.ContentLocation[idx:]
249-
req = NewRequest(t, "GET", url)
250-
req = addTokenAuthHeader(req, "Bearer 8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
247+
req = NewRequest(t, "GET", url).
248+
AddTokenAuth("8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
251249
resp = MakeRequest(t, req, http.StatusOK)
252250
body := strings.Repeat(bodyChar, 1024)
253251
assert.Equal(t, resp.Body.String(), body)
@@ -262,8 +260,7 @@ func TestActionsArtifactUploadWithRetentionDays(t *testing.T) {
262260
Type: "actions_storage",
263261
Name: "artifact-retention-days",
264262
RetentionDays: 9,
265-
})
266-
req = addTokenAuthHeader(req, "Bearer 8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
263+
}).AddTokenAuth("8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
267264
resp := MakeRequest(t, req, http.StatusOK)
268265
var uploadResp uploadArtifactResponse
269266
DecodeJSON(t, resp, &uploadResp)
@@ -276,17 +273,17 @@ func TestActionsArtifactUploadWithRetentionDays(t *testing.T) {
276273

277274
// upload artifact chunk
278275
body := strings.Repeat("A", 1024)
279-
req = NewRequestWithBody(t, "PUT", url, strings.NewReader(body))
280-
req = addTokenAuthHeader(req, "Bearer 8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
281-
req.Header.Add("Content-Range", "bytes 0-1023/1024")
282-
req.Header.Add("x-tfs-filelength", "1024")
283-
req.Header.Add("x-actions-results-md5", "1HsSe8LeLWh93ILaw1TEFQ==") // base64(md5(body))
276+
req = NewRequestWithBody(t, "PUT", url, strings.NewReader(body)).
277+
AddTokenAuth("8061e833a55f6fc0157c98b883e91fcfeeb1a71a").
278+
SetHeader("Content-Range", "bytes 0-1023/1024").
279+
SetHeader("x-tfs-filelength", "1024").
280+
SetHeader("x-actions-results-md5", "1HsSe8LeLWh93ILaw1TEFQ==") // base64(md5(body))
284281
MakeRequest(t, req, http.StatusOK)
285282

286283
t.Logf("Create artifact confirm")
287284

288285
// confirm artifact upload
289-
req = NewRequest(t, "PATCH", "/api/actions_pipeline/_apis/pipelines/workflows/791/artifacts?artifactName=artifact-retention-days")
290-
req = addTokenAuthHeader(req, "Bearer 8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
286+
req = NewRequest(t, "PATCH", "/api/actions_pipeline/_apis/pipelines/workflows/791/artifacts?artifactName=artifact-retention-days").
287+
AddTokenAuth("8061e833a55f6fc0157c98b883e91fcfeeb1a71a")
291288
MakeRequest(t, req, http.StatusOK)
292289
}

tests/integration/api_activitypub_person_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestActivityPubPerson(t *testing.T) {
3232
onGiteaRun(t, func(*testing.T, *url.URL) {
3333
userID := 2
3434
username := "user2"
35-
req := NewRequestf(t, "GET", fmt.Sprintf("/api/v1/activitypub/user-id/%v", userID))
35+
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/activitypub/user-id/%v", userID))
3636
resp := MakeRequest(t, req, http.StatusOK)
3737
body := resp.Body.Bytes()
3838
assert.Contains(t, string(body), "@context")
@@ -68,7 +68,7 @@ func TestActivityPubMissingPerson(t *testing.T) {
6868
}()
6969

7070
onGiteaRun(t, func(*testing.T, *url.URL) {
71-
req := NewRequestf(t, "GET", "/api/v1/activitypub/user-id/999999999")
71+
req := NewRequest(t, "GET", "/api/v1/activitypub/user-id/999999999")
7272
resp := MakeRequest(t, req, http.StatusNotFound)
7373
assert.Contains(t, resp.Body.String(), "user does not exist")
7474
})

tests/integration/api_admin_org_test.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ func TestAPIAdminOrgCreate(t *testing.T) {
3131
Location: "Shanghai",
3232
Visibility: "private",
3333
}
34-
req := NewRequestWithJSON(t, "POST", "/api/v1/admin/users/user2/orgs?token="+token, &org)
34+
req := NewRequestWithJSON(t, "POST", "/api/v1/admin/users/user2/orgs", &org).
35+
AddTokenAuth(token)
3536
resp := MakeRequest(t, req, http.StatusCreated)
3637

3738
var apiOrg api.Organization
@@ -65,7 +66,8 @@ func TestAPIAdminOrgCreateBadVisibility(t *testing.T) {
6566
Location: "Shanghai",
6667
Visibility: "notvalid",
6768
}
68-
req := NewRequestWithJSON(t, "POST", "/api/v1/admin/users/user2/orgs?token="+token, &org)
69+
req := NewRequestWithJSON(t, "POST", "/api/v1/admin/users/user2/orgs", &org).
70+
AddTokenAuth(token)
6971
MakeRequest(t, req, http.StatusUnprocessableEntity)
7072
})
7173
}
@@ -83,6 +85,7 @@ func TestAPIAdminOrgCreateNotAdmin(t *testing.T) {
8385
Location: "Shanghai",
8486
Visibility: "public",
8587
}
86-
req := NewRequestWithJSON(t, "POST", "/api/v1/admin/users/user2/orgs?token="+token, &org)
88+
req := NewRequestWithJSON(t, "POST", "/api/v1/admin/users/user2/orgs", &org).
89+
AddTokenAuth(token)
8790
MakeRequest(t, req, http.StatusForbidden)
8891
}

0 commit comments

Comments
 (0)