1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "encoding/json"
11 "fmt"
12 "net/http"
13 "strings"
14 "testing"
15 "time"
16
17 "github.com/ProtonMail/go-crypto/openpgp"
18 "github.com/google/go-cmp/cmp"
19 )
20
21 func TestCommit_Marshal(t *testing.T) {
22 testJSONMarshal(t, &Commit{}, "{}")
23
24 u := &Commit{
25 SHA: String("s"),
26 Author: &CommitAuthor{
27 Date: &Timestamp{referenceTime},
28 Name: String("n"),
29 Email: String("e"),
30 Login: String("u"),
31 },
32 Committer: &CommitAuthor{
33 Date: &Timestamp{referenceTime},
34 Name: String("n"),
35 Email: String("e"),
36 Login: String("u"),
37 },
38 Message: String("m"),
39 Tree: &Tree{
40 SHA: String("s"),
41 Entries: []*TreeEntry{{
42 SHA: String("s"),
43 Path: String("p"),
44 Mode: String("m"),
45 Type: String("t"),
46 Size: Int(1),
47 Content: String("c"),
48 URL: String("u"),
49 }},
50 Truncated: Bool(false),
51 },
52 Parents: nil,
53 Stats: &CommitStats{
54 Additions: Int(1),
55 Deletions: Int(1),
56 Total: Int(1),
57 },
58 HTMLURL: String("h"),
59 URL: String("u"),
60 Verification: &SignatureVerification{
61 Verified: Bool(false),
62 Reason: String("r"),
63 Signature: String("s"),
64 Payload: String("p"),
65 },
66 NodeID: String("n"),
67 CommentCount: Int(1),
68 SigningKey: &openpgp.Entity{},
69 }
70
71 want := `{
72 "sha": "s",
73 "author": {
74 "date": ` + referenceTimeStr + `,
75 "name": "n",
76 "email": "e",
77 "username": "u"
78 },
79 "committer": {
80 "date": ` + referenceTimeStr + `,
81 "name": "n",
82 "email": "e",
83 "username": "u"
84 },
85 "message": "m",
86 "tree": {
87 "sha": "s",
88 "tree": [
89 {
90 "sha": "s",
91 "path": "p",
92 "mode": "m",
93 "type": "t",
94 "size": 1,
95 "content": "c",
96 "url": "u"
97 }
98 ],
99 "truncated": false
100 },
101 "stats": {
102 "additions": 1,
103 "deletions": 1,
104 "total": 1
105 },
106 "html_url": "h",
107 "url": "u",
108 "verification": {
109 "verified": false,
110 "reason": "r",
111 "signature": "s",
112 "payload": "p"
113 },
114 "node_id": "n",
115 "comment_count": 1
116 }`
117
118 testJSONMarshal(t, u, want)
119 }
120
121 func TestGitService_GetCommit(t *testing.T) {
122 client, mux, _, teardown := setup()
123 defer teardown()
124
125 mux.HandleFunc("/repos/o/r/git/commits/s", func(w http.ResponseWriter, r *http.Request) {
126 testMethod(t, r, "GET")
127 fmt.Fprint(w, `{"sha":"s","message":"Commit Message.","author":{"name":"n"}}`)
128 })
129
130 ctx := context.Background()
131 commit, _, err := client.Git.GetCommit(ctx, "o", "r", "s")
132 if err != nil {
133 t.Errorf("Git.GetCommit returned error: %v", err)
134 }
135
136 want := &Commit{SHA: String("s"), Message: String("Commit Message."), Author: &CommitAuthor{Name: String("n")}}
137 if !cmp.Equal(commit, want) {
138 t.Errorf("Git.GetCommit returned %+v, want %+v", commit, want)
139 }
140
141 const methodName = "GetCommit"
142 testBadOptions(t, methodName, func() (err error) {
143 _, _, err = client.Git.GetCommit(ctx, "\n", "\n", "\n")
144 return err
145 })
146
147 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
148 got, resp, err := client.Git.GetCommit(ctx, "o", "r", "s")
149 if got != nil {
150 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
151 }
152 return resp, err
153 })
154 }
155
156 func TestGitService_GetCommit_invalidOwner(t *testing.T) {
157 client, _, _, teardown := setup()
158 defer teardown()
159
160 ctx := context.Background()
161 _, _, err := client.Git.GetCommit(ctx, "%", "%", "%")
162 testURLParseError(t, err)
163 }
164
165 func TestGitService_CreateCommit(t *testing.T) {
166 client, mux, _, teardown := setup()
167 defer teardown()
168
169 input := &Commit{
170 Message: String("Commit Message."),
171 Tree: &Tree{SHA: String("t")},
172 Parents: []*Commit{{SHA: String("p")}},
173 }
174
175 mux.HandleFunc("/repos/o/r/git/commits", func(w http.ResponseWriter, r *http.Request) {
176 v := new(createCommit)
177 json.NewDecoder(r.Body).Decode(v)
178
179 testMethod(t, r, "POST")
180
181 want := &createCommit{
182 Message: input.Message,
183 Tree: String("t"),
184 Parents: []string{"p"},
185 }
186 if !cmp.Equal(v, want) {
187 t.Errorf("Request body = %+v, want %+v", v, want)
188 }
189 fmt.Fprint(w, `{"sha":"s"}`)
190 })
191
192 ctx := context.Background()
193 commit, _, err := client.Git.CreateCommit(ctx, "o", "r", input)
194 if err != nil {
195 t.Errorf("Git.CreateCommit returned error: %v", err)
196 }
197
198 want := &Commit{SHA: String("s")}
199 if !cmp.Equal(commit, want) {
200 t.Errorf("Git.CreateCommit returned %+v, want %+v", commit, want)
201 }
202
203 const methodName = "CreateCommit"
204 testBadOptions(t, methodName, func() (err error) {
205 _, _, err = client.Git.CreateCommit(ctx, "\n", "\n", input)
206 return err
207 })
208
209 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
210 got, resp, err := client.Git.CreateCommit(ctx, "o", "r", input)
211 if got != nil {
212 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
213 }
214 return resp, err
215 })
216 }
217
218 func TestGitService_CreateSignedCommit(t *testing.T) {
219 client, mux, _, teardown := setup()
220 defer teardown()
221
222 signature := "----- BEGIN PGP SIGNATURE -----\n\naaaa\naaaa\n----- END PGP SIGNATURE -----"
223
224 input := &Commit{
225 Message: String("Commit Message."),
226 Tree: &Tree{SHA: String("t")},
227 Parents: []*Commit{{SHA: String("p")}},
228 Verification: &SignatureVerification{
229 Signature: String(signature),
230 },
231 }
232
233 mux.HandleFunc("/repos/o/r/git/commits", func(w http.ResponseWriter, r *http.Request) {
234 v := new(createCommit)
235 json.NewDecoder(r.Body).Decode(v)
236
237 testMethod(t, r, "POST")
238
239 want := &createCommit{
240 Message: input.Message,
241 Tree: String("t"),
242 Parents: []string{"p"},
243 Signature: String(signature),
244 }
245 if !cmp.Equal(v, want) {
246 t.Errorf("Request body = %+v, want %+v", v, want)
247 }
248 fmt.Fprint(w, `{"sha":"commitSha"}`)
249 })
250
251 ctx := context.Background()
252 commit, _, err := client.Git.CreateCommit(ctx, "o", "r", input)
253 if err != nil {
254 t.Errorf("Git.CreateCommit returned error: %v", err)
255 }
256
257 want := &Commit{SHA: String("commitSha")}
258 if !cmp.Equal(commit, want) {
259 t.Errorf("Git.CreateCommit returned %+v, want %+v", commit, want)
260 }
261
262 const methodName = "CreateCommit"
263 testBadOptions(t, methodName, func() (err error) {
264 _, _, err = client.Git.CreateCommit(ctx, "\n", "\n", input)
265 return err
266 })
267
268 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
269 got, resp, err := client.Git.CreateCommit(ctx, "o", "r", input)
270 if got != nil {
271 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
272 }
273 return resp, err
274 })
275 }
276
277 func TestGitService_CreateSignedCommitWithInvalidParams(t *testing.T) {
278 client, _, _, teardown := setup()
279 defer teardown()
280
281 input := &Commit{
282 SigningKey: &openpgp.Entity{},
283 }
284
285 ctx := context.Background()
286 _, _, err := client.Git.CreateCommit(ctx, "o", "r", input)
287 if err == nil {
288 t.Errorf("Expected error to be returned because invalid params were passed")
289 }
290 }
291
292 func TestGitService_CreateSignedCommitWithNilCommit(t *testing.T) {
293 client, _, _, teardown := setup()
294 defer teardown()
295
296 ctx := context.Background()
297 _, _, err := client.Git.CreateCommit(ctx, "o", "r", nil)
298 if err == nil {
299 t.Errorf("Expected error to be returned because commit=nil")
300 }
301 }
302
303 func TestGitService_CreateSignedCommitWithKey(t *testing.T) {
304 client, mux, _, teardown := setup()
305 defer teardown()
306 s := strings.NewReader(testGPGKey)
307 keyring, err := openpgp.ReadArmoredKeyRing(s)
308 if err != nil {
309 t.Errorf("Error reading keyring: %+v", err)
310 }
311
312
313 keyring[0].Identities["go-github <go-github@github.com>"].SelfSignature.KeyLifetimeSecs = nil
314
315 date, _ := time.Parse("Mon Jan 02 15:04:05 2006 -0700", "Thu May 04 00:03:43 2017 +0200")
316 author := CommitAuthor{
317 Name: String("go-github"),
318 Email: String("go-github@github.com"),
319 Date: &Timestamp{date},
320 }
321 input := &Commit{
322 Message: String("Commit Message."),
323 Tree: &Tree{SHA: String("t")},
324 Parents: []*Commit{{SHA: String("p")}},
325 SigningKey: keyring[0],
326 Author: &author,
327 }
328
329 messageReader := strings.NewReader(`tree t
330 parent p
331 author go-github <go-github@github.com> 1493849023 +0200
332 committer go-github <go-github@github.com> 1493849023 +0200
333
334 Commit Message.`)
335
336 mux.HandleFunc("/repos/o/r/git/commits", func(w http.ResponseWriter, r *http.Request) {
337 v := new(createCommit)
338 json.NewDecoder(r.Body).Decode(v)
339
340 testMethod(t, r, "POST")
341
342 want := &createCommit{
343 Message: input.Message,
344 Tree: String("t"),
345 Parents: []string{"p"},
346 Author: &author,
347 }
348
349 sigReader := strings.NewReader(*v.Signature)
350 signer, err := openpgp.CheckArmoredDetachedSignature(keyring, messageReader, sigReader, nil)
351 if err != nil {
352 t.Errorf("Error verifying signature: %+v", err)
353 }
354 if signer.Identities["go-github <go-github@github.com>"].Name != "go-github <go-github@github.com>" {
355 t.Errorf("Signer is incorrect. got: %+v, want %+v", signer.Identities["go-github <go-github@github.com>"].Name, "go-github <go-github@github.com>")
356 }
357
358 v.Signature = nil
359 if !cmp.Equal(v, want) {
360 t.Errorf("Request body = %+v, want %+v", v, want)
361 }
362 fmt.Fprint(w, `{"sha":"commitSha"}`)
363 })
364
365 ctx := context.Background()
366 commit, _, err := client.Git.CreateCommit(ctx, "o", "r", input)
367 if err != nil {
368 t.Errorf("Git.CreateCommit returned error: %v", err)
369 }
370
371 want := &Commit{SHA: String("commitSha")}
372 if !cmp.Equal(commit, want) {
373 t.Errorf("Git.CreateCommit returned %+v, want %+v", commit, want)
374 }
375 }
376
377 func TestGitService_createSignature_nilSigningKey(t *testing.T) {
378 a := &createCommit{
379 Message: String("Commit Message."),
380 Tree: String("t"),
381 Parents: []string{"p"},
382 }
383
384 _, err := createSignature(nil, a)
385
386 if err == nil {
387 t.Errorf("Expected error to be returned because no author was passed")
388 }
389 }
390
391 func TestGitService_createSignature_nilCommit(t *testing.T) {
392 _, err := createSignature(&openpgp.Entity{}, nil)
393
394 if err == nil {
395 t.Errorf("Expected error to be returned because no author was passed")
396 }
397 }
398
399 func TestGitService_createSignature_noAuthor(t *testing.T) {
400 a := &createCommit{
401 Message: String("Commit Message."),
402 Tree: String("t"),
403 Parents: []string{"p"},
404 }
405
406 _, err := createSignature(&openpgp.Entity{}, a)
407
408 if err == nil {
409 t.Errorf("Expected error to be returned because no author was passed")
410 }
411 }
412
413 func TestGitService_createSignature_invalidKey(t *testing.T) {
414 date, _ := time.Parse("Mon Jan 02 15:04:05 2006 -0700", "Thu May 04 00:03:43 2017 +0200")
415 authorName := "go-github"
416 authorEmail := "go-github@github.com"
417
418 signKey, _ := openpgp.NewEntity(authorName, "", authorEmail, nil)
419 _ = signKey.RevokeKey(0, "Invalidate key", nil)
420
421 _, err := createSignature(signKey, &createCommit{
422 Message: String("Commit Message."),
423 Tree: String("t"),
424 Parents: []string{"p"},
425 Author: &CommitAuthor{
426 Name: String("go-github"),
427 Email: String("go-github@github.com"),
428 Date: &Timestamp{date},
429 },
430 })
431
432 if err == nil {
433 t.Errorf("Expected error to be returned due to invalid key")
434 }
435 }
436
437 func TestGitService_createSignatureMessage_nilCommit(t *testing.T) {
438 _, err := createSignatureMessage(nil)
439 if err == nil {
440 t.Errorf("Expected error to be returned due to nil key")
441 }
442 }
443
444 func TestGitService_createSignatureMessage_nilMessage(t *testing.T) {
445 date, _ := time.Parse("Mon Jan 02 15:04:05 2006 -0700", "Thu May 04 00:03:43 2017 +0200")
446
447 _, err := createSignatureMessage(&createCommit{
448 Message: nil,
449 Parents: []string{"p"},
450 Author: &CommitAuthor{
451 Name: String("go-github"),
452 Email: String("go-github@github.com"),
453 Date: &Timestamp{date},
454 },
455 })
456 if err == nil {
457 t.Errorf("Expected error to be returned due to nil key")
458 }
459 }
460
461 func TestGitService_createSignatureMessage_emptyMessage(t *testing.T) {
462 date, _ := time.Parse("Mon Jan 02 15:04:05 2006 -0700", "Thu May 04 00:03:43 2017 +0200")
463 emptyString := ""
464 _, err := createSignatureMessage(&createCommit{
465 Message: &emptyString,
466 Parents: []string{"p"},
467 Author: &CommitAuthor{
468 Name: String("go-github"),
469 Email: String("go-github@github.com"),
470 Date: &Timestamp{date},
471 },
472 })
473 if err == nil {
474 t.Errorf("Expected error to be returned due to nil key")
475 }
476 }
477
478 func TestGitService_createSignatureMessage_nilAuthor(t *testing.T) {
479 _, err := createSignatureMessage(&createCommit{
480 Message: String("Commit Message."),
481 Parents: []string{"p"},
482 Author: nil,
483 })
484 if err == nil {
485 t.Errorf("Expected error to be returned due to nil key")
486 }
487 }
488
489 func TestGitService_createSignatureMessage_withoutTree(t *testing.T) {
490 date, _ := time.Parse("Mon Jan 02 15:04:05 2006 -0700", "Thu May 04 00:03:43 2017 +0200")
491
492 msg, _ := createSignatureMessage(&createCommit{
493 Message: String("Commit Message."),
494 Parents: []string{"p"},
495 Author: &CommitAuthor{
496 Name: String("go-github"),
497 Email: String("go-github@github.com"),
498 Date: &Timestamp{date},
499 },
500 })
501 expected := `parent p
502 author go-github <go-github@github.com> 1493849023 +0200
503 committer go-github <go-github@github.com> 1493849023 +0200
504
505 Commit Message.`
506 if msg != expected {
507 t.Errorf("Returned message incorrect. returned %s, want %s", msg, expected)
508 }
509 }
510
511 func TestGitService_createSignatureMessage_withoutCommitter(t *testing.T) {
512 date, _ := time.Parse("Mon Jan 02 15:04:05 2006 -0700", "Thu May 04 00:03:43 2017 +0200")
513
514 msg, _ := createSignatureMessage(&createCommit{
515 Message: String("Commit Message."),
516 Parents: []string{"p"},
517 Author: &CommitAuthor{
518 Name: String("go-github"),
519 Email: String("go-github@github.com"),
520 Date: &Timestamp{date},
521 },
522 Committer: &CommitAuthor{
523 Name: String("foo"),
524 Email: String("foo@bar.com"),
525 Date: &Timestamp{date},
526 },
527 })
528 expected := `parent p
529 author go-github <go-github@github.com> 1493849023 +0200
530 committer foo <foo@bar.com> 1493849023 +0200
531
532 Commit Message.`
533 if msg != expected {
534 t.Errorf("Returned message incorrect. returned %s, want %s", msg, expected)
535 }
536 }
537
538 func TestGitService_CreateCommit_invalidOwner(t *testing.T) {
539 client, _, _, teardown := setup()
540 defer teardown()
541
542 ctx := context.Background()
543 _, _, err := client.Git.CreateCommit(ctx, "%", "%", &Commit{})
544 testURLParseError(t, err)
545 }
546
547 const testGPGKey = `
548 -----BEGIN PGP PRIVATE KEY BLOCK-----
549
550 lQOYBFyi1qYBCAD3EPfLJzIt4qkAceUKkhdvfaIvOsBwXbfr5sSu/lkMqL0Wq47+
551 iv+SRwOC7zvN8SlB8nPUgs5dbTRCJJfG5MAqTRR7KZRbyq2jBpi4BtmO30Ul/qId
552 3A18cVUfgVbxH85K9bdnyOxep/Q2NjLjTKmWLkzgmgkfbUmSLuWW9HRXPjYy9B7i
553 dOFD6GdkN/HwPAaId8ym0TE1mIuSpw8UQHyxusAkK52Pn4h/PgJhLTzbSi1X2eDt
554 OgzjhbdxTPzKFQfs97dY8y9C7Bt+CqH6Bvr3785LeKdxiUnCjfUJ+WAoJy780ec+
555 IVwSpPp1CaEtzu73w6GH5945GELHE8HRe25FABEBAAEAB/9dtx72/VAoXZCTbaBe
556 iRnAnZwWZCe4t6PbJHa4lhv7FEpdPggIf3r/5lXrpYk+zdpDfI75LgDPKWwoJq83
557 r29A3GoHabcvtkp0yzzEmTyO2BvnlJWz09N9v5N1Vt8+qTzb7CZ8hJc8NGMK6TYW
558 R+8P21In4+XP+OluPMGzp9g1etHScLhQUtF/xcN3JQGkeq4CPX6jUSYlJNeEtuLm
559 xjBTLBdg8zK5mJ3tolvnS/VhSTdiBeUaYtVt/qxq+fPqdFGHrO5H9ORbt56ahU+f
560 Ne86sOjQfJZPsx9z8ffP+XhLZPT1ZUGJMI/Vysx9gwDiEnaxrCJ02fO0Dnqsj/o2
561 T14lBAD55+KtaS0C0OpHpA/F+XhL3IDcYQOYgu8idBTshr4vv7M+jdZqpECOn72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594 bfaoIQz1h8fX5uSGKU+hXatI6sltD9PrhwwhdqJNcQ0K1dRkm24olO4I/sJwactI
595 G3r1UTq6BMV94eIyS/zZH5xChlOUavy9PrgU3kAK21bdmAFuNwbHnN34BBUk9J6f
596 IIxEZUOxw2CrKhsubUOuiQE8BBgBCAAmFiEELZ6AMqOpBMVblK0uiKTQXVy+MAsF
597 Alyi1qYCGwwFCQPCZwAACgkQiKTQXVy+MAstJAf/Tm2hfagVjzgJ5pFHmpP+fYxp
598 8dIPZLonP5HW12iaSOXThtvWBY578Cb9RmU+WkHyPXg8SyshW7aco4HrUDk+Qmyi
599 f9BvHS5RsLbyPlhgCqNkn+3QS62fZiIlbHLrQ/6iHXkgLV04Fnj+F4v8YYpOI9nY
600 NFc5iWm0zZRcLiRKZk1up8SCngyolcjVuTuCXDKyAUX1jRqDu7tlN0qVH0CYDGch
601 BqTKXNkzAvV+CKOyaUILSBBWdef+cxVrDCJuuC3894x3G1FjJycOy0m9PArvGtSG
602 g7/0Bp9oLXwiHzFoUMDvx+WlPnPHQNcufmQXUNdZvg+Ad4/unEU81EGDBDz3Eg==
603 =VFSn
604 -----END PGP PRIVATE KEY BLOCK-----`
605
606 func TestSignatureVerification_Marshal(t *testing.T) {
607 testJSONMarshal(t, &SignatureVerification{}, "{}")
608
609 u := &SignatureVerification{
610 Verified: Bool(true),
611 Reason: String("reason"),
612 Signature: String("sign"),
613 Payload: String("payload"),
614 }
615
616 want := `{
617 "verified": true,
618 "reason": "reason",
619 "signature": "sign",
620 "payload": "payload"
621 }`
622
623 testJSONMarshal(t, u, want)
624 }
625
626 func TestCommitAuthor_Marshal(t *testing.T) {
627 testJSONMarshal(t, &CommitAuthor{}, "{}")
628
629 u := &CommitAuthor{
630 Date: &Timestamp{referenceTime},
631 Name: String("name"),
632 Email: String("email"),
633 Login: String("login"),
634 }
635
636 want := `{
637 "date": ` + referenceTimeStr + `,
638 "name": "name",
639 "email": "email",
640 "username": "login"
641 }`
642
643 testJSONMarshal(t, u, want)
644 }
645
646 func TestCreateCommit_Marshal(t *testing.T) {
647 testJSONMarshal(t, &createCommit{}, "{}")
648
649 u := &createCommit{
650 Author: &CommitAuthor{
651 Date: &Timestamp{referenceTime},
652 Name: String("name"),
653 Email: String("email"),
654 Login: String("login"),
655 },
656 Committer: &CommitAuthor{
657 Date: &Timestamp{referenceTime},
658 Name: String("name"),
659 Email: String("email"),
660 Login: String("login"),
661 },
662 Message: String("message"),
663 Tree: String("tree"),
664 Parents: []string{"p"},
665 Signature: String("sign"),
666 }
667
668 want := `{
669 "author": {
670 "date": ` + referenceTimeStr + `,
671 "name": "name",
672 "email": "email",
673 "username": "login"
674 },
675 "committer": {
676 "date": ` + referenceTimeStr + `,
677 "name": "name",
678 "email": "email",
679 "username": "login"
680 },
681 "message": "message",
682 "tree": "tree",
683 "parents": [
684 "p"
685 ],
686 "signature": "sign"
687 }`
688
689 testJSONMarshal(t, u, want)
690 }
691
View as plain text