...

Source file src/github.com/google/go-github/v45/github/git_commits_test.go

Documentation: github.com/google/go-github/v45/github

     1  // Copyright 2013 The go-github AUTHORS. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     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/google/go-cmp/cmp"
    18  	"golang.org/x/crypto/openpgp"
    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:  &referenceTime,
    28  			Name:  String("n"),
    29  			Email: String("e"),
    30  			Login: String("u"),
    31  		},
    32  		Committer: &CommitAuthor{
    33  			Date:  &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  	date, _ := time.Parse("Mon Jan 02 15:04:05 2006 -0700", "Thu May 04 00:03:43 2017 +0200")
   313  	author := CommitAuthor{
   314  		Name:  String("go-github"),
   315  		Email: String("go-github@github.com"),
   316  		Date:  &date,
   317  	}
   318  	input := &Commit{
   319  		Message:    String("Commit Message."),
   320  		Tree:       &Tree{SHA: String("t")},
   321  		Parents:    []*Commit{{SHA: String("p")}},
   322  		SigningKey: keyring[0],
   323  		Author:     &author,
   324  	}
   325  
   326  	messageReader := strings.NewReader(`tree t
   327  parent p
   328  author go-github <go-github@github.com> 1493849023 +0200
   329  committer go-github <go-github@github.com> 1493849023 +0200
   330  
   331  Commit Message.`)
   332  
   333  	mux.HandleFunc("/repos/o/r/git/commits", func(w http.ResponseWriter, r *http.Request) {
   334  		v := new(createCommit)
   335  		json.NewDecoder(r.Body).Decode(v)
   336  
   337  		testMethod(t, r, "POST")
   338  
   339  		want := &createCommit{
   340  			Message: input.Message,
   341  			Tree:    String("t"),
   342  			Parents: []string{"p"},
   343  			Author:  &author,
   344  		}
   345  
   346  		sigReader := strings.NewReader(*v.Signature)
   347  		signer, err := openpgp.CheckArmoredDetachedSignature(keyring, messageReader, sigReader)
   348  		if err != nil {
   349  			t.Errorf("Error verifying signature: %+v", err)
   350  		}
   351  		if signer.Identities["go-github <go-github@github.com>"].Name != "go-github <go-github@github.com>" {
   352  			t.Errorf("Signer is incorrect. got: %+v, want %+v", signer.Identities["go-github <go-github@github.com>"].Name, "go-github <go-github@github.com>")
   353  		}
   354  		// Nullify Signature since we checked it above
   355  		v.Signature = nil
   356  		if !cmp.Equal(v, want) {
   357  			t.Errorf("Request body = %+v, want %+v", v, want)
   358  		}
   359  		fmt.Fprint(w, `{"sha":"commitSha"}`)
   360  	})
   361  
   362  	ctx := context.Background()
   363  	commit, _, err := client.Git.CreateCommit(ctx, "o", "r", input)
   364  	if err != nil {
   365  		t.Errorf("Git.CreateCommit returned error: %v", err)
   366  	}
   367  
   368  	want := &Commit{SHA: String("commitSha")}
   369  	if !cmp.Equal(commit, want) {
   370  		t.Errorf("Git.CreateCommit returned %+v, want %+v", commit, want)
   371  	}
   372  }
   373  
   374  func TestGitService_createSignature_nilSigningKey(t *testing.T) {
   375  	a := &createCommit{
   376  		Message: String("Commit Message."),
   377  		Tree:    String("t"),
   378  		Parents: []string{"p"},
   379  	}
   380  
   381  	_, err := createSignature(nil, a)
   382  
   383  	if err == nil {
   384  		t.Errorf("Expected error to be returned because no author was passed")
   385  	}
   386  }
   387  
   388  func TestGitService_createSignature_nilCommit(t *testing.T) {
   389  	_, err := createSignature(&openpgp.Entity{}, nil)
   390  
   391  	if err == nil {
   392  		t.Errorf("Expected error to be returned because no author was passed")
   393  	}
   394  }
   395  
   396  func TestGitService_createSignature_noAuthor(t *testing.T) {
   397  	a := &createCommit{
   398  		Message: String("Commit Message."),
   399  		Tree:    String("t"),
   400  		Parents: []string{"p"},
   401  	}
   402  
   403  	_, err := createSignature(&openpgp.Entity{}, a)
   404  
   405  	if err == nil {
   406  		t.Errorf("Expected error to be returned because no author was passed")
   407  	}
   408  }
   409  
   410  func TestGitService_createSignature_invalidKey(t *testing.T) {
   411  	date, _ := time.Parse("Mon Jan 02 15:04:05 2006 -0700", "Thu May 04 00:03:43 2017 +0200")
   412  
   413  	_, err := createSignature(&openpgp.Entity{}, &createCommit{
   414  		Message: String("Commit Message."),
   415  		Tree:    String("t"),
   416  		Parents: []string{"p"},
   417  		Author: &CommitAuthor{
   418  			Name:  String("go-github"),
   419  			Email: String("go-github@github.com"),
   420  			Date:  &date,
   421  		},
   422  	})
   423  
   424  	if err == nil {
   425  		t.Errorf("Expected error to be returned due to invalid key")
   426  	}
   427  }
   428  
   429  func TestGitService_createSignatureMessage_nilCommit(t *testing.T) {
   430  	_, err := createSignatureMessage(nil)
   431  	if err == nil {
   432  		t.Errorf("Expected error to be returned due to nil key")
   433  	}
   434  }
   435  
   436  func TestGitService_createSignatureMessage_nilMessage(t *testing.T) {
   437  	date, _ := time.Parse("Mon Jan 02 15:04:05 2006 -0700", "Thu May 04 00:03:43 2017 +0200")
   438  
   439  	_, err := createSignatureMessage(&createCommit{
   440  		Message: nil,
   441  		Parents: []string{"p"},
   442  		Author: &CommitAuthor{
   443  			Name:  String("go-github"),
   444  			Email: String("go-github@github.com"),
   445  			Date:  &date,
   446  		},
   447  	})
   448  	if err == nil {
   449  		t.Errorf("Expected error to be returned due to nil key")
   450  	}
   451  }
   452  
   453  func TestGitService_createSignatureMessage_emptyMessage(t *testing.T) {
   454  	date, _ := time.Parse("Mon Jan 02 15:04:05 2006 -0700", "Thu May 04 00:03:43 2017 +0200")
   455  	emptyString := ""
   456  	_, err := createSignatureMessage(&createCommit{
   457  		Message: &emptyString,
   458  		Parents: []string{"p"},
   459  		Author: &CommitAuthor{
   460  			Name:  String("go-github"),
   461  			Email: String("go-github@github.com"),
   462  			Date:  &date,
   463  		},
   464  	})
   465  	if err == nil {
   466  		t.Errorf("Expected error to be returned due to nil key")
   467  	}
   468  }
   469  
   470  func TestGitService_createSignatureMessage_nilAuthor(t *testing.T) {
   471  	_, err := createSignatureMessage(&createCommit{
   472  		Message: String("Commit Message."),
   473  		Parents: []string{"p"},
   474  		Author:  nil,
   475  	})
   476  	if err == nil {
   477  		t.Errorf("Expected error to be returned due to nil key")
   478  	}
   479  }
   480  
   481  func TestGitService_createSignatureMessage_withoutTree(t *testing.T) {
   482  	date, _ := time.Parse("Mon Jan 02 15:04:05 2006 -0700", "Thu May 04 00:03:43 2017 +0200")
   483  
   484  	msg, _ := createSignatureMessage(&createCommit{
   485  		Message: String("Commit Message."),
   486  		Parents: []string{"p"},
   487  		Author: &CommitAuthor{
   488  			Name:  String("go-github"),
   489  			Email: String("go-github@github.com"),
   490  			Date:  &date,
   491  		},
   492  	})
   493  	expected := `parent p
   494  author go-github <go-github@github.com> 1493849023 +0200
   495  committer go-github <go-github@github.com> 1493849023 +0200
   496  
   497  Commit Message.`
   498  	if msg != expected {
   499  		t.Errorf("Returned message incorrect. returned %s, want %s", msg, expected)
   500  	}
   501  }
   502  
   503  func TestGitService_createSignatureMessage_withoutCommitter(t *testing.T) {
   504  	date, _ := time.Parse("Mon Jan 02 15:04:05 2006 -0700", "Thu May 04 00:03:43 2017 +0200")
   505  
   506  	msg, _ := createSignatureMessage(&createCommit{
   507  		Message: String("Commit Message."),
   508  		Parents: []string{"p"},
   509  		Author: &CommitAuthor{
   510  			Name:  String("go-github"),
   511  			Email: String("go-github@github.com"),
   512  			Date:  &date,
   513  		},
   514  		Committer: &CommitAuthor{
   515  			Name:  String("foo"),
   516  			Email: String("foo@bar.com"),
   517  			Date:  &date,
   518  		},
   519  	})
   520  	expected := `parent p
   521  author go-github <go-github@github.com> 1493849023 +0200
   522  committer foo <foo@bar.com> 1493849023 +0200
   523  
   524  Commit Message.`
   525  	if msg != expected {
   526  		t.Errorf("Returned message incorrect. returned %s, want %s", msg, expected)
   527  	}
   528  }
   529  
   530  func TestGitService_CreateCommit_invalidOwner(t *testing.T) {
   531  	client, _, _, teardown := setup()
   532  	defer teardown()
   533  
   534  	ctx := context.Background()
   535  	_, _, err := client.Git.CreateCommit(ctx, "%", "%", &Commit{})
   536  	testURLParseError(t, err)
   537  }
   538  
   539  const testGPGKey = `
   540  -----BEGIN PGP PRIVATE KEY BLOCK-----
   541  
   542  lQOYBFyi1qYBCAD3EPfLJzIt4qkAceUKkhdvfaIvOsBwXbfr5sSu/lkMqL0Wq47+
   543  iv+SRwOC7zvN8SlB8nPUgs5dbTRCJJfG5MAqTRR7KZRbyq2jBpi4BtmO30Ul/qId
   544  3A18cVUfgVbxH85K9bdnyOxep/Q2NjLjTKmWLkzgmgkfbUmSLuWW9HRXPjYy9B7i
   545  dOFD6GdkN/HwPAaId8ym0TE1mIuSpw8UQHyxusAkK52Pn4h/PgJhLTzbSi1X2eDt
   546  OgzjhbdxTPzKFQfs97dY8y9C7Bt+CqH6Bvr3785LeKdxiUnCjfUJ+WAoJy780ec+
   547  IVwSpPp1CaEtzu73w6GH5945GELHE8HRe25FABEBAAEAB/9dtx72/VAoXZCTbaBe
   548  iRnAnZwWZCe4t6PbJHa4lhv7FEpdPggIf3r/5lXrpYk+zdpDfI75LgDPKWwoJq83
   549  r29A3GoHabcvtkp0yzzEmTyO2BvnlJWz09N9v5N1Vt8+qTzb7CZ8hJc8NGMK6TYW
   550  R+8P21In4+XP+OluPMGzp9g1etHScLhQUtF/xcN3JQGkeq4CPX6jUSYlJNeEtuLm
   551  xjBTLBdg8zK5mJ3tolvnS/VhSTdiBeUaYtVt/qxq+fPqdFGHrO5H9ORbt56ahU+f
   552  Ne86sOjQfJZPsx9z8ffP+XhLZPT1ZUGJMI/Vysx9gwDiEnaxrCJ02fO0Dnqsj/o2
   553  T14lBAD55+KtaS0C0OpHpA/F+XhL3IDcYQOYgu8idBTshr4vv7M+jdZqpECOn72Q
   554  8SZJ+gYMcA9Z07Afnin1DVdtxiMN/tbyOu7e1BE7y77eA+zQw4PjLJPZJMbco7z+
   555  q9ZnZF3GyRyil6HkKUTfrao8AMtb0allZnqXwpPb5Mza32VqtwQA/RdbG6OIS6og
   556  OpP7zKu4GP4guBk8NrVpVuV5Xz4r8JlL+POt0TadlT93coW/SajLrN/eeUwk6jQw
   557  wrabmIGMarG5mrC4tnXLze5LICJTpOuqCACyFwL6w/ag+c7Qt9t9hvLMDFifcZW/
   558  mylqY7Z1eVcnbOcFsQG+0LzJBU0qouMEAKkXmJcQ3lJM8yoJuYOvbwexVR+5Y+5v
   559  FNEGPlp3H/fq6ETYWHjMxPOE5dvGbQL8oKWZgkkHEGAKAavEGebM/y/qIPOCAluT
   560  tn1sfx//n6kTMhswpg/3+BciUaJFjwYbIwUH5XD0vFbe9O2VOfTVdo1p19wegVs5
   561  LMf8rWFWYXtqUgG0IGdvLWdpdGh1YiA8Z28tZ2l0aHViQGdpdGh1Yi5jb20+iQFU
   562  BBMBCAA+FiEELZ6AMqOpBMVblK0uiKTQXVy+MAsFAlyi1qYCGwMFCQPCZwAFCwkI
   563  BwIGFQoJCAsCBBYCAwECHgECF4AACgkQiKTQXVy+MAtEYggA0LRecz71HUjEKXJj
   564  C5Wgds1hZ0q+g3ew7zms4fuascd/2PqT5lItHU3oezdzMOHetSPvPzJILjl7RYcY
   565  pWvoyzEBC5MutlmuzfwUa7qYCiuRDkYRjke8a4o8ijsxc8ANXwulXcI3udjAZdV0
   566  CKjrjPTyrHFUnPyZyaZp8p2eX62iPYhaXkoBnEiarf0xKtJuT/8IlP5n/redlKYz
   567  GIHG5Svg3uDq9E09BOjFsgemhPyqbf7yrh5aRwDOIdHtn9mNevFPfQ1jO8lI/wbe
   568  4kC6zXM7te0/ZkM06DYRhcaeoYdeyY/gvE+w7wU/+f7Wzqt+LxOMIjKk0oDxZIv9
   569  praEM50DmARcotamAQgAsiO75WZvjt7BEAzdTvWekWXqBo4NOes2UgzSYToVs6xW
   570  8iXnE+mpDS7GHtNQLU6oeC0vizUjCwBfU+qGqw1JjI3I1pwv7xRqBIlA6f5ancVK
   571  KiMx+/HxasbBrbav8DmZT8E8VaJhYM614Kav91W8YoqK5YXmP/A+OwwhkVEGo8v3
   572  Iy7mnJPMSjNiNTpiDgc5wvRiTan+uf+AtNPUS0k0fbrTZWosbrSmBymhrEy8stMj
   573  rG2wZX5aRY7AXrQXoIXedqvP3kW/nqd0wvuiD11ZZWvoawjZRRVsT27DED0x2+o6
   574  aAEKrSLj8LlWvGVkD/jP9lSkC81uwGgD5VIMeXv6EQARAQABAAf7BHef8SdJ+ee9
   575  KLVh4WaIdPX80fBDBaZP5OvcZMLLo4dZYNYxfs7XxfRb1I8RDinQUL81V4TcHZ0D
   576  Rvv1J5n8M7GkjTk6fIDjDb0RayzNQfKeIwNh8AMHvllApyYTMG+JWDYs2KrrTT2x
   577  0vHrLMUyJbh6tjnO5eCU9u8dcmL5Syc6DzGUvDl6ZdJxlHEEJOwMlVCwQn5LQDVI
   578  t0KEXigqs7eDCpTduJeAI7oA96s/8LwdlG5t6q9vbkEjl1XpR5FfKvJcZbd7Kmk9
   579  6R0EdbH6Ffe8qAp8lGmjx+91gqeL7jyl500H4gK/ybzlxQczIsbQ7WcZTPEnROIX
   580  tCFWh6puvwQAyV6ygcatz+1BfCfgxWNYFXyowwOGSP9Nma+/aDVdeRCjZ69Is0lz
   581  GV0NNqh7hpaoVbXS9Vc3sFOwBr5ZyKQaf07BoCDW+XJtvPyyZNLb004smtB5uHCf
   582  uWDBpQ9erlrpSkOLgifbzfkYHdSvhc2ws9Tgab7Mk7P/ExOZjnUJPOcEAOJ3q/2/
   583  0wqRnkSelgkWwUmZ+hFIBz6lgWS3KTJs6Qc5WBnXono+EOoqhFxsiRM4lewExxHM
   584  kPIcxb+0hiNz8hJkWOHEdgkXNim9Q08J0HPz6owtlD/rtmOi2+7d5BukbY/3JEXs
   585  r2bjqbXXIE7heytIn/dQv7aEDyDqexiJKnpHBACQItjuYlewLt94NMNdGcwxmKdJ
   586  bfaoIQz1h8fX5uSGKU+hXatI6sltD9PrhwwhdqJNcQ0K1dRkm24olO4I/sJwactI
   587  G3r1UTq6BMV94eIyS/zZH5xChlOUavy9PrgU3kAK21bdmAFuNwbHnN34BBUk9J6f
   588  IIxEZUOxw2CrKhsubUOuiQE8BBgBCAAmFiEELZ6AMqOpBMVblK0uiKTQXVy+MAsF
   589  Alyi1qYCGwwFCQPCZwAACgkQiKTQXVy+MAstJAf/Tm2hfagVjzgJ5pFHmpP+fYxp
   590  8dIPZLonP5HW12iaSOXThtvWBY578Cb9RmU+WkHyPXg8SyshW7aco4HrUDk+Qmyi
   591  f9BvHS5RsLbyPlhgCqNkn+3QS62fZiIlbHLrQ/6iHXkgLV04Fnj+F4v8YYpOI9nY
   592  NFc5iWm0zZRcLiRKZk1up8SCngyolcjVuTuCXDKyAUX1jRqDu7tlN0qVH0CYDGch
   593  BqTKXNkzAvV+CKOyaUILSBBWdef+cxVrDCJuuC3894x3G1FjJycOy0m9PArvGtSG
   594  g7/0Bp9oLXwiHzFoUMDvx+WlPnPHQNcufmQXUNdZvg+Ad4/unEU81EGDBDz3Eg==
   595  =VFSn
   596  -----END PGP PRIVATE KEY BLOCK-----`
   597  
   598  func TestSignatureVerification_Marshal(t *testing.T) {
   599  	testJSONMarshal(t, &SignatureVerification{}, "{}")
   600  
   601  	u := &SignatureVerification{
   602  		Verified:  Bool(true),
   603  		Reason:    String("reason"),
   604  		Signature: String("sign"),
   605  		Payload:   String("payload"),
   606  	}
   607  
   608  	want := `{
   609  		"verified": true,
   610  		"reason": "reason",
   611  		"signature": "sign",
   612  		"payload": "payload"
   613  	}`
   614  
   615  	testJSONMarshal(t, u, want)
   616  }
   617  
   618  func TestCommitAuthor_Marshal(t *testing.T) {
   619  	testJSONMarshal(t, &CommitAuthor{}, "{}")
   620  
   621  	u := &CommitAuthor{
   622  		Date:  &referenceTime,
   623  		Name:  String("name"),
   624  		Email: String("email"),
   625  		Login: String("login"),
   626  	}
   627  
   628  	want := `{
   629  		"date": ` + referenceTimeStr + `,
   630  		"name": "name",
   631  		"email": "email",
   632  		"username": "login"
   633  	}`
   634  
   635  	testJSONMarshal(t, u, want)
   636  }
   637  
   638  func TestCreateCommit_Marshal(t *testing.T) {
   639  	testJSONMarshal(t, &createCommit{}, "{}")
   640  
   641  	u := &createCommit{
   642  		Author: &CommitAuthor{
   643  			Date:  &referenceTime,
   644  			Name:  String("name"),
   645  			Email: String("email"),
   646  			Login: String("login"),
   647  		},
   648  		Committer: &CommitAuthor{
   649  			Date:  &referenceTime,
   650  			Name:  String("name"),
   651  			Email: String("email"),
   652  			Login: String("login"),
   653  		},
   654  		Message:   String("message"),
   655  		Tree:      String("tree"),
   656  		Parents:   []string{"p"},
   657  		Signature: String("sign"),
   658  	}
   659  
   660  	want := `{
   661  		"author": {
   662  			"date": ` + referenceTimeStr + `,
   663  			"name": "name",
   664  			"email": "email",
   665  			"username": "login"
   666  		},
   667  		"committer": {
   668  			"date": ` + referenceTimeStr + `,
   669  			"name": "name",
   670  			"email": "email",
   671  			"username": "login"
   672  		},
   673  		"message": "message",
   674  		"tree": "tree",
   675  		"parents": [
   676  			"p"
   677  		],
   678  		"signature": "sign"
   679  	}`
   680  
   681  	testJSONMarshal(t, u, want)
   682  }
   683  

View as plain text