Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathcode_intel_upload_test.go
More file actions
352 lines (334 loc) · 10.1 KB
/
Copy pathcode_intel_upload_test.go
File metadata and controls
352 lines (334 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package main
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/sourcegraph/sourcegraph/lib/errors"
)
// validSGToken is a well-formed (but not real) Sourcegraph personal access token.
const validSGToken = "sgp_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
func TestUploadFailureReason(t *testing.T) {
tests := []struct {
name string
err *ErrUnexpectedStatusCode
want string
}{
{"401 with body", &ErrUnexpectedStatusCode{Code: 401, Body: "Invalid access token."}, "Invalid access token."},
{"401 without body", &ErrUnexpectedStatusCode{Code: 401}, "unauthorized"},
{"403 with body", &ErrUnexpectedStatusCode{Code: 403, Body: "no write permission"}, "no write permission"},
{"403 without body", &ErrUnexpectedStatusCode{Code: 403}, "forbidden"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, uploadFailureReason(tt.err))
})
}
}
func TestSourcegraphAccessTokenHint(t *testing.T) {
tests := []struct {
name string
accessToken string
isUnauthorized bool
isForbidden bool
wantContains string
}{
{
name: "401 no token",
isUnauthorized: true,
wantContains: "No Sourcegraph access token was provided",
},
{
name: "401 malformed token",
accessToken: "not-a-valid-token",
isUnauthorized: true,
wantContains: "does not match the expected format",
},
{
name: "401 valid format token",
accessToken: validSGToken,
isUnauthorized: true,
wantContains: "may be invalid, expired",
},
{
name: "403",
isForbidden: true,
wantContains: "may not have sufficient permissions",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := sourcegraphAccessTokenHint(tt.accessToken, tt.isUnauthorized, tt.isForbidden)
assert.Contains(t, got, tt.wantContains)
})
}
}
func TestCodeHostTokenHints(t *testing.T) {
tests := []struct {
name string
repo string
gitHubToken string
gitLabToken string
isUnauthorized bool
wantContains []string
}{
{
name: "github repo no token 401",
repo: "github.com/org/repo",
isUnauthorized: true,
wantContains: []string{"No -github-token was provided", "github.com/org/repo"},
},
{
name: "github repo no token 403",
repo: "github.com/org/repo",
isUnauthorized: false,
wantContains: []string{"No -github-token was provided"},
},
{
name: "github repo with token 401",
repo: "github.com/org/repo",
gitHubToken: "ghp_xxx",
isUnauthorized: true,
wantContains: []string{"-github-token may be invalid"},
},
{
name: "github repo with token 403",
repo: "github.com/org/repo",
gitHubToken: "ghp_xxx",
isUnauthorized: false,
wantContains: []string{"-github-token may lack the required permissions"},
},
{
name: "gitlab repo no token 401",
repo: "gitlab.com/org/repo",
isUnauthorized: true,
wantContains: []string{"No -gitlab-token was provided", "gitlab.com/org/repo"},
},
{
name: "gitlab repo no token 403",
repo: "gitlab.com/org/repo",
isUnauthorized: false,
wantContains: []string{"No -gitlab-token was provided"},
},
{
name: "gitlab repo with token 401",
repo: "gitlab.com/org/repo",
gitLabToken: "glpat-xxx",
isUnauthorized: true,
wantContains: []string{"-gitlab-token may be invalid"},
},
{
name: "gitlab repo with token 403",
repo: "gitlab.com/org/repo",
gitLabToken: "glpat-xxx",
isUnauthorized: false,
wantContains: []string{"-gitlab-token may lack the required permissions"},
},
{
name: "other repo no tokens",
repo: "bitbucket.org/org/repo",
isUnauthorized: true,
wantContains: []string{"Code host verification is supported for github.com and gitlab.com"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
saved := codeintelUploadFlags
defer func() { codeintelUploadFlags = saved }()
codeintelUploadFlags.repo = tt.repo
codeintelUploadFlags.gitHubToken = tt.gitHubToken
codeintelUploadFlags.gitLabToken = tt.gitLabToken
hints := codeHostTokenHints(tt.isUnauthorized)
joined := strings.Join(hints, "\n")
for _, s := range tt.wantContains {
assert.Contains(t, joined, s)
}
})
}
}
func TestUploadHints(t *testing.T) {
tests := []struct {
name string
accessToken string
repo string
gitHubToken string
gitLabToken string
isUnauthorized bool
isForbidden bool
wantContains []string
}{
{
name: "401 no SG token github repo",
repo: "github.com/org/repo",
isUnauthorized: true,
wantContains: []string{
"Possible causes:",
"- No Sourcegraph access token was provided",
"- No -github-token was provided",
"sourcegraph.com/docs/cli/references/code-intel/upload",
},
},
{
name: "401 valid SG token github token supplied",
accessToken: validSGToken,
repo: "github.com/org/repo",
gitHubToken: "ghp_xxx",
isUnauthorized: true,
wantContains: []string{
"Possible causes:",
"- The Sourcegraph access token may be invalid, expired",
"- The supplied -github-token may be invalid",
"sourcegraph.com/docs/cli/references/code-intel/upload",
},
},
{
name: "403 gitlab token supplied",
accessToken: validSGToken,
repo: "gitlab.com/org/repo",
gitLabToken: "glpat-xxx",
isForbidden: true,
wantContains: []string{
"Possible causes:",
"- You may not have sufficient permissions",
"- The supplied -gitlab-token may lack the required permissions",
"sourcegraph.com/docs/cli/references/code-intel/upload",
},
},
{
name: "401 bitbucket repo catch-all",
accessToken: validSGToken,
repo: "bitbucket.org/org/repo",
isUnauthorized: true,
wantContains: []string{
"Possible causes:",
"- The Sourcegraph access token may be invalid, expired",
"- Code host verification is supported for github.com and gitlab.com",
"sourcegraph.com/docs/cli/references/code-intel/upload",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
saved := codeintelUploadFlags
defer func() { codeintelUploadFlags = saved }()
codeintelUploadFlags.repo = tt.repo
codeintelUploadFlags.gitHubToken = tt.gitHubToken
codeintelUploadFlags.gitLabToken = tt.gitLabToken
got := uploadHints(tt.accessToken, tt.isUnauthorized, tt.isForbidden)
for _, s := range tt.wantContains {
assert.Contains(t, got, s)
}
})
}
}
func TestHandleUploadError(t *testing.T) {
tests := []struct {
name string
accessToken string
repo string
gitHubToken string
err error
wantContains []string
wantNil bool
}{
{
name: "401 with server body",
accessToken: validSGToken,
repo: "github.com/org/repo",
err: &ErrUnexpectedStatusCode{Code: 401, Body: "Invalid access token."},
wantContains: []string{
"upload failed: Invalid access token.",
"Possible causes:",
"- The Sourcegraph access token may be invalid, expired",
},
},
{
name: "401 without body",
accessToken: "",
repo: "github.com/org/repo",
err: &ErrUnexpectedStatusCode{Code: 401},
wantContains: []string{
"upload failed: unauthorized",
"Possible causes:",
"- No Sourcegraph access token was provided",
},
},
{
name: "403 with server body",
accessToken: validSGToken,
repo: "github.com/org/repo",
gitHubToken: "ghp_xxx",
err: &ErrUnexpectedStatusCode{Code: 403, Body: "no write permission"},
wantContains: []string{
"upload failed: no write permission",
"Possible causes:",
"- You may not have sufficient permissions",
},
},
{
name: "500 passthrough",
accessToken: validSGToken,
repo: "github.com/org/repo",
err: &ErrUnexpectedStatusCode{Code: 500, Body: "internal error"},
wantContains: []string{"unexpected status code: 500"},
},
{
name: "non-http error passthrough",
accessToken: validSGToken,
repo: "github.com/org/repo",
err: errors.New("connection refused"),
wantContains: []string{"connection refused"},
},
{
name: "combined 502 + 403 from retries",
accessToken: validSGToken,
repo: "github.com/org/repo",
gitHubToken: "ghp_xxx",
err: errors.CombineErrors(
&ErrUnexpectedStatusCode{Code: 502},
&ErrUnexpectedStatusCode{Code: 403, Body: "no write permission"},
),
wantContains: []string{
"upload failed: no write permission",
"Possible causes:",
"- You may not have sufficient permissions",
},
},
{
name: "combined 502 + 401 from retries",
accessToken: validSGToken,
repo: "github.com/org/repo",
err: errors.CombineErrors(
&ErrUnexpectedStatusCode{Code: 502},
&ErrUnexpectedStatusCode{Code: 401, Body: "Invalid access token."},
),
wantContains: []string{
"upload failed: Invalid access token.",
"Possible causes:",
"- The Sourcegraph access token may be invalid, expired",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
saved := codeintelUploadFlags
defer func() { codeintelUploadFlags = saved }()
codeintelUploadFlags.repo = tt.repo
codeintelUploadFlags.gitHubToken = tt.gitHubToken
codeintelUploadFlags.ignoreUploadFailures = false
got := handleUploadError(tt.accessToken, tt.err)
require.NotNil(t, got)
for _, s := range tt.wantContains {
assert.Contains(t, got.Error(), s)
}
})
}
}
func TestHandleUploadErrorIgnoreFailures(t *testing.T) {
saved := codeintelUploadFlags
defer func() { codeintelUploadFlags = saved }()
codeintelUploadFlags.repo = "github.com/org/repo"
codeintelUploadFlags.ignoreUploadFailures = true
got := handleUploadError(validSGToken, &ErrUnexpectedStatusCode{Code: 401, Body: "bad token"})
assert.Nil(t, got)
}
You can’t perform that action at this time.
