-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
gerrit_test.go
614 lines (535 loc) · 16.8 KB
/
gerrit_test.go
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
package gerrit_test
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strings"
"testing"
"github.com/andygrunwald/go-gerrit"
)
const (
// testGerritInstanceURL is a test instance url that won`t be called
testGerritInstanceURL = "https://go-review.googlesource.com/"
)
var (
// testMux is the HTTP request multiplexer used with the test server.
testMux *http.ServeMux
// testClient is the gerrit client being tested.
testClient *gerrit.Client
// testServer is a test HTTP server used to provide mock API responses.
testServer *httptest.Server
)
type testValues map[string]string
// setup sets up a test HTTP server along with a gerrit.Client that is configured to talk to that test server.
// Tests should register handlers on mux which provide mock responses for the API method being tested.
func setup() {
// Test server
testMux = http.NewServeMux()
testServer = httptest.NewServer(testMux)
// gerrit client configured to use test server
testClient, _ = gerrit.NewClient(context.Background(), testServer.URL, nil)
}
// teardown closes the test HTTP server.
func teardown() {
testServer.Close()
}
// makedigestheader takes the incoming request and produces a string
// which can be used for the WWW-Authenticate header.
func makedigestheader(request *http.Request) string {
return fmt.Sprintf(
`Digest realm="Gerrit Code Review", domain="http://%s/", qop="auth", nonce="fakevaluefortesting"`,
request.Host)
}
// writeresponse writes the requested value to the provided response writer and sets
// the http code
func writeresponse(t *testing.T, writer http.ResponseWriter, value interface{}, code int) {
writer.WriteHeader(code)
unmarshalled, err := json.Marshal(value)
if err != nil {
t.Error(err.Error())
return
}
data := []byte(`)]}'` + "\n" + string(unmarshalled))
if _, err := writer.Write(data); err != nil {
t.Error(err.Error())
return
}
}
func testMethod(t *testing.T, r *http.Request, want string) {
if got := r.Method; got != want {
t.Errorf("Request method: %v, want %v", got, want)
}
}
func testRequestURL(t *testing.T, r *http.Request, want string) { // nolint: unparam
if got := r.URL.String(); got != want {
t.Errorf("Request URL: %v, want %v", got, want)
}
}
func testFormValues(t *testing.T, r *http.Request, values testValues) {
want := url.Values{}
for k, v := range values {
want.Add(k, v)
}
if err := r.ParseForm(); err != nil {
t.Error(err)
}
if got := r.Form; !reflect.DeepEqual(got, want) {
t.Errorf("Request parameters: %v, want %v", got, want)
}
}
func testQueryValues(t *testing.T, r *http.Request, values testValues) {
want := url.Values{}
for k, v := range values {
want.Add(k, v)
}
if got := r.URL.Query(); !reflect.DeepEqual(got, want) {
t.Errorf("Request parameters: %v, want %v", got, want)
}
}
func TestNewClient_NoGerritInstance(t *testing.T) {
mockData := []string{"", "://not-existing"}
for _, data := range mockData {
c, err := gerrit.NewClient(context.Background(), data, nil)
if c != nil {
t.Errorf("NewClient return is not nil. Expected no client. Go %+v", c)
}
if err == nil {
t.Error("No error occured by empty Gerrit Instance. Expected one.")
}
}
}
func TestNewClient_Services(t *testing.T) {
c, err := gerrit.NewClient(context.Background(), "https://gerrit-review.googlesource.com/", nil)
if err != nil {
t.Errorf("An error occured. Expected nil. Got %+v.", err)
}
if c.Authentication == nil {
t.Error("No AuthenticationService found.")
}
if c.Access == nil {
t.Error("No AccessService found.")
}
if c.Accounts == nil {
t.Error("No AccountsService found.")
}
if c.Changes == nil {
t.Error("No ChangesService found.")
}
if c.Config == nil {
t.Error("No ConfigService found.")
}
if c.Groups == nil {
t.Error("No GroupsService found.")
}
if c.Plugins == nil {
t.Error("No PluginsService found.")
}
if c.Projects == nil {
t.Error("No ProjectsService found.")
}
}
func TestNewClient_TestErrNoInstanceGiven(t *testing.T) {
_, err := gerrit.NewClient(context.Background(), "", nil)
if err != gerrit.ErrNoInstanceGiven {
t.Error("Expected `ErrNoInstanceGiven`")
}
}
func TestNewClient_NoCredentials(t *testing.T) {
client, err := gerrit.NewClient(context.Background(), "http://localhost/", nil)
if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
}
if client.Authentication.HasAuth() {
t.Error("Expected HasAuth() to return false")
}
}
func TestNewClient_UsernameWithoutPassword(t *testing.T) {
_, err := gerrit.NewClient(context.Background(), "http://foo@localhost/", nil)
if err != gerrit.ErrUserProvidedWithoutPassword {
t.Error("Expected ErrUserProvidedWithoutPassword")
}
}
func TestNewClient_AuthenticationFailed(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/a/accounts/self", func(w http.ResponseWriter, r *http.Request) {
writeresponse(t, w, nil, http.StatusUnauthorized)
})
ctx := context.Background()
serverURL := fmt.Sprintf("http://admin:secret@%s/", testServer.Listener.Addr().String())
client, err := gerrit.NewClient(ctx, serverURL, nil)
if err != gerrit.ErrAuthenticationFailed {
t.Error(err)
}
if client.Authentication.HasAuth() {
t.Error("Expected HasAuth() == false")
}
}
func TestNewClient_DigestAuth(t *testing.T) {
setup()
defer teardown()
account := gerrit.AccountInfo{
AccountID: 100000,
Name: "test",
Email: "test@localhost",
Username: "test"}
hits := 0
testMux.HandleFunc("/a/accounts/self", func(w http.ResponseWriter, r *http.Request) {
hits++
switch hits {
case 1:
w.Header().Set("WWW-Authenticate", makedigestheader(r))
writeresponse(t, w, nil, http.StatusUnauthorized)
case 2:
// go-gerrit should set Authorization in response to a `WWW-Authenticate` header
if !strings.Contains(r.Header.Get("Authorization"), `username="admin"`) {
t.Error(`Missing username="admin"`)
}
writeresponse(t, w, account, http.StatusOK)
case 3:
t.Error("Did not expect another request")
}
})
ctx := context.Background()
serverURL := fmt.Sprintf("http://admin:secret@%s/", testServer.Listener.Addr().String())
client, err := gerrit.NewClient(ctx, serverURL, nil)
if err != nil {
t.Error(err)
}
if !client.Authentication.HasDigestAuth() {
t.Error("Expected HasDigestAuth() == true")
}
}
func TestNewClient_BasicAuth(t *testing.T) {
setup()
defer teardown()
account := gerrit.AccountInfo{
AccountID: 100000,
Name: "test",
Email: "test@localhost",
Username: "test"}
hits := 0
testMux.HandleFunc("/a/accounts/self", func(w http.ResponseWriter, r *http.Request) {
hits++
switch hits {
case 1:
writeresponse(t, w, nil, http.StatusUnauthorized)
case 2:
// The second request should be a basic auth request if the first request, which is for
// digest based auth, fails.
if !strings.HasPrefix(r.Header.Get("Authorization"), "Basic ") {
t.Error("Missing 'Basic ' prefix")
}
writeresponse(t, w, account, http.StatusOK)
case 3:
t.Error("Did not expect another request")
}
})
serverURL := fmt.Sprintf("http://admin:secret@%s/", testServer.Listener.Addr().String())
client, err := gerrit.NewClient(context.Background(), serverURL, nil)
if err != nil {
t.Error(err)
}
if !client.Authentication.HasBasicAuth() {
t.Error("Expected HasBasicAuth() == true")
}
}
func TestNewClient_ReParseURL(t *testing.T) {
urls := map[string][]string{
"http://admin:ZOSOKjgV/kgEkN0bzPJp+oGeJLqpXykqWFJpon/[email protected]:5000/": {
"http://127.0.0.1:5000/", "admin", "ZOSOKjgV/kgEkN0bzPJp+oGeJLqpXykqWFJpon/Ckg",
},
"http://admin:ZOSOKjgV/kgEkN0bzPJp+oGeJLqpXykqWFJpon/[email protected]:5000/foo": {
"http://127.0.0.1:5000/foo", "admin", "ZOSOKjgV/kgEkN0bzPJp+oGeJLqpXykqWFJpon/Ckg",
},
"http://admin:ZOSOKjgV/kgEkN0bzPJp+oGeJLqpXykqWFJpon/[email protected]:5000": {
"http://127.0.0.1:5000", "admin", "ZOSOKjgV/kgEkN0bzPJp+oGeJLqpXykqWFJpon/Ckg",
},
"https://admin:foo/bar@localhost:5": {
"https://localhost:5", "admin", "foo/bar",
},
}
for input, expectations := range urls {
submatches := gerrit.ReParseURL.FindAllStringSubmatch(input, -1)
submatch := submatches[0]
username := submatch[2]
password := submatch[3]
endpoint := fmt.Sprintf(
"%s://%s:%s%s", submatch[1], submatch[4], submatch[5], submatch[6])
if endpoint != expectations[0] {
t.Errorf("%s != %s", expectations[0], endpoint)
}
if username != expectations[1] {
t.Errorf("%s != %s", expectations[1], username)
}
if password != expectations[2] {
t.Errorf("%s != %s", expectations[2], password)
}
}
}
func TestNewClient_BasicAuth_PasswordWithSlashes(t *testing.T) {
setup()
defer teardown()
account := gerrit.AccountInfo{
AccountID: 100000,
Name: "test",
Email: "test@localhost",
Username: "test"}
hits := 0
testMux.HandleFunc("/a/accounts/self", func(w http.ResponseWriter, r *http.Request) {
hits++
switch hits {
case 1:
writeresponse(t, w, nil, http.StatusUnauthorized)
case 2:
// The second request should be a basic auth request if the first request, which is for
// digest based auth, fails.
if !strings.HasPrefix(r.Header.Get("Authorization"), "Basic ") {
t.Error("Missing 'Basic ' prefix")
}
writeresponse(t, w, account, http.StatusOK)
case 3:
t.Error("Did not expect another request")
}
})
serverURL := fmt.Sprintf(
"http://admin:ZOSOKjgV/kgEkN0bzPJp+oGeJLqpXykqWFJpon/Ckg@%s",
testServer.Listener.Addr().String())
client, err := gerrit.NewClient(context.Background(), serverURL, nil)
if err != nil {
t.Error(err)
}
if !client.Authentication.HasAuth() {
t.Error("Expected HasAuth() == true")
}
}
func TestNewClient_CookieAuth(t *testing.T) {
setup()
defer teardown()
account := gerrit.AccountInfo{
AccountID: 100000,
Name: "test",
Email: "test@localhost",
Username: "test"}
hits := 0
testMux.HandleFunc("/a/accounts/self", func(w http.ResponseWriter, r *http.Request) {
hits++
switch hits {
case 1:
writeresponse(t, w, nil, http.StatusUnauthorized)
case 2:
writeresponse(t, w, nil, http.StatusUnauthorized)
case 3:
if r.Header.Get("Cookie") != "admin=secret" {
t.Error("Expected cookie to equal 'admin=secret")
}
writeresponse(t, w, account, http.StatusOK)
case 4:
t.Error("Did not expect another request")
}
})
serverURL := fmt.Sprintf("http://admin:secret@%s/", testServer.Listener.Addr().String())
client, err := gerrit.NewClient(context.Background(), serverURL, nil)
if err != nil {
t.Error(err)
}
if !client.Authentication.HasCookieAuth() {
t.Error("Expected HasCookieAuth() == true")
}
}
func TestNewRequest(t *testing.T) {
ctx := context.Background()
c, err := gerrit.NewClient(ctx, testGerritInstanceURL, nil)
if err != nil {
t.Errorf("An error occured. Expected nil. Got %+v.", err)
}
inURL, outURL := "/foo", testGerritInstanceURL+"foo"
inBody, outBody := &gerrit.PermissionRuleInfo{Action: "ALLOW", Force: true, Min: 0, Max: 0}, `{"action":"ALLOW","force":true,"min":0,"max":0}`+"\n"
req, _ := c.NewRequest(ctx, "GET", inURL, inBody)
// Test that relative URL was expanded
if got, want := req.URL.String(), outURL; got != want {
t.Errorf("NewRequest(%q) URL is %v, want %v", inURL, got, want)
}
// Test that body was JSON encoded
body, _ := io.ReadAll(req.Body)
if got, want := string(body), outBody; got != want {
t.Errorf("NewRequest Body is %v, want %v", got, want)
}
}
func TestNewRawPutRequest(t *testing.T) {
ctx := context.Background()
c, err := gerrit.NewClient(ctx, testGerritInstanceURL, nil)
if err != nil {
t.Errorf("An error occured. Expected nil. Got %+v.", err)
}
inURL, outURL := "/foo", testGerritInstanceURL+"foo"
req, _ := c.NewRawPutRequest(ctx, inURL, "test raw PUT contents")
// Test that relative URL was expanded
if got, want := req.URL.String(), outURL; got != want {
t.Errorf("NewRequest(%q) URL is %v, want %v", inURL, got, want)
}
// Test that body was JSON encoded
body, _ := io.ReadAll(req.Body)
if got, want := string(body), "test raw PUT contents"; got != want {
t.Errorf("NewRequest Body is %v, want %v", got, want)
}
}
func testURLParseError(t *testing.T, err error) {
if err == nil {
t.Errorf("Expected error to be returned")
}
if err, ok := err.(*url.Error); !ok || err.Op != "parse" {
t.Errorf("Expected URL parse error, got %+v", err)
}
}
func TestNewRequest_BadURL(t *testing.T) {
ctx := context.Background()
c, err := gerrit.NewClient(ctx, testGerritInstanceURL, nil)
if err != nil {
t.Errorf("An error occured. Expected nil. Got %+v.", err)
}
_, err = c.NewRequest(ctx, "GET", ":", nil)
testURLParseError(t, err)
}
// If a nil body is passed to gerrit.NewRequest, make sure that nil is also passed to http.NewRequest.
// In most cases, passing an io.Reader that returns no content is fine,
// since there is no difference between an HTTP request body that is an empty string versus one that is not set at all.
// However in certain cases, intermediate systems may treat these differently resulting in subtle errors.
func TestNewRequest_EmptyBody(t *testing.T) {
ctx := context.Background()
c, err := gerrit.NewClient(ctx, testGerritInstanceURL, nil)
if err != nil {
t.Errorf("An error occured. Expected nil. Got %+v.", err)
}
req, err := c.NewRequest(ctx, "GET", "/", nil)
if err != nil {
t.Fatalf("NewRequest returned unexpected error: %v", err)
}
if req.Body != nil {
t.Fatalf("constructed request contains a non-nil Body")
}
}
func TestDo(t *testing.T) {
setup()
defer teardown()
type foo struct {
A string
}
testMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if m := "GET"; m != r.Method {
t.Errorf("Request method = %v, want %v", r.Method, m)
}
fmt.Fprint(w, `)]}'`+"\n"+`{"A":"a"}`)
})
req, err := testClient.NewRequest(context.Background(), "GET", "/", nil)
if err != nil {
t.Error(err)
}
body := new(foo)
if _, err := testClient.Do(req, body); err != nil {
t.Error(err)
}
want := &foo{"a"}
if !reflect.DeepEqual(body, want) {
t.Errorf("Response body = %v, want %v", body, want)
}
}
func TestDo_ioWriter(t *testing.T) {
setup()
defer teardown()
content := `)]}'` + "\n" + `{"A":"a"}`
testMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if m := "GET"; m != r.Method {
t.Errorf("Request method = %v, want %v", r.Method, m)
}
fmt.Fprint(w, content)
})
req, err := testClient.NewRequest(context.Background(), "GET", "/", nil)
if err != nil {
t.Error(err)
}
var buf []byte
actual := bytes.NewBuffer(buf)
if _, err := testClient.Do(req, actual); err != nil {
t.Error(err)
}
expected := []byte(content)
if !reflect.DeepEqual(actual.Bytes(), expected) {
t.Errorf("Response body = %v, want %v", actual, string(expected))
}
}
func TestDo_HTTPError(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Bad Request", 400)
})
req, _ := testClient.NewRequest(context.Background(), "GET", "/", nil)
_, err := testClient.Do(req, nil)
if err == nil {
t.Error("Expected HTTP 400 error.")
}
}
// Test handling of an error caused by the internal http client's Do() function.
// A redirect loop is pretty unlikely to occur within the Gerrit API, but does allow us to exercise the right code path.
func TestDo_RedirectLoop(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusFound)
})
req, _ := testClient.NewRequest(context.Background(), "GET", "/", nil)
_, err := testClient.Do(req, nil)
if err == nil {
t.Error("Expected error to be returned.")
}
if err, ok := err.(*url.Error); !ok {
t.Errorf("Expected a URL error; got %#v.", err)
}
}
func TestRemoveMagicPrefixLine(t *testing.T) {
mockData := []struct {
Current, Expected []byte
}{
{[]byte(`{"A":"a"}`), []byte(`{"A":"a"}`)},
{[]byte(`)]}'` + "\n" + `{"A":"a"}`), []byte(`{"A":"a"}`)},
}
for _, mock := range mockData {
body := gerrit.RemoveMagicPrefixLine(mock.Current)
if !reflect.DeepEqual(body, mock.Expected) {
t.Errorf("Response body = %v, want %v", body, mock.Expected)
}
}
}
func TestRemoveMagicPrefixLineDoesNothingWithoutPrefix(t *testing.T) {
mockData := []struct {
Current, Expected []byte
}{
{[]byte(`{"A":"a"}`), []byte(`{"A":"a"}`)},
{[]byte(`{"A":"a"}`), []byte(`{"A":"a"}`)},
}
for _, mock := range mockData {
body := gerrit.RemoveMagicPrefixLine(mock.Current)
if !reflect.DeepEqual(body, mock.Expected) {
t.Errorf("Response body = %v, want %v", body, mock.Expected)
}
}
}
func TestNewClientFailsOnDeadConnection(t *testing.T) {
setup()
serverURL := fmt.Sprintf("http://admin:secret@%s/", testServer.Listener.Addr().String())
teardown() // Closes the server
_, err := gerrit.NewClient(context.Background(), serverURL, nil)
if err == nil {
t.Fatal("Expected err to not be nil")
}
if !strings.Contains(err.Error(), "connection refused") {
t.Fatalf("Unexpected error. 'connected refused' not found in %s", err.Error())
}
}